解决办法一
MySQL忘记超级用户口令的解决办法
如果MySQL正在运行,首先杀之: killall -TERM mysqld。
启动MySQL:
代码如下
bin/safe_mysqld --skip-grant-tables &
就可以不需要密码就进入MySQL了
然后就是
代码如下
>use mysql
>update user set password=password("new_pass") where user="root";
>flush privileges;
重新杀MySQL,用正常方法启动MySQL 。
解决方法二
最安全的状态是到服务器的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 ]
恢复方法:
通过运行mysqld_safe 启动命令,跳过账号密码检查,这样不用账号、密码即可登陆数据库,从而可以重新设置mysql数据库的root超级账号、密码。
演示如下:
首先,用putty等ssh工具,登陆服务器,运行
01
代码如下
./etc/init.d/mysqld stop
service mysqld stop
把mysql停止,
接着,运行
/usr/bin/mysqld_safe --skip-grant-tables
启动mysql,这样运行后即可跳过权限检查。无需账号、密码即可登陆数据库,如下图:
selectable="true" outfunc="null" initialized="true" status="2" zoomfile="data/attachment/forum/201204/18/12052110j9eekungzrxa1a.jpg" file="data/attachment/forum/201204/18/12052110j9eekungzrxa1a.jpg" inpost="1" />
最后,进入mysql库的user表,修改数据库root超级账号的密码,如下图;
请注意,在编辑root的页面中,password这一行的函数这项里要选择加密方式。
修改后,重起服务器