Skip to content

Instantly share code, notes, and snippets.

@nickcernis
Created July 13, 2020 15:13
Show Gist options
  • Save nickcernis/1c2a1c98a0660d4798d55b1999d52b4c to your computer and use it in GitHub Desktop.
Save nickcernis/1c2a1c98a0660d4798d55b1999d52b4c to your computer and use it in GitHub Desktop.
Install MariaDB with brew on macOS and fix the “access denied” issue

Attempting mysql -u root fails with Access denied for user 'root'@'localhost immediately after doing brew install mariadb and starting mariadb with brew services start mariadb.

To fix it (with MariaDB still running):

  1. sudo mysql then enter your Mac user password
  2. ALTER USER 'root'@'localhost' IDENTIFIED BY 'newrootpassword'; replacing newrootpassword with the password you wish to use for the MariaDB root user.
  3. Ctrl-C to exit mysql.

You should then be able to connect to MariaDB with mysql -u root -p, then entering the root password when prompted.

@saiftheboss7
Copy link

This is was very helpful. Thanks a lot!

@nandastone
Copy link

Thanks!

@stefan-1st
Copy link

Worked like a charm. Thanks

@Josephcoder
Copy link

Thanks kbs.

@anjanesh
Copy link

Thanks for this. How do I bookmark this in GitHub ?

@owocc
Copy link

owocc commented Oct 22, 2023

谢谢你!这真的很有用!!太酷了~

@mantr88
Copy link

mantr88 commented Nov 11, 2023

Thanks. This has helped me.

@CarlosVP120
Copy link

Man is a life saver!

@oSamDavis
Copy link

Hero !!

@zielu92
Copy link

zielu92 commented Feb 2, 2024

If it is still not working for someone, then the root username is the same as the Mac user.

@fahkrymalta21
Copy link

Thank you so much, it work on me

@ken717w
Copy link

ken717w commented Mar 12, 2024

Saved my day!

@michael-picard
Copy link

Thanks !

@sunchuo
Copy link

sunchuo commented Apr 14, 2024

👍

@emi-kh
Copy link

emi-kh commented Jun 20, 2024

Thank you so much. I was stuck on this bug for hours and this is the only thing that worked!

@ouun
Copy link

ouun commented Jul 13, 2024

Thanks for the hint. Ended up withALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword'; flush privileges; exit;.

@jsvini
Copy link

jsvini commented Jul 15, 2024

thanks Nick, you saved me! 👍

@mwangaben
Copy link

Thanks alot

@vanderw
Copy link

vanderw commented Aug 10, 2024

Thanks
At first, i don't believe a reset is needed after a new installation. But i really hard reset it over years. I wont past the relative commands i used to prevent misleading
useful knowledges increased.

@netzbub
Copy link

netzbub commented Aug 31, 2024

Thanks a lot !!
Still good - even four years later ... 😅

@xiaozongyang
Copy link

cool!

@fathurrohman26
Copy link

Another tricks

❯ mariadb-secure-installation -u $(whoami)

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):
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.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

❯ mariadb -u root -proot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 11.7.2-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)]> ^DBye

@imvenj
Copy link

imvenj commented Apr 8, 2025

@fathurrohman26 Thank you. This is a better way to initialize mariadb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment