MySQL is a popular open-source relational database management system used by many web applications. When setting up MySQL on Ubuntu, the root user is granted full privileges and is often used for administrative tasks. It is essential to secure your MySQL installation by modifying the default root password.
In this guide, we will walk you through the steps to modify the root password for MySQL 8.X in Ubuntu. Please note that this guide specifically applies to MySQL 8.X versions.
Step 1: Stop the MySQL Service
Before modifying the root password, we need to stop the MySQL service. Open a terminal on your Ubuntu system and run the following command:
sudo systemctl stop mysql.service
This command will stop the MySQL service, ensuring that no active connections interfere with the password modification process.
Step 2: Set MySQL Options
Next, we will set the MySQL options to skip grant tables and networking. Run the following command to set the MySQL options:
sudo systemctl set-environment MYSQLD_OPTS="--skip-networking --skip-grant-tables"
This command sets the environment variable
MYSQLD_OPTS to include the options
--skip-grant-tables, which will skip the grant tables and networking when starting the MySQL service.
Step 3: Start the MySQL Service
Now, we can start the MySQL service with the modified options. Run the following command to start the MySQL service:
sudo systemctl start mysql.service
This command will start the MySQL service with the specified options, allowing us to modify the root password.
Step 4: Connect to the MySQL Server
Once the MySQL service has started with the skip grant tables and networking options, we can connect to the MySQL server without a password. Open a new terminal window and run the following command to access the MySQL shell:
sudo mysql -u root
Since we started the MySQL service with the skip grant tables and networking options, we can now connect to the server without a password.
Step 5: Modify the Root Password
Once you are logged in to the MySQL shell, execute the following commands in sequence:
Flush the privileges first.
This command switches the current database context to the MySQL system database.
'new_password' with your desired password. Ensure that your password meets the MySQL password requirements. It is recommended to use a strong and unique password for security purposes.
ALTER USER 'root'@'localhost' IDENTIFIED BY '(new_password)';
Step 6: Applying the Changes
After running the password change command, apply the changes by executing the following command in the MySQL shell:
This command reloads the privilege tables and ensures that the changes you made are immediately applied.
Step 7: Exit and Restart MySQL
Exit the MySQL shell by typing:
Then, stop the MySQL service by running the following command:
sudo systemctl stop mysql
Finally, start the MySQL service to apply the password changes:
sudo systemctl start mysql
Congratulations! You have successfully modified the root password for MySQL 8.X in Ubuntu while skipping the grant tables and networking steps. Remember to use the new password when accessing the MySQL server for future administrative tasks.
Note: Skipping the grant tables and networking steps should be done with caution, as it temporarily disables security measures. Ensure that you follow best practices for securing your MySQL installation once the password modification is complete.