????????????
哈嘍!大家好,我是【IT邦德】,江湖人稱jeames007,10余年DBA工作經(jīng)驗
一位上進心十足的【大數(shù)據(jù)領(lǐng)域博主】!??????
中國DBA聯(lián)盟(ACDU)成員,目前從事DBA及程序編程
擅長主流數(shù)據(jù)Oracle、MySQL、PG 運維開發(fā),備份恢復(fù),安裝遷移,性能優(yōu)化、故障應(yīng)急處理等。
? 如果有對【數(shù)據(jù)庫】感興趣的【小可愛】,歡迎關(guān)注【IT邦德】??????
??????感謝各位大可愛小可愛!??????
前言
最近有粉絲和網(wǎng)友常問到,如何找回MySQL管理員root的密碼的方法,總結(jié)以下方法分享給大家?? 1.init-file找回
在MySQL中,若root密碼丟失則無法直接找回,只能通過特殊方式來修改密碼。
步驟1:先停止MySQL服務(wù) Kill -9 進程號
步驟2:編輯修改密碼文件
alter user 'root'@'%' identified by 'jeames';
alter user 'root'@'localhost' identified by 'jeames';
步驟3:用如下方法啟動MySQL
mysqld_safe --defaults-file=/etc/my.cnf --init-file=/tmp/mysql-init.sql &
步驟4:修改密碼
alter user root@'localhost' identified with mysql_native_password by 'root';
alter user root@'%' identified with mysql_native_password by 'root';
flush privileges;
步驟4:關(guān)閉數(shù)據(jù)庫后重啟
若是 Windows 服務(wù),則可以通過如下命令啟動:
D:\MySQL\mysql-8.0.15-win64\bin\mysqld --defaults-file=D:\MySQL\mysql-8.0.15-win64\data803314\mysql803314.ini --init-file=d:\mysql-init.sql --console
?????? 注意,此時可以以任意一個密碼登陸也可以以一個空密碼登陸 MySQL
?? 2.skip-grant-tables找回
步驟1:先停止MySQL服務(wù)
步驟2:啟動 MySQL 服務(wù)
mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-tables &
注意,此時可以以任意一個密碼登陸也可以以一個空密碼登陸 MySQL
若 MySQL 是 8.0 且安裝在 Windows 上,則需要加上–shared-memory 參數(shù):
G:\mysql-8.0.23-winx64\bin\mysqld --datadir=G:\mysql-8.0.23-winx64\data80323308 --console --skip-grant-tables --shared-memory
然后再開一個窗口,執(zhí)行下面命令,此種方法使用內(nèi)存的方式啟動
cd G:\mysql-8.0.23-winx64\bin
?? 3.修改參數(shù)文件找回
1.用命令編輯/etc/my.cnf配置文件,即:vim /etc/my.cnf 或者 vi /etc/my.cnf
2.在[mysqld]下添加skip-grant-tables,然后保存并退出
3.重啟mysql服務(wù):service mysqld restart
4.更改root用戶名
重啟以后,執(zhí)行mysql命令進入mysql命令行
5.修改root用戶密碼,此處注意,有時候會報不允許修改,先flush privileges再執(zhí)行即可
--5.7版本
SQL> update mysql.user set authentication_string=password('root') where user='root';
SQL> flush privileges;
--8.0版本
mysql> alter user root@'localhost' identified with mysql_native_password by '1';
--查詢
mysql> select user,host,grant_priv,super_priv,authentication_string,password_last_changed
from mysql.user;
6.把/etc/my.cnf中的skip-grant-tables注釋掉,然后重啟mysql,即:service mysqld restart
好了,下面就可以用root新的密碼登錄了!
文章來源:http://www.zghlxwxcb.cn/news/detail-443683.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-443683.html
到了這里,關(guān)于教你找回MySQL管理員root密碼的3個妙招的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!