(Last Updated On: May 15, 2018)

This guide is to help you with Installing MySQL Server on CentOS 7 / CentOS 6. I’ll show you how to install MySQL 5.5/5.6/5.7 and the latest stable release of MySQL, which as of this writing is MySQL 8.0

To start installing MySQL server on CentOS 7/6, you need to add the official MySQL community repository to your system. Run below commands to add it,

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

Install MySQL 8 on CentOS

Now that repo is added, you can install MysQL 8 without editing repository content since repo for 8 is enabled by default.

# yum --enablerepo=mysql80-community install mysql-community-server

Install MySQL 5.7 on CentOS

To install MySQL 5.7, you need to disable mysql80-community repository then download it.

# yum --disablerepo=mysql80-community --enablerepo=mysql57-community \
install mysql-community-server

Install MySQL 5.6 on CentOS

To install MySQL 5.6, you need to disable mysql80-community repository then download it.

# yum --disablerepo=mysql80-community --enablerepo=mysql56-community \
install mysql-community-server

Start MySQL Service

For CentOS 7, use systemd to start mysql service:

# systemctl start mysqld.service
# systemctl enable mysqld.service

For CentOS 6, use service command line tool.

# /etc/init.d/mysql start 
or 
# chkconfig --levels 235 mysqld on

Set MySQL root password

Installation of MySQL on CentOS 6 generates a temporary password for you. You can get it by running:

# grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log |tail -1
2018-05-10T07:37:34.048034Z 1 [Note] A temporary password is generated for root@localhost: ?h(UwcrvQ7jr

It will look like below:

# root@localhost: ?h(UwcrvQ7jr

Change mysql root user password

# mysqladmin -u root password [your_password_here]
or
# mysql_secure_installation

Configure Firewall

With iptables:

# iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# service iptables restartt

Firewalld:

# firewall-cmd --add-service mysql --permanent
# firewall-cmd --reload

Test your settings:

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> show databases;