Resetting the root password for MySQL running on RHEL or CentOS

I recently had to reset the MySQL root password due to the fact that initializing it the way I assumed it should did not work. The following procedure will work in CentOS/RHEL/Scientific Linux and Fedora.

After installing MySQL using

# yum install mysql-server

I ran the command

# mysqladmin -u root password 'new-password'

Trying to log in with the following failed

# mysql -u root -p

with the following error

Access denied for user 'root'@'localhost'

Decided to not spend more time as it’s a fresh MySQL installation. And did the following to reset the root password for MySQL.

Resetting the root password

1) Stopped the MySQL service.

# service mysqld stop

2) Started MySQL in safe mode.

# mysqld_safe --skip-grant-tables &

3) Logged in using root.

# mysql -u root

4) Reset the password.

> use mysql;
> update user set password=PASSWORD("mynewpassword") where User='root';
> flush privileges;
> quit

5) Stop MySQL in safe mode.

# service mysqld stop

6) Start MySQL.

# service mysqld start

7) Log in using the new password.

# mysql -u root -p

Success!

// CrashMAG

9 thoughts on “Resetting the root password for MySQL running on RHEL or CentOS”

  1. please help..
    cause
    I follow all process
    and enter code > # mysql -u root -p
    my terminal prompt Enter Password:
    but I could not type password there cause there is no key strock effect
    and when I hit ENTER it show error message

  2. I’m using Scientific Linux 6.3 that came with mysqld already installed.
    I managed as root user to connect to mysqld using the following client command by itself:

    # mysql

Leave a Reply

Your email address will not be published. Required fields are marked *

*