偶尔会忘记mysql密码,特别是在测试环境中,此时该怎么办呢?
mysql忘记密码修改
1、关闭mysql
[root@vm01 ~]# service mysqld stop
Stopping mysqld: [ OK ]
2、启动mysql时跳过mysql的用户验证
[root@vm01 ~]# /usr/libexec/mysqld --skip-grant-tables
170512 21:27:16 InnoDB: Initializing buffer pool, size = 8.0M
170512 21:27:16 InnoDB: Completed initialization of buffer pool
170512 21:27:17 InnoDB: Started; log sequence number 0 900270
170512 21:27:17 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.1.73' socket: '/var/lib/mysql/mysql.sock' port: 3306 Source distribution
3、另开一个终端,登录mysql(不要指定用户密码,因为此时没有开启mysql用户验证)
[root@vm01 src]# mysql -h127.0.0.1
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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
mysql> select host,user,password from user;
+-------------------------+------+-------------------------------------------+
| host | user | password |
+-------------------------+------+-------------------------------------------+
| localhost | root | *DBB5837ZB74329105ER4568DDA7DC67ED2CA2ABC |
| tongyeyun01.localdomain | root | *DBB5837ZB74329105ER4568DDA7DC67ED2CA2ABC |
| 127.0.0.1 | root | *DBB5837ZB74329105ER4568DDA7DC67ED2CA2ABC |
+-------------------------+------+-------------------------------------------+
3 rows in set (0.00 sec)
此时,看不到密码原文,只看到密码md5加密后的密文。
4、mysql命令行中重新修改密码
# password函数中字符串为你需要设置的密码
mysql> update user set password=password("123456");
Query OK, 0 rows affected (0.00 sec)
Rows matched: 3 Changed: 0 Warnings: 0
5、杀掉mysqld进程
[root@vm01 ~]# kill -9 `ps -ef|grep mysqld|grep -v grep|awk '{print $2}'`
[root@vm01 ~]# ps -ef|grep mysqld
root 14971 14500 0 21:49 pts/3 00:00:00 grep mysqld
6、重启mysql服务,并使用新设置密码登录
[root@vm01 ~]# service mysqld start
Starting mysqld: [ OK ]
[root@vm01 ~]# mysql -h127.0.0.1 -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, 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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cmdb_v2 |
| monitor |
| mysql |
| ops |
| test |
| test_ops |
+--------------------+
7 rows in set (0.00 sec)
mysql>
ok,密码已修改成功,并可正常使用。