Linux下的解決方法:
一. MySQL密碼的恢復方法之一
若是忘記了MySQL的root密碼,能夠用如下方法從新設置:
1. KILL掉系統裏的MySQL進程;
killall -TERM mysqld
2. 用如下命令啓動MySQL,以不檢查權限的方式啓動;
safe_mysqld --skip-grant-tables &
3. 而後用空密碼方式使用root用戶登陸 MySQL;
mysql -u root
4. 修改root用戶的密碼;
mysql> update mysql.user set password=PASSWORD('新密碼') where User='root';
mysql> flush privileges;
mysql> quit
從新啓動MySQL,就可使用新密碼登陸了。
二. MySQL密碼的恢復方法二
有可能你的系統沒有 safe_mysqld 程序(好比我如今用的 ubuntu操做系統, apt-get安裝的mysql) , 下面方法能夠恢復
1. 中止mysqld;
/etc/init.d/mysql stop
(您可能有其它的方法,總之中止mysqld的運行就能夠了)
2. 用如下命令啓動MySQL,以不檢查權限的方式啓動;
mysqld --skip-grant-tables &
3. 而後用空密碼方式使用root用戶登陸 MySQL;
mysql -u root
4. 修改root用戶的密碼;
mysql> update mysql.user set password=PASSWORD('newpassword') where User='root';
mysql> flush privileges;
mysql> quit
從新啓動MySQL
/etc/init.d/mysql restart
就可使用新密碼 newpassword 登陸了。
Windows下的解決方法:
一、編輯MySQL的配置文件:my.ini
通常在MySQL安裝目錄下有my.ini即MySQL的配置文件。
在此配置文件的最後添加以下一行:
skip-grant-tables 保存退出編輯。 二、而後重啓MySQL服務 在命令行下執行: net stop MySQL net start MySQL 三、設置新的ROOT密碼 而後再在命令行下執行: MySQL -u root -p MySQL或mysql -u root -p 直接回車無需密碼便可進入數據庫了。 此時,在命令行下執行 use mysql; 如今咱們執行以下語句把root密碼更新爲: update user set password=password("root") where user="root"; (注意:此時不用使用mysqladmin -u root -p password '你的新密碼'這條命令修改密碼,由於'skip-grant-tables'配置, 不信的話,你能夠試用一下,它確定會報以下所示的錯誤: F:\Documents and Settings\long>mysqladmin -u root -p password 'root' Enter password: Warning: single quotes were not trimmed from the password by your command line client, as you might have expected. mysqladmin: You cannot use 'password' command as mysqld runs with grant tables disabled (was started with --skip-grant-tables). Use: "mysqladmin flush-privileges password '*'" instead) exit 退出MySQL。 四、還原配置文件並重啓服務 而後修改MySQL配置文件把剛纔添加的那一行'skip-grant-tables'刪除。 再次重起MySQL服務,密碼修改完畢。 用新密碼root試一下吧,又能登入從新進入mysql了.