windows 下重設 mysql 的 root 密碼

來源:互聯網
上載者:User

標籤:mysq   注入   版本號碼   for   進程   and   ota   ima   rect   

今天發現 WordPress 串連不上資料庫,登入 window server 伺服器查看,所有服務均運行正常。

使用 root 帳號登入 mysql 資料庫,結果提示密碼不匹配。我突然意識到,伺服器可能遭受到 SQL注入 攻擊了……

至於事故發生的原因和之後所做的補救措施,以後有機會我會聊一聊的。這裡我主要講一下 mysql 使用者密碼的重設步驟。

 

重設 root 密碼

在忘記 root 密碼的情況下,通過 mysql 的安全模式重設 root 密碼。

 

1. 停止 MySQL 服務

開啟命令提示字元視窗,輸入   net stop mysql   關閉 MySQL 服務。

C:\Users\Administrator>net stop mysql57MySQL57 服務正在停止..MySQL57 服務已成功停止。

↑ 服務名稱不一定都是 mysql,比如我的就是 mysql57,57代表版本號碼為5.7

當然你也可以通過電腦管理面板關閉 MySQL 服務。

 

2. 切換到 bin 目錄

在命令提示字元視窗中,通過 cd 命令切換到 mysql 安裝目錄下的 bin 目錄。

C:\Users\Administrator>

cd C:\Program Files\MySQL\MySQL Server 5.7\binC:\Program Files\MySQL\MySQL Server 5.7\bin>

↑ 預設安裝目錄為 C:\Program Files\MySQL\MySQL Server

 

3. 進入安全模式

在 bin 目錄下輸入   mysqld --skip-grant-tables   ,跳過許可權檢查啟動 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 檔案中指定了資料的存放路徑,如果不引入設定檔,則會提示 No such file or directory 錯誤。

 

4. 重設賬戶密碼

開啟另一個命令提示字元視窗(別關閉安全強制回應視窗),同樣切換到 mysql \ bin 目錄,輸入   mysql   跳過許可權驗證串連資料庫。

C:\Program Files\MySQL\MySQL Server 5.7\bin>mysqlWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 7Server 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>

↑ 也可以指定串連參數 mysql -u <使用者名稱> -p <密碼> -h <串連地址> -P <連接埠號碼> -D <資料庫>

 

執行   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: rootauthentication_string:*************************** 2. row ***************************                 user: mysql.sysauthentication_string: *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE2 rows in set (0.00 sec)

↑ root 使用者的 authentication_string 欄位已經被清空了

 

5. 重新整理許可權表

執行   flush privileges;   命令重新整理許可權表,密碼已經重設完成,輸入   quit   退出。

mysql> flush privileges;Query OK, 0 rows affected (0.02 sec)mysql> quitBye

關閉所有命令提示字元視窗,通過工作管理員結束 mysqld.exe 進程。重啟 MySQL 服務,之後就可以直接登入 root 帳號了。

 

修改 root 密碼

出於安全考慮,root 密碼不宜為空白,所以需要在密碼重設之後,再重新設定一個密碼。

 

方法一:SET PASSWORD
  • SET PASSWORD FOR "username"=PASSWORD("new password");

以 root 身份登入 mysql,再使用 set password 命令修改密碼:

mysql> set password for [email protected] = password("pswd");Query OK, 0 rows affected, 1 warning (0.00 sec)

 

方法二:mysqladmin
  • mysqladmin -u "username" -p password "new password"

執行該命名之後會提示輸入原密碼,輸入正確後即可修改。

C:\Program Files\MySQL\MySQL Server 5.7\bin> mysqladmin -u root -p password pswdEnter 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 password=PASSWORD("new password")
  • WHERE user="username";

在重設 root 密碼的同時,也可以設定預設密碼(5.7 中為 authentication_string 欄位)。

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)

 

windows 下重設 mysql 的 root 密碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.