Installing MySQL CentOS 7


MySQL is an open-source database management system, commonly installed as part of the popular LEMP(Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl) stack. It uses a relational database and SQL (Structured Query Language) to manage its data.

CentOS 7 prefers MariaDB, a fork of MySQL managed by the original MySQL developers and designed as a replacement for MySQL. If you run yum install mysql on CentOS 7, it is MariaDB that is installed rather than MySQL. If you’re wondering about MySQL vs. MariaDB, MariaDB will generally work seamlessly in place of MySQL, so unless you have a specific use-case for MySQL, see the How To Install MariaDB on Centos 7 guide.

This tutorial will explain how to install MySQL version 5.7 on a CentOS 7 server.


To follow this tutorial, you will need:

  • A CentOS 7 with a non-root user with sudo privileges. You can learn more about how to set up a user with these privileges in the Initial Server Setup with CentOS 7 guide.

Step 1 — Installing MySQL

As mentioned in the introduction, the Yum command to install MySQL in fact installs MariaDB. To install MySQL, we’ll need to visit the MySQL community Yum Repository which provides packages for MySQL.

In a web browser, visit:

Note that the prominent Download links don’t lead directly to the files. Instead, they lead to a subsequent page where you’re invited to log in or sign up for an account. If you don’t want to create an account, you can locate the text “No thanks, just start my download”, then right-click and copy the link location, or you can edit the version number in the commands below.

Locate the desired version, and update it as needed in the link below:



Once the rpm file is saved, we will verify the integrity of the download by running md5sum and comparing it with the corresponding MD5 value listed on the site:

md5sum mysql57-community-release-el7-9.noarch.rpm

1a29601dc380ef2c7bc25e2a0e25d31e  mysql57-community-release-el7-9.noarch.rpm


Compare this output with the appropriate MD5 value on the site:


Now that we’ve verified that the file wasn’t corrupted or changed, we’ll install the package:


sudo rpm -ivh mysql57-community-release-el7-9.noarch.rpm

This adds two new MySQL yum repositories, and we can now use them to install MySQL server:

sudo yum install mysql-server

Press y to confirm that you want to proceed. Since we’ve just added the package, we’ll also be prompted to accept its GPG key. Press y to download it and complete the install.

Step 2 — Starting MySQL

We’ll start the daemon with the following command:

sudo systemctl start mysqld

systemctl doesn’t display the outcome of all service management commands, so to be sure we succeeded, we’ll use the following command:

sudo systemctl status mysqld

If MySQL has successfully started, the output should contain Active: active (running) and the final line should look something like:

Dec 01 19:02:20 centos-512mb-sfo2-02 systemd[1]: Started MySQL Server.
Note: MySQL is automatically enabled to start at boot when it is installed. You can change that default behavior with 
sudo systemctl disable mysqld 


During the installation process, a temporary password is generated for the MySQL root user. Locate it in the mysqld.log with this command:

sudo grep 'temporary password' /var/log/mysqld.log
2016-12-01T00:22:31.416107Z 1 [Note] A temporary password is generated for root@localhost: mqRfBU_3Xk>r

Make note of the password, which you will need in the next step to secure the installation and where you will be forced to change it. The default password policy requires 12 characters, with at least one uppercase letter, one lowercase letter, one number and one special character.

Step 3 — Configuring MySQL

MySQL includes a security script to change some of the less secure default options for things like remote root logins and sample users.

Use this command to run the security script.

sudo mysql_secure_installation

This will prompt you for the default root password. As soon as you enter it, you will be required to change it.

The existing password for the user account root has expired. Please set a new password.

New password:

Enter a new 12-character password that contains at least one uppercase letter, one lowercase letter, one number and one special character. Re-enter it when prompted.

You’ll receive feedback on the strength of your new password, and then you’ll be immediately prompted to change it again. Since you just did, you can confidently say No:



Estimated strength of the password: 100
Change the password for root ? (Press y|Y for Yes, any other key for No) :


After we decline the prompt to change the password again, we’ll press Y and then ENTER to all the subsequent questions in order to remove anonymous users, disallow remote root login, remove the test database and access to it, and reload the privilege tables.

Now that we’ve secured the installation, let’s test it.

Step 4 — Testing MySQL

We can verify our installation and get information about it by connecting with the mysqladmin tool, a client that lets you run administrative commands. Use the following command to connect to MySQL as root(-u root), prompt for a password (-p), and return the version.

mysqladmin -u root -p version

You should see output similar to this:


mysqladmin  Ver 8.42 Distrib 5.7.16, for Linux on x86_64
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

Server version          5.7.16
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 2 min 17 sec

Threads: 1  Questions: 6  Slow queries: 0  Opens: 107  Flush tables: 1  Open tables: 100  Queries per second avg: 0.043


This indicates your installation has been successful.


Install Apache, MariaDB and PHP7 (LAMP Stack) on Ubuntu 16.04 LTS

Ubuntu 16.04 LTS Xenial Xerus comes with PHP7 by default so you don’t have to rely on third-party PPA to get PHP7 installed. In this tutorial, we are going to look at how to install Apache, MariaDB and PHP7 (LAMP stack) on Ubuntu 16.04 LTS Xenial Xerus.

Update: This tutorial is also successfully tested on Ubuntu 16.10 Yakkety Yak.

Step 1: Update Ubuntu 16.04 LTS

Before we install any software, it’s always a good idea to update repository and software packages. SSH into your Ubuntu 16.04 server and enter the below commands.

sudo apt-get update

sudo apt-get upgrade

sudo apt-get dist-upgrade

Step 2: Install Apache Web Server

Enter this command to install Apache Web server.

sudo apt-get install apache2 apache2-utils


After it’s installed, Apache should be automatically started. Check out its status with systemctl.


systemctl status apache2


● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
   Active: active (running) since Wed 2016-04-20 18:32:57 EDT; 32s ag

If it’s not running, use systemctl to start it.

sudo systemctl start apache2

It’s also a good idea to enable Apache to automatically start when Ubuntu 16.04 is rebooted.

sudo systemctl enable apache2

Check Apache version:

apache2 -v


Server version: Apache/2.4.18 (Ubuntu)
Server built: 2016-04-15T18:00:57

Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “It works!” Web page which means Apache Web server is running correctly.



You can use the following command to fetch the public IP address of Ubuntu 16.04 server.

sudo apt-get install curl


If you are installing LAMP on your local Ubuntu 16.04 box, just type or localhost in the browser address bar.

Finally, we need to make www-data (Apache user) as the owner of web root directory.

sudo chown www-data /var/www/html/ -R

Step 3: Install MariaDB . or just >

sudo apt install mysql-server php7.0-mysql

MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who concerned that Oracle might turn MySQL into a closed-source product. Many Linux distributions and companies have migrated to MariaDB. So we’re going to install MariaDB instead of MySQL.

sudo apt-get install mariadb-server mariadb-client

After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mysql


● mysql.service - LSB: Start and stop the mysql database server daemon
 Loaded: loaded (/etc/init.d/mysql; bad; vendor preset: enabled)
 Active: active (running) since Wed 2016-04-20 18:52:01 EDT; 1min 30s ago
 Docs: man:systemd-sysv-generator(8)




If it’s not running, start it with this command:

sudo systemctl start mysql

To enable MariaDB to automatically start when Ubuntu 16.04 is rebooted:

sudo systemctl enable mysql

Now run the post installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press enter because you have not set the root password yet. Then enter y to set the root password for MariaDB server.





Next you can just press Enter to answer all the remaining questions. This will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.



Step 4: Install PHP7

Enter the following command to install PHP7 and PHP7 extensions.

sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl libapache2-mod-php7.0

Enable the Apache php7.0 module then restart Apache Web server.

sudo a2enmod php7.0

sudo systemctl restart apache2

Step 5: Test PHP

To test the cli version of PHP7, we just need to enter this command:

user@www:~$ php --version
PHP 7.0.4-7ubuntu2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
  with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

To test PHP with Apache server, first create a test.php file in the Web root directory.

sudo nano /var/www/html/test.php

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file. Now in the browser address bar, enter server-ip-address/test.php. Replace sever-ip-address with your actual IP. Of course, if you follow this tutorial on your local computer, then type localhost/test.php.

You should see your server’s PHP information. This means PHP processing is fine. You can find that Zend OPcache is enabled.



Apache PHP7.0 Module vs PHP-FPM

There are now basically two ways to run PHP code with Apache web server:

  • Apache PHP module
  • PHP-FPM.

The above configuration uses the Apache PHP7.0 module to handle PHP code. It’s totally fine and if you are happy with it, then delete test.php file now so that no one else can see your server’s information and don’t follow the instructions below.

But if you want to use PHP-FPM to run PHP code, then you need to enable Apache mod_proxy_fcgi module with the following command:

sudo a2enmod proxy_fcgi

Then edit the virtual host configuration file. This tutorial uses the default virtual host as an example.

sudo nano /etc/apache2/sites-available/000-default.conf

Add the ProxyPassMatch directive to this file.


ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php7.0-fpm.sock|fcgi://localhost/var/www/html/


Save and close this file. Restart Apache2.

sudo systemctl restart apache2

Start php7.0-fpm

sudo systemctl start php7.0-fpm

Enable php7.0-fpm to start at boot time.

sudo systemctl enable php7.0-fpm

Check status:

systemctl status php7.0-fpm


● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
 Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor pre
set: enabled)
 Active: active (running) since Wed 2016-04-20 19:21:05 EDT; 2s ago

Now if you refresh the test.php page in your browser, you will find that Server API is FPM/FastCGI which means Apache web server will pass PHP requests to PHP-FPM.


For your server’s security, you should delete test.php file now.

Congrats! You have successfully installed Apache, MariaDB and PHP7 on Ubuntu 16.04 LTS Xenial Xerus.

Rate this tutorial

Continue reading “Install Apache, MariaDB and PHP7 (LAMP Stack) on Ubuntu 16.04 LTS”