Setting the MySQL root user password on OS X
I just installed MySQL on Mac OS X. The next step was setting the root user password, so I did this next:
-
Launch the terminal app to access the Unix command line.
-
Under the Unix prompt I executed these commands:
cd /usr/local/mysql/bin ./mysqladmin -u root password 'password'
But, when I execute the command
./mysql -u root
, this is the answer:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 224
Server version: 5.5.13 MySQL Community Server (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
I can get into the mysql
command line without any password!
Why is this?
Try the command FLUSH PRIVILEGES
when you log into the MySQL terminal. If that doesn't work, try the following set of commands while in the MySQL terminal
mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET password=PASSWORD("NEWPASSWORD") WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
Change out NEWPASSWORD with whatever password you want. Should be all set!
Update: As of MySQL 5.7, the password
field has been renamed authentication_string
. When changing the password, use the following query to change the password. All other commands remain the same:
mysql> UPDATE user SET authentication_string=PASSWORD("NEWPASSWORD") WHERE User='root';
On 8.0.15 (maybe already before that version) the PASSWORD() function does not work, as mentioned in the comments below. You have to use:
UPDATE mysql.user SET authentication_string='password' WHERE User='root';
If you don't remember the password you set for root and need to reset it, follow these steps:
-
Stop the mysqld server, this varies per install
-
Run the server in safe mode with privilege bypass
sudo mysqld_safe --skip-grant-tables;
-
In a new window connect to the database, set a new password and flush the permissions & quit:
mysql -u root
For MySQL older than MySQL 5.7 use:
UPDATE mysql.user SET Password=PASSWORD('your-password') WHERE User='root';
For MySQL 5.7+ use:
USE mysql;
UPDATE mysql.user SET authentication_string=PASSWORD("your-password") WHERE User='root';
Refresh and quit:
FLUSH PRIVILEGES;
\q
-
Stop the safe mode server and start your regular server back. The new password should work now. It worked like a charm for me :)