环境
系统 Centos 7.3
MySQL 5.7.24
设置
一、查看初始密码
[deployer@lqas home]$ grep 'temporary password' /var/log/mysqld.log
2019-10-25T02:18:49.492601Z 1 [Note] A temporary password is generated for root@localhost: %ss.wKa3FchV
%ss.wKa3FchV
就是初始密码
####二、使用初始密码登陆
[deployer@lqas home]$ mysql -u root -p
Enter password:
输入刚才查到的初始密码,然后进入 MySQL
####三、修改密码
# 设置新的 root 密码
mysql> alter user 'root'@'localhost' identified by '你的密码';
Query OK, 1 rows affected (0.00 sec)
# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 可以远程登陆
mysql> grant all privileges on *.* to 'root'@'%' identified by '你的密码' with grant option;
Query OK, 1 rows affected (0.00 sec)
# 刷新
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
注意中间的两次刷新不要少,少了可能会报错~
四、测试修改是否成功
重启 MySQL 服务 systemctl; restart mysqld
使用新密码登陆 mysql -u root -p
在数据库中更换数据库 use msyql;
结果如下:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.24 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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed