Set MySQL Server "root" Password on CentOS

This section provides a tutorial example on how to set or reset 'root' password, in case it has no password or you can not remember the password, for MySQL 8.0 server on CentOS 8 systems.

When MySQL database server is installed on CentOS, it will create a "root" user name to access the database contents. This "root" user has all types permissions to access all database objects. But it has no password by default.

So before adding any data to your MySQL database, you should set a password for this "root" user to protect your data. Here what I did on my MySQL 8 server on my CentOS 8 computer.

1. Connect to MySQL 8 server with the "root" user without any password:

herong$ mysql -u root -p
Enter password: <Enter>

...
mysql>

2. Checking the current password of "root". I see that the "root" use has not password:

mysql> select user, host, plugin, authentication_string from mysql.user;
+------------------+-----------+-----------------------+------------------------
| user             | host      | plugin                | authentication_string
+------------------+-----------+-----------------------+------------------------
| mysql.infoschema | localhost | caching_sha2_password | $A$005$THIS...
| mysql.session    | localhost | caching_sha2_password | $A$005$THIS...
| mysql.sys        | localhost | caching_sha2_password | $A$005$THIS...
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+------------------------

3. Set a new password for the current login user, "root".

mysql> set password = 'TopSecret';

mysql> select user, host, plugin, authentication_string from mysql.user;

+------------------+-----------+-----------------------+------------------------
| user             | host      | plugin                | authentication_string
+------------------+-----------+-----------------------+------------------------
| mysql.infoschema | localhost | caching_sha2_password | $A$005$THIS...
| mysql.session    | localhost | caching_sha2_password | $A$005$THIS...
| mysql.sys        | localhost | caching_sha2_password | $A$005$THIS...
| root             | localhost | mysql_native_password | *8E4FBDE7FB...
+------------------+-----------+-----------------------+------------------------

4. Disconnect and connect agin with the new password:

mysql> exit;
Bye

herong$ mysql -u root -p
Enter password: TopSecret<Enter>
...
mysql>

Cool. I have added a password to the "root" user on my MySQL 8 server.

Note that there are other ways to set or reset the "root" password:

If you lost the "root" password, there is only one way to recover it by running MySQL server without password protection:

herong$ sudo service mysqld stop

herong$ sudo mysqld --skip-grant-tables &

herong$ mysql -u root
mysql> set password = 'TopSecret';
mysql> quit;

herong$ killall mysqld

herong$ sudo service mysqld start

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

 Bulk Changes on Large Tables

 MySQL Server on macOS

Installing MySQL Server on Linux

 Install MySQL Database Server on CentOS

 Manage MySQL Server 'mysqld' on CentOS

Set MySQL Server "root" Password on CentOS

 MySQL Server File Locations on CentOS

 MySQL Server Data Backups on CentOS

 MySQL Server Log Files on CentOS

 "Multiple files found for the same tablespace ID" Error

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB