How to Reset MySQL Server Password on Linux: Easy Steps

MySQL is a popular database management system 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 security practice, as anyone can access your database.

If your database has a root password, but you lost track of it, this guide will help you reset a MySQL root password on Linux and Windows.

Follow the steps outlined below to change your MySQL root password in Linux. For this tutorial, we will use Ubuntu 22.

STEP 1: Stop the MySQL Server

  • Before changing the password, stop the MySQL server if it is active. Run the following command to stop the mysqld process:
command : sudo /etc/init.d/mysql stop

STEP 2 : Create Directory and give proper permissions.

  • Create a new directory named mysqld inside the /var/run directory.
  • Change the ownership of the MySQL runtime directory to the mysql user, typically to ensure that the MySQL server process has the necessary permissions to function properly
commands:  sudo mkdir /var/run/mysqld    

           sudo chown mysql /var/run/mysqld

STEP 3: Start MySQL in safe mode.

  • Starts the MySQL server in safe mode without loading the user privilege information, and it runs in the background with superuser privileges. This is typically used for emergency access or recovery purposes, such as resetting the MySQL root password. However, it’s important to be cautious when using --skip-grant-tables because it effectively removes all access controls from the MySQL server, making it vulnerable to unauthorized access.
command: sudo mysqld_safe --skip-grant-tables&

STEP 4: Open new terminal and entered in MySQL as root user.

  • Start the MySQL command-line client with elevated privileges (via sudo) and connects to the MySQL server as the root user, accessing the mysql database. This allows the user to interact with MySQL using administrative privileges, potentially performing tasks such as managing databases, users, and privileges within the MySQL system.
command: sudo mysql --user=root mysql

STEP 5: Execute SQL query to update password.

command: update mysql.user set authentication_string=null where User='root';
         flush privileges;
         alter user 'root'@'localhost' identified with mysql_native_password by 'Root@1234';
         flush privileges;
  • if host-name is localhost use localhost and if host name is % use %.

STEP 6: END up the first terminal process using ctrl + c.

STEP 7: Stop and clean Up all the MySQL processes.

command: sudo /etc/init.d/mysql stop
         sudo service mysql stop
         sudo killall -KILL mysql mysqld_safe mysqld

STEP 8: Start MySQL server again and Login to MySQL using new password.

command: sudo /etc/init.d/mysql start
         sudo service mysql start

THANK YOU

Leave a Reply

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