MySQL is one of the most used database for web based application. It’s the M in LAMP stack.
Ubuntu 16.04 Xenial Xerus shipped with MySQL 5.7 so we can easily install MySQL 5.7 without adding additional repository.
In this tutorial we’ll learn how to install MySQL 5.7 on Ubuntu 16.04 LTS (Xenial Xerus).
This tutorial assume that we will install MySQL 5.7 on a fresh Ubuntu 16.04 installation or on a system that have no MySQL server or similar database like MariaDB or percona server for MySQL installed.
Install MySQL 5.7
Update system apt-get metadata using command below:
$ sudo apt-get update
Install MySQL 5.7 using command below
$ sudo apt-get install mysql-server
Y to continue the installation process.
The installation process will take some times to finish since it needs to download mysql-server and all dependencies from internet.
The installer will ask for MySQL
root user password. Please not that MySQL root and Linux system root is different.
root user password.
After the installation is finished you can check the service status using command below.
$ sudo systemctl status mysql
The output should look similar to output below. The output below shows that MySQL is running.
● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2017-07-17 19:33:34 UTC; 1min 27s ago Main PID: 14443 (mysqld) CGroup: /system.slice/mysql.service └─14443 /usr/sbin/mysqld Jul 17 19:33:33 ubuntu-xenial systemd: Starting MySQL Community Server... Jul 17 19:33:34 ubuntu-xenial systemd: Started MySQL Community Server.
To stop MySQL Server you can use command below.
$ sudo systemctl stop mysql
When we re-check the status it will show that the server is stopped.
$ sudo systemctl status mysql ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: inactive (dead) since Mon 2017-07-17 19:35:56 UTC; 14s ago Process: 14607 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS) Process: 14606 ExecStart=/usr/sbin/mysqld (code=exited, status=0/SUCCESS) Process: 14597 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 14606 (code=exited, status=0/SUCCESS) Jul 17 19:35:47 ubuntu-xenial systemd: Starting MySQL Community Server... Jul 17 19:35:48 ubuntu-xenial systemd: Started MySQL Community Server. Jul 17 19:35:55 ubuntu-xenial systemd: Stopping MySQL Community Server... Jul 17 19:35:56 ubuntu-xenial systemd: Stopped MySQL Community Server.
To start the server we can use command below
$ sudo systemctl start mysql
We can also restart mysql using command below
$ sudo systemctl restart mysql
Login to MySQL Server 5.7
Now let’s login to MySQL Server that we just installed.
$ mysql -u root -p Enter password:
root user password that we set on installation process.
After successful login we will get to MySQL console.
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 6 Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2017, 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>
CTRL+D to exit from MySQL console.
Securing MySQL Server 5.7
We can secure MySQL server 5.7 with tools shipped with MySQL called
mysql_secure_installation. We don’t need to do
sudo to run this application. Let’s start securing our MySQL installation.
$ mysql_secure_installation Securing the MySQL server deployment. Enter password for user root:
Enter MySQL’s root password.
First step is to enable Validate password plugin. We will enable this plugin by entering
VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: Y
We will use the highest level of password validation policy, the STRONG one. Enter 2 to use STRONG password validation policy,
There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
The script will check for current root password strength.
My current root password strenght is 50, I choose to change the root password that I use.
Using existing password for root. Estimated strength of the password: 50 Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y
Input new password and confirm new password.
New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
Remove anonymous user by choosing
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? (Press y|Y for Yes, any other key for No) : Y Success.
Ensure root can only login locally from the database machine, this will block root login remotely.
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? (Press y|Y for Yes, any other key for No) : Y Success.
Remove default test database
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? (Press y|Y for Yes, any other key for No) : Y - Dropping test database... Success. - Removing privileges on test database... Success.
Reload all privilege tables.
Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y Success. All done!
We finished securing our MySQL database installation.
Input Sample Database
MySQL provides several <a href=”https://dev.mysql.com/doc/index-other.html”>sample database</a>. We will use Sakila sample database to be imported to our mysql database installation.
Download the database dump
$ wget -c http://downloads.mysql.com/docs/sakila-db.tar.gz
Extract downloaded archive
$ tar xzf sakila-db.tar.gz
Go to extracted directory
$ cd sakila-db
Input sakila schema. This will create database and tables.
> mysql -u root -p < sakila-schema.sql
Input sakila data
> mysql -u root -p < sakila-data.sql
Now login to MySQL database
$ mysql -u root -p
After successfully login lets see list of databases.
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sakila | | sys | +--------------------+ 5 rows in set (0.01 sec)
Now let’s use Sakila database
mysql> use sakila
Show all tables inside
mysql> show tables; +----------------------------+ | Tables_in_sakila | +----------------------------+ | actor | | actor_info | | address | | category | | city | | country | | customer | | customer_list | | film | | film_actor | | film_category | | film_list | | film_text | | inventory | | language | | nicer_but_slower_film_list | | payment | | rental | | sales_by_film_category | | sales_by_store | | staff | | staff_list | | store | +----------------------------+ 23 rows in set (0.00 sec)
In this tutorial we learned how to install MySQL 5.7 on Ubuntu 16.04. We learned securing MySQL installation using
mysql_secure_installation script provided by MySQL.
We also learned importing Sakila sample database and do several queries to the database. Have fun with MySQL!