場景一:重置root密碼
mysql登錄密碼為password()算法加密,解密成本太高,以下為通用方案;
原理:mysql提供了特殊啟動(dòng)方式,即跳過權(quán)限表驗(yàn)證,啟動(dòng)后,登錄不需要提供密碼;
登錄后,即可修改mysql數(shù)據(jù)庫的user表,重置密碼,然后刷新權(quán)限,重啟mysql服務(wù)即可;
注意:此時(shí)mysql服務(wù)將面臨高風(fēng)險(xiǎn),請?jiān)诤线m時(shí)間執(zhí)行;
#停止正在運(yùn)行的mysql服務(wù)
service mysqld stop
#以--skip-grant-tables選項(xiàng)啟動(dòng)服務(wù),跳過權(quán)限表驗(yàn)證,有2種方式
方式1:指定運(yùn)行選項(xiàng),只在本次啟動(dòng)生效
./bin/mysqld_safe --skip-grant-tables --user=root & 如果本機(jī)沒有mysqld_safe服務(wù),運(yùn)行mysqld效果相同
方式2:修改配置文件,使用service、systemctl啟動(dòng)均生效
修改配置文件my.cnf,添加 skip-grant-tables my.cnf可能存在多個(gè),請使用 sudo mysql --help | grep my.cnf 或 mysql --help | grep 'Default options' -A 1 確認(rèn)加載順序
#root賬號(hào)登錄mysql,此時(shí)不需要提供密碼 mysql -uroot #切換到mysql數(shù)據(jù)庫,登錄賬號(hào)與權(quán)限在此數(shù)據(jù)庫中 use mysql; #查看mysql版本 select version(); #查看當(dāng)前賬戶信息,根據(jù)mysql版本執(zhí)行 #5.7+版本密碼為authentication_string(生效),password; #5.7-版本密碼為password #user=用戶名,host=登錄IP,即允許該賬戶登錄的IP地址,每個(gè)IP一條user表記錄,%表示任意IP select user,host,authentication_string,password from user where user='root'; #5.7+設(shè)置密碼 update user set authentication_string=password('password') where user='root'; --and Host='localhost'; #5.7-設(shè)置密碼 update mysql.user set password=password('password') where user='root'; --host='localhost'; #5.7+支持2個(gè)密碼字段,直接設(shè)置2個(gè),生效為authentication_string update user set authentication_string=password('password'),password=password('password') where user='root' ; --and Host='localhost'; #刷新權(quán)限表 flush privileges; #退出mysql連接 quit; #重啟mysql服務(wù) service mysqld restart
場景二:增加賬號(hào)與授權(quán)
以上--skip-grant-tables模式不驗(yàn)證權(quán)限,同時(shí)無法增加賬號(hào)授權(quán),所以增加賬號(hào)的登錄IP,需要以正常模式啟動(dòng)登錄
#密碼登錄重啟后的mysql服務(wù) mysql -u root -p #切換mysql數(shù)據(jù)庫,賬號(hào)和權(quán)限在此數(shù)據(jù)庫 use mysql; #增加賬號(hào)授權(quán) grant all privileges on *.* to "root"@"ip" identified by "password" with grant option; #刷新權(quán)限表 flush privileges; #退出mysql連接 quit; #無需重啟服務(wù)
場景三:修改登錄密碼
1> 更新mysql.user表,需要登錄MySQL執(zhí)行,需要刷新權(quán)限列表生
mysql> use mysql; #5.7前后版本密碼字段不一致,且 user 表同時(shí)存在2個(gè)字段 # mysql5.7之前 mysql> update user set password=password('123456') where user='root' and host='localhost'; # mysql5.7之后 mysql> update user set authentication_string=password('123456') where user='root' and host='localhost'; mysql> flush privileges; #刷新權(quán)限列表
2> 用set password命令,需要登錄MySQL執(zhí)行,自動(dòng)刷新權(quán)限列表
語法:set password for '用戶名'@'域'=password(‘密碼’) mysql> set password for 'root'@'localhost'=password('123456');
3> alter user命令,需要登錄MySQL執(zhí)行,自動(dòng)刷新權(quán)限列表
語法:ALTER USER '用戶名'@'域' IDENTIFIED BY 'xxxx'; #初始化時(shí)root賬號(hào)只有l(wèi)ocalhost mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
4> grant命令,需要登錄MySQL執(zhí)行,自動(dòng)刷新權(quán)限列表
語法:GRANT 權(quán)限列表(逗號(hào)分隔) ON 數(shù)據(jù)庫.數(shù)據(jù)表 TO '用戶'@'域' IDENTIFIED BY '密碼'; #grant語句自動(dòng)創(chuàng)建用戶以及設(shè)置密碼 #權(quán)限支持 create、update、select、lete、drop、execute等,也可以指定 all privileges 授權(quán)所有權(quán)限 #grant語句最后可以指定 WITH GRANT OPTION 指定用戶可以將權(quán)限傳遞授權(quán)給其他用戶。 #數(shù)據(jù)庫與數(shù)據(jù)表支持 * 指定全部,如 testdb.* 或 *.*,其他情況只能一條授權(quán)一個(gè)數(shù)據(jù)表 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.11.31' IDENTIFIED BY 'Mysql.pass.123' WITH GRANT OPTION;
5> mysqladmin,無需登錄MySQL執(zhí)行,自動(dòng)刷新權(quán)限列表
語法:mysqladmin -u用戶名 -p舊的密碼 password 新密碼 #該方式為明文傳輸密碼,不安全 [root@localhost ~]# mysqladmin -uroot -p123456 password 1234abcd mysqladmin: [Warning] Using a password on the command line interface can be insecure. New password: Confirm new password: Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
審核編輯:黃飛
-
MySQL
+關(guān)注
關(guān)注
1文章
849瀏覽量
27571 -
root
+關(guān)注
關(guān)注
1文章
86瀏覽量
21628
原文標(biāo)題:MySQL忘記root密碼解決方案
文章出處:【微信號(hào):magedu-Linux,微信公眾號(hào):馬哥Linux運(yùn)維】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評(píng)論請先 登錄
MySQL root密碼忘記怎么辦?
不知道root密碼怎么修改
windowsxp忘記密碼
若忘記了Linux系統(tǒng)的root密碼,該怎么辦?
工控機(jī)忘記密碼的解決方法
不知道嵌入式Linux系統(tǒng)下的root密碼,修改新密碼并進(jìn)入系統(tǒng)

忘記Mysql用戶密碼怎么辦

評(píng)論