How to Install MySQL or MariaDB on CentOS 7

How to Install MySQL or MariaDB on CentOS 7

As of CentOS 7 the default database is MariaDB and a drop in replacement for MySQL and should be fine for most people but if you need MySQL, there are some features in MySQL and not in MariaDB and vice versa.

So with that said following these steps should get you MySQL installed in no time! If you are OK with MariaDB then you can follow steps 2 - 3 only and this will install MariaDB.

Step 1 - Install MySQL Repository:

First we have to install the community repository with

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

to download the repository then

rpm -ivh mysql-community-release-el7-5.noarch.rpm

Step 2 - Install MySQL:

to install it. Now we have to update yum with

yum update

Once this is done we can install mysql client and server with

yum install mysql-server mysql

and now start MySQL with

systemctl start mysqld

Step 3 - Secure MySQL:

Now we have MySQL installed and started we have to secure it by setting the root password, removing anonymous user accounts, disable the root login outside of localhost and remove the test databases. we do this with

mysql_secure_installation

and you will be presented with this

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

If you have had MySQL installed before then enter the root password here but if this is a clean install just press enter the next question will be

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y

We want to set a password for root so answer 'y' and add your password. After you have set your password you will be asked

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y

We want to answer yes as we don't want anonymous users next question will be

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y

We again want to answer 'y' to this as we don't want root to be accessed from the outside world! Next we have

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y

We answer 'y' again as we do not want the test databases left installed, we have no need of them and finally

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y

we will be asked to reload the privilege tables which we do want to so 'y' again.

With that done we can test our MySQL by logging into the mySQL shell with

mysql -u root -p

enter your password and you should see

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

Now we have MySQL 5.6.35 installed. Enter 'q' and press enter to quit the MySQL shell.

Step 4 - Updating to MySQL 5.7.x

As of writing this MySQL 5.7.x was released but this repo has has that turned off so the update MySQL to 5.7.x first we enable it by

nano /etc/yum.repos.d/mysql-community.repo

the entry we are looking is

[mysql57-community-dmr]
name=MySQL 5.7 Community Server Development Milestone Release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

Change the 'enabled=0' to 'enabled=1' like

[mysql57-community-dmr]
name=MySQL 5.7 Community Server Development Milestone Release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

Save and exit the editor and the update yum with

yum update

Yum should now update MySQL to version 5.7.x totes this enter the MySQL shell again with

mysql -u root -p

and you should see some like this

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

And that's it we have now installed MySQL 5.7.x

Watch on YouTube: Installing MySQL on CentOS 7


15/11/2016 19:11:00 Shaun Freeman Filed Under: MySQL CentOS, Linux, MySQL, Server

Twitter Feed
Shaun Freeman @Zendmaster

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/RnEYvUDFx2 The Josephus Problem - Numberphile

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/TEGV4DhLGI Blown Like the Wind

Shaun Freeman @Zendmaster

I liked a @YouTube video https://t.co/lSFWmpHTX1 Patrick Stewart talks about meeting Sting on the set of DUNE (Funny to the EXTREME)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/pmXSmod4ti Anonymous - This will Change Everything You Know... (2018-2019)

Shaun Freeman @Zendmaster

I added a video to a @YouTube playlist https://t.co/GkwTCvBfes Will Artificial Intelligence Take Over The World?