MySQL and MariaDB are the most popular open source database systems meant for Linux based operating systems.
In today's digital age, most database interactions are handled programmatically by some software or script that is granted access to the database through initial setup process.
Therefore, users sometimes do not remember the password of the database users. In the case where a password is needed, users can change the MySQL/MariaDB password of a specific database user to regain access to the database.
This process described below works for most Linux-based server operating systems, including Ubuntu, Debian, CentOS and more.
Note: The MySQL default root password is empty, meaning there is no password assigned upon fresh installation of MySQL or MariaDB. Simply press "enter/return" after entering the username to proceed.
How to Change MySQL User Password
MySQL command to reset root user password:
- Login to server via SSH as root user
- Enter the following command:
sudo mysql -u root
- Once connected with the MySQL server, select the MySQL database:
use mysql;
- Use the below command to change the MySQL root user password:
update user set password=PASSWORD("mynewpassword") where User='root'; flush privileges; quit
- Re-enter the mysql server:
use mysql
- Restart the mysql service:
systemctl restart mysql
Mysql Command to Change a User Password
You can find out all MySQL database users just by using command mentioned below. From here you can select the user of which password needs to be reset.
- Enter the following command:
SELECT User FROM mysql.user;
- Open the command terminal and connect to the MySQL user and enter the MySQL root user password to log in:
sudo mysql -u root -p
- Switch to MySQL database:
use mysql;
- Use the following command to change the USER password in MySQL database server version 5.7.5 or older versions:
SET PASSWORD FOR 'user-name-here'@'localhost' = PASSWORD('new-password');
- To change the MySQL user password in MySQL database server version 5.7.6 or above, use the following command:
ALTER USER 'user-name-here'@'localhost' IDENTIFIED BY 'newPassword';
For shared hosting or reseller hosting clients, feel free to open a support ticket for managed support.