CentOS7.4安装MySQL踩坑记录

时间:2023-05-15 12:03:56

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 http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
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

MySQL5.7开启远程连接

1.连接到mysql数据库

mysql -uroot -p

2.选择数据库

use mysql

3.开启远程连接:

# mysql安装后默认是localhost访问,如果需要外部访问可以设置一个新的账号把host改为%,意味着所有ip均可以访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;

由于密码策略问题报错:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

原因: MySQL5.6.6版本之后增加了密码强度验证插件validate_password,相关参数设置的较为严格。

使用了该插件会检查设置的密码是否符合当前设置的强度规则,若不满足则拒绝设置。影响的语句和函数有:create user,grant,set password,password(),old password。

解决参考:

# 查看mysql全局参数配置
mysql> select @@validate_password_policy;
+----------------------------+
| @@validate_password_policy |
+----------------------------+
| MEDIUM |
+----------------------------+
1 row in set (0.01 sec) mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.00 sec)
# 修改mysql参数配置
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_number_count=3;
Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_special_char_count=0;
Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=3;
Query OK, 0 rows affected (0.00 sec) mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 3 |
| validate_password_mixed_case_count | 0 |
| validate_password_number_count | 3 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 0 |
+--------------------------------------+-------+
7 rows in set (0.01 sec)
# 配置完即可修改简单密码
mysql> set password for 'root'@'localhost' = password('简单密码');
Query OK, 0 rows affected, 1 warning (0.00 sec)

4.重新配置并重启服务

# 此时配置远程连接无报错
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '简单密码' WITH GRANT OPTIONN;
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec) mysql> ^DBye
# 重启mysql服务
systemctl restart mysqld.service

ok, 可以远程连接了