Change and Reset MySQL root Password

»»»Change and Reset MySQL root Password
There are many ways to reset and change the root password for mySQL database, in the event that the password is forgotten or lost, or you simply want to make the password securer with complex algorithm and regular change. The following instructions explain in details the alternative way at the last part of the guide mentioned above, where no additional file needs to be created:

  1. Login as root to the Windows or Unix-like (Unix, Linux or BSD) machine with the MySQL server.
  2. Stop the MySQL server by using either of the following command:

    Linux:

    /etc/rc.d/init.d/mysql stop

    FreeBSD:

    /usr/local/etc/rc.d/mysql-server.sh stop
  3. Open the mysql server start-up script (i.e. mysql-server.sh or mysql which is the file executed to start or stop MySQL server).
  4. ADD –skip-grant-tables to the end of the line that contains the mysqld_safe command as its parameter.
  5. Start MySQL server with the following command:

    Linux:

    /etc/rc.d/init.d/mysql start

    FreeBSD:

    /usr/local/etc/rc.d/mysql-server.sh start
  6. Alternatively, start the MySQL server directly and skip the editing with the following command (if you’re using this step, you don’t need to modify the MySQL startup script):
    mysqld_safe --skip-grant-tables &

    Depending on your path environment, you may need to point to the correct directory where mysqld_safe is instead.

  7. Run the following commands to login as the mysql user and connect to mysql user/permission database:
    # mysql -u root mysql
  8. Run the update queries to change the MySQL password:
    mysql> UPDATE user SET Password=PASSWORD('newrootpassword') WHERE User='root';
    mysql> FLUSH PRIVILEGES;

    Note: Replace newrootpassword with the new root password for MySQL server. Flush Privileges is needed to making the password change effect immediately.

  9. Exit mysql database client by typing exit.
  10. Stop MySQL server with commands listed at step 2.
  11. Open the mysql server startup script edit in step 3 again and REMOVE the –skip-grant-tables parameter that has been added.
  12. Start MySQL server by using command from step 5 or 6.

For Redhat Linux users, use the following instructions as the root user of Redhat Linux machine:

  1. Stop MySQL process by using command:
    # killall mysqld
  2. Start the MySQL server with following options:
    # /usr/libexec/mysqld -Sg --user=root &
  3. Start the MySQL client:
    # mysql

    You should see the following message:

    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 1 to server version: 3.xx.xx

    Type ‘help;’ or ‘h’ for help. Type ‘c’ to clear the buffer.

    mysql>

  4. Use mysql database:
    mysql> USE mysql 

    You should see the following message:

    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed

  5. Then, update the password for the root user with the following command:
    UPDATE user SET password=password("newpassword") WHERE user="root";

    Replace newpassword with your desired password. You should see the following message:

    Query OK, 2 rows affected (0.03 sec)
    Rows matched: 2 Changed: 2 Warnings: 0

    Rows affected may be different, but the Query OK should be there.

  6. Flush the database privileges to reload it in order to make the changes effective:
    mysql> flush privileges; 

    You should get the following result:

    Query OK, 0 rows affected (0.02 sec)

  7. Exit the MySQL client by typing exit.
  8. Kill the MySQL server process by typing killall mysqld.
  9. Then start MySQL again:
    /etc/init.d/mysqld start

By | 2016-12-09T08:42:30+00:00 December 9th, 2016|Categories: Databases|Tags: |10 Comments

About the Author:

LK is a technology writer for Tech Journey with background of system and network administrator. He has be documenting his experiences in digital and technology world for over 15 years.Connect with LK through Tech Journey on Facebook, Twitter or Google+.

STAY IN TOUCH

close-link