[Ubuntu] MySQL誤刪root忘記密碼

剛剛不小心誤刪root…真是悲劇

1.關閉服務
#/etc/init.d/mysql stop

2.安全模式啟動,不檢查權限
#mysqld_safe –-skip-grant-tables &

3.進入mysql指令
#mysql

4.選擇mysql資料庫
mysql> use mysql

5.加入名為root的user
mysql> insert into user set user=’root’;

6.更新密碼
mysql> update user set password=password(‘123456′) where user=’root’;

7.更新權限設定
mysql> update user set Host=’localhost’,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’,create_user_priv=’y’,Event_priv=’y’,Trigger_priv=’y’ where user=’root’;

8.套用新權限
mysql> flush privileges;

9.離開mysql
mysql> quit

10.強制關閉mysql服務
#killall mysqld

11.正常啟動mysql
#/etc/init.d/mysql start