How to reset root password in MySQL 8

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

Resetting the MySQL root password, particularly in MySQL 8, requires bypassing authentication to update the user table. While MySQL 5 allowed a straightforward approach using the --skip-grant-tables option, MySQL 8 necessitates a different strategy. The recommended method involves stopping the MySQL service, creating an initialization file with an ALTER USER SQL command to set the new password, and then restarting MySQL with the --init-file option pointing to this script. Alternatively, for MySQL 8, one can start the service using --skip-grant-tables along with --console and --shared-memory, then log in as root with an empty password to execute an UPDATE statement on the mysql.user table.

รหัสผ่านผู้ใช้ใน MySQL ถูกจัดเก็บไว้ในตาราง user การรีเซ็ตรหัสผ่านคือการเปลี่ยนค่าของเร็กคอร์ดในตารางนี้ ในการเปลี่ยนรหัสผ่านในกรณีที่ลืมรหัสผ่าน แนวคิดคือการข้ามการตรวจสอบสิทธิ์ของ MySQL และเข้าสู่ระบบและอัปเดตค่ารหัสผ่านเร็กคอร์ดด้วยคำสั่ง SQL

ใน MySQL 5 เราสามารถเริ่มบริการ MySQL ด้วยตัวเลือก --skip-grant-tables ตัวเลือกนี้จะบอกให้บริการข้ามการโหลดตารางสิทธิ์เมื่อเริ่มต้น ดังนั้นผู้ใช้ root สามารถเข้าสู่ระบบด้วยรหัสผ่านว่างเปล่าได้

mysqld –skip-grant-tables

 หลังจากเข้าสู่ระบบ สามารถรันคำสั่ง SQL ด้านล่างเพื่อเปลี่ยนรหัสผ่าน

UPDATE user SET authentication_string='' WHERE user='root';

ซึ่งจะตั้งรหัสผ่านเป็นค่าว่างเปล่า

อย่างไรก็ตาม ดูเหมือนว่า --skip-grant-tables ไม่สามารถทำงานได้อย่างถูกต้องหากไม่มีการปรับแต่งบางอย่างใน MySQL 8 แล้วเราจะทำอย่างไรได้บ้าง มีสองตัวเลือกที่เป็นไปได้

  1. สร้างไฟล์ init และรันบริการ MySQL ด้วยตัวเลือก --init-file ในไฟล์ init ให้ใส่คำสั่ง SQL ซึ่งใช้เพื่ออัปเดตค่ารหัสผ่าน
  2. เจาะลึกในการตรวจสอบวิธีการใช้ --skip-grant-tables ใน MySQL 8

มาดูว่าตัวเลือกเหล่านี้ทำงานอย่างไร

ตัวเลือกที่ 1: ตัวเลือก --init-file

ตัวเลือกนี้จะระบุไฟล์ที่มีคำสั่ง SQL ที่จะดำเนินการก่อนที่บริการจะเริ่มต้น ดังนั้น เราเพียงแค่ต้องใส่คำสั่งเพื่ออัปเดตรหัสผ่านในไฟล์นี้และเริ่มบริการ MySQL ด้วยตัวเลือกนี้ รหัสผ่านจะถูกรีเซ็ต/อัปเดต

ขั้นตอนที่ 1: หยุดบริการ MySQL

net stop mysql

ขั้นตอนที่ 2: สร้างไฟล์ txt และใส่คำสั่งด้านล่าง

ALTER USER 'root'@'localhost' IDENTIFIED BY '';

ขั้นตอนที่ 3: เริ่มบริการบนบรรทัดคำสั่งและด้วยตัวเลือก --init-file

mysqld --init-file=/some/path/to/cmd.txt --console

เสร็จสิ้น หมายเหตุ: โปรดรันคำสั่งในฐานะผู้ใช้ที่มีสิทธิ์เมื่อคุณพบข้อผิดพลาดเกี่ยวกับการปฏิเสธสิทธิ์ หากคุณเห็นข้อผิดพลาดเช่น:

2018-12-25T02:51:23.739089Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.13) starting as process 1912
2018-12-25T02:51:23.759426Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2018-12-25T02:51:23.761196Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2018-12-25T02:51:23.762550Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-12-25T02:51:23.766230Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.

โปรดรันคำสั่งด้านล่างเพื่อเริ่มต้นไดเรกทอรีข้อมูล

mysqld --initialize --console

ตัวเลือกที่ 2: ใช้ตัวเลือก --skip-grant-tables

เมื่อเทียบกับ MySQL 5 จำเป็นต้องเพิ่มตัวเลือกเพิ่มเติมใน MySQL 8 

mysqld --console --skip-grant-tables --shared-memory

หลังจากเริ่มบริการ ให้เข้าสู่ระบบด้วยรหัสผ่านว่างเปล่า

mysql -u root

จากนั้นดำเนินการคำสั่ง SQL เพื่ออัปเดตรหัสผ่าน

UPDATE mysql.user SET authentication_string='' WHERE user='root' and host='localhost';

โดยปกติแล้วจะแนะนำตัวเลือกที่ 1 และหวังว่าสิ่งเหล่านี้จะเป็นประโยชน์

MYSQL PASSWORD MYSQL 8

  RELATED

  COMMENTS

5
Anonymous
Feb 7, 2019 at 2:38 am

2019-02-07T08:33:35.790267Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.14) starting as process 9932
2019-02-07T08:33:35.793300Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Program Files\MySQL\MySQL Server 8.0\data\DESKTOP-7CTR7J5.lower-test
2019-02-07T08:33:35.793361Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Program Files\MySQL\MySQL Server 8.0\data\DESKTOP-7CTR7J5.lower-test
2019-02-07T08:33:35.793534Z 0 [ERROR] [MY-013276] [Server] Failed to set datadir to 'C:\Program Files\MySQL\MySQL Server 8.0\data\' (OS errno: 2 - No such file or directory)
2019-02-07T08:33:35.807438Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-02-07T08:33:35.807626Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.14)  MySQL Community Server - GPL.

Hi! Thanks for the guide. I am getting this error tho.  Grateful for your help.

Ke Pi
Feb 7, 2019 at 10:09 am

Can you create 'C:\Program Files\MySQL\MySQL Server 8.0\data directory and try again? 

jamil
Apr 12, 2019 at 8:18 pm

I tried the first option. Now my old password AND new password both don't work... Worst comes to worst. I'm just going to delete mysql, and re-download it...

Nikhil Karanjkar
Oct 12, 2019 at 10:45 am

This saved my day. Thank you

Anonymous
Oct 24, 2019 at 2:08 pm

Tested directly Option2 and it worked.