MySQL的root密码忘记或丢失的解决方法一例

-bash-4.2$ service mysql status
 SUCCESS! MysqL running (2670)
-bash-4.2$ MysqL -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

以下是密码忘记的解决方法:

修改MysqL的参数设置: 
# vi /etc/my.cnf 
在[MysqLd]的段最后一行加上一句:skip-grant-tables 
例如: 
[MysqLd] 
........................
log-bin=MysqL-bin
innodb_data_file_path = ibdata1:12M;ibdata2:100M;ibdata3:300M:autoextend
innodb_file_per_table=on
max_connections=500
skip-grant-tables    --新增加
保存并且退出vi.

重启MysqL服务:
[root@chenfeng subsys]# service MysqL restart
Shutting down MysqL. SUCCESS! 
Starting MysqL. SUCCESS! 

-bash-4.2$ MysqL    --不用输密码直接登录
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MysqL connection id is 2
Server version: 5.6.15-log Source distribution


copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MysqL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenfeng           |
| MysqL              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)


MysqL [(none)]> 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 [MysqL]>  UPDATE user SET Password = password ('12345678') WHERE User ='root';   --修改为新密码12345678
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

MysqL [MysqL]> 
MysqL [MysqL]> flush privileges;   --刷新一下权限表
Query OK, 0 rows affected (0.00 sec)

MysqL [MysqL]> quit
Bye

MysqL的参数设置修改回来:
# vi /etc/my.cnf 
将刚才在[MysqLd]的段中最后一行加上的skip-grant-tables删除,然后保存退出
 
重启MysqL服务:
[root@chenfeng subsys]# service MysqL restart
Shutting down MysqL. SUCCESS! 
Starting MysqL. SUCCESS! 


重新用新密码登录:
-bash-4.2$ MysqL -u root -p12345678      --12345678为新密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MysqL connection id is 7
Server version: 5.6.15-log Source distribution


copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


MysqL [(none)]> 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 [MysqL]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenfeng           |
| MysqL              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MysqL [MysqL]> 

相关文章

优化MySQL数据库发布系统存储的方法有:1.mysql库主从读写分...
使用mysql的方法:在“我的电脑”→右键→“管理”→“服务”...
在mysql中查看root用户权限的方法:1.命令行启动mysql服务;...
MySQL主从复制是用来备份一个与主数据库一样环境的从数据库,...
运行mysql的方法1.启动mysql服务,在“我的电脑”→右键→“...
开启mysql的方法1.可以通过快捷键win+r,输入cmd,打开窗口,...