mysql 5.7 默认密码及密码修改

 

1. Mysql 5.7 在自动初始化数据库的时候,会生成root用户的默认密码。

通过 grep "temporary password" /var/log/mysqld.log 命令,返回结果最后冒号后面的字符串就是root的默认密码。

 

使用此密码登录后,Mysql 会要求第一件做的事就是改root密码,而且是要求强密码。可以通过set password=password('密码')来更改。

2. 无法找到密码,忘记设置的是啥了

在/etc/my.cnf 中添加 skip-grant-tables 参数。此参数的作用是登录Mysql 数据库不进行用户密码验证。

修改后, 重启服务  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 id is 3
Server version: 5.7.26 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> update mysql.user set authentication_string=password('123456pswd') where user='root' and Host = 'localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

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 id is 3
Server version: 5.7.26

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> 

 

 ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

设置一个简单的测试密码的话,如123456,会提示这个错误.

mysql>   alter user 'root'@'localhost' identified by 'password';

或者  mysql>   set password=password("password");

mysql> show databases;
ERROR 1820 (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.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ops |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

mysql> quit
Bye

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-06-23 11:05  mingetty  阅读(609)  评论(0)    收藏  举报