EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Root
 

MySQL Root

Payal Udhani
Article byPayal Udhani
EDUCBA
Reviewed byRavi Rathore

Updated May 12, 2023

MySQL Root

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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 –

MySQL Root 1

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 –

MySQL Root 2

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 –

MySQL Root 3

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 –

MySQL Root 4

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 –

desc user

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 –

Sudo-mySQL

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-

admin command

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 –

Using ALTER command

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 –

Stopped

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.

Output 10

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.

  1. MySQL IN Operator
  2. Working on MySQL Subquery
  3. MySQL Constraints
  4. MySQL MAX() Function

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW