Skip to content

Instantly share code, notes, and snippets.

@fallwith
Created May 23, 2011 22:10
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save fallwith/987731 to your computer and use it in GitHub Desktop.
Save fallwith/987731 to your computer and use it in GitHub Desktop.
Reset MySQL root password (Homebrew)
$> launchctl unload -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
$> /usr/local/Cellar/mysql/5.5.9/bin/mysqld --basedir=/usr/local/Cellar/mysql/5.5.9 --datadir=/usr/local/Cellar/mysql/5.5.9/data --plugin-dir=/usr/local/Cellar/mysql/5.5.9/lib/plugin --log-error=/usr/local/Cellar/mysql/5.5.9/data/errors.err --pid-file=/usr/local/Cellar/mysql/5.5.9/data/pidfile.pid --skip-grant-tables
$> mysql
mysql> UPDATE mysql.user SET Password=PASSWORD('root_password') WHERE User='root';
mysql> FLUSH PRIVILEGES;
$> kill `cat /usr/local/Cellar/mysql/5.5.9/data/pidfile.pid`
$> ps auwx | grep mysql
$> launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
@preston
Copy link

preston commented Dec 17, 2015

Note that on my current 5.7 install, you can just:

mysql.server restart --skip-grant-tables

..and then set your password.

@raags
Copy link

raags commented Dec 28, 2016

This is what worked for me for mysql 5.7.9

UPDATE mysql.user
    SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'
    WHERE User = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES;

@sebastianwebber
Copy link

The utility mysql_secure_installation can do the trick as well.

Thanks all for sharing this info.

@jflopezr11
Copy link

Hey i know im way late to the party but i just want to thank you all for this. this helped a lot!

@tsecore
Copy link

tsecore commented Sep 26, 2019

I have mysql 5.7 on my OS majave 10.14.6 - installed using Brew. I can start and stop mysql with brew services start mysql@5.7, but I can't connect to mysql, it throws this error ERROR 1045 (28000): Access denied for user' (using password: NO). My question is if I can't connect to mysql, how can I run @raags script. Also, I tried executing @preston mysql.server restart --skip-grant-tables but it erred out with unable to restart due to PID issue and the error said it's stopped mysql server - but when I ran brew services list it showed 3 local mysql instances still running, which I ended up killing all 3 instances with kill -9 {PID} - what is the cleanest way to reset my root password at this point - do you know if Brew offers this option? Help would be greatly appreciated!

Copy link

ghost commented Oct 4, 2019

When I installed MySQL on Homebrew, I tried mysql_secure_installation and I put the new password (because there was no assigned password) but when I pressed enter, I got a 'ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)'. Any way to help?

@xiandong79
Copy link

same with @aristonlorenzo

@agussudarmanto
Copy link

No reinstall needed for me,
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

@roadev
Copy link

roadev commented Apr 16, 2020

mysql_secure_installation

it doesn't work for me :(

@odurgeau
Copy link

try :
sudo mysql_secure_installation

@congfandi
Copy link

try :
sudo mysql_secure_installation

it working for me on Catalina 10.15.6

@dillanjwilding
Copy link

I did brew install mysql and then ran sudo mysql_secure_installation. The first thing it asks is the password for root. I've tried my login password, root, and no password and nothing and everything gives an Access denied for user 'root'@'localhost' error. It supposedly said it installed it without a password but not supplying one doesn't work. I've also tried mariadb and that doesn't work either. Can anyone help me get unstuck? Thanks!

@imamalys
Copy link

$ 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

this work perfectly for me

@pelmered
Copy link

pelmered commented Oct 10, 2020

$ 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

this work perfectly for me

Thanks @imamalys! This was the only thing that worked for me!

@petrewoo
Copy link

$ 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

this work perfectly for me

Thanks @imamalys! It is worked for me!

@tilkofjin
Copy link

$ 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

this work perfectly for me

@imamalys! It worked, Thanks so much!

@kkasaei
Copy link

kkasaei commented Jul 29, 2021

$ 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

this work perfectly for me

@imamalys! It works for me, Thanks

@Johnhughes814
Copy link

Johnhughes814 commented Jan 31, 2022

$ 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

this work perfectly for me
@imamalys THANK YOU!!!

@rajeswarivmarimuthu
Copy link

Everytime I restart my system, I will have to re-do these steps to work with mysql! Is there a way I can make this a permanent change?

@vijit-chandhok
Copy link

brew services restart mysql

thanks for this. it worked for me

@SaimumIslam
Copy link

/opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql to run safe server

@brandondebison
Copy link

Everytime I restart my system, I will have to re-do these steps to work with mysql! Is there a way I can make this a permanent change?

@rajeswarivmarimuthu Did you ever figure out what is causing this? mine is doing the same thing

@AlMahfuzKhanD
Copy link

$ 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

this work perfectly for me @imamalys THANK YOU!!!

Not working form me in macOS Sonoma 14.1.1
Any other solution?

@itay-shoushan
Copy link

$ 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
this work perfectly for me @imamalys THANK YOU!!!

Not working form me in macOS Sonoma 14.1.1 Any other solution?

try this, It's works for me :)

https://aungzanbaw.medium.com/how-to-reset-root-user-password-in-mysql-8-0-a5c328d098a8

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