Computer >> Computer tutorials >  >> Troubleshooting >> MAC

Resetting MySQL Root Password on your Mac | MacUpdate Blog

 If you have never assigned a root password for MySQL, the server does not require a password at all for connecting as root. However, this is insecure. It’s highly recommended that you set a password.  

To set up a root password for the first time, there are two ways to go about it:

  1. Use the mysql_secure_installation command. This command will ask for both the old and new MySQL root password and will also conduct some other security settings, including disabling the test database.

Here is how:

Launch Terminal and type the command: mysql_secure_installation

Resetting MySQL Root Password on your Mac | MacUpdate Blog

  • Answer the questions as shown below:

Change the root password? [Y/n] <-- y

New password: <-- Enter a new MySQL root password

Re-enter new password: <-- Repeat the MySQL root password

Remove anonymous users? [Y/n] <-- y

Disallow root login remotely? [Y/n] <-- y

Remove test database and access to it? [Y/n] <-- y

Reload privilege tables now? [Y/n] <-- y

Resetting MySQL Root Password on your Mac | MacUpdate Blog

P.S. The most common reason for this error is an empty password. You have to specify the root password while connecting to the Mysql database:

mysqladmin -u root -pROOT-PASSWORD.

2. Use the mysqladmin command at the shell prompt as follows:

  • Open Terminal and type the following command: mysqladmin -u root password newpass

Resetting MySQL Root Password on your Mac | MacUpdate Blog

  • Enter password.

Note that If you see the following:

mysqladmin: connect to server at 'localhost' failed

error: 'Access denied for user 'root'@'localhost' (using password: YES)'

You need to follow the instructions below on how to recover your MySQL password.

Resetting MySQL Root Password on your Mac | MacUpdate Blog

By the way, the word 'password' in the above example is part of the command, so don’t replace it with your password. Enter the new password in the 'newpass' section.

Okay, it’s been a while since you have installed MySQL on your Mac and set the root user password. You can easily forget the MySQL root password after some time. That’s your case, right? No worries, if you have forgotten the MySQL root password, can’t remember or want to break in, you can easily reset your MySQL database password from the command line as long as you know the root user password of the box you are on.

Resetting the MySQL password is not difficult, yet it varies from the version you are using.

So, this is how to reset the MySQL password on a Mac:

  1. Stop the MySQL Server.  To do this, you need to go to System Preferences and choose MySQL. Then, select Stop MySQL Server.

Tip: By the way, you can also use Terminal to stop the MySQL Server. You need to type in the Terminal the following command:

service mysql stop

Resetting MySQL Root Password on your Mac | MacUpdate Blog

You will get the following output correspondingly:

Resetting MySQL Root Password on your Mac | MacUpdate Blog

or Stopping MySQL database server: mysqld.

Resetting MySQL Root Password on your Mac | MacUpdate Blog

2.  Start the server in safe mode with privilege bypass: In the Terminal, type the following command:

 

sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables

You will see the following:

Resetting MySQL Root Password on your Mac | MacUpdate Blog

Connect to the MySQL server using the MySQL client: in a new terminal window and type the following command (ensure you type it line by line):

mysql -u root

The output is the following:

Resetting MySQL Root Password on your Mac | MacUpdate Blog

4.  Set a new MySQL root user password:

The next command depends on your MySQL version

  • For MySQL 5.7.5 and earlier

Resetting MySQL Root Password on your Mac | MacUpdate Blog

 

MySQL 5.7.6 and newer

Resetting MySQL Root Password on your Mac | MacUpdate Blog

 

5. Stop the MySQL server: use this command to stop MySQL:

killall mysqld

Resetting MySQL Root Password on your Mac | MacUpdate Blog

Finally, start the MySQL server again and test it:

Resetting MySQL Root Password on your Mac | MacUpdate Blog

Key Considerations

MySQL can be used without a password, but for security reasons, it’s better to set the root password. Don’t worry, if you happen to forget that password, it’s not that difficult to recover MySQL root password on your Mac. Simply use this guide.