How to install mysql server 5.7 on Ubuntu 16.04 LTS ( Xenial Xerus )

MySQL is a popular database management system used for my web applications. How do I install MySQL server version 5.7 on Ubuntu Linux 16.04 LTS ( Xenial Xerus )?

This tutorial will introduce how to install, configure and manage MySQL on a Ubuntu Linux 16.04 LTS (Xenial Xerus). The latest version of MySQL database server is 5.7 and can be installed using the apt-get command or apt command:

  1. mysql-server – Metapackage depending on the latest version (server)
  2. mysql-client – Metapackage depending on the latest version (client)

Step 1: Update your system by typing the following commands:

$ sudo apt update
$ sudo apt upgrade

Fig.01: Before you begin, update your Ubuntu server

Step 2: Install mysql version 5.7 on Ubuntu 16.04

Type the following command:
$ sudo apt install mysql-server mysql-client
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libterm-readkey-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common
  mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl
  libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
  libdbd-mysql-perl libdbi-perl libhtml-template-perl libmysqlclient18
  libterm-readkey-perl mysql-client mysql-client-5.7 mysql-client-core-5.7
  mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 21.7 MB of archives.
After this operation, 155 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

You need to type the password for the MySQL root user:

Fig.02: During the installation process set the password for the MySQL root account

Step 3: Run mysql_secure_installation to secure your installation

For all production server you must run the following command:
$ sudo mysql_secure_installation
Sample outputs:

Fig,03: Harden MySQL server on Ubuntu

The mysql_secure_installation command will change the root password, disable and remove anonymous users from MySQL server, turn off the MySQL root user login remotely, delete test database, and reload privilege tables on the system. If you’ve completed all of the steps and answer yes to these questions. Your MySQL installation should now be secure.

Step 4: How do I use MySQL server?

To log in as the root user, type:
$ mysql -u root -p
When prompted, enter the MySQL root password, and you should see mysql> prompt as follows:

Fig.04: Your first login

Step 5: How do I create a new MySQL server database and user account?

The sql syntax is as follows to create a new mysql database and setup password:


For example, create a wpblogdb as the database, vivek as the user, and fooBarPwd8-4_2 as the user’s password, run (type at the mysql> prompt:)
mysql> create database wpblogdb;
mysql> grant all on wpblogdb.* to 'vivek' identified by 'fooBarPwd8-4_2';
mysql> quit;

Sample session:

Fig.05: Mysql admin: New MySQL User and Database

Now, try to log in as vivek user, enter:
$ mysql -u vivek -p wpblogdb

Sample session:
Fig.06: Log back in as vivek user and wpblogdb database

Of course you can create a mysql tables and insert data too:
mysql> CREATE TABLE authors (id INT, name VARCHAR(20), email VARCHAR(20));
mysql> INSERT INTO authors (id,name,email) VALUES(1,"Vivek","");
mysql> INSERT INTO authors (id,name,email) VALUES(2,"Wendy","");
mysql> INSERT INTO authors (id,name,email) VALUES(3,"Tom","");
mysql> SELECT * FROM authors;
mysql> quit;

Sample outputs:
Fig.07: Create a demo authors table in wpblogdb database

How do I start MySQL server?

$ sudo systemctl start mysql
$ sudo systemctl start mysql.service

How do I stop MySQL server?

$ sudo systemctl stop mysql
$ sudo systemctl stop mysql.service

How do I restart MySQL server?

$ sudo systemctl restart mysql
$ sudo systemctl restart mysql.service

How do I find out if MySQL running/active?

$ sudo systemctl status mysql.service
Sample outputs:

● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enab
   Active: active (running) since Mon 2016-03-28 14:20:54 CDT; 8s ago
  Process: 24181 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exi
  Process: 24176 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exite
 Main PID: 24180 (mysqld_safe)
    Tasks: 23 (limit: 512)
   Memory: 106.1M
      CPU: 514ms
   CGroup: /system.slice/mysql.service
           ├─24180 /bin/sh /usr/bin/mysqld_safe
           └─24528 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plug

Mar 28 14:20:53 xenial systemd[1]: Starting MySQL Community Server...
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Can't log t
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Logging to 
Mar 28 14:20:53 xenial mysqld_safe[24180]: 160328 14:20:53 mysqld_safe Starting my
Mar 28 14:20:54 xenial systemd[1]: Started MySQL Community Server.

How do I reset the mysql root account password?

You need to type the following command, if you would like to change the MySQL root password:
$ sudo dpkg-reconfigure mysql-server-5.7
See “Recover the MySQL root Password” for more information.

A note about MySQL server configuration

You may edit the /etc/mysql/my.cnf file to configure the basic settings such as TCP/IP port, IP address binding, and other options. However, The MySQL database server configuration file on the Ubuntu 16.04 LTS is located at /etc/mysql/mysql.conf.d/mysqld.cnf and one can edit using a text editor such as vi or nano:
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
After making a change to /etc/mysql/mysql.conf.d/mysqld.cnf the MySQL server will need to be restarted:
$ sudo systemctl restart mysql.service
And, there you have it, the MySQL database version 5.7 installed and working correctly on the latest version of Ubuntu Linux 16.04 LTS. For more information see MySQL 5.7 Reference Manual.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.


How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...