windows下重置mysql的root密碼方法介紹
今天發(fā)現(xiàn) WordPress 連接不上數(shù)據(jù)庫(kù),登錄 window server 服務(wù)器查看,所有服務(wù)均運(yùn)行正常。
使用 root 賬號(hào)登錄 mysql 數(shù)據(jù)庫(kù),結(jié)果提示密碼不匹配。我突然意識(shí)到,服務(wù)器可能遭受到 SQL注入 攻擊了……
至于事故發(fā)生的原因和之后所做的補(bǔ)救措施,以后有機(jī)會(huì)我會(huì)聊一聊的。這里我主要講一下 mysql 用戶密碼的重置步驟。
重置 root 密碼
在忘記 root 密碼的情況下,可以進(jìn)入 mysql 的安全模式,重置 root 密碼。
1. 停止 MySQL 服務(wù)
打開命令提示符窗口,輸入 net stop mysql 關(guān)閉 MySQL 服務(wù)。
C:\Users\Administrator>net stop mysql57 MySQL57 服務(wù)正在停止.. MySQL57 服務(wù)已成功停止。
↑ 服務(wù)名稱不一定都是 mysql,比如我的就是 mysql57,57代表版本號(hào)為5.7
當(dāng)然你也可以通過(guò)計(jì)算機(jī)管理面板關(guān)閉 MySQL 服務(wù)。
2. 切換到 bin 目錄
在命令提示符窗口中,通過(guò) cd 命令切換到 mysql 安裝目錄下的 bin 目錄。
C:\Users\Administrator> cd C:\Program Files\MySQL\MySQL Server 5.7\bin C:\Program Files\MySQL\MySQL Server 5.7\bin>
↑ 默認(rèn)安裝目錄為 C:\Program Files\MySQL\MySQL Server
3. 進(jìn)入安全模式
在 bin 目錄下輸入 mysqld --skip-grant-tables
,跳過(guò)權(quán)限檢查啟動(dòng) mysql。
如果你配置了 my.ini 文件,則需要將其引入: mysqld --defaults-file="../my.ini" --skip-grant-tables
[mysqld] basedir = "C:\ProgramData\MySQL\MySQL Server 5.7" datadir = "C:\ProgramData\MySQL\MySQL Server 5.7\Data"
↑ 我在 my.ini 文件中指定了數(shù)據(jù)的存放路徑,如果不引入配置文件,則會(huì)提示 No such file or directory 錯(cuò)誤。
4. 重置賬戶密碼
打開另一個(gè)命令提示符窗口(別關(guān)閉安全模式窗口),同樣切換到 mysql \ bin 目錄,輸入 mysql 跳過(guò)權(quán)限驗(yàn)證連接數(shù)據(jù)庫(kù)。
C:\Program Files\MySQL\MySQL Server 5.7\bin>mysql Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
↑ 也可以指定連接參數(shù) mysql -u <用戶名> -p <密碼> -h <連接地址> -P <端口號(hào)> -D <數(shù)據(jù)庫(kù)>
執(zhí)行 update mysql.user set authentication_string="" where user="root"; 重置 root 用戶的密碼(5.7 之前為 password 字段)。
mysql> update mysql.user set authentication_string="" where user="root"; Query OK, 1 row affected (0.00 sec) mysql> select user,authentication_string from mysql.user\G *************************** 1. row *************************** user: root authentication_string: *************************** 2. row *************************** user: mysql.sys authentication_string: *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE 2 rows in set (0.00 sec)
↑ root 用戶的 authentication_string 字段已經(jīng)被清空了
5. 刷新權(quán)限表
執(zhí)行 flush privileges; 命令刷新權(quán)限表,密碼已經(jīng)重置完成,輸入 quit 退出。
mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> quit Bye
關(guān)閉所有命令提示符窗口,通過(guò)任務(wù)管理器結(jié)束 mysqld.exe 進(jìn)程。重啟 MySQL 服務(wù),之后就可以直接登錄 root 賬號(hào)了。
修改 root 密碼
出于安全考慮,root 密碼不宜為空,所以需要在密碼重置之后,再重新設(shè)置一個(gè)密碼。
方法一:SET PASSWORD
SET PASSWORD FOR "username"=PASSWORD("new password");
以 root 身份登錄 mysql,再使用 set password 命令修改密碼:
mysql> set password for root@localhost = password("pswd"); Query OK, 0 rows affected, 1 warning (0.00 sec)
方法二:mysqladmin
mysqladmin -u "username" -p password "new password"
執(zhí)行該命名之后會(huì)提示輸入原密碼,輸入正確后即可修改。
C:\Program Files\MySQL\MySQL Server 5.7\bin> mysqladmin -u root -p password pswd Enter password: **** mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
方法三:UPDATE TABLE
UPDATE mysql.user SET authentication_string=PASSWORD("new password") WHERE user="username";
在重置 root 密碼的同時(shí),也可以設(shè)置默認(rèn)密碼。不過(guò)密碼不能為明文,必須使用 password() 函數(shù)加密。
mysql> update mysql.user set authentication_string=password("pswd") where user="root"; Query OK, 1 row affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec)
總結(jié)
以上就是本文關(guān)于windows下重置mysql的root密碼方法介紹的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
MySQL數(shù)據(jù)庫(kù)設(shè)計(jì)之利用Python操作Schema方法詳解
mysql中使用instr進(jìn)行模糊查詢方法介紹
MySQL中or語(yǔ)句用法示例
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。