MySQL8 设置简单密码
虚拟机里安装了 MySQL 8.0.33,想把密码设置成 “root”。
重点在“修改密码验证策略”
安装
CentOS 7.6
找到 CentOS 的 YUM 安装方式的 链接:MySQL Yum Repository
上传到自己喜欢的目录
# 用 yum 包管理器安装名为 mysql80-community-release-el7-7.noarch.rpm 的 RPM 包。
yum -y install mysql80-community-release-el7-7.noarch.rpm
# 默认安装 MySQL 8.0
yum -y install mysql-community-server
启动
systemctl start mysqld
安装 5.7
如果要求不安装 8.0 ,安装 5.7
yum -y install mysql80-community-release-el7-7.noarch.rpm
# 禁用 MySQL 8.0 存储库
sudo yum-config-manager --disable mysql80-community
# 启用 MySQL 5.7 存储库
sudo yum-config-manager --enable mysql57-community
# 安装 MySQL 5.7
yum -y install mysql-community-server
关闭 SELinux
CentOS 7.6 启动 MySQL8 时需要关闭 SELinux
要关闭 SELinux,请按照以下步骤进行操作(适用于 CentOS/RHEL 系统):
-
编辑 SELinux 配置文件:使用文本编辑器打开
/etc/selinux/config文件。vi /etc/selinux/config -
在配置文件中找到
SELINUX行,并将其值更改为disabled。SELINUX=disabled -
保存文件并关闭编辑器。
-
重新启动系统:重启计算机以使 SELinux 更改生效。
reboot -
验证 SELinux 状态:在系统重新启动后,可以使用以下命令验证 SELinux 是否已关闭:
getenforce如果输出为
Disabled,则表示 SELinux 已成功关闭。
请注意,关闭 SELinux 可能会降低系统的安全性,因为 SELinux 提供了强大的访问控制功能来保护系统资源免受恶意软件和未经授权的访问。在关闭 SELinux 之前,请确保您有足够的理由和措施来维护系统的安全性,例如通过其他安全机制来弥补关闭 SELinux 带来的风险。
另外,关闭 SELinux 只是一种解决问题的方法,如果您遇到与 SELinux 相关的错误或问题,建议考虑调整 SELinux 策略或配置来适应您的需求,而不是直接关闭 SELinux。
正常登录
安装后获取密码
grep "password" /var/log/mysqld.log
先正常登录
mysql -uroot -p
先设置一个复杂点的密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '!1024.Ran';
添加一个远程登陆账户
create user 'root'@'%' identified by '!1024.Ran';
grant all on *.* to 'root'@'%';
flush privileges;
exit
开放防火墙3306端口
修改密码验证策略
查询
SHOW VARIABLES LIKE 'validate_password%';
结果
| Variable_name | Value |
|---|---|
| validate_password.check_user_name | ON |
| 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 |
设置
SET GLOBAL validate_password.check_user_name = OFF;
SET GLOBAL validate_password.length = 1;
SET GLOBAL validate_password.mixed_case_count = 0;
SET GLOBAL validate_password.number_count = 0;
SET global validate_password.policy=LOW;
SET GLOBAL validate_password.special_char_count = 0;
如果执行报错,那就把查出来的 validate_password.check_user_name 等字符复制到设置命令里,再执行试试。
设置密码为 “root”

重启 mysql
systemctl restart mysqld
操作记录
[root@bogon ~]# grep "password" /var/log/mysqld.log
2023-06-07T08:28:36.475084Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ac>gejUOf0Oy
[root@bogon ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements




浙公网安备 33010602011771号