MySQL Root Password Reset (Linux)

April 28th, 2010

I ran into a case earlier today where I was setting up a LAMP (Linux, Apache, MySQL, PHP) Server whcih already had most of these components pre-installed.  However, no one was really sure what the MySQL Root User password was - and there clearly was one, as attempts to connect without it failed.

The obvious answer was to reset the Root user password - the method of doing so was less obvious.

First, you need to stop the running mysqld daemon.  First try:

# /etc/init.d/mysqld stop

If that doesn't work for any reason, try this sequence:

# find / -name *.pid

Locate the mysqld.pid file, then:

# cat /path_to/mysqld.pid

This will return a number pointing to the process we need to stop. Replace xxxxx below with the returned number.

# kill xxxxx

Now the current running MySQL has been stopped. Time to start it up again, with a twist:

# mysqld_safe --skip-grant-tables &

Now MySQL is running, though very unsecurely. Quickly now!

# mysql -u root -p

This will connect you to the MySQL Command Line interface. If it asks for a password, leave it blank.

mysql> use mysql

This command tells MySQL to switch to a database named 'mysql' - where MySQL stores it's own information such as Users.

mysql> UPDATE user SET password=PASSWORD('new_root_pass')
     WHERE User='root';

This will update MySQL's User table, changing the password for root to 'new_root_pass'. YOu may notice that several rows are updated - there's often a 'root' user for each Host (localhost, 127.0.0.1, and your server IP).

Lastly, shut down the Unsecure MySQL Server and restart it as normal.

mysql>  exit
# /etc/init.d/mysqld stop
# /etc/init.d/mysqld start

MySQL will now start back up with proper User Permissions.

# mysql -u root -p

You'll now be prompted for a Password. Enter your NEW root password, and you'll be up and running!

Add a Comment:

 

Musings on Coding, Gaming, and Life in General
Loading...