Updated May 12, 2023
Introduction to MySQL Root
The MySQL database assigns different privileges to multiple users. But, when you want to perform database operations that require many higher privileges, you can use the account created by default in MySQL, which has almost all the privileges assigned to it that is the root user. When installing MySQL on a machine, the installation process creates a user named root by default. The installation prompts the user to set a password for the root user. If this does not happen and you want to set the password for the default root user, you can do so later.
Further, there might be a situation where you have forgotten your root password and wish to set a new password, or sometimes you will want to set the root user’s password as stronger than the previous one. Creating robust passwords for your root user is crucial to ensure security and safeguard the sensitive data stored in your MySQL database. In this article, we will learn how to change or set the password to the root user.
Login using root user
We can use the following command to enter the MySQL database using the root user –
sudo mysql -u root -p
that will further prompt for the password set by you while installation of MySQL on your machine and gives the following output –
Now, you will see the MySQL shell and work on your database. Let us know the list of users in my MySQL database server. The MySQL database automatically generates the user table and stores the users’ data in it. Let us first check all the databases using the following command –
Show Databases;
that gives the following output –
Now, we will specify that we have to use the MySQL database using the following command –
use MySQL;
that gives the following output after execution –
Now, we will fire the select query on the user table in the MySQL database using the following query statement –
select user, host from a user;
The execution of the above query gives the following output –
We can see that the user root is present in the database with the local host as the host. I did not create the root user, unlike the newUserForRep user. The database already had it upon installation. Many properties and informatical fields are stored n the user table for all the user-related information. You can describe the user table and see the list of the columns stored in the user table using the following command –
desc user;
that gives the following output –
After selecting these columns for the root user, you will observe that the root user has almost all the privileges assigned to it and have a ‘Y’ value stored in the privilege column that stands for yes.
Alternatively, one more command can be used to login as the root user into a MySQL database that is as follows –
sudo -i mysql
that gives the following output –
At this point, you don’t need to provide your username or password. The configuration file called my.cnf or mysqld.cnf, which is stored in the directory /etc/MySQL/mysql.conf.d/mysqld.cnf already contains this information.
Setting up the password for the root user
Now, let us see how to set the root user’s password or change the existing password. There are multiple ways in which we can change the root user’s password in case you have forgotten the current password or wish to set up a stronger password. They are listed below –
We will see all the methods one by one in detail.
1. Using mysqladmin command
By default, a password is asked during the installation of MySQL for the root user. But suppose that for some reason, it was not requested, and now you wish to set the password, then you can make use of the mysqladmin command in the following way to assign the new password to the root user –
sudo mysqladmin -u root password myPassWhichIWant
that gives the following output-
2. Using mysql_secure_connection command
We can use the alternative mysql_secure_connection command instead of mysqladmin. That is way more secure and advanced as it not only allows you to set the password for the root user but also gives the facility to add and set many certain features to your MySQL database, such as restricting the login of the root user from remote machines, anonymous users can be removed, and the test database can also be removed. This command can be used in the following way by simply typing
mysql_secure_connection
that asks you multiple questions whose answers will set all the properties you wish to assign.
3. Using ALTER command to change the password of the user
You can change the password of any user using the ALTER command. We can also use this command to set up the password of the root user in the following way:
ALTER USER 'root'@'localhost' IDENTIFIED BY 'a';
a is the new password that we wish to assign. Executing the above command gives the following output –
Save that file for further reference as ~/MySQL-password.
The next step lies in stopping the MySQL service by using the following command –
sudo systemctl stop MySQL
For this, you will have to exit the MySQL command prompt by clicking ctrl+z and then fire the command that gives the following output –
Now, fire the command for initializing the process of setting the password
sudo mysqld -init-file=~/mysql-password
Then you will again have to start your MySQL service using the following command –
sudo systemctl start MySQL
That results in the following output.
Further, you can use MySQL -u root -p command to log in with the changed password.
Conclusion – MySQL Root
During the installation of MySQL, it creates the root user by default. It has many privileges assigned to it. When installing, the system will prompt you to create a password. You can utilize any of the methods mentioned above if you require changing it at a later time.
Recommended Articles
We hope that this EDUCBA information on “MySQL Root” benefited you. You can view EDUCBA’s recommended articles for more information.