Install MySQL in Ubuntu 16

This article is the second in a mini-series of quick reference guides for installing Apache, PHP, and MySQL in Ubuntu 16. In this article we will cover the installation of MySQL Server.

My environment for these steps is Ubuntu 16 running on VirtualBox in Windows 10 x64.


Check out the previous article where we installed Apache HTTP Server.

Install MySQL in Ubuntu

  1. First we can check if MySQL is already installed. There are several ways to do this - for a variety of methods check this StackOverflow question: How can I check if mysql is installed on ubuntu? We’ll use the accepted answer:

    Open a new terminal and type

    dpkg --get-selections | grep mysql

    If MySQL is not installed you will see nothing returned from the command.

    If MySQL is installed you will see something like this:

    MySQL install prompt

  2. Next install the MySQL server and client

    sudo apt-get install mysql-server mysql-client

    You will be notified that additional disk space will be used and prompted to continue. Hit y then Enter to continue.

    MySQL install prompt

  3. Next you will be prompted to set a password for the MySQL administrative “root” user

    Enter a password and hit Enter.

    You will be prompted a second time for the password. Do this and then hit Enter again to proceed.

    MySQL install prompt

  4. Verify the installation by checking the installed MySQL version

    mysql --version

    The command should return something like this:

    mysql Ver 14.14 Distrib 5.7.19, for Linux (x86_64) using EditLine wrapper

Verifying the installation

  1. MySQL was installed to the /etc/mysql directory

    Another important directory is /var/lib/mysql which is where the database data files are stored.

  2. By default the MySQL service will run automatically when Ubuntu starts.

    We can check it’s status with the following command:

    sudo service mysql status

    MySQL install prompt

    Here are some other useful commands for controlling the MySQL service:

    Restart MySQL service:

    sudo service mysql restart

    Stop MySQL service:

    sudo service mysql stop

    Start MySQL service:

    sudo service mysql start
  3. Test connectivity to the MySQL server by using the MySQL client, ensuring to use the password set during the MySQL installation.

    mysql -u root -p[password]

    We could also specify a hostname using the -h flag, but it will default to localhost so we can omit it.

    If the connection has worked and you will see a welcome message followed by the mysql> prompt.

    MySQL install prompt

  4. We can list the available databases (there will only be a few default ones at this stage):

    show databases;
  5. Finally, exit the MySQL client by typing exit to return to the normal bash prompt.


MySQL Help

The mysql command help can be displayed by typing mysql --help at the command prompt (not the mysql prompt).

For help within mysql> prompt you can type help.

Uninstall MySQL in Ubuntu

If you ever need to completely uninstall MySQL I found this StackOverflow answer worked well: Removing MySQL 5.5 Completely.

Wrapping up

That’s it for this quick article. In the next part we’ll install PHP in Ubuntu.