且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linux下mysql的root密码忘记解决方

更新时间:2022-09-30 12:37:51

1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库。因为在重新设置MySQL

的root密码的期间,MySQL数据库完全出于没有密码保护的状态下,其他的用户也可以任意地登录和修改

MySQL的信息。可以采用将MySQL对外的端口封闭,并且停止Apache以及所有的用户进程的方法实现服务

器的准安全状态。最安全的状态是到服务器的Console上面操作,并且拔掉网线。 

2.修改MySQL的登录设置: 

# vi /etc/my.cnf 

在[mysqld]的段中加上一句:skip-grant-tables 

例如: 

[mysqld] 

datadir=/var/lib/mysql 

socket=/var/lib/mysql/mysql.sock 

skip-grant-tables 

保存并且退出vi。 

3.重新启动mysqld 

# /etc/init.d/mysqld restart 

Stopping MySQL: [ OK ] 

Starting MySQL: [ OK ] 

4.登录并修改MySQL的root密码 

# /usr/bin/mysql 

Welcome to the MySQL monitor. Commands end with ; or \g. 

Your MySQL connection id is 3 to server version: 3.23.56 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer. 

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> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root' ; 

Query OK, 0 rows affected (0.00 sec) 

Rows matched: 2 Changed: 0 Warnings: 0 

mysql> flush privileges ; 

Query OK, 0 rows affected (0.01 sec) 

mysql> quit 

Bye 

5.将MySQL的登录设置修改回来 

# vi /etc/my.cnf 

将刚才在[mysqld]的段中加上的skip-grant-tables删除 

保存并且退出vi。 

6.重新启动mysqld 

# /etc/init.d/mysqld restart 

Stopping MySQL: [ OK ] 

Starting MySQL: [ OK ]




1.用root或者运行mysqld的用户登录系统;

2.利用kill命令结束掉mysqld的进程;

3.使用–skip-grant-tables参数启动MySQL Server

#mysqld_safe –skip-grant-tables &

4.然后用空密码方式使用root用户登录 MySQL;mysql -u root

5.为root@localhost设置新密码

mysql> update mysql.user set password=PASSWORD('新密码') where User='root'mysql> flush privileges;mysql> quit

6. 重新启动MySQL










一、一般忘记密码的解决办法,需要重启Mysql
1、skip-grant-tables
我们常用的方法是使用skip-grant-tables选项,mysqld server启动之后并不使用权限系统(privilege system)。用户不需要任何账号、不受任何限制的访问数据库中所有数据。为了安全起见,通常加上 skip-networking ,mysqld不侦听任何TCP/IP连接请求。操作过程如下,
1)修改my.cnf配置文件,在mysqld选项中添加skip-grant-tables和skip-networking。
2)再重启mysqld server。
3)通过sql语句修改mysql.user表中存储密码。执行flush privileges,重新启用mysql权限系统。


UPDATE mysql.USER SET Password=PASSWORD('newpwd')WHERE User='root';


FLUSH PRIVILEGES;


4)删除或者注释配置文件中skip-grant-tables和skip-networking的参数选项。如果使用skip-networking,则需要再次重启mysqld。因为skip-networking不是系统变量,只是mysqld的参数选项,而不能通过系统变量动态进行设置。如果没有适用skip-networking,只需要执行flush privileges就可以使权限系统重新生效。
2. --init-file
mysqld_safe可以使–init-file参数选项来执行重新设定密码的sql语句。
1)新建一个初始化文件,如/tmp/initfile,文件内容为上面修改密码的sql语句。


UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';


FLUSH PRIVILEGES;


2)关闭mysqld服务进程。


3)使用mysqld_safe启动mysqld;


mysqld_safe --init-file=/home/me/mysql-init &


上面的两种方法是在忘记root密码情况下重新设置密码的方法,可以发现都需要重启mysqld服务。很多人都是使用第一种进行重置root密码,但是比较推荐的做法反而是第二种,即安全有快捷简单。


二、不重启mysqld的方法

1、首先得有一个可以拥有修改权限的mysql数据库账号,当前的mysql实例账号(较低权限的账号,比如可以修改test数据库)或者其他相同版本实例的账号。把data/mysql目录下面的user表相关的文件复制到data/test目录下面。



[root@localhost mysql]# cp mysql/user.* test/


[root@localhost mysql]# chown mysql.mysql test/user.*


2、使用另一个较低权限的账号链接数据库,设置test数据库中的user存储的密码数据。



[root@localhost mysql]# mysql -utest -p12345


Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.25a-log Source distribution


Copyright (c) 2000, 2011, 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 test


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> update user set password=password('yayun') where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 5  Changed: 0  Warnings: 0

mysql>


3、把修改后的user.MYD和user.MYI复制到mysql目录下,记得备份之前的文件。



mv mysql/user.MYD mysql/user.MYD.bak


mv mysql/user.MYI mysql/user.MYI.bak


cp test/user.MY* mysql/


chown mysql.mysql mysql/user.*


4、查找mysql进程号,并且发送SIGHUP信号,重新加载权限表。



[root@localhost mysql]# pgrep -n mysql
2184
[root@localhost mysql]#
[root@localhost mysql]# kill -SIGHUP 2184


5、登陆测试



[root@localhost mysql]# mysql -uroot -pyayun


Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.25a-log Source distribution


Copyright (c) 2000, 2011, 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>











本文转自 chengxuyonghu 51CTO博客,原文链接:http://blog.51cto.com/6226001001/1545542,如需转载请自行联系原作者