Access denied for user root - mysql on MAC OS - mysql

I know how do skip this problem on ubuntu, but how can i do it on MAC OS?
How can i set password for mysql on MAC?
1) Doesn't work
mysqladmin -u root password NEWPASSWORD
2)Doesn't work
mysqld --skip-grant-tables --skip-networking &
3) This works:
mysql root password forgotten

You can do the following on Mac (El Capitan)
Open a Terminal window, use the command below to stop mysql if it's already running.
sudo /usr/local/mysql/support-files/mysql.server stop
You can also check System Preferences > MySQL to see if it is running
Start MySQL with this command:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
Open a new terminal window/tab:
sudo /usr/local/mysql/bin/mysql -u root
This should open "mysql" prompt. Execute the following command:
$mysql> UPDATE user SET authentication_string=PASSWORD("my_password") WHERE User='root';
Troubleshooting tips:
A) The command for MySql versions before 5.7 was:
$mysql> UPDATE user SET Password=PASSWORD('my_password') where USER='root';
B) If you see ERROR 1046 (3D000): No database selected, then run this command first:
use mysql;
C) If you see unknown "Password" field error, then run this command:
UPDATE USER SET AUTHENTICATION_STRING=password('NewPassword') WHERE user='root'; $mysql> FLUSH PRIVILEGES; $mysql> EXIT
D) If you see - ERROR 1064 (42000): You have an error in your SQL syntax; It is because password function was removed in version 8.0.11. Use bare string:
UPDATE USER SET AUTHENTICATION_STRING='NewPassword' WHERE user='root';
Stop MySql server
sudo /usr/local/mysql/support-files/mysql.server stop
Restart MySQL, either through System Preferences > MySql or using a command.

The solution of
UPDATE user SET authentication_string=PASSWORD("my_password") WHERE User='root';
wasn’t working for me, but I did
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPassword';
And was able to proceed. I’m using Ver 8.0.12.

You can do the following on iMac or Mac (High Sierra)
Open a Terminal window, and stop the mysql if it's already running. You can also check this System Preferences > MySQL > see if it is running.
Start MySQL with this command for skipping the main table
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
Open a new terminal window/tab..
sudo /usr/local/mysql/bin/mysql -u root
This should open "mysql" prompt. Execute the below command:
A ) MySQL 5.6 and below
UPDATE mysql.user SET password=PASSWORD('NewPassord') WHERE user='root';
-- or --
B) MySQL 5.7+
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassord') WHERE user='root';
Restart MySQL, either through System Preferences > MySql or using a command.

MACOS 10.14 MOJAVE || MYSQL 8.0.15
This didn't work on my mac:
sudo /usr/local/mysql/support-files/mysql.server stop
BUT THIS ACTUALLY WORKED:
sudo /usr/local/mysql-8.0.15-macos10.14-x86_64/support-files/mysql.server stop
The installation folder might vary per user, BE AWARE!
Or just Check > System preferences > MySQL > if the server is running, stop it.
then,
Start MySQL with this command:
sudo /usr/local/mysql-8.0.15-macos10.14-x86_64/bin/mysqld_safe --skip-grant-tables
Open a new terminal window/tab:
sudo /usr/local/mysql-8.0.15-macos10.14-x86_64/bin/mysql -u root
This should open "mysql" prompt. Execute the following command (*scroll right if you don't the full query):
UPDATE mysql.user SET authentication_string='your-password-goes-here' WHERE user='root' and host='localhost';
REMEMBER THAT
mysql-8.0.15-macos10.14-x86_64
(in my case) is the installation folder on your local machine, and it might or might not be different than mine because of OS versions, mysql versions, installation methods used, etc.

Very Simple Fix for MariaDB version: 10.4.6-MariaD on Mojave macOS
I have gone through all the answers. Some of them worked for me some of them not. I found one simple way to fix this on macOS or OSX. Here are the steps:
Prerequisites:
Homebrew should be installed. Use the following link to install homebrew on macOS or OSX.
Install mariadb:
brew install mariadb
Start MySQL Server: mysql.server start or run brew services start mariadb
to start MySQL Server at login to the computer.
Get into MySQL instance sudo mysql -u root
NOTE: mysql -u root will throw error ERROR 1698 (28000): Access denied for user 'root'#'localhost' so use sudo to run this command.
Now to change the password of the root user I tried the following commands:
UPDATE user SET password=PASSWORD("mypassword") WHERE User='root';
This has thrown an error: ERROR 1348 (HY000): Column 'Password' is not updatable
UPDATE user SET authentication_string=PASSWORD("mypassword") WHERE User='root';
This has thrown an error: ERROR 1348 (HY000): Column 'authentication_string' is not updatable
But the following command worked:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'mypassword';
The response was: Query OK, 0 rows affected (0.009 sec)
So, it was a simple fix for me for the version 10.4.6-MariaD installed through brew. Hope this will help you too.

For MySQL 5.7 I had to use:
UPDATE user SET authentication_string = PASSWORD('YourNewPassword'),
password_expired = 'N' WHERE User = 'root';

sudo mysql -uroot
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('new_password');
flush privileges;
Ctrl+D
mysql -uroot -pnew_password # will work now
Notice the sudo on the first line.

I used to try all solutions but nothing worked. Finally and suddenly I found the solution! I use 10.5.8-MariaDB Homebrew.
USE mysql;
SELECT user, authentication_string, plugin, host FROM mysql.user;
For some reasons authentication_string is invalid and it's what we need to fix
Then just run the command below:
ALTER USER 'root'#'localhost' IDENTIFIED BY '';

I had a very hard time in fixing this issue on MAC Sierra, 10.12.6, MySql version 5.7.17
Following steps worked for me:
Open a Terminal window, use the command below to stop mysql if it's already running.
sudo /usr/local/mysql/support-files/mysql.server stop
Start MySQL with this command:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
Open a new terminal window/tab:
sudo /usr/local/mysql/bin/mysql -u root
This will open "mysql" prompt.
Execute following command in mysql prompt one by one:
use mysql;
UPDATE user SET authentication_string = PASSWORD('my_new_password'), password_expired = 'N' WHERE User = 'root';
FLUSH PRIVILEGES;
EXIT
Now Stop MySql server first then start it using below commands
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
Hope this solves your issue.

I discovered that in Mac Mojave, at least if you do the install straight from downloading MySQL Community Package rather than through brew, apparently you still need to insert the password you choose for 'root' through the System Preferences screen after stopping, restarting with safe mode (--skip-grant-tables), and flushing privileges. Then you can log in as root in phpMyAdmin. This was after trying at least 20 different sets of advice/instruction for fixing this, including the ones listed above on this page. Hope it helps someone!

I am using Mac and my solution is a bit different like above
cd to folder that I installed mysql. In my case it is cd /opt/homebrew/Cellar/mysql/8.0.27/bin because I used brew to install mysql
using mysqld_safe will not help. Try this syntax mysqld --skip-grant-tables &
type mysql
type FLUSH PRIVILEGES;
type ALTER USER 'root'#'localhost' IDENTIFIED BY 'password you want';
If you see ERROR 1819 (HY000): Your password does not satisfy the current policy requirements error. Just type the password with capital letter + number + special character

Mac OSX 12.1 (Monterey)
Installed Oracle MySql: mysql Ver 8.0.28 for macos11 on x86_64 (MySQL Community Server - GPL)
Using the package install after installation it appears the root password you set does not get saved in the mysql database within the server. I tried a number of the update user commands from above and ultimately what fixed the issue was System Preferences > MySql > Initialize Database > [set password again].
Initialize DB Screen
Stop the DB and restart was able to create a session with the root user. Now that it is working I kind of want to trash it just for the headache and use mariadb instead.

Related

how to reset my mysql password in mac os 10.13.3

I have a problem with MySQL. I forgot the password I used when I installed it
so, I can not access to the server now.
I tried deleting the MySQL and install it again but it didn't show the password again.
So I tried to do it by the terminal and this is the result ...
first i stopped the MySQL server
then i put sudo /usr/local/mysql/bin/mysqld_safe –skip-grant-tables in the terminal
after that in new terminal window i wrote sudo /usr/local/mysql/bin/mysql -u root
UPDATE mysql.user SET Password=PASSWORD('root') WHERE User='root';
FLUSH PRIVILEGES;
\q
the result was "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)"
these are all the commands
Try this command. I believe you should have mysql running. If that doesn't work try with mysql stopped.
sudo mysql_secure_installation
Hopefully should get you to prompt a password change.
Also, for the socket error, you can try following this link.
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
You can try reseting the root password by running MySQL in Safe Mode.
Here are the steps:
Stop MySQL:
sudo /usr/local/mysql/support-files/mysql.server stop
Start it in safe mode:
sudo mysqld_safe --skip-grant-tables
This will be an ongoing command until the process is finished so open another shell/terminal window, and..
Log in without a password as root:
mysql -u root
Update root (and any other user's) password)
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
\q
Restart MySQL in normal mode
sudo /usr/local/mysql/support-files/mysql.server start
Reference: https://coolestguidesontheplanet.com/how-to-change-the-mysql-root-password/
Note: this is pretty standard reset procedure, but just documented better in the above guide compared to mysql reference docs.

Resetting mysql server root password on OS X

First of all, I know there are several threads, but I have tried so many solutions and I cant get anything to work.
I dont have any experience with mysql server and Terminal.
I downloaded mysql server 5.7.19
Following the answer from redtek, here: Setting the MySQL root user password on OS X
I open mysql from system setting, click stop server. Then I open the terminal and write
sudo mysqld_safe --skip-grant-tables
I asks me for my password (I assume this is the same when I start my computer). I get a message that command not found.
MacBook-Pro:~ XXXXXX$ sudo mysqld_safe --skip-grant-tables
Password:
sudo: mysqld_safe: command not found
MacBook-Pro:~ XXXXXX$
UPDATE: When I run the solution below, after opening a new window I get the following errors:
Last login: Sun Aug 13 16:51:49 on ttys002
MacBook-Pro:~ XXXXX$ mysql -u root
-bash: mysql: command not found
MacBook-Pro:~ XXXXX$ UPDATE mysql.user SET Password=PASSWORD('my-new-password') WHERE User='root';
-bash: syntax error near unexpected token `('
MacBook-Pro:~ XXXXX$ FLUSH PRIVILEGES;
-bash: FLUSH: command not found
MacBook-Pro:~ XXXXX$ \q
Stop the MySQL server.
sudo /usr/local/mysql/support-files/mysql.server stop
Restart it with the --skip-grant-tables option.
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
Open another terminal to connect to the MySQL server using the mysql client.
cd /usr/local/mysql/bin
mysql
Tell the server to reload the grant tables so that account-management statements work.
FLUSH PRIVILEGES;
Now reset the password for root user
MySQL 5.7.6 and later:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Stop the server and restart it normally
sudo /usr/local/mysql/support-files/mysql.server stop
sudo /usr/local/mysql/support-files/mysql.server start
First step is to stop MySQL service.
sudo /usr/local/mysql/support-files/mysql.server stop
Then you need to start it in safe mode
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
secondly: let's open another shell/terminal window, log in with no password
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('my-new-password') WHERE User='root';
FLUSH PRIVILEGES;
\q
Because in MySQL 5.7, the password field in mysql.user table is removed, now the field name is 'authentication_string'.
mysql -u root
UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE User='root';
FLUSH PRIVILEGES;
\q
Now again yu need to start the MySQL server
sudo /usr/local/mysql/support-files/mysql.server start
The command is not found because MySQL installation folder ( /usr/local/mysql/ ) is not included in the system variable PATH.
You can add to PATH
OR you can use full path /usr/local/mysql/bin/mysqld_safe
It took me a while in resolving this, considering most solutions around are for versions lower than MySQL version 5.7
Follow this below and it could help get you sorted as well.
For Safely ensuring process:
- Turn off the tick on "Automatically Start MySQL Server on Startup" inside System Preferences of MySQL (spotlight - mysql)
Open Terminal and type sudo /usr/local/mysql/support-files/mysql.server start -PS: This is for ensuring its in-line, times were that the next processes were breaking on me.
Now shut the MySQL service: sudo /usr/local/mysql/support-files/mysql.server stop
Type sudo mysqld_safe --skip-grant-tables
This will have now bypassed the security for MySQL - not safe for operations and not a permanent solution to always allow you to use MySQL.
Currently, as you would see, its in a process... This will allow us to do following steps. Leave this tab of Terminal OPEN throughout remaining process!!
Now Cmd+N (new terminal window), and in the new terminal:
- sudo /usr/local/mysql/support-files/mysql.server start
- update user set authentication_string=password(‘jj’) where user='root'
This on older version would have been as update user set password=PASSWORD(“jj”) where user='root’;
- FLUSH PRIVILEGES; //This is essential (updates disk instead of cache) to ensuring the next time around when you close mysql and get back it stays accessible as you setup.
- \q or quit
Close it all down - All terminals, give your computer a restart, and ensure everything is in order (ofcourse this entails - restart - open terminal - mysql -u root -p (enter) - respond with password you gave on steps above).
In my answer: jj was the password set
Cool-Stuff for General knowledge of fairly new (this somehow immediately worked for me after saying Password is not a field or something of sorts, on going in this new Terminal at update user set authentication_string=password(‘jj’) where user='root', so if you had the same, go at it in following steps - in >mysql itself where you are..):
- use mysql;
- show tables;
- describe user;
and then continue as steps above from the point of update user set authentication_string=password(‘jj’) where user='root'

Access Denied for MYSQL ERROR 1045

I just got a new macbook pro (OS X 10.8.2) and am attempting to get mysql set up on it. So far I've been able to get it installed but I cannot get my root user access (or any user for that matter). I plan on using this for Python, on my other computer I only use MYSQL (no MAMP) and I prefer to keep it that way.
For reference, I did the following:
$ alias mysql=/usr/local/mysql/bin/mysql
$ sudo /Library/StartupItems/MySQLCOM/MySQLCOM start
$ alias mysqladmin=/usr/local/mysql/bin/mysqladmin
When i enter mysql or mysql -u root -p it gives me this:
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES)
or
ERROR 1045 (28000): Access denied for user 'jmitch'#'localhost' (using password: NO)
Depending on which phrasing I use
MYSQL is running in my system preferences. Thank you for your help.
Maybe updating the package the updater overwrote the root password.
To restore it:
Stop mysqld deamons.
$ sudo service mysqld stop
Go to mysql/bin directory
$ cd /usr/bin
Start a mysql deamon with this option:
$ sudo mysqld_safe --skip-grant-tables
Open another terminal and open a mysql session to execute this:
$ mysql
mysql> use mysql;
see Note1 below for next line.
mysql> UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD_HERE') WHERE user = 'root';
mysql> exit;
Now kill the mysqld_safe process and restart mysqld normally:
$ sudo service mysqld start
Note1: password is the column name in table mysql.user prior to version 5.7. After which it became authentication_string. Change your update statement accordingly.
on Mac OSX 10.9 Mavericks I used the 'mysql.server' script in the support-files directory instead of the mysqld_safe and service script.
$sudo ./mysql.server stop
$sudo ./mysql.server start --skip-grant-tables
$ mysql
mysql> use mysql;
mysql> UPDATE user SET password=PASSWORD('YOUR_NEW_PASSWORD_HERE') WHERE user = 'root';
mysql> exit;
$sudo ./mysql.server stop
$sudo ./mysql.server start
I was having a similar issue trying to access MAMP's MySQL through the terminal on Mountain Lion.
The --no-defaults flag solved it for me.
/Applications/MAMP/Library/bin/mysql --no-defaults -u root -proot -h localhost
I want to add that for MySQL 5.7 simply changing the authentication_string column doesn't work. This is because MySQL never actually uses those values for root authentication, it uses a plugin. As far as I can tell this plugin verifies that you are also root on the host account (so you have to sudo mysql -u root).
The only way I was able to get this to work was to run this:
UPDATE mysql.user
SET authentication_string=PASSWORD(''), plugin=''
WHERE mysql.user = 'root';
It should also be noted that the official MySQL documentation for 5.7 never mentions this. Following this documentation to the letter gets you nowhere at all.

MacOSX homebrew mysql root password

For some reason MySQL stopped giving access for root. Uninstalled and reinstalled with Homebrew. Fresh install, fresh tables but when I enter
mysql -u root -p
I get this error:
Access denied for user 'root'#'localhost' (using password: NO)
I reinstalled MySQL five times but it is still asking for a password. How do I fix this?
None of these worked for me. I think i already had mysql somewhere on my computer so a password was set there or something. After spending hours trying every solution out there this is what worked for me:
$ brew services stop mysql
$ pkill mysqld
$ rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
$ brew postinstall mysql
$ brew services restart mysql
$ mysql -uroot
all credit to #Ghrua
Just run this command (where NEWPASS is your password):
$(brew --prefix mysql)/bin/mysqladmin -u root password NEWPASS
I have had the same error and fixed it this way.
In case you have inadvertently set and forgot the root password, and you don't want to wipe all your databases and start over because you are lazy and forgot to have a back up solution in place, and you are using a fairly recent Homebrew install (Winter 2013), here are steps to reset your password for MySQL.
Stop the currently running MySQL instance
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Now start mysql by hand skipping the grant tables and networking
$(brew --prefix mysql)/bin/mysqld_safe --skip-grant-tables --skip-networking
Note that if when you run echo $(brew --prefix mysql) and it does not respond as "/usr/local/opt/mysql" in bash, you will need to adjust the path accordingly.
Once you have done this, you now should have a running, unprotected MySQL instance up.
Log in and set the password
mysql -u root
At the prompt, enter the following MySQL command to set a new password for the effected user.
mysql> update mysql.user set password=PASSWORD('new_password_here') WHERE user='root';
If all went to plan it should say:
Query OK, 1 row affected (0.02 sec)
Rows matched: 4 Changed: 1 Warnings: 0
Exit out of the MySQL prompt.
mysql> exit
Bye
Stop server:
mysqladmin -u root shutdown
Now, lets put back the launch daemon so we have our MySQL at the ready again:
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Congratulations. You've just reset your mysql root password. Pour yourself a coffee and get a backup solution in place!
I had the same problem a couple days ago. It happens when you install mysql via homebrew and run the initialization script (mysql_install_db) before starting the mysql daemon.
To fix it, you can delete mysql data files, restart the service and then run the initialization script:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
rm -r /usr/local/var/mysql/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
Got this error after installing mysql via home brew.
So first remove the installation. Then Reinstall via Homebrew
brew update
brew doctor
brew install mysql
Then restart mysql service
mysql.server restart
Then run this command to set your new root password.
mysql_secure_installation
Finally it will ask to reload the privileges. Say yes. Then login to mysql again. And use the new password you have set.
mysql -u root -p
If you run on Mojave, Catalina, Big Sur and now on macOS Monterey:
brew install mariadb
...
brew services start mariadb
==> Successfully started `mariadb` (label: homebrew.mxcl.mariadb)
$(brew --prefix mariadb)/bin/mysqladmin -u root password newpass
/usr/local/opt/mariadb/bin/mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost''
also login with root account fails:
mariadb -u root
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
then default admin user is created same name as your MacOS account username, e.g. johnsmit.
To login as root and set root password, issue (use your username):
mariadb -u johnsmit
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.4.11-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER USER 'root'#'localhost' IDENTIFIED BY 'ROOT-PASSWORD';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> exit
Bye
So you change root password form mysql on localhost.
Bonus: to change current or other user pass you can use mysqladmin command:
$(brew --prefix mariadb)/bin/mysqladmin -u arunas password 'newsecret'
but this does not affect localhost for some reason, but should work for app login.
Or use native MySQL change user password SQL, which explicitly specifies host, in my case 'localhost' account of the user:
mariadb -u arunas
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.5.9-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> ALTER USER 'arunas'#'localhost' IDENTIFIED BY 'newsecret';
Query OK, 0 rows affected (0.006 sec)
MariaDB [(none)]> exit
Bye
Now let's try to login without password:
mariadb -u arunas
ERROR 1045 (28000): Access denied for user 'arunas'#'localhost' (using password: NO)
you see login failed, thus now we need specify the need of password:
mariadb -u arunas -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.5.9-MariaDB Homebrew
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
Happy usage!
Since the question was asked/answered long time ago, those top answers do not work for me. Here's my solution, in 2020.
Background: Fresh mysql/mariadb installed by homebrew.
Problem: The password for root is not empty and unknown.
The fix:
mysql -u YOUR-SYSTEM-USERNAME -p
The password is empty (press enter)
ALTER USER 'root'#'localhost' IDENTIFIED BY 'NEW-ROOT-PASSWORD';
FLUSH PRIVILEGES;
The reason:
Homebrew will create a user with root privileges named by the current MacOS username.
it has no password
Since it has all privileges, just reset the root password with that user.
The initial password for root was randomly generated.
This worked for me:
sudo mysql -u root
If you are using macOS and install the MariaDB via Homebrew you can use the OS root password and then reset the password to whatever you want, in this case I removed the root's password:
sudo mysqladmin -u root password ''
or if you want to set a password you can put the password between the single quotations:
sudo mysqladmin -u root password 'NEW-PASSWORD-HERE'
Try with sudo to avoid the "Access denied" error:
sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS
go to apple icon --> system preferences
open Mysql
in instances you will see "initialize Database"
click on that
you will be asked to set password for root --> set a strong password there
use that password to login in mysql from next time
Hope this helps.
I had this problem on a fresh install on Mac. I installed MariaDB with:
brew install mariadb
Then started the service:
brew services start mariadb
I was unable to run 'mysql_secure_installation' as it prompted for the root password. Then I noticed in the install output:
mysql_install_db --verbose --user=jonny --basedir=/usr/local/Cellar/ ....
So I tried logging in as the username specified in the mysql_install_db output and was successful e.g.
mysql -u jonny
Then at the mysql prompt if you want to set a password for the root user:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('ToPsEcReT');
I've just noticed something common to most of the answers here, and confirmed on my fresh install. It's actually obvious if you look at the recommendations to run mysqladmin -u root without -p.
There is no password.
Brew sets mysql up with just a root user and no password at all. This makes sense, I guess, but the post-install Caveats don't mention it at all.
This worked for me for MAC https://flipdazed.github.io/blog/osx%20maintenance/set-up-mysql-osx
Start mysql by running
brew services start mysql
Run the installation script
mysql_secure_installation
You will be asked to set up a setup VALIDATE PASSWORD plugin. Enter y to do this.
Select the required password validation (Doesn’t really matter if it is just you using the database)
Now select y for all the remaining options: Remove anon. users; disallow remote root logins; remove test database; reload privileges tables.
Now you should receive a message of
All done!
This worked for me. Hopefully this works for you too!!!
Follow them.
brew services stop mysql
pkill mysqld
# NB: the following command will REMOVE all your databases!
# Make sure you have backups or SQL dumps if you have important data in them already.
rm -rf /usr/local/var/mysql/
brew services restart mysql
mysql -uroot
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password
BY'YOUR_PASS_WORD!!!!';
Running these lines in the terminal did the trick for me and several others who had the same problem. These instructions are listed in the terminal after brew installs mysql sucessfully.
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.25a/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
/usr/local/Cellar/mysql/5.5.25a/bin/mysqladmin -u root password 'YOURPASSWORD'
where YOURPASSWORD is the password for root.
Check that you don't have a .my.cnf hiding in your homedir. That was my problem.
The default password when you install mysql via brew is root try this, it worked for me
mysql -uroot -proot
So, in case someone has the same situation and configuration as I had and is also about to go mad - this worked for me.
After a long story I had a brew-installed MariaDB which kept automatically restarting when I killed its process (this was brew's doing), which had a root password, which I did not know.
$ brew services list
This shows something like:
mariadb started jdoe /path/to/homebrew.mxcl.mariadb.plist
Stop the MySQL server with:
$ brew services stop mariadb
Then start it again without the root user (and not using brew):
$ mariadbd --skip-grant-tables &
Here, mysql_secure_installation did not work for me because of the --skip-grant-tables, and it would not work without the --skip-grant-tables because it needed the password (which I did not have).
Trying $(brew --prefix mysql)/bin/mysqladmin -u root password hunter2 only returned strange errors and did nothing; $(brew --prefix mariadb)/bin/mysqladmin -u root password hunter2 also didn't work, gave different errors, and suggestions that did not work for me.
But you can log into mysql now without credentials: $ mysql
Here, the old method of updating the user table for root doesn't work because "Column 'Password' is not updatable".
The new method uses alter user BUT only works after you have done flush privileges; so do that first.
Then:
MariaDB [(none)]> alter user 'root'#'localhost' identified by 'hunter2';
(MariaDB [(none)]> is the MySQL prompt here)
Then do flush privileges; again.
Exit the MySQL client.
Now as far as brew is concerned, MariaDB is still not running, and so use $ ps aux | grep -i mariadb to find the pid and $ kill -9 <pid> it.
Then use $ brew services start mariadb to start it again.
I stumbled across this too and the solution was unironically to simply run this:
mysql
Terminal 1:
$ mysql_safe
Terminal 2:
$ mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('new-password') WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
Iam using Catalina and use this mysql_secure_installation command and now works for me:
$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none): << enter root here >>
i enter root as current password
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
and do the rest
For me, mysql was setup with a root user and no password.
I wanted to be able to login as my current user and not require the -u root bit. I used the following command to setup a super user:
mysql -u root -e "CREATE USER '$USER'#'localhost';"
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO '$USER'#'localhost';"
mysql -u root -e "flush privileges;"
Any value for $USER will work.
I personally concatenated all the above with a semicolon but reformatted to make it hopefully easier for all to read.
Followed the article from #Roman Escart.
I guess the key is to use '$brew link --force mysql#5.7'
https://medium.com/macoclock/setup-mysql-in-a-specific-version-on-macos-35d8ad89c699
login to DB as root :
sudo mysql -u root
you may run secure db as:
sudo mysql_secure_installation
Use init file to start mysql to change the root password.
brew services stop mysql
pkill mysqld
echo "ALTER USER 'root'#'localhost' IDENTIFIED BY 'newRootPass';" > /tmp/mysql-init
$(brew --prefix mysql)/bin/mysqld --init-file=/tmp/mysql-init
Your root password is now changed. Make sure to shutdown server properly to save password change. In new terminal window execute
mysqladmin -u root -p shutdown
and enter your new pass.
Start your service and remove the init file
brew services start mysql
rm /tmp/mysql-init
Tested on mysql version 8.0.19
What is the easiest way to run Mysql on a Mac ?
My solution was to install MAMP.
https://www.mamp.info/en/mac/

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';
for MySQL 8.0+ Don't use
mysql> UPDATE mysql.user SET authentication_string='password' WHERE User='root';
as it overwrites the authentication_string, which is supposed to be a hash and not plain text, instead use:
mysql> `ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';`
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 :)
Note
Run
UPDATE mysql.user SET authentication_string=null WHERE User='root';
if you don't want to set a password for root user. Or if PASSWORD() function doesn't work for you.
Once you've installed MySQL, you'll need to establish the "root" password. If you don't establish a root password, then, well, there is no root password, and you don't need a password to log in.
So, that being said, you need to establish a root password.
Using terminal enter the following:
Installation: Set root user password:
/usr/local/mysql/bin/mysqladmin -u root password NEW_PASSWORD_HERE
If you've made a mistake, or need to change the root password use the following:
Change root password:
cd /usr/local/mysql/bin/
./mysql -u root -p
> Enter password: [type old password invisibly]
use mysql;
update user set password=PASSWORD("NEW_PASSWORD_HERE") where User='root';
flush privileges;
quit
The instructions provided in the mysql website is so clear, than the above mentioned
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
/usr/local/mysql/bin/mysql
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
mysql> exit or Ctrl + z
$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/support-files/mysql.server start
/usr/local/mysql/support-files/mysql -u root -p
Enter the new password i.e MyNewPass
Reference: http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
Stop the mysqld server.
Mac OS X: System Preferences → MySQL → Stop MySQL Server
Linux (From Terminal): sudo systemctl stop mysqld.service
Start the server in safe mode with privilege bypass
From Terminal: sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
In a new terminal window:
sudo /usr/local/mysql/bin/mysql -u root
This will open the MySQL command-line client. From here enter:
UPDATE mysql.user SET authentication_string=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
quit
Stop the mysqld server again and restart it in normal mode.
Mac OS X (From Terminal): sudo /usr/local/mysql/support-files/mysql.server restart
Linux Terminal: sudo systemctl restart mysqld
For the new MySQL 5.7, for some reason the binary commands of MySQL aren't attached to the shell, and you have to do:
Restart the Mac after the installation.
Start MySQL:
System Preferences → MySQL → Start button
Go to MySQL install folder in the terminal:
cd /usr/local/mysql/bin/
Access to MySQL:
./mysql -u root -p
And enter the initial password given to the installation.
In the MySQL client, change the password:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPassword';
In the terminal, write mysql -u root -p and hit Return.
Enter the current MySQL password that you must have noted down.
And set the password:
SET PASSWORD = PASSWORD('new_password');
Please refer to this documentation here for more details.
If you have forgot the MySQL root password, can’t remember or want to break in….. you can reset the MySQL database password from the command line in either Linux or OS X as long as you know the root user password of the box you are on:
(1) Stop MySQL
sudo /usr/local/mysql/support-files/mysql.server stop
(2) Start it in safe mode:
sudo mysqld_safe --skip-grant-tables
(3) This will be an ongoing command until the process is finished so open another shell/terminal window, log in without a password:
mysql -u root
UPDATE mysql.user SET Password=PASSWORD('password') WHERE User='root';
In the UPDATE command above just replace the 'password' with your own new password, make sure to keep the quotation marks
(4) Save and quite
FLUSH PRIVILEGES;
\q
(5) Start MySQL
sudo /usr/local/mysql/support-files/mysql.server start
I solved this by:
Shutting down my MySQL server: mysql.server stop
Running MySQL in safe mode: mysqld_safe --skip-grant-tables
In another terminal, login with mysql -u root
In the same terminal, run UPDATE mysql.user SET authentication_string=null WHERE User='root';, then FLUSH PRIVILEGES; and then exit with exit;
Stop the safe mode server with mysql.server stop and then start the normal one; mysql.server start
Now you can set your new password with
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
None of the previous comments solved the issue on my Mac.
I used the commands below and it worked.
brew services stop mysql
pkill mysqld
rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
brew postinstall mysql
brew services restart mysql
mysql -u root
When I installed OS X v10.10 (Yosemite), I got a problem with MySQL. I tried lot of methods, but none worked. I actually found a quite easy way. Try this out.
First log in to a terminal from super user (su) privileges.
sudo su
Stop MySQL
sudo /usr/local/mysql/support-files/mysql.server stop
Start in safe mode:
sudo mysqld_safe --skip-grant-tables
Open another terminal, log in as su privileges, and then, log in to the MySQL client (mysql) without a password
mysql -u root
Change the password
UPDATE mysql.user SET Password=PASSWORD('new_password') WHERE User='root';
Flush privileges
FLUSH PRIVILEGES;
You are done now.
The methods mentioned in existing answers don't work for MySQL 5.7.6 or later. According the MySQL documentation, this is the recommended way.
B.5.3.2.3 Resetting the Root Password: Generic Instructions
MySQL 5.7.6 and later:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
Reference: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
If you can't remember your password, #radtek's answer worked for me except in my case I had set up MySQL using brew which meant that steps 1 and 2 of his answer had to be changed to:
/usr/local/bin/mysql.server stop
/usr/local/bin/mysqld_safe --skip-grant-tables
Note: the lack of sudo.
I think this should work:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOURNEWPASSWORD'
(Note that you should probably replace root with your username if it isn't root.)
This is what exactly worked for me:
Make sure no other MySQL process is running. To check this do the
following:
From the terminal, run this command:
lsof -i:3306
If any PID is returned, kill it using kill -9 PID
Go to System Preferences → MySQL → check if any MySQL instances are running, stop them.
Start MySQL with the command:
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
The password for every user is stored in the mysql.user table under columns User and authentication_string respectively. We can update the table as:
UPDATE mysql.user SET authentication_string='your_password' where User='root'
Stopping MySQL Server
sudo /usr/local/mysql/support-files/mysql.server stop
Starting MySQL in safe mode
sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
Changing the root password
/usr/local/mysql/bin/mysql -u root
use mysql;
UPDATE user SET authentication_string=PASSWORD('NEW_PASSWORD') WHERE user='root';
FLUSH PRIVILEGES;
exit
Testing
Run /usr/local/mysql/bin/mysql -u root
Now enter the new password to start using MySQL.
To reference MySQL 8.0.15 + , the password() function is not available. Use the command below.
Kindly use
UPDATE mysql.user SET authentication_string='password' WHERE User='root';
You can manually turn-off MySQL on Mac, by clicking on  Apple menu and open System Preferences. Choose the “MySQL” preference panel, and then click on the “Stop MySQL Server” button to stop MySQL Server on Mac.
After you stop your MySQL, you'll need to follow these steps.
You'll need to start MySQL in skip-grant-tables mode
sudo /usr/local/mysql/support-files/mysql.server start --skip-grant-tables
In your terminal itself, enter this command to flush existing privileges
/usr/local/mysql/bin/mysql mysql> FLUSH PRIVILEGES;
Now you need to alter the user password
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'newpassword';
mysql> exit
Then you can go to  Apple menu and open System Preferences. Choose the “MySQL” preference panel, then click on the “Stop MySQL Server” button to stop MySQL Server on Mac.
Finally you can again go to  Apple menu and open System Preferences. Choose the “MySQL” preference panel, then click on the “Start MySQL Server” button to start MySQL Server on Mac.
This workaround works on my laptop!
Mac with macOS v10.14.5 (Mojave).
MySQL 8.0.17 was installed with Homebrew.
I run the following command to locate the path of MySQL
brew info mysql
Once the path is known, I run this:
/usr/local/Cellar/mysql/8.0.17/bin/mysqld_safe --skip-grant-table
In another terminal I run:
mysql -u root
Inside that terminal, I changed the root password using:
update mysql.user set authentication_string='NewPassword' where user='root';
and to finish I run:
FLUSH PRIVILEGES;
And voilà, the password was reset.
References
Try this in a terminal:
/usr/local/bin/mysql_secure_installation
macOS v10.14 (Mojave) and later with 5.7.26 installed from the Mac OS X DMG installer.
When attempting to use the UPDATE command posted by other users, it results in the following error:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
Copy the password that was presented to you by the installer, open a terminal, and do the following:
mysql -uroot -p
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOURPASSWORDHERE';
For MySQL 8
Shutdown MySQL server
Go to System Preferences -> MySQL
Click Stop MySQL Server button
Open two terminal [command-line] windows
In the first terminal window run the following:
mysqld_safe --skip-grant-tables
In the second terminal window do the following:
4.1. Login to MySQL
mysql -u root
4.2. Run the following in the MySQL prompt:
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'NEWPASSWORD';
4.3. Exit MySQL
exit;
Go back to the first terminal window and shutdown mysqld_safe
5.1. Press CTRL + Z
5.2. Run the following command
mysqladmin -u root -p shutdown
5.3. Enter the new password you set in 4.2. when prompted.
Start MySQL Server [see 1.]
If you forgot your password or want to change it to your MySQL:
Start your terminal and enter:
sudo su
Enter the password for you system
Stop your MySQL server:
sudo /usr/local/mysql/support-files/mysql.server stop
Leave this window open, run second terminal window and enter here:
mysql -u root
And change your password for MySQL:
UPDATE mysql.user SET authentication_string=PASSWORD('new_password') WHERE User='root';
where "new_password" - your new password. You don't need old password for MySQL.
Flush, quit and check your new password:
FLUSH PRIVILEGES;
Close all windows and check your new password for MySQL.
Much has changed for MySQL 8. I've found the following modification of the MySQL 8.0 "How to Reset the Root Password" documentation works with Mac OS X.
Create a temporary file, $HOME/mysql.root.txt, with the SQL to update the root password:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY '<new-password>';
This uses mysql_native_password to avoid the Authentication plugin 'caching_sha2_password' cannot be loaded error, which I get if I omit the option.
Stop the server, start with an --init-file option to set the root password, and then restart the server:
mysql.server stop
mysql.server start --init-file=$HOME/mysql.root.txt
mysql.server stop
mysql.server start
mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string='yourpasswd' WHERE User='root';
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
I somehow need to do this every time my MacBook restarts.
$ export PATH=$PATH:/usr/local/mysql/bin
now,to make this permanent:
$ echo 'export PATH=$PATH:/usr/local/mysql/bin' >> ~/.bash_profile
next, start mysql in safe mode:
$ sudo mysqld_safe --skip-grant-tables;
If this does not work, go to System Preferences and stop MySQL server.
next, On the **other** terminal, you may use the below:
$ mysql -u root
mysql> USE mysql;
mysql> UPDATE mysql.user SET authentication_string=null WHERE
User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ mysql -u root
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH
caching_sha2_password BY 'yourpassword';
$ mysql -u root -p
Enter password:
mysql> SELECT user();
next, start the mysql server in normal mode. and you're done with resetting your root password. this worked for mysql 8.0.17 ver. for me.
thanks to everyone on top, https://stackoverflow.com/questions/36099028/error-1064-42000-you-have-an-error-in-your-sql-syntax-want-to-configure-a-pa,
https://www.houseninetytwo.com/how-to-use-mysql-in-terminal-on-mac-os-high-sierra/#:~:text=You%20may%20have%20gotten%20something,%2Fmysql%2Fbin%2Fmysql.&text=It%20should%20execute%20the%20right,return%20your%20version%20of%20MySQL.
Read more here.
As of Dec 2022, the following works for MySQL 8.0.26 on macOS Big Sur 11.2.3 :
Go to system preferences > mysql > stop server
Open terminal and run: mysqld_safe --skip-grant-tables
Open a new terminal and run: mysql -u root
Run: ALTER USER 'root'#'localhost' IDENTIFIED BY 'ROOT';
ROOT will be your new password.
Run: FLUSH PRIVILEGES;
Run: exit
Go to system preferences > mysql > start server