参考链接:https://www.cnblogs.com/ThinkVenus/p/7670722.html
问题背景:登录mysql失败,密码错误,由此想到需要更改密码
然而,进入数据库后,只能看到information_schema/test这两个库,mysql数据库都看不到,导致无法更改user表的密码,这是权限出了问题。
解决步骤:
1.关闭Mysql:执行 service mysql stop
2.用安全模式启动Mysql,如果是自己通过tar包手动安装的Mysql,mysqld_safe命令在“${mysql}/bin”下
mysqld_safe --skip-grant-tables
执行这条命令后,当前ssh命令行会停住,如下图
3. 复制当前链接,再打开一个ssh连接,登录到mysql数据库
直接用命令mysql,就进入了数据了,不需要密码
修改mysql.user表
use mysql;
delete from `user`;
INSERT INTO `user` (`Host`, `User`, `Password`, `Select_priv`, `Insert_priv`, `Update_priv`, `Delete_priv`, `Create_priv`, `Drop_priv`, `Reload_priv`, `Shutdown_priv`, `Process_priv`, `File_priv`, `Grant_priv`, `References_priv`, `Index_priv`, `Alter_priv`, `Show_db_priv`, `Super_priv`, `Create_tmp_table_priv`, `Lock_tables_priv`, `Execute_priv`, `Repl_slave_priv`, `Repl_client_priv`, `Create_view_priv`, `Show_view_priv`, `Create_routine_priv`, `Alter_routine_priv`, `Create_user_priv`, `Event_priv`, `Trigger_priv`, `Create_tablespace_priv`, `ssl_type`, `ssl_cipher`, `x509_issuer`, `x509_subject`, `max_questions`, `max_updates`, `max_connections`, `max_user_connections`, `plugin`, `authentication_string`) VALUES
('%','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'',''),
('127.0.0.1','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'',''), ('::1','root','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'',''), ('localhost','','','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','','','','',0,0,0,0,'','');
4.重启mysql: service mysql restart
启动后登录mysql数据库,可以正常看到所有的数据库了。
5.设置mysql密码:进入到mysql的bin目录下,./mysqladmin -u root password '密码‘
6.修改密码命令:
在mysql系统外,使用mysql/bin/mysqladmin
# mysqladmin -u root -p password "123456"
Enter password: 【输入原来的密码】