When using Mac, sometimes you may forget your MySQL password, which makes accessing the database a very difficult thing. Fortunately, there are some easy methods to help you reset your MySQL password. Here are a few possible methods to help you quickly solve this problem.
Method 1: Using System Preferences
First open System Preferences, and then click the "MySQL" icon. Click the "Stop MySQL Server" button in the pop-up window, which will stop the MySQL service.
Next, enter the following command in the terminal:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
This command will let The MySQL service starts without verifying the password. You can log in to MySQL using the following command:
mysql -u root
Then, reset the password and exit MySQL:
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
mysql> exit;
Finally, start the MySQL service again :
sudo /usr/local/mysql/support-files/mysql.server start
Method 2: Use the command line
First open the terminal and enter the following command in it :
sudo /usr/local/mysql/support-files/mysql.server stop
Next, enter the following command and reset the password:
sudo mysqld_safe -- skip-grant-tables &
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
exit;
Finally, enter the following command to start the MySQL service:
sudo /usr/local/mysql/support-files/mysql.server start
Method three: Reset password file
Enter the following command in the terminal:
sudo nano /usr/local/mysql/data/my.cnf
Add the following two lines in the file :
[mysqld]
skip-grant-tables
Save and exit the file, then restart the MySQL service:
sudo /usr/local/ mysql/support-files/mysql.server restart
Then, use the following command to log in to MySQL:
mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
exit;
Finally, restore the contents in the my.cnf file to the initial state and restart the MySQL service.
Summary
Since MySQL is installed using the MySQL server on Mac, forgetting the MySQL password is a common situation. Although this question may seem difficult, with a few easy methods mentioned above, you can reset your MySQL password quickly and easily. These methods are very useful for users who have MySQL databases on Mac.
The above is the detailed content of mac mysql forgot password. For more information, please follow other related articles on the PHP Chinese website!