[乐意黎]Centos 7 安装 MySQL 5.7.31填坑记

安装前检查

若不需要新安装, 请忽略以下.
执行以下命令检查系统中是否存在使用rpm安装的MySQL或者MariaDB。

rpm -qa | grep mysql
rpm -qa | grep mariadb

如果已经安装,则运行以下任一个命令删除。

rpm -e 软件名 #注意:这里的软件名必须包含软件的版本信息,如rpm -e mariadb-libs-5.5.52-1.el7.x86_64。一般使用此命令即可卸载成功。
rpm -e --nodeps 软件名 #卸载不成功时使用此命令强制卸载

卸载后,再用 rpm -qa|grep mariadb 或者 rpm -qa|grep mysql 查看结果。



1. Mysql 地址: https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

Mysql用户组及用户名: mysql
Mysql安装路径: /usr/local/mysql

[root@aerchi-centos ~] cd /usr/local/src
[root@aerchi-centos src] wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@aerchi-centos src]# groupadd mysql
[root@aerchi-centos src]# useradd -g mysql mysql
[root@aerchi-centos src]# chown -R mysql:mysql /usr/local/mysql
[root@aerchi-centos src]# tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz

[root@VM-0-16-centos mysql-5.7.31-linux-glibc2.12-x86_64]# mkdir /usr/local/mysql
[root@VM-0-16-centos mysql-5.7.31-linux-glibc2.12-x86_64]# cp -a /usr/local/src/mysql-5.7.31-linux-glibc2.12-x86_64/* /usr/local/mysql


若有需要, 还须安装以下插件.

yum -y install autoconf
yum -y install numactl.x86_64

 

2. MySQL 服务

把mysql.server文件复制到系统启动:

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

其它:

# groupadd mysql
# useradd -r -g mysql mysql
# chown -R mysql:mysql /usr/local/mysql
# mkdir -p /usr/local/mysql/data
# chown -R mysql:mysql /usr/local/mysql/data

增加服务: chmod +x /etc/rc.d/init.d/mysql

3、增加mysqld服务控制脚本执行权限

[root@VM_0_16_centos mysql]# chmod +x /etc/rc.d/init.d/mysqld

4、将mysqld服务加入到系统服务

[root@VM_0_16_centos mysql]# chkconfig --add mysqld

5、检查mysqld服务是否已经生效

[root@VM_0_16_centos mysql]# chkconfig --list mysqld

命令输出类似下面的结果:

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

将mysql的bin目录加入PATH环境变量

[root@VM_0_16_centos lib]# vim ~/.bash_profile
  •  

在文件最后添加如下信息:export PATH=$PATH:/usr/local/mysql/bin ,wq保存并退出
使配置文件即时生效:

[root@VM_0_16_centos lib]# source ~/.bash_profile

8、登录mysql

[root@VM_0_16_centos ~]# mysql -uroot -p

9、查找密码:

[root@aerchi-centos src]# grep -i password /usr/local/mysql/logs/mysql.err
2020-09-30T07:04:21.774141Z 1 [Note] A temporary password is generated for root@localhost: qpjw2,+1QdpA
 


错误解决:

1. error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

yum install libncurses*

2.  mysqld_safe mysqld from pid file /usr/local/mysql/data/mysqld.pid ended

 

3.ERROR! MySQL is not running, but lock file (/var/lock/subsys/mysql) exists

rm -rf /var/lock/subsys/mysql

 

4.Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/mysqld.pid).

修改 my.cnf

[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket  = /usr/local/mysql/logs/mysql.sock
log-error=/usr/local/mysql/logs/mysql.err
pid-file =/usr/local/mysql/data/mysql.pid
#character config
character_set_server=utf8mb4
symbolic-links=0
explicit_defaults_for_timestamp=true

初始化数据库:

# /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize

启动:   /etc/rc.d/init.d/mysql start

查看状态: /etc/rc.d/init.d/mysql status

 

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

解决方法:
1. 建立软链接
ln -s /usr/local/mysql/logs/mysql.sock /tmp/mysql.sock

2. 在 /etc/my.cnf 里加入

socket  = /usr/local/mysql/logs/mysql.sock
过程如下:

[root@aerchi-centos bin]# ./mysql start
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@aerchi-centos bin]# vim /etc/my.cnf
[root@aerchi-centos bin]# rm /tmp/mysql.sock
rm: remove symbolic link '/tmp/mysql.sock'? y
[root@aerchi-centos bin]# ./mysql start
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@aerchi-centos bin]# mysql --socket=/usr/local/mysql/logs/mysql.sock
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/logs/mysql.sock' (2)
[root@aerchi-centos bin]# ./mysql start
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[rootaerchi-centos bin]# ln -s /usr/local/mysql/logs/mysql.sock /tmp/mysql.sock
[root@aerchi-centos bin]# ./mysql start
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@aerchi-centos bin]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
[root@aerchi-centos bin]# vim /etc/my.cnf
[root@aerchi-centos bin]# service mysql restart
 ERROR! MySQL server process #19726 is not running!
Starting MySQL. SUCCESS!
[root@aerchi-centos bin]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@aerchi-centos bin]# mysql -uaerchi -pqpjw2,+1QdpAE
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31

Copyright (c) 2000, 2020, 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>


乐意黎
20200930

 

 

 

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页