mysql忘記root密碼與root帳號被刪除處理方法
說明:請根據自己主機系統及問題選擇對應步驟。
1、查找修改配置文件
修改my.ini(windows系統)或my.cnf(linux系統)文件,
windows系統:my.ini在mysql的安裝目錄下,如果是我司默認環境,路徑是:d:\SOFT_PHP_PACKAGE\mysql\my.ini
用記事本打開編輯在 [mysqld] 節下加入下面一行
[mysqld]
skip-grant-tables
linux系統:用ssh登陸服務器后執行mysql --help |grep my.cnf 回車
[root@west9853 etc]# mysql --help |grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /www/wdlinux/etc/my.cnf ~/.my.cnf
可查找到my.cnf位置,然后執行vi /www/wdlinux/etc/my.cnf 回車,按 i 進入編輯狀態
在 [mysqld] 下加入下面一行
[mysqld]
skip-grant-tables
按esc,再按: 輸入wq 回車
2.重啟 MySQL 服務
windows系統在運行里面輸入services.msc,打開服務從中找到mysql,然后右鍵選擇重啟。
linux系統執行service mysqld restart
3.重置密碼
windows系統:打開 開始-運行,輸入cmd回車,在彈出的黑色窗口中輸入:
mysql 回車
use mysql;回車
linux系統:輸入:
mysql 回車
use mysql;回車
(1)如果是誤刪了root用戶,則使用下面的語句創建root用戶:
insert into user set Host='localhost',User='root',Password=Password('111111'),select_priv='y', insert_priv='y',update_priv='y',Alter_priv='y',delete_priv='y',create_priv='y',drop_priv='y',
reload_priv='y',shutdown_priv='y',Process_priv='y',file_priv='y',grant_priv='y',References_priv='y',
index_priv='y',create_user_priv='y',show_db_priv='y',super_priv='y',create_tmp_table_priv='y',
Lock_tables_priv='y',execute_priv='y',repl_slave_priv='y',repl_client_priv='y',create_view_priv='y',
show_view_priv='y',create_routine_priv='y',alter_routine_priv='y',Event_priv='Y',Trigger_priv='Y';
(注意:復制語句后,把回車換行符去掉)
(2)如果是忘記了root用戶的密碼,使用下面的語句修改root密碼輸入(注意版本):
mysql5.1、mysql5.6版本
update user set Password=Password('111111') where User='root'; //按回車,其中111111換成您需要設定新的密碼
flush privileges; //刷新權限
mysql5.7版本
update mysql.user set authentication_string = password('testpassword') where user='root'; //testpassword替換為需要設定的新密碼
flush privileges; //刷新權限
4.還原mysql配置
windows系統:
找到my.ini或my.cnf文件,將[mysqld] 節下面的skip-grant-tables 去掉,或者前面加上#字符保存以后重啟mysql服務。
linux系統:
vi /www/wdlinux/etc/my.cnf,按上下鍵移動找到skip-grant-tables 連按兩次d即可刪除,然后輸入:wq保存退出,輸入service mysqld restart重啟
來源:西部數碼