Install MySQL in Ubuntu 18

This article is the second in a mini-series of quick reference guides for installing Apache, PHP, and MySQL in Ubuntu 18. In this article we will cover the installation of MySQL Server in Ubuntu 18, and take a quick look at using the mysql client.

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

Previous articles

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

Update Ubuntu package list

  1. First, make sure your Ubuntu packages list is up to date.

    sudo apt update
    

Install MySQL in Ubuntu

  1. You 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 package list

  2. Next install the MySQL server and client

    sudo apt 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

    MySQL will be downloaded and installed. This may take a few minutes depending on your internet connection.

  3. 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.25, for Linux (x86_64) using EditLine wrapper

    MySQL version

Configure MySQL

There are some configuration options you’ll need to take care of, such as setting the root password for MySQL. This is done by running the MySQL security script and answering the prompts.

  1. Run the MySQL security script:

    sudo mysql_secure_installation
    
  2. First, you’ll be prompted if you want to use the VALIDATE PASSWORD PLUGIN to test your password strength. Hit Y if you are security conscious, otherwise hit any other key.

    If you hit Y you will then be prompted to set the level of password security as low (0), medium (1), or high (2). This setting determines the complexity and length requirements your MySQL passwords will be tested for.

    MySQL validate password plugin

  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 hit Enter again to confirm the password.

    Note: If you chose to use the Validate Password Plugin you will be shown the estimated strength of the entered password. Regardless of the password strength, you will have the option to proceed with the current password or enter a different password.

  4. Proceed through the remaining prompts, selecting either Y or N. The prompts will be:

    • Remove anonymous users?
    • Disallow root login remotely?
    • Remove test database and access to it?
    • Reload privilege tables now?

    I have entered Y for all of these.

This concludes the MySQL install steps. By default, MySQL is installed to the /etc/mysql directory and database data files are stored in the /var/lib/mysql directory.

Starting and stopping the MySQL service

  1. 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 service status

    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
    

Using the MySQL client

  1. Test connectivity to the MySQL server by using the MySQL client, ensuring to use the password set during the MySQL installation.

    sudo mysql -u root -p[password]
    

    By default the client will attempt to connect to localhost. You can specify a hostname using the -h flag if needed.

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

    MySQL client

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

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

    exit
    

MySQL Help

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

For help within the mysql> prompt you can type help.

Wrapping up

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