Lấy lại mật khẩu mysql

Đầu tiên các bạn đều phải stop Mysql service ( Administrative Tools >>> Services )

Phương pháp 1 (skip grant tables)

  1. Mở Command Prompt (cmd) và tìm đến thư mục bin nơi cài đặt MySQL VD:C:/Program Files/Mysql/bin
  2. Chạy lệnh
    mysqld.exe -u root –skip-grant-tables
  3. Tương tự như trên, bạn tiếp tục mở command prompt mới và nhập lệnh
    mysql

    cũng ngay tại đường dẫn thư mục cài đặt MySQL

    use mysql

    (Command này để kết nối MySQL server)

  4. Tiếp theo chúng ta sẽ khôi phục MySQL Root Password bằng lệnh thông dụng :
    UPDATE user SET Password = PASSWORD(‘mat_khau_moi_cua_ban’) WHERE User = ‘root’;
  5. Start lại MySQL service.

Phương pháp 2 (init file)

  1. Mở NotePad trên máy chủ và tạo ra một text file như sau:
    UPDATE mysql.user SET Password=PASSWORD(‘mat_khau_moi_cua_ban’) WHERE User=’root’;
    FLUSH PRIVILEGES;
  2. Lưu file trên vào một vị trí dễ nhớ (ví dụ như C: ) với tên: mysql-init.txt
  3. Mở Command Prompt (cmd) và khởi động MySQL với lựa chọn init-file ( lưu ý thay đổi đường dẫn của mysqld đến vị trí thực tế trên máy chủ VD:C:/Mysql/bin) :
    C:mysqlbinmysqld –init-file=C:\mysql-init.txt

    Nếu MySQL đã được cài đặt bằng cách sử dụng MySQL Installation Wizard , thì bạn có thể sử dụng lệnh sau :

    C:> "C:Program FilesMySQLMySQL Server 5.0binmysqld-nt.exe"
    --defaults-file="C:\Program Files\MySQL\MySQL Server 5.0\my.ini"
    --init-file=C:\mysql-init.txt
  4. Stop MySQL ( từ Services ) và sau đó Start lại một lần nữa , đồng thời xóa file mysql-init.txt cho mục đích an toàn hệ thống ( vì file có lưu mật khẩu )

Tip : trường hợp các bạn không thể xác định chính xác thư mục cài đặt MySQL thì hãy tìm kiếm tại Services Manager. Từ Administrative Tools >>> Services >>> Properties MySQL Server >>> Path to executable.

Bây giờ bạn có thể đăng nhập bằng mật khẩu mới vừa thiết lập.

Chúc các bạn thành công!

Lấy lại mật khẩu mysql

Lâu lâu không chịu nhớ mật khẩu MySQL, toàn ghi chú rồi copy - paste, giờ lỡ tay làm mất cái file lưu pass nên lại phải reset lại mật khẩu. Làm cái note này để ghi chú lại. :D

Yêu cầu để có thể thực hiện:

Bạn phải có quyền thực thi commandline trên hệ thống (1 cách nào đó trực tiếp hoặc gián tiếp) còn nếu không có quyền này thì khỏi cần đọc tiếp. (Remote qua Telnet, SSH, Netcat… hay cái gì đại loại thế - hoặc ngồi trực tiếp với hệ thống)

1. Máy chạy Windows

Bước 1. Tắt process MySQL đang chạy (nếu có)

  • Kill Process (nếu bạn ko chạy nó như một service)

  • Stop Service: Start -> Run -> services.msc. Tìm đến service mysql -> Stop. Hoặc gõ lệnh thông qua commandline:

  net stop mysql

Bước 2. Tạo 2 file

  • Tạo file config.txt có nội dung vào trong ổ C (hoặc nơi nào đó tùy bạn):
  UPDATE mysql.user SET Password=PASSWORD('newpass') WHERE User='root';
  FLUSH PRIVILEGES;
  • Tạo file run.bat có nội dung như hướng dẫn sau:

    • Nếu bạn cài MySQL thông qua MySQL Installation Wizard
    set MysqlPath=C:\Program Files\MySQL\MySQL Server 5.1
    cd %MysqlPath%\bin
    mysqld --defaults-file="%MysqlPath%\\my.ini" --init-file=C:\\config.txt
    
    • Nếu sử dụng MySQL dạng portable như trong XAMMP, WAMP Trường hợp này lưu luôn file run.bat này vào thư mục chứa file thực thi của MySQL cho gọn. text mysqld-nt --init-file=C:\mysql-init.txt Xong, chạy file run.bat này. Password sẽ được đưa về thành newpass - bạn có thể đổi thành bất cứ thứ gì bạn muốn. :D

Một cách khác Gọn hơn chút

  • Đầu tiên là phải tắt service MySQL như cách trên.

  • Muốn sử dụng dòng lệnh cho bờ rồ thì làm như sau: Change directory đến thư mục có file thực thi của MySQL rồi gõ lệnh:

  cd C:\Program Files\MySQL\MySQL Server 5.0\bin
  mysqld-nt –skip-grant-tables
  • Giữ nguyên cửa sổ command này, mở 1 cửa sổ command line khác, cũng change directory về giống cửa sổ trước, gõ lệnh:
  mysql -uroot
  • Sau khi đã login được vào MySQL, tiến hành đổi mật khẩu bằng các câu lệnh sau:
  mysql>use mysql;
  mysql>update user set Password=PASSWORD(’new_pass’) WHERE User=’root’;
  mysql>flush privileges;
  Ok done.
  +mysql>exit

=> đến đây thì đã xong, mật khẩu đã được reset thành new_pass.

  • Restart lại service MySQL để tiếp tục sử dụng.

2. Máy chạy Linux

Trường hợp cài đặt bằng gói RPM

Bước 1. Lên quyền root Gain root access to your Linux system

[user@localhost ~]$ su -
Password:
[root@localhost user]#

Bước 2. Đầu tiên là dừng dịch vụ mysql lại First you have to stop the daemon

[root@localhost root]# /etc/init.d/mysqld stop
[root@localhost root]#

Bước 3. Nếu sử dụng RedHat, CentOS… thì dùng lệnh sau: With RedHat, Centos

[root@localhost root]# mysqld_safe --skip-grant-tables --skip-networking
[root@localhost root]#

Nếu dùng OS khác (Ubuntu…) thì thay thế services mysqld_safe thành safe_mysqld

Chạy mysql trong safe_mode mà không cần đăng nhập và bạn cũng sẽ disable networking cho mysql luôn - đề phòng bị tranh thủ tấn công).

You will now start MySQL in safe mode without reading the grant tables with all MySQL database passwords and also you will disable networking. The “safe_mysqld” command will do this trick for you.

Bước 4. Sử dụng mysqladmin sẽ reset lại password. Trong trường hợp này ta phải sử dụng lệnh sau để set 1 pass mới.

The “mysqladmin” command will now reset[rewrite] the root password. In this case we are setting it to “newpassword”.

[root@localhost root]# mysqladmin -u root flush-privileges password "newpassword"
[root@localhost root]#

Bước 5. Dừng chạy chế độ mysql_safe Stop the running daemon

kill 'cat /var/run/mysqld/mysqld.pid'

Bước 6. Cuối cùng khởi động lại dịch vụ mysql And finally restart it

[root@localhost root]# /etc/init.d/mysqld start
[root@localhost root]#

Bước 7. Bây giờ thì dùng password mới You can use now your new root password

[root@localhost root]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.0.20-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Đến đây là xong.

Trường hợp cài thông qua source

Tìm thư mục cài đặt Mysql và tìm thư mục libexec bên trong nó. Bạn chạy lệnh:

[root@localhosthost libexec]./mysqld stop
[root@localhosthost libexec]./mysqld --skip-grant-tables --user=root

Sau đó có thể tiến hành đăng nhập không cần password và reset password như trên Windows.