How to Reset or Change MySQL Root Password on Linux?

How to Reset or Change MySQL Root Password on Linux?

May 29, 2020 / Eternal Team

MySQL is a database management system operated under the Open Source software model. It has become a very popular software package for web application software. Like many web services, MySQL has an administrator-level or root password. The root password allows a user to perform all top-level functions in the database.

If you’ve never set a root password on your MySQL database, you should be able to connect to it. However, this is not a good idea as it means anyone can access your database.

If your database already has a root password, but you lost track of it, this guide will help you reset a Mysql Root password on Linux

Use the following steps to reset a MySQL root password by using the command line interface.

  • Step 1: Stop the MySQL service
$ sudo /etc/init.d/mysql stop

step-1

  • Step 2: Run the following command. The ampersand (&) at the end of the command is required.
$ sudo mysqld_safe --skip-grant-tables &

step-2

  • Step 3: Step 3 ( Optional ) In some cases, you’ve to create the /var/run/mysqld first:
$ sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld

step-3

  • Step 4: Connect to MySQL
  • Run the following command:
$ mysql -uroot

step-4

  • Step 5: Set a new MySQL root password
  • Run the following command:
  • Change the database to mysql
mysql> use mysql;

step-5a

  • Replace “mynewpassword”
    • mysql> update user set authentication_string=PASSWORD(“mynewpassword”) where User=’root’;

step-5b

    • mysql> flush privileges;

step-5c

    • mysql> exit
  • Step 6: Stop and start the MySQL service
  • Stop the mysql service
$ sudo /etc/init.d/mysql stop

step-6a

  • Start the mysql service
$ sudo /etc/init.d/mysql start

step-6b

  • Step 7: Log in to the database
  • Test the new password by logging in to the database.
$ mysql -u root -p

step-7a

  • Enter your new password when prompted.

step-7b

AWS-Consulting-Partner

Talk to AWS Certified Consultant

    Want to start a project?

    It’s simple.

    Contact us