不使用yum CentOS安装mysql

这类的教程网上有很多,但是多半最后都会出错,好不容易觅得一种不易出错的方法,现在记录在这里。
启动mysql:(1)service mysql start (2)mysql -uroot -p

一、先在官网下载相应的版本

(1)进入官网https://www.mysql.com/
点击DOWNLOADS
这里写图片描述
(2)选择社区版本
这里写图片描述
(3)选择linux通用版本,这里默认下载最新版,如果你想下载往期版本,可以点击Looking for previous GA versios?
这里写图片描述
(4)最后选择系统的位数,我这里下载的是64位的。
这里写图片描述

二、将下载的mysql压缩包上传至VM虚拟机

这里写图片描述

三、解压和准备工作

(1)执行解压命令:tar -zxvf mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz

[gznc-hadoop@slave01 Desktop]$ tar -zxvf mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz 

(2)将用户切换为root(下面几步没有特殊说明都是在root用户下),复制解压后的mysql目录到系统的本地软件目录;这个过程有点慢,要等一会儿。

[gznc-hadoop@slave01 Desktop]$ su root
Password: 
[root@slave01 Desktop]# cp mysql-5.5.60-linux-glibc2.12-x86_64 /usr/local/mysql -r

注意:目录结尾不要加/

(3)添加系统mysql组和mysql用户:

[root@slave01 Desktop]# groupadd mysql
[root@slave01 Desktop]# useradd -r -g mysql mysql
四、安装数据库

(1)进入安装mysql软件目录:执行命令 cd /usr/local/mysql

[root@slave01 Desktop]# cd /usr/local/mysql/
[root@slave01 mysql]# 

(2)修改当前目录拥有者为mysql用户:

[root@slave01 mysql]# chown -R mysql:mysql ./

(3)安装数据库:执行命令./scripts/mysql_install_db --user=mysql

[root@slave01 mysql]# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
180515 14:37:56 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180515 14:37:56 [Note] ./bin/mysqld (mysqld 5.5.60) starting as process 3438 ...
OK
Filling help tables...
180515 14:37:57 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180515 14:37:57 [Note] ./bin/mysqld (mysqld 5.5.60) starting as process 3445 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h slave01 password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

[root@slave01 mysql]# 

(4)修改当前目录拥有者为root用户:

[root@slave01 mysql]# chown -R root:root ./

(5)修改当前data目录拥有者为mysql用户

[root@slave01 mysql]# chown -R mysql:mysql data

到此数据库安装完毕

五、启动mysql服务和添加开机启动mysql服务

(1)添加开机启动:还是再初始化目录执行

[root@slave01 mysql]# cp support-files/mysql.server /etc/init.d/mysql

(2)启动mysql服务:执行命令service mysql start

[root@slave01 mysql]# service mysql start
Starting MySQL.. SUCCESS! 
[root@slave01 mysql]# 

(3)执行命令:ps -ef|grep mysql 看到mysql服务说明启动成功

root       3788      1  0 05:01 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/slave01.pid
mysql      3952   3788  0 05:01 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/lib/mysql/slave01.pid --socket=/var/lib/mysql/mysql.sock
root       4073   4056  0 05:09 pts/1    00:00:00 grep mysql

(4)修改mysql的root用户密码,root初始密码为空的:

执行命令:./bin/mysqladmin -u root password ‘密码’

[root@slave01 ~]# cd /usr/local/mysql/
[root@slave01 mysql]# ./bin/mysqladmin -u root password 'rootadmin'

(5)把mysql客户端放到默认路径:

[root@slave01 mysql]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

注意:建议使用软链过去,不要直接包文件复制,便于系统安装多个版本的mysql

六、常见问题

(1)报错

[root@slave01 Desktop]$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (13)

解决办法

[root@slave01 Desktop]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
[root@slave01 Desktop]# service mysql restart
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 

(2)报错信息

[root@slave01 mysql]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@slave01 mysql]# 

解决办法:
先在my.cnf文件加入skip-grant-tables用来跳过密码验证的过程

[root@slave01 Desktop]# vim /etc/my.cnf 

这里写图片描述
重启mysql重置密码

[root@slave01 Desktop]# service mysql restart
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@slave01 Desktop]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.60 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password("rootadmin") where user="root'@'localhost";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0  Changed: 0  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

然后把刚才添加的skip-grant-tables删除或者注释掉。
这里写图片描述
最后再重启MySQL就可以了,输入的密码为新密码

[root@slave01 Desktop]# service mysql restart
Shutting down MySQL. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@slave01 Desktop]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.60 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页