数据库mysql的user授权表默认有三条root账号记录,如图。
如操作误删除,同时没有正确插入,再次登陆mysql会报错如下:
ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)
启动到安全模式下重新授权也无法成功登陆,因安全模式已经跳过授权表,报错如下:
ERROR 1290 (HY000): The MySQL server is running with the –skip-grant-tables option so it cannot execute this statement
可参考如下步骤处理,建议在操作前创建磁盘快照进行备份,避免操作失误后可以迅速恢复。
1、修改mysql配置文件启动到安全模式下:
命令:vi /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
保存并退出vi编辑器,重新启动mysqld
命令:/etc/init.d/mysqld restart
2、登陆mysql,并重新插入
命令:/usr/bin/mysql
命令:use mysql;
密码用password函数加密,比如密码为“123456”:
mysql> select password(‘123456);
mysql> 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,max_questions,max_updates,max_connections,max_user_connections) values (“localhost”,”root”,”*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9”,”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”);
命令:flush privileges;
命令:quit
3、打开mysql配置文件,注释”skip-grant-tables” ,保存并退出
重启mysql后测试登陆正常
命令:/etc/init.d/mysqld restart
命令:mysql -uroot -paliyun