Install MySQL 8 on CentOS 7

 
 

How to install MySQL 8 on CentOS 7 is our next quick tutorial where we will learn in just a few easy steps how to install and test latest MySQL 8 available – as of today – from Oracle on a CentOS 7 server by using only our terminal window. These days most of modern companies are using Cloud managed services for their databases, some of these with fancy names, different functions, speeds and sizes but sometimes we may have no other choice than to use the classic MySQL. Depending on the project requirements we may have to perform these deployments being on-premises or using Cloud VMs, anyway this tutorial it really is a good starting point no matter where our MySQL 8 will end up. This tutorial will cover only the basic installation and tests without any advanced configuration for MySQL (my.cnf) or CentOS also, we will show here how to obtain and change the default MySQL root password.

Table of contents

Disable SELinux
Download MySQL 8 repo for CentOS 7
Install MySQL 8 and MySQL client for CentOS 7
Enable and Start MySQL 8 service
Check MySQL 8 Installation
Get MySQL 8 root password
Access MySQL 8 using default password provided
Change default MySQL 8 root password

Disable SELinux

On our first step we have to disable SELinux, we must mention that this particular step is not mandatory but to avoid some possible issues in terms of permissions we will first disable this. If your server has a public IP assigned and port 3306 accessible from the Internet then for security reasons please leave SELinux enabled or at least re-enable it once MySQL installation is completed. So lets edit our SELinux config file as shown below:

vi /etc/selinux/config
 

Replace the value SELINUX=enforcing with SELINUX=disabled, your final configuration file settings should look like in the example below:

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

Now let’s save this change and perform a reboot in order for SELinux changes to take effect.

Download MySQL 8 repo for CentOS 7

Having a fresh VM / server for our new MySQL 8 installation we can proceed to install MySQL 8 repository using yum utility like show here:

yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

If no errors were shown on our terminal window and repository installation went fine then we can move to our next step.

Install MySQL 8 and MySQL Client for CentOS 7

Knowing now that our MySQL 8 repository is now present on our server we can start installing MySQL Client and also MySQL Server using once again yum utility. So lets run the next command in order to install these packages and all its dependencies:

yum install -y mysql mysql-server

Enable and Start MySQL 8 service

If everything went fine with our installation then we should be able to test MySQL to see if this has been installed correctly by invoking mysql service status command as shown here:

systemctl status mysqld

A successful terminal output will look like this:

Unit mysql.service could not be found.
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 

The above output confirms the fact that our MySQL 8 installation was successful but also shows that our service is currently inactive (Active: inactive (dead)), meaning that MySQL service is not started yet, nothing to worry about as we will start this service quite soon.

Before starting the service let’s make sure that our service is enabled first to start automatically each time our server gets rebooted, please use the below command in order to enable MySQL service to start automatically:

systemctl enable mysqld

Finally we can now start our MySQL service by running the next CLI command onto our terminal window:

systemctl start mysqld
 

Once again lets do a check to see how MySQL service looks like after executing start command:

systemctl status mysqld
 

If our previous start command did not shown any errors on our terminal window then we should be able to see an output similar to this:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-05-14 10:59:07 UTC; 4s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 4727 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 4806 (mysqld)
   Status: "SERVER_OPERATING"
   CGroup: /system.slice/mysqld.service
           └─4806 /usr/sbin/mysqld

We can clearly notice now that the status of MySQL service changed from Active: inactive to Active: active (running)... meaning that everything is set up correctly, MySQL service is up and running and we can start using our freshly installed MySQL.

Check MySQL 8 Installation

We can actually double check if MySQL is running and can be used by simply checking its version using MySQL Client command like in the example below:

mysql --version
 

A successful output should look similar to this one listed below:

mysql  Ver 8.0.16 for Linux on x86_64 (MySQL Community Server - GPL)

Please note that your terminal output may look different, no worries, that is expected as Oracle does frequent releases for MySQL.

Get MySQL 8 root password

By default when installing MySQL a password for root user will be automatically issued so we can access all default databases. This auto-generate MySQL password can be found on the default MySQL log file called mysqld.log, this password can be used as we said to get into MySQL and we have to change this as we can’t perform any other SQL queries when using it.

Please use the next command in order to find the default MySQL 8 password:

cat /var/log/mysqld.log | grep pass
 

The above command should give us an output similar to this:

2019-05-14T10:59:04.657649Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ,g2gkqf/;gwZ

As you can see our root account password is ,g2gkqf/;gwZ, yours will be different as this is generated by MySQL for each new install.

Access MySQL 8 using default password provided

Knowing now the default password for our mysql root account lets try to authenticate using MySQL CLI client as shown in the next example:

mysql -u root -p

You’ll be prompted to type the password that we previously got from mysqld.log file, you may use copy and paste for this as well.

 

If the password provided was correctly typed then we should be able to see this:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Congratulation, this means that we’re done with our MySQL 8 Installation on CentOS 7, we were able to install it, start it and also log into MySQL using the default password.

Change default MySQL 8 root password

On our final step for this tutorial we will see how to change the default MySQL password. Without changing the default password provided by MySQL we won’t be able to perform any SQL queries.

Please run the following SQL query in order to change the default password for root account:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Fbs@WCh5bwrtmGaK';
mysql> FLUSH PRIVILEGES;

Make sure that you replace Fbs@WCh5bwrtmGaK with your own password.

 

Open now a new terminal window and try to log in to MySQL using your new root account password like this:

mysql -u root -p

Once prompted please use your new password, if the new password has been setup correctly then you should be able to see the default MySQL client output.

Video

No video posted for this page.

Screenshots

No screenshots posted for this page.

Source code

No code posted for this page.

About this page

Article
Install MySQL 8 on CentOS 7
Author
Category
Published
14/05/2019
Updated
19/07/2019
Tags

Share this page

If you found this page useful please share it with your friends or colleagues.