MySQL用户密码管理问题总结
一、忘记root用户密码
如果忘记了root密码,可以采用以下方法:
1、停止mysql服务
[root@bogon /]# systemctl stop mysqld
2、修改配置文件(my.cnf)
在【mysqld】选项中加入skip grant tables
[mysqld]
skip-grant-tables
.....
3、启动mysql服务
[root@bogon /]# systemctl start mysqld
[root@bogon /]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27-log 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.
此时,可以不输入密码登录。
4、更改密码
此时无法使用set password命令,可以使用update更新user表。命令如下:
mysql> select host,user from mysql.user;
+-------------+---------------+
| host | user |
+-------------+---------------+
| 192.168.1.% | repl |
| localhost | admin |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
| localhost | wang |
+-------------+---------------+
6 rows in set (0.00 sec)
mysql> update mysql.user set authentication_string=password('Wgx123456.') where user='root' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5、修改配置文件
重新修改配置文件,把【mysqld】选项中的skip grant tables命令删除或者注释掉。
[mysqld]
# skip-grant-tables
.....
6、重新启动mysql服务
[root@bogon /]# systemctl restart mysqld
[root@bogon /]# mysql -uroot -pWgx123456.
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 2
Server version: 5.7.27-log 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了。
二、修改root用户密码(知道root账户的原有密码)
1、使用mysqladmin命令修改用户密码
该命令不需要登录MySQL,格式如下:
shell> mysqladmin -uroot -p password "newpwd";
例如:修改root用户的密码,执行该命令后输入原来的root账户密码,则root密码被设置为新密码。
[root@bogon ~]# mysqladmin -uroot -p password 'Zhao@123456'
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@bogon ~]# mysql -uroot -pZhao@123456
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 12
Server version: 5.7.27-log 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.
2、登录MySQL,修改root账户密码
(1)使用set passwor命令
mysql> set password=password('Wgx123456.');
Query OK, 0 rows affected, 1 warning (0.01 sec)
(2)修改user表
mysql> update mysql.user set authentication_string=password('Wgx123456.')
where user='root' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
三、修改其他用户的密码
1、使用root账户登录
先使用root账户登录,使用以下命令可以修改任何用户的密码。
set password for '用户名'@'主机名' = password ('新密码');
例如,修改用户wang的登录密码。
mysql> select host,user from mysql.user;
+-------------+---------------+
| host | user |
+-------------+---------------+
| 192.168.1.% | repl |
| localhost | admin |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
| localhost | wang |
+-------------+---------------+
6 rows in set (0.00 sec)
mysql> set password for 'wang'@'localhost' = password('Wang@123456');
Query OK, 0 rows affected, 1 warning (0.01 sec)
2、用要修改密码的账户登录
[root@bogon /]# mysql -uwang -pWang@123456
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-log 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> set password=password('Wang123456.');
Query OK, 0 rows affected, 1 warning (0.01 sec)