您的位置:首页 > 数据库 > MySQL

MySQL root密码找回

2016-03-23 20:06 639 查看

1、系统信息

系统信息

[root@zhang ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@zhang ~]# uname -r
2.6.32-431.el6.x86_64
[root@zhang ~]# uname -m
x86_64
数据库信息

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.72 |
+-----------+
1 row in set (0.06 sec)

2、单实例

1、关闭数据库

[root@zhang ~]# /etc/init.d/mysqld stop
Shutting down MySQL....                                    [  OK  ]
[root@zhang ~]# netstat -lntp | grep '330'
[root@zhang ~]#

2、使用--skip-grant-tables 启动MySQL,忽略授权验证登录

[root@zhang ~]# mysqld_safe --skip-grant-tables --user=mysql &	# 跳过权限启动
[1] 8013
[root@zhang ~]# 160323 17:11:59 mysqld_safe Logging to '/application/mysql5.1.72/data/zhang.err'.
160323 17:11:59 mysqld_safe Starting mysqld daemon with databases from /application/mysql5.1.72/data
[root@zhang ~]# netstat -lntp | grep '330'
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      8632/mysqld

3、无需密码即可登录MySQL

[root@zhang ~]# mysql		# 免账号登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.72 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>

4、为root账号设置新密码

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | zhang     |                                           |
| root | 127.0.0.1 |                                           |
|      | localhost |                                           |
|      | zhang     |                                           |
+------+-----------+-------------------------------------------+
5 rows in set (0.05 sec)
mysql> update mysql.user set password=password('zhang123') where user='root' and host='localhost';		# password('zhang123') 调用函数  会对密码进行加密存放
Query OK, 1 row affected (0.13 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush  privileges;		# 刷新权限信息
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *D8048838D5F7310362A9F330ED8E88D3372967C3 |
| root | zhang     |                                           |
| root | 127.0.0.1 |                                           |
|      | localhost |                                           |
|      | zhang     |                                           |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)

5、重启服务再登录

[root@zhang ~]# mysqladmin -uroot -pzhang123 shutdown	# 优雅关闭【不能使用kill命令,防止重启失败】   或者使用 /etc/init.d/mysqld stop
160323 17:23:05 mysqld_safe mysqld from pid file /application/mysql5.1.72/data/zhang.pid ended
[1]+  Done                    mysqld_safe --skip-grant-tables --user=mysql
[root@zhang ~]# ps -ef | grep 'mysql'
root      8238  1951  0 17:23 pts/0    00:00:00 grep --color mysql
[root@zhang ~]# /etc/init.d/mysqld start		# 重启
Starting MySQL.                                            [  OK  ]
[root@zhang ~]# mysql -uroot -pzhang123		# 登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.72 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>

3、多实例

1、关闭数据库

在多实例中关闭数据库需要root密码。所以如果真的丢失,那么只有使用kill命令了

[root@mode-6 3308]# netstat -lntp | grep '330'
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 2377/mysqld
[root@mode-6 3308]# pkill mysql
[root@mode-6 3308]# netstat -lntp | grep '330'
[root@mode-6 3308]#

2、使用--skip-grant-tables 启动MySQL,忽略授权验证登录

[root@mode-6 3308]# pwd
/data/3308
[root@mode-6 3308]# ll
total 28
drwx------ 9 mysql mysql  4096 Mar 23 10:44 data
-rw-rw-r-- 1 mysql mysql  1901 Mar 21 14:17 my.cnf
-rwx------ 1 root  root   1307 Mar 22 21:36 mysql
-rw-r----- 1 mysql mysql 16109 Mar 23 17:49 mysql_oldboy3308.err
[root@mode-6 3308]# mysqld_safe --defaults-file=/data/3308/my.cnf --skip-grant-tables &  #参数前后位置不要改变
[1] 2967
[root@mode-6 3308]# 160323 17:52:18 mysqld_safe Logging to '/data/3308/mysql_oldboy3308.err'.
160323 17:52:18 mysqld_safe Starting mysqld daemon with databases from /data/3308/data
[root@mode-6 3308]# netstat -lntp | grep '330'
tcp        0      0 0.0.0.0:3308           0.0.0.0:*             LISTEN      3680/mysqld

3、无需密码即可登录MySQL

[root@mode-6 3308]# ll
total 40
drwx------ 9 mysql mysql  4096 Mar 23 17:52 data
-rw-rw-r-- 1 mysql mysql  1901 Mar 21 14:17 my.cnf
-rwx------ 1 root  root   1307 Mar 22 21:36 mysql
srwxrwxrwx 1 mysql mysql     0 Mar 23 17:52 mysql.sock
-rw-r----- 1 mysql mysql 17136 Mar 23 17:52 mysql_oldboy3308.err
-rw-rw---- 1 mysql mysql     5 Mar 23 17:52 mysqld.pid
[root@mode-6 3308]# mysql -S /data/3308/mysql.sock 		# 免账号登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 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>

4、为root账号设置新密码

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *FD571203974BA9AFE270FE62151AE967ECA5E0AA |
| root | mode-6.5  |                                           |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
|      | localhost |                                           |
|      | mode-6.5  |                                           |
| bing | %         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| bing | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| ying | %         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
9 rows in set (0.02 sec)

mysql> update mysql.user set password=password('zhang123') where user='root' and host='localhost';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> flush  privileges;		# 刷新权限信息
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *D8048838D5F7310362A9F330ED8E88D3372967C3 |
| root | mode-6.5  |                                           |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
|      | localhost |                                           |
|      | mode-6.5  |                                           |
| bing | %         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| bing | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| ying | %         | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
9 rows in set (0.00 sec)

5、重启服务再登录

[root@mode-6 3308]# mysqladmin -S /data/3308/mysql.sock -uroot -pzhang123 shutdown	 # 关闭数据库
160323 18:03:41 mysqld_safe mysqld from pid file /data/3308/mysqld.pid ended
[1]+  Done                    mysqld_safe --defaults-file=/data/3308/my.cnf --skip-grant-tables
[root@mode-6 3308]# netstat -lntp | grep '330'
[root@mode-6 3308]#
[root@mode-6 3308]# /data/3308/mysql start		# 启动数据库
Starting MySQL...
[root@mode-6 3308]# mysql -S /data/3308/mysql.sock -uroot -pzhang123	#登录
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 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>


       提示:使用--skip-grant-tables
参数启动数据库,为root账号重置密码后一定要重启数据库。防止出现安全问题或其他问题。


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: