1. Mysql 5.7 在自动初始化数据库的时候,会生成root用户的默认密码。mysql
经过 grep "temporary password" /var/log/mysqld.log 命令,返回结果最后冒号后面的字符串就是root的默认密码。sql
使用此密码登陆后,Mysql 会要求第一件作的事就是改root密码,并且是要求强密码。能够经过set password=password('密码')来更改。数据库
2. 没法找到密码,忘记设置的是啥了centos
在/etc/my.cnf 中添加 skip-grant-tables 参数。此参数的做用是登陆Mysql 数据库不进行用户密码验证。ide
修改后, 重启服务 systemctl restart mysqld.
[root@VM_0_15_centos ~]#service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[root@VM_0_15_centos~]#mysql
Welcome to the MySQL monitor. Commands end with ; or\g.
Your MySQL connection idis 3Server version:5.7.26MySQL Community Server (GPL)
Copyright (c)2000, 2019, Oracle and/orits affiliates. All rights reserved.
Oracleis a registered trademark of Oracle Corporation and/orits
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('123456pswd') where user='root' and Host = 'localhost';
Query OK,1 row affected, 1 warning (0.00sec)
Rows matched:1 Changed: 1 Warnings: 1mysql>flush privileges;
Query OK, 0 rows affected (0.00sec)
mysql>quit
Bye
# 将/etc/my.cnf 中 skip-grant-tables 注释,重启服务
[root@VM_0_15_centos ~]#mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ;or\g.
Your MySQL connection idis 3Server version:5.7.26Copyright (c)2000, 2019, Oracle and/orits affiliates. All rights reserved.
Oracleis a registered trademark of Oracle Corporation and/orits
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>
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.测试
设置一个简单的测试密码的话,如123456,会提示这个错误.ui
mysql> alter user 'root'@'localhost' identified by 'password';this
或者 mysql> set password=password("password");spa
mysql>show databases;
ERROR1820(HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user 'root'@'localhost' identified by '123456pswd';
Query OK, 0 rows affected (0.00sec)
mysql>flush privileges;
Query OK, 0 rows affected (0.00sec)
mysql>show databases;+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ops |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00sec)
mysql>quit
Bye