Skip to content

Instantly share code, notes, and snippets.

@zubaer-ahammed
Last active April 4, 2024 10:38
Show Gist options
  • Save zubaer-ahammed/c81c9a0e37adc1cb9a6cdc61c4190f52 to your computer and use it in GitHub Desktop.
Save zubaer-ahammed/c81c9a0e37adc1cb9a6cdc61c4190f52 to your computer and use it in GitHub Desktop.
Reset MySQL Root Password in Mac OS

Reset mysql root password in Mac OS:

First Stop MySQL:

  1. Go to: 'System Preferences' >> 'MySQL' and stop MySQL

OR,

  1. sudo /usr/local/mysql/support-files/mysql.server start
  2. sudo /usr/local/mysql/support-files/mysql.server stop
  3. sudo /usr/local/mysql/support-files/mysql.server status

Process to Reset MySQL Root Pass in Mac:

  1. Make sure you have Stopped MySQL first (above).

  2. Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables

  3. In a new window connect to the database, set a new password and flush the permissions & quit: mysql -u root

  4. For MySQL older than MySQL 5.7 use:

    UPDATE mysql.user SET Password=PASSWORD('rootpass') WHERE User='root';

    For MySQL 5.7+ use:

    UPDATE mysql.user SET authentication_string=PASSWORD("rootpass") WHERE User='root';
  5. Now flush privileges:

    FLUSH PRIVILEGES;
  6. Restart MySQL server.

  7. More info: http://stackoverflow.com/questions/6474775/setting-the-mysql-root-user-password-on-os-x

@anice1
Copy link

anice1 commented Oct 27, 2021

MacOSX MySql root Password Reset master solution Step by step guide- $ 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

Thanks this worked for me

@monawoh
Copy link

monawoh commented Oct 27, 2021

hey , I have a problem
when I enter ' sudo mysqld_safe --skip-grant-tables ' and the doing my password the answer that I get is 'sudo: mysqld_safe: command not found' , what can I do ? I can't reset my password for mysql ...
need help

I have the same error. :(

@EmmaDevp1
Copy link

For Mac users, the best option is to uninstall the current version and re install. Then you would have option to enter a new password. It worked for me.

@ilyas-shah
Copy link

ilyas-shah commented Feb 10, 2022

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:
Make sure you have Stopped MySQL first (above).
Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
Then
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

Thanks!

@luxcode007
Copy link

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

Make sure you have Stopped MySQL first (above).
Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
Then
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

I am using MYSQL 8.0.28 on mac m1 and this worked for me, but make sure you close down MYSQL safe start before restarting it after this process and your new password set here will work

@Funbi13
Copy link

Funbi13 commented Feb 24, 2022

Amazing!

@demirezenmert
Copy link

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

  1. Make sure you have Stopped MySQL first (above).
  2. Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
    mysql -u root
    UPDATE mysql.user SET authentication_string=null WHERE User='root';
    FLUSH PRIVILEGES;
    exit;
    Then
    mysql -u root
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

Worked.

@fetiu
Copy link

fetiu commented Jun 2, 2022

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

  1. Make sure you have Stopped MySQL first (above).
  2. Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
    mysql -u root
    UPDATE mysql.user SET authentication_string=null WHERE User='root';
    FLUSH PRIVILEGES;
    exit;
    Then
    mysql -u root
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

For the people who have errors like:

ER_NOT_SUPPORTED_AUTH_MODE
'cryptography' package is required for sha256_password or caching_sha2_password auth methods

use ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'yourpasswd'; instead.
caching_sha2_password doesn't seem like natively supported on some client applications. So in my case mysql_native_password was needed.

@hassan-sempro
Copy link

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

Make sure you have Stopped MySQL first (above).
Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
Then
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

I am using MYSQL 8.0.28 on mac m1 and this worked for me, but make sure you close down MYSQL safe start before restarting it after this process and your new password set here will work

I apologize for such a basic question - I have no idea how to do any of this. I am brand new to MySQL and tech in general. Where am I supposed to enter this? In Terminal? I have an M1 as well and the same version of MySQL Workbench. This has been frustrating me for a few days now.

@namxhung
Copy link

namxhung commented Jun 10, 2022

> **MacOSX MySql root Password Reset master solution**
> **Step by step guide-**
> $ 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

This has worked for me

Thanks 👍

WORKED FOR ME <3

@josefophe
Copy link

the bad news : no solution from above worked for me the good news : just i initialize database from with new password, and it's working now

worked for me. 👍🏾

@JasmineDaniels
Copy link

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

  1. Make sure you have Stopped MySQL first (above).
  2. Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
    mysql -u root
    UPDATE mysql.user SET authentication_string=null WHERE User='root';
    FLUSH PRIVILEGES;
    exit;
    Then
    mysql -u root
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

This worked for me. Just dont forget to exit server, stop server and then restart.

Thanks Paul, this worked!

@vovs03
Copy link

vovs03 commented Aug 9, 2022

@BeKind-Rewind
Copy link

I use homebrew but this only succeeded in returning me back to the original password...

@hashaaan
Copy link

hashaaan commented Sep 6, 2022

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

Make sure you have Stopped MySQL first (above).
Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
Then
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

I am using MYSQL 8.0.28 on mac m1 and this worked for me, but make sure you close down MYSQL safe start before restarting it after this process and your new password set here will work

This really works!!!

@daniellemaxwell
Copy link

daniellemaxwell commented Sep 24, 2022

Here's what worked for me:
Make sure you have stopped MySQL (as mentioned above)
Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
In a new terminal: mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
exit
Shut down the server in safe mode
Then
mysql -u root
If mysql -u root doesn't work try: mysql -u root -p

@mahshad
Copy link

mahshad commented Oct 3, 2022

Thank you all for your help and input ... I'm using M1 chip silicon setup, and the following worked for me;

brew services stop mysql
pkill mysqld
rm -rf /usr/local/var/mysql/
brew postinstall mysql
brew services restart mysql
mysql -u root
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 'secret';

Worked for me. Thanks!

@dyfloveslife
Copy link

Update: On 8.0.15 (maybe already before that version) the PASSWORD() function does not work You have to do:

  1. Make sure you have Stopped MySQL first (above).
  2. Run the server in safe mode with privilege bypass: sudo mysqld_safe --skip-grant-tables
    mysql -u root
    UPDATE mysql.user SET authentication_string=null WHERE User='root';
    FLUSH PRIVILEGES;
    exit;
    Then
    mysql -u root
    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';

Worked for me. Thanks!

@ohadnav
Copy link

ohadnav commented Nov 14, 2022

> **MacOSX MySql root Password Reset master solution**
> **Step by step guide-**
> $ 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

This has worked for me

Thanks 👍

WORKED FOR ME <3

running rm -rf /usr/local/var/mysql/ ** deleted all my files 😱

@kerenren
Copy link

this works for me. thank you!

@santex
Copy link

santex commented Dec 29, 2022

mysql -u root

cheers

@wiyantotan
Copy link

For mysql 8 in MacOS

  1. stop all mysql server service
  2. cd /usr/local/mysql/bin
  3. sudo ./mysqld_safe --skip-grant-tables
  4. this will start mysql server in this terminal

Start new terminal

  1. cd /usr/local/mysql/bin
  2. sudo ./mysql -u root
  3. FLUSH PRIVILEGES;
  4. UPDATE mysql.user SET authentication_string=null WHERE User='root';
  5. FLUSH PRIVILEGES;
  6. exit;
  7. sudo ./mysql -u root
  8. ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
  9. FLUSH PRIVILEGES;
  10. exit;
  11. sudo ./mysqladmin -u root shutdown -p (this will shutdown mysql server that started at the other terminal)
  12. start mysql with the normal way

@MinimalDesigns
Copy link

MacOSX MySql root Password Reset master solution
Step by step guide-
$ 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

Thanks a lot!! This really works!!

@basiltime
Copy link

Initializing the database from Mac's System Settings worked 👏 I tried a few of the other methods and none worked for me.

System Settings -> MySQL -> Initialize Database.

It will ask for a password, that's where you choose a new one.

@DouggyC
Copy link

DouggyC commented Mar 15, 2023

For mysql 8 in MacOS

  1. stop all mysql server service
  2. cd /usr/local/mysql/bin
  3. sudo ./mysqld_safe --skip-grant-tables
  4. this will start mysql server in this terminal

Start new terminal

  1. cd /usr/local/mysql/bin
  2. sudo ./mysql -u root
  3. FLUSH PRIVILEGES;
  4. UPDATE mysql.user SET authentication_string=null WHERE User='root';
  5. FLUSH PRIVILEGES;
  6. exit;
  7. sudo ./mysql -u root
  8. ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
  9. FLUSH PRIVILEGES;
  10. exit;
  11. sudo ./mysqladmin -u root shutdown -p (this will shutdown mysql server that started at the other terminal)
  12. start mysql with the normal way

Thanks! works for mysql 8.0.28

@httol
Copy link

httol commented May 16, 2023

brew services restart mysql

Yes

@quicksilverr
Copy link

quicksilverr commented Jun 3, 2023

new terminal: mysql -u root
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
exit
Shut down the server in safe mode
Then
mysql -u root
If mysql -u root doesn't work try: mysql -u root -p

while doing this

I'm getting this error
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

@v-zhg3nti
Copy link

mysql -u root

oh thanks i can go to sleep <3 :D

@s-gorton
Copy link

s-gorton commented Oct 4, 2023

For mysql 8 in MacOS

  1. stop all mysql server service
  2. cd /usr/local/mysql/bin
  3. sudo ./mysqld_safe --skip-grant-tables
  4. this will start mysql server in this terminal

Start new terminal

  1. cd /usr/local/mysql/bin
  2. sudo ./mysql -u root
  3. FLUSH PRIVILEGES;
  4. UPDATE mysql.user SET authentication_string=null WHERE User='root';
  5. FLUSH PRIVILEGES;
  6. exit;
  7. sudo ./mysql -u root
  8. ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
  9. FLUSH PRIVILEGES;
  10. exit;
  11. sudo ./mysqladmin -u root shutdown -p (this will shutdown mysql server that started at the other terminal)
  12. start mysql with the normal way

When i get to 'flush privileges' i get a key icon - i attempted the flush privileges three times and it says 'incorrect passwords attempts'...? what does this mean?

@Bouff6
Copy link

Bouff6 commented Apr 4, 2024

For mysql 8 in MacOS

  1. stop all mysql server service
  2. cd /usr/local/mysql/bin
  3. sudo ./mysqld_safe --skip-grant-tables
  4. this will start mysql server in this terminal

Start new terminal

  1. cd /usr/local/mysql/bin
  2. sudo ./mysql -u root
  3. FLUSH PRIVILEGES;
  4. UPDATE mysql.user SET authentication_string=null WHERE User='root';
  5. FLUSH PRIVILEGES;
  6. exit;
  7. sudo ./mysql -u root
  8. ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
  9. FLUSH PRIVILEGES;
  10. exit;
  11. sudo ./mysqladmin -u root shutdown -p (this will shutdown mysql server that started at the other terminal)
  12. start mysql with the normal way

Thanks! works for mysql 8.0.28

This worked for me, thank you!

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