Losing or forgetting the MySQL root password can be a haunting situation. In this article, we walk you through the steps to reset MySQL root password on an Ubuntu system.
Prerequisites
- Access to the Ubuntu system where MySQL is installed.
- Basic knowledge of the terminal and MySQL.
Steps to Reset MySQL Root user Password
1. Stop the MySQL Server
Stop the MySQL server to prepare for the password reset:
sudo systemctl stop mysql
2. Start MySQL in Safe Mode
Start MySQL in safe mode, which allows us to reset the root password without authentication:
sudo mysqld_safe --skip-grant-tables --skip-networking &
Incase you get an error such as the one below:
sudo mysqld_safe --skip-grant-tables --skip-networking &
[1] 3213
ubuntu@ip-172-31-17-141:~$ 2024-04-26T10:37:41.749653Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2024-04-26T10:37:41.752947Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
Proceed and create the missing directory using the command below, then rerun the previous command:
sudo mkdir -p /var/run/mysqld
3. Access MySQL without a Password
Open a new terminal window and connect to MySQL as the root user without a password:
mysql -u root
USE mysql;
FLUSH PRIVILEGES;
4. Set a New Root Password
Run the following SQL commands to set a new password for the root user:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass';
5. Exit MySQL and Restart the MySQL Server
Exit from MYSQL
EXIT;
Then stop the running mysqld_safe
process:
sudo pkill mysqld
Start the MySQL server normally:
sudo systemctl start mysql
6. Verify the New Password
Having set a new root password, login in with the new password.
mysql -u root -p #Enter new password on prompt
Conclusion
You have successfully reset the root password. If MySQL was installed without a prompt to enter a root password, use this method to set a new password.
Make a donation to support us
Web Hosting and email hosting Packages
Related content:
- A Practical Tutorial for Dockerizing Software Applications
- How to Configure a Docker App to a Domain Name
- Getting Started with Docker | Docker commands
- How To Run Scripts in Linux
- Deploy a Django Application on EC2 Instance with Nginx
- How to configure a domain to a docker container and install an SSL certificate on AWS