≡ Menu

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

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

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

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

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:

CREATE DATABASE DATABASE-NAME-HERE;
GRANT ALL ON DATABASE-NAME-HERE.* TO 'DATABASE-USERNAME-HERE' IDENTIFIED BY 'DATABASE-PASSWORD-HERE';

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

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


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

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

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","xuz@foo.gmail.com");
mysql> INSERT INTO authors (id,name,email) VALUES(2,"Wendy","bar@foo.gmail.com");
mysql> INSERT INTO authors (id,name,email) VALUES(3,"Tom","tom@foo.gmail.com");
mysql> SELECT * FROM authors;
mysql> quit;

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

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

How do I start MySQL server?

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

How do I stop MySQL server?

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

How do I restart MySQL server?

$ sudo systemctl restart mysql
OR
$ 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
OR
$ 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.

Share this tutorial on:

{ 9 comments… add one }
  • Jouni "rautamiekka" Järvinen March 29, 2016, 12:50 am

    Don’t install vanilla Oracle MySQL, and don’t install 5.6, install MariaDB 10.1 which is combination of 5.6 and 5.7. It sports new engines for special purposes in addition to TokuDB, an improved InnoDB.

  • Julian Alarcon March 29, 2016, 3:30 pm

    Hey

    Your post is now outdated, MySQL default version now is 5.7 in Ubuntu 16.04, but there is still 5.6 version available.

    • Vivek Gite March 29, 2016, 5:46 pm

      No. I checked it again:

      $ lsb_release -r
      Release:	16.04
      $ apt-cache search 5.? | grep mysql-server
      mysql-server - MySQL database server (metapackage depending on the latest version)
      mysql-server-5.6 - MySQL database server binaries and system database setup
      mysql-server-core-5.6 - MySQL database server binaries
      

      There is no 5.7 version. Here is a search result.

  • Ruud April 27, 2016, 10:48 pm

    Anyone got an idea on how to change the datadir in MySQL?

    The old way doesn’t seem to work (even though I changed the data path in the apparmor config).

  • ank91 April 29, 2016, 3:48 am

    Thanks, you saved my day.

    How do i configure mysql service for low RAM usage, right now it is taking around 800 MB RAM. Please help.

  • melina May 12, 2016, 6:11 pm

    I have a problem. Didn’t open the place that I need to type the password for the MySQL root user. How can I fix this?

  • QUHO May 13, 2016, 2:46 pm

    Thanks! Very clear!
    It works for me ;)

  • Tim Oh May 15, 2016, 8:44 pm

    :~$ sudo apt install mysql-server
    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Some packages could not be installed. This may mean that you have
    requested an impossible situation or if you are using the unstable
    distribution that some required packages have not yet been created
    or been moved out of Incoming.
    The following information may help to resolve the situation:

    The following packages have unmet dependencies:
    mysql-server : Depends: mysql-community-server (= 5.7.12-1ubuntu15.10) but it is not going to be installed
    E: Unable to correct problems, you have held broken packages.

  • Jay May 27, 2016, 10:47 am

    Sorry, This is false advertising. I came here to get help installing 5.6 not 5.7 and certainly not mariadb (ever tried going back?).
    IMO 16.04 desktop maybe great, as a server it sucks.

Leave a Comment


   Tagged with: , , , , ,