Solution of Forgetting MySQL root Password in MACOS

  • 2021-07-26 09:00:00
  • OfStack

MySQL is a relational database management system, developed by MySQL AB Company of Sweden, and currently belongs to Oracle products. MySQL is one of the most popular relational database management systems. In WEB application, MySQL is the best RDBMS (Relational Database Management System, relational database management system) application software.

MySQL is a relational database management system that stores data in separate tables, rather than putting all data in one big warehouse, which increases speed and flexibility.

The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts the dual authorization policy, which is divided into community version and commercial version. Because of its small size, high speed, low total cost of ownership, especially open source, MySQL is selected as the website database for the development of small and medium-sized websites.

Mac After installing MySQL, the software will generate a default password to us. However, when I use Navicat to establish a connection is prompted password invalidation, helpless, can only modify the database default password.

Next, record the entire root password modification process.

You must stop the mysql service before starting the following steps!


 cd /usr/local/mysql/bin/
 sudo su
 ./mysqld_safe --skip-grant-tables & // This 1 The step is used to cross permission verification 
 ./mysql -uroot // With root Identity login, because the 3 You don't need a password for the reason of step. Commands after this do not need to be preceded by ./mysql It's over 
 use mysql;
 update user set authentication_string='123456' where User='root';

All the versions circulating on the Internet are set password = '', so write 1 and report an error directly, saying that the 'password' column does not exist!

Finally, with the sql command, only authentication_string field was found, and there was no password field.

After the previous step, I thought I could log in, but when I tested the navicat connection, it appeared:


ERROR 1862 (HY000): Your password has expired. To log in you must
change it using a client that supports expired passwords.

Therefore, the following steps are needed


 cd /usr/local/mysql/bin/
 sudo su
 ./mysql -uroot -p123456
 set password = password('123456')

User name: root, password: 12345

Successful modification

Supplement: Although the above was successfully revised, it still took a lot of detours. The above just records the whole process, and we will summarize the simplest and most effective methods below

This process is sad. There are as many materials on the Internet as dogs. The key is that each has its own wrong method. After trying for a long time, there was no right one. When I was about to break the psychological defense line and turn over the MySQL document, I succeeded. No article tells me the complete answer. I made a car behind closed doors with reference to several strategies. Give yourself a compliment. Needless to say, follow me step by step.

1. Shut down the mysql server


sudo /usr/local/mysql/support-files/mysql.server stop


It can also be turned off in MySQL in system preferences.

2. cd/usr/local/mysql/bin Enter Directory

3. sudo su access

4. ./mysqld_safe --skip-grant-tables & Restart the server

5. Reopen a terminal,

Configure short commands:


alias mysql=/usr/local/mysql/bin/mysql

6. Enter mysql to enter mysql command mode

7. use mysql enters mysql database

8. flush privileges; Probably just get permission, or he won't let you change it.

9. set password for 'root'@'localhost'=password('新密码'); Complete the modification

10. I'm finally done.

Ok, the method has been taught to everyone, and I hope it can be helpful to everyone


Related articles: