要连接到某服务的数据库,但是忘记了root密码,试了几次都无法登陆
1.在/etc/my.cnf [MySQLd] 配置部分添加"skip-grant-tables"
vim /etc/my.cnf
[mysqld]
skip-grant-tables
2.重启mysql服务
systemctl restart mysqld
3.登入mysql
mysql -uroot -p123 #密码随便输入都可以
4.刷新一下库权限
mysql> flush privileges;
如果不执行这句话,会报以下错误! ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
5.新增一个远程管理员账号,然后退出数据库
grant all privileges on *.* to 'admin'@'%' identified by 'foshan123' with grant option;
6.去除配置后重启数据库
(1)注释/etc/my.cnf中skip-grant-tables
(2)执行systemctl restart mysqld命令重启数据库