linux下安装mysql5.7方法与常见问题

时间:2022-11-21 07:40:55

1、下载tar包,这里使用wget从官网下载

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

2、将mysql安装到/usr/local/mysql下

# 解压

tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

# 移动

mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/

# 重命名

mv /usr/local/mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql

3、新建data目录

mkdir /usr/local/mysql/data

4、新建mysql用户、mysql用户组

# mysql用户组

groupadd mysql

# mysql用户

useradd mysql -g mysql

5、将/usr/local/mysql的所有者及所属组改为mysql

chown -R mysql.mysql /usr/local/mysql

6、配置

/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

# 如果出现以下错误:

linux下安装mysql5.7方法与常见问题
2018-07-14 06:40:32 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-07-14 06:40:32 [ERROR] Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
2018-07-14 06:40:32 [ERROR] Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql
-- server log begin -- -- server log end --
linux下安装mysql5.7方法与常见问题

# 则使用以下命令:

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

# 如果出现以下错误:

/usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

# 则执行以下命令:

yum -y install numactl

# 完成后继续安装:

/usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

# 编辑/etc/my.cnf

linux下安装mysql5.7方法与常见问题
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=3306
character-set-server=utf8
# 取消密码验证
skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# skip-grant-tables
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
linux下安装mysql5.7方法与常见问题

7、开启服务

# 将mysql加入服务

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

# 开机自启

chkconfig mysql on

# 开启

service mysql start

8、设置密码

# 登录(由于/etc/my.cnf中设置了取消密码验证,所以此处密码任意)

/usr/local/mysql/bin/mysql -u root -p

# 操作mysql数据库

>>use mysql;

# 修改密码

>>update user set authentication_string=password('你的密码') where user='root';

>>flush privileges;

>>exit;

9、将/etc/my.cnf中的skip-grant-tables删除

10、登录再次设置密码(不知道为啥如果不再次设置密码就操作不了数据库了)

/usr/local/mysql/bin/mysql -u root -p

>>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';

>>exit;

11、允许远程连接

/usr/local/mysql/bin/mysql -u root -p

>>use mysql;

>>update user set host='%' where user = 'root';

>>flush privileges;

>>eixt;

12、添加快捷方式

ln -s /usr/local/mysql/bin/mysql /usr/bin

常见问题:

1、

mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.

在Linux<CentOS>服务器上安装Mysql,由于Centos自身的yum源中用Mysql的分支Mariadb代替了MySQL,所以不得不选择rpm或tar.gz包的方式安装,

但是为了以后在其他LInux如Ubuntu中也能熟练安装MySQL,所以推荐使用tar.gz,安装教程—http://blog.csdn.net/qq_32331073/article/details/76252559

运行命令:service mysqld start

出现如下错误:

Starting MySQL.Logging to '/usr/local/mysql/data/iZ2ze8hspx8qsmxx7b1i9sZ.err'.
170728 09:47:55 mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/iZ2ze8hspx8qsmxx7b1i9sZ.pid).
随即执行命令:cat  /usr/local/mysql/data/iZ2ze8hspx8qsmxx7b1i9sZ.err查看错误原因
error内容如下:

[ERROR] Can't start server : Bind on unix socket: Permission denied
[ERROR] Do you already have another mysqld server running on socket: /var/lib/mysql/mysql.sock ?
结合先前写入配置/etc/my.cnf:

linux下安装mysql5.7方法与常见问题

由上不难理解——

第一:

mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
    “var/lib/mysql”目录不存在,首要先创建:

mkdir   /var/lib/mysql
第二:

Mysql客户端和服务器端是通过初始化mysql.sock 文件来进行通信,所以服务器在启动时需要写入该文件,因此该文件所存放的目录必须双方都

具有访问权限。而异常日志中表示:

[ERROR] Can't start server : Bind on unix socket: Permission denied
     所以,MySQL服务不具备目录 /var/lib/mysql 的写入权限,无法生成mysql.sock文件,自然服务启动时无法找到该文件。

给目录 /var/lib/mysql 付予写入权限或者最大权限—chmod 777  /var/lib/mysql

2、

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

登录服务器,使用root用户连接mysql时出现错误提示:

$ bin/mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

尝试重启mysql

linux下安装mysql5.7方法与常见问题
$ ./mysql.server restart
Shutting down MySQL (Percona Server)....... [ OK ]
rm: cannot remove `/var/lock/subsys/mysql': Permission denied
Starting MySQL (Percona Server).. [ OK ]
[mysql@tongxtx mysql]$ bin/mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
linux下安装mysql5.7方法与常见问题

还是报同样的错。先看一下my.cnf

more my.cnf |grep sock
socket = /opt/mysql/mysql.sock
socket = /opt/mysql/mysql.sock

可以看到我的mysql已经声明了mysql.sock的文件位置,而错误提示为/tmp/mysql.sock; 也就是说虽然我的mysql声明了sock文件的位置,但是客户端在连接的时候, linux系统总是去/tmp目录下去找,找到了原因,就可以对症下药了:

为mysql.sock创建链接文件:

$ ln -s /opt/mysql/mysql.sock /tmp/mysql.sock

然后再次连接mysql,成功连接

或者指定mysql去找sock文件的位置

mysql --help|grep sock

./bin/mysql -S /opt/mysql/mysql.sock