CentOS7.4安装MySQL踩坑记录

CentOS7.4安装MySQL踩坑记录

time: 2018.3.19

CentOS7.4安装MySQL时网上的文档虽然多但是不靠谱的也多, 可能因为版本与时间的问题, 所以记录下自己踩坑的过程, 如果你发现进坑了, 欢迎参考本篇文章:)

第一次尝试遇到的问题:

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

尝试卸载重新安装, 参考步骤:

1.查看yum是否安装过mysql
yum list installed mysql*
yum list installed | grep mysql*
没有显示结果, 说明yum中没有安装mysql(对yum与rpm并不是很了解, 如有错误欢迎指出)
2.删除配置文件与文件夹
rm -rf /var/lib/mysql
rm /etc/my.cnf
3.查看rpm中的安装并卸载
rpm -qa | grep -i mysql
以下根据上面命令显示的列表修改

rpm -e mysql-community-server-5.7.21-1.el7.x86_64 mysql-community-common-5.7.21-1.el7.x86_64 mysql-community-libs-5.7.21-1.el7.x86_64 mysql57-community-release-el7-11.noarch mysql-community-client-5.7.21-1.el7.x86_64 mysql-community-libs-compat-5.7.21-1.el7.x86_64 --nodeps

3.清除余项
whereis mysql
删除上面命令显示的路径
rm -rf /usr/share/mysql/
4.删除配置
rm -rf /usr/my.cnf
rm -rf /root/.mysql*# 无结果

# 笔者机器上无结果 chkconfig --list | grep -i mysql chkconfig --del mysqld systemctl list-dependencies | grep -i mysql 5.重新安装

下载mysql源并安装到rpm:

wget rpm -ivh mysql57-community-release-el7-11.noarch.rpm

更新yum并安装mysql(时间较长):

# 更新yum软件包 yum check-update # 更新系统 yum update # 安装mysql yum install mysql mysql-server

注意事项
更新yum后可能需要重新编辑/usr/bin/yum文件头(因为笔者将默认的python更改为python3), 编辑后再次安装即可, 出现的错误如下:

[root@centos ~]# yum install mysql mysql-server File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax 安装完成后配置

未跳过grant-tables授权表时启动MySQL会出现:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

这里需要修改root的密码, 这条命令是给mysql加上一个启动参数--skip-grant-tables, 顾名思义,就是在启动mysql时不启动grant-tables授权表, 用于忘记管理员密码后的修改
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables --user=mysql &
但是, MySQL 5.7.6 版本开始默认是不安装mysqld_safe了, 以下为新方法:
1.停止 mysql 服务
service mysqld stop
2.设置 mysqld 选项 --skip-grant-tables参数:
systemctl set-environment MYSQLD_OPTS='--skip-grant-tables'
3.重新启动mysql
systemctl start mysqld
4.执行 mysql -u root 登录mysql并更改密码

[root@centos ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.21 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> UPDATE mysql.user -> SET authentication_string = PASSWORD('toor'), password_expired = 'N' -> WHERE User = 'root' AND Host = 'localhost'; Query OK, 1 row affected, 1 warning (0.65 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql>

password_expired = 'N'过期状态设置为No, flush privileges; 刷新权限记录, 详见

5.设置完密码后去掉--skip-grant-tables参数, 重启mysql即可用设置的密码登录root用户
systemctl unset-environment MYSQLD_OPTS
systemctl restart mysqld
mysql -uroot -p

由于笔者代码运行在服务器系统上, 故不此设置mysql远程访问

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxssw.html