【MySQL】密码管理
【测试环境】
- CentOS7-64-1810
- MySQL5.7
【获取初始密码】
数据库安装初始进入,需要获取临时密码,进入数据库后需要修改密码哟!
grep 'temporary password' /var/log/mysqld.log
使用临时密码登录
mysql -uroot -p0O4bBGgtiM*b
【修改密码】
修改密码前,我们需要先看一下密码规则情况(如果是复杂密码,则不需要看哈【默认一般是长度至少8位,大小写字母+数字+特殊符号】)
……好吧,初始时查看强制要求设置密码……
那还是先修改规则,改了密码之后再查看:
# 设置密码复杂度(0=最低)
set global validate_password_policy=0;
# 设置密码长度(0=不限)
set global validate_password_length=0;
现在就可以设置密码了:
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
# 或者 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');
# 设置远程可访问
# (注意:mysql8.0 不支持此语句)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
# mysql8.0 (使用此方式修改远程)
# use mysql;
# update user set host = '%' where user = 'root';
# 刷新 即时生效
flush privileges;
现在退出,使用新密码重新登陆即可(也可以远程使用工具如Navicat登录哟)
mysql> exit
Bye
[root@localhost ~]# mysql -uroot -p123456
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.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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>
现在我们再来查看密码规则(详细的后续提供哈):
SHOW VARIABLES LIKE 'validate_password%';
当然还有其它的修改密码的方法,本文仅供参考哈。