161

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?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
Alisher Ulugbekov
  • 2,039
  • 2
  • 13
  • 8

27 Answers27

188

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

stackPusher
  • 6,076
  • 3
  • 35
  • 36
  • 9
    the previous posts did not work for me, but this one while destructive worked great. – Clay Jun 04 '18 at 17:28
  • 7
    when you are logged into mysql eventually, use `ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';` to set your new root password. – Martin Braun Dec 20 '19 at 17:41
  • This worked for me without deleting my existing database – Timothy Dec 28 '19 at 09:09
  • 1
    Finally a proper working solution. @2020 MacOs catalina. – Mindaugas Kurlys Feb 24 '20 at 13:25
  • This worked for me when the previous solution did not. Thank you! – Les Brown Jun 08 '20 at 17:24
  • Worked for me too – Andrew Sithole Jun 19 '20 at 17:41
  • None of the above proposed solutions worked for me but @Martin Braun's solution works. On Catelina, brewing mariadb sets up your user's account without password by default (not root!) So entering ```mysql``` without arguments logs in as (in my case) rvw@localhost. At this moment you can assign a password to root using ```ALTER USER 'root'@'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';``` – woens Aug 28 '20 at 16:14
  • `brew postinstall mysql` worked for me. thanks! – lucasvscn Dec 11 '20 at 19:00
  • Doesn't work for me. I still get ERROR 1698 (28000): Access denied for user 'root'@'localhost' – BSUK Dec 13 '20 at 12:07
  • 1
    This almost worked for me, what I did: `brew services stop mysql; brew uninstall mysql; pkill mysqld; rm -rf /usr/local/var/mysql; rm /usr/local/etc/my.cnf; brew postinstall mysql; brew install mysql; brew services restart mysql; mysql -uroot` – aubreypwd Aug 16 '21 at 05:45
157

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.

Michael
  • 8,362
  • 6
  • 61
  • 88
egermano
  • 1,809
  • 1
  • 12
  • 13
  • 17
    Worked for me. Obviously (perhaps), the dollar sign ($) must be input as part of the command (it's not just a shell prompt). – hobs Oct 19 '12 at 19:12
  • This is the cleanest solution here. Just ensure that mySQL is running (if not, `mysql.server start`) – Brent Faust Oct 20 '14 at 18:44
  • 2
    @BrentFoust Of course. I was disambiguating the usage of the `$` char at the beginning of a command line shell command. Others might be confused because the `$` sign is often used as a shell prompt marker on SO and other forums. But the lack of a space between the `$` and `(` should clue folks in as well. – hobs Oct 20 '14 at 19:00
  • 5
    I would recommend not including NEWPASS in the command to avoid storing in your shell's history file. That command will automatically ask for input if you run it without the pass so there's no reason to it. :) – levifig Dec 10 '14 at 21:08
  • 1
    Warning: /usr/local/opt/mysql/bin/mysqladmin: unknown variable 'loose-local-infile=1' /usr/local/opt/mysql/bin/mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost' (using password: YES)' – Alex Ryan Dec 02 '15 at 20:37
  • Doesn't work. I used `brew services mysql start` and I get an error `Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!`. It's already running. – BugHunterUK Aug 02 '16 at 17:52
  • I get the error `mysqladmin: unable to change password; error: 'File './mysql/user.MYD' not found (Errcode: 2 - No such file or directory)'` – Agent Zebra Sep 06 '16 at 05:48
  • 6
    if using mariadb: `$(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS` – bryceadams Jan 28 '17 at 09:46
  • This works well for Maria DB as well - have an updoot. – Richard Cagle Sep 25 '19 at 02:19
  • 11
    Not working for me, with mariadb (even with @bryceadams note): /usr/local/opt/mariadb/bin/mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user 'root'@'localhost'' – trainoasis Oct 22 '19 at 08:39
  • 21
    for mariadb, if you get access denied for user: `sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS` – upful Mar 20 '20 at 15:47
76

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!

Yasin Okumuş
  • 2,299
  • 7
  • 31
  • 62
ablemike
  • 3,384
  • 3
  • 22
  • 21
  • 6
    To stop the unprotected MySQL server after changing the password: `mysqladmin -u root shutdown` – Vebjorn Ljosa Jul 06 '15 at 02:08
  • 9
    I just ran into this problem, and found this answer to be the closest. I did notice that `password` was not a valid column in mysql 5.7.9. Instead I had to use `authentication_string` instead. The rest of the instructions worked perfectly. Thanks! – M. Scott Ford Nov 13 '15 at 03:07
  • 5
    As M. Scott Ford said, the proper command is: `update user set authentication_string=password('new_password_here') where user='root';` – Panayotis Nov 14 '15 at 09:48
  • 3
    I had to run `update mysql.user set authentication_string=password('none') where user='root';` for it to work. (Like the comment right above me, but prepend `mysql.` to `user`.) – Steve Meisner Oct 08 '16 at 15:02
  • 3
    password function is removed since mysql 8.0.11. details [more](https://dev.mysql.com/doc/refman/8.0/en/encryption-functions.html#function_password) – chancyWu Apr 01 '19 at 01:35
  • 1
    Column name has changed... UPDATE mysql.user SET authentication_string=PASSWORD('xxxx!') WHERE User='root'; – kevinc Oct 08 '19 at 14:45
65

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
Britto
  • 808
  • 5
  • 4
  • worked perfectly for me. I don't actually want to set a password for my local machine, so the other solutions aren't really what i'm looking for – brad Dec 16 '13 at 03:13
  • 3
    mysql_install_db is deprecated, consider using `mysqld --initialize` – Brian Jun 28 '16 at 15:16
  • 1
    I just get `No such file or directory` when I run that first command. – Agent Zebra Sep 06 '16 at 05:45
  • 1
    after running mysqld --initialize command you will notice in log message on console that it set temporary password for root user.. copy that and use password for next time you fire command "mysql -uroot -p" -- It will definitely work .. and then mysql will ask you to reset the password .. check this out http://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html – Vikrant Labde Oct 13 '16 at 06:22
  • @VikrantLabde "It will definitely work" - very ambitious statement. I tried this, used the temporary password it gave me but it still did not grant access. Did you have to escape any of the punctuation with a backslash? – Max Goodridge Aug 01 '17 at 19:49
55

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
Nirojan Selvanathan
  • 10,066
  • 5
  • 61
  • 82
  • 1
    This is the perfect solution. I'd already tried running `mysql_secure_installation` but the key part was the `mysql.server restart` – Johan Dec 02 '16 at 09:16
  • mysql_secure_installation was the key piece – Nik Dec 05 '16 at 20:13
  • 1
    To avoid the headache...just type use the restart command like nirojan says, then run mysql_secure_installation – Dan Zuzevich Feb 22 '18 at 09:23
  • This worked on Ventura 13.4. Something got "configured" on reboot, and was able to continue with my work. This should be added to the brew installer for ease of use. – vsecades Aug 21 '23 at 23:11
33

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!

Arunas Bartisius
  • 1,879
  • 22
  • 23
12

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:

  1. mysql -u YOUR-SYSTEM-USERNAME -p
  2. The password is empty (press enter)
  3. ALTER USER 'root'@'localhost' IDENTIFIED BY 'NEW-ROOT-PASSWORD';
  4. FLUSH PRIVILEGES;

The reason:

  1. Homebrew will create a user with root privileges named by the current MacOS username.
  2. it has no password
  3. Since it has all privileges, just reset the root password with that user.
  4. The initial password for root was randomly generated.
Shiji.J
  • 1,561
  • 2
  • 17
  • 31
10

This worked for me:

sudo mysql -u root
Béatrice Cassistat
  • 1,048
  • 12
  • 37
10
  1. go to apple icon --> system preferences
  2. open Mysql
  3. in instances you will see "initialize Database"
  4. click on that
  5. you will be asked to set password for root --> set a strong password there
  6. use that password to login in mysql from next time

Hope this helps.

user3325650
  • 121
  • 2
  • 5
7

Try with sudo to avoid the "Access denied" error:

sudo $(brew --prefix mariadb)/bin/mysqladmin -u root password NEWPASS

7

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'
MajAfy
  • 3,007
  • 10
  • 47
  • 83
4

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');
Dharman
  • 30,962
  • 25
  • 85
  • 135
jonny
  • 508
  • 5
  • 11
  • So the information on this website is incorrect? , because it claims the `mysql_secure_installation` should work? https://mariadb.com/resources/blog/installing-mariadb-10-1-16-on-mac-os-x-with-homebrew/ – Luuk Oct 31 '20 at 15:27
  • When I ran brew install mysql_install_db was triggered by brew. That blog post suggests running it manually. Maybe the brew procedure changed :shrug: – jonny Nov 01 '20 at 16:16
  • I just installed mariadb 10.5.6 on my mac, and was able to use `mariadb -u root` (without password) – Luuk Nov 01 '20 at 17:02
2

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.

Spyder
  • 1,882
  • 13
  • 23
  • 3
    I had the access denied issue assuming no root password so I don't think the issue is obvious. – Jack Frost Oct 28 '13 at 18:07
  • I'm not saying the issue is obvious; for anyone who has used MySQL under normal circumstances it's utterly confusing. But looking at the other responses, the reason is clear. – Spyder Oct 29 '13 at 22:19
2

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!

Katie Lee
  • 21
  • 1
  • to start mysql 5.7 use https://stackoverflow.com/questions/51573145/formula-mysql-is-not-installed?answertab=active#tab-top – Winnipass Nov 03 '19 at 06:43
2

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!!!!';
MSpreij
  • 1,142
  • 13
  • 20
heyfranksmile
  • 169
  • 1
  • 5
  • 2
    You have to warn that, running `rm -rf /usr/local/var/mysql/` command will delete all databases and Mysql data on your computer! – Lashae Nov 25 '19 at 07:44
1

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.

erin
  • 1,130
  • 3
  • 17
  • 28
1

Check that you don't have a .my.cnf hiding in your homedir. That was my problem.

1

The default password when you install mysql via brew is root try this, it worked for me

mysql -uroot -proot

user3805033
  • 157
  • 1
  • 3
1

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.

MSpreij
  • 1,142
  • 13
  • 20
1

I stumbled across this too and the solution was unironically to simply run this:

mysql

iomv
  • 2,409
  • 18
  • 28
0

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
ssc
  • 9,528
  • 10
  • 64
  • 94
  • This might have worked at the time the answer was posted, but broke in the meantime; with (current) MySQL version 5.7.21, it fails with ` Unknown column 'Password' in 'field list'`. – ssc Mar 04 '18 at 18:50
  • UPDATE mysql.user SET authentication_string=PASSWORD('xxxxxx!') WHERE User='root'; – kevinc Oct 08 '19 at 14:44
0

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

0

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.

kbrock
  • 958
  • 12
  • 15
0

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

SekharKari
  • 501
  • 5
  • 10
0

login to DB as root :

sudo mysql -u root

you may run secure db as:

sudo mysql_secure_installation
Vikram
  • 187
  • 9
-1

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

Sten
  • 162
  • 7
-1

What is the easiest way to run Mysql on a Mac ?

My solution was to install MAMP.

https://www.mamp.info/en/mac/

David Raleche
  • 436
  • 5
  • 11