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.

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
19 comments… add one
  • Jouni "rautamiekka" Järvinen Mar 29, 2016 @ 0:50

    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 Mar 29, 2016 @ 15:30


    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 Mar 29, 2016 @ 17:46

      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 Apr 27, 2016 @ 22:48

    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 Apr 29, 2016 @ 3:48

    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 @ 18:11

    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 @ 14:46

    Thanks! Very clear!
    It works for me ;)

  • Tim Oh May 15, 2016 @ 20:44

    :~$ 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

    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.

  • ank91 Jul 26, 2016 @ 5:34

    Silent install script –

    # Install MySQL Server in a Non-Interactive mode. Default root password will be "root"
    echo "mysql-server-5.7 mysql-server/root_password password root" | sudo debconf-set-selections
    echo "mysql-server-5.7 mysql-server/root_password_again password root" | sudo debconf-set-selections
    sudo apt-get -y install mysql-server-5.7
  • IQRA ASLAM Sep 2, 2016 @ 5:13

    I am having mysql 5.7 on my linux server Ubuntu16.04 . i am installing Atutor (a learning Management system ) on my server .Every thing is fine but mysql 5.7 is find disabled or not found on it how can i fix this issue Please guide me i am in trouble.

  • Jonathan Nascimento Sep 12, 2016 @ 17:47

    Thank you very much! very good tutorial was of great help.

  • Nishanthi Oct 5, 2016 @ 14:42

    Wow ,this is what i am looking for very useful thanks

  • Nisith Palo Oct 17, 2016 @ 4:39

    i finishing Run mysql_secure_installation to secure your installation then next it shows

    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    – Dropping test database…

    – Removing privileges on test database…

    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

    All done!
    nisith@nisith-inspiron-3521:~$ mysql -u root -p
    Enter password:
    ERROR 1698 (28000): Access denied for user ‘root’@’localhost’

  • Nisith Palo Nov 3, 2016 @ 7:44

    Very very Thanks vivek.

  • Dario Jan 4, 2017 @ 5:43

    Thanks for this posting. It saved my life. I was struggling with a wrong installation of mysql and this article made it so easy for me!

  • John_Betong Apr 8, 2017 @ 10:42

    Many thanks for the tutorial.
    May I add:

    Yes I know I am shouting but a numeric password is accepted but does not work as I learned to my detriment. I just spent quite a few hours trying to find a solution.

  • scott Apr 24, 2017 @ 12:33

    Followed a link to get 5.6 and find an article on mysql 5.7 which I am more than capable of doing on my own just struggling with 5.6 as this is required by the business, next time can you correctly advertise or have your links accurately labelled or linking

  • Harish May 30, 2017 @ 11:41

    I have tried so many links none of them rectify my error but this link successfully rectify thank you so much and make a great job

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @