How to change the MySQL root password in Linux or OSX via the command Line

If you have forgot the MySQL root password, or can’t remember it. You can reset the MySQL database password from the command line in Linux or OS X as long as you know the root user password of the server/system you’re on.

First you need to stop MySQL using:

~# sudo /usr/local/mysql/support-files/mysql.server stop
or
~# sudo /etc/init.d/mysqld stop

Then start MySQL in safe mode using:

~# sudo mysqld_safe –skip-grant-tables

This could take a while to finis so open another shell/terminal window, log in without a password:

mysql -u root

Then using this command:

mysql> UPDATE mysql.user SET Password=PASSWORD(‘password_you_want’) WHERE User=’root’;

Then using:

mysql> FLUSH PRIVILEGES;

mysql> quit
or
mysql> \q

Then start MySQL again using:

~# sudo /usr/local/mysql/support-files/mysql.server start
or
~# sudo /etc/init.d/mysqld start

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s