在安裝MYSQL時,默認會彈出密碼配置界面,顯示設置root用戶密碼,但是一旦忘記就只能想辦法重設了。重設ROOT密碼有如下幾種方法。
# ./mysqladmin -u root password 'newpassword'
# ./mysqladmin -u root -h host_name password 'newpassword'
Usually mysqladmin's path is /usr/bin, host_name is your real host name, e.g. localhost.localdomain.
password後面的引號不是必須的,不過如果密碼包含空格或者一些特殊的符號,需要用引號。
# ./mysql -u root
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');
mysql> SET PASSWORD FOR 'root'@'host_name' = PASSWORD('newpassword');
# ./mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('newpassword') WHERE User = 'root';
mysql> FLUSH PRIVILEGES;
1、停止MySQL進程
執行:/etc/init.d/mysql stop,具體位置可能隨系統不同而不同,也可能是/etc/init.d/mysql,/etc/init.d/mysqld等路徑,或下面直接終止(最好不要使用下面這個強制語句):
# killall -TERM mysqld
2、以安全模式啟動MySQL
# mysqld_safe –skip-grant-tables &
或,
# mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
提示:mysqld_safe一般在/usr/local/mysql/bin/目錄下。
3、登陸MYSQL
完成上述兩步以後就可以不用密碼進入MySQL了
# mysql -u root
或,
# /usr/local/mysql/bin/mysql -u root mysql
4、更改ROOT密碼
以下幾句依次執行:
use mysql;
select host, user, password from user;
update user set password=password(“newpassword”) where user=”root”
flush privileges;
5.退出控制台,重啟MYSQL服務
service mysqld restart
或,
/etc/init.d/mysql restart