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
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:
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
Next you will be prompted to set a password for the MySQL administrative “root” user
Enter a password and hit
You will be prompted a second time for the password. Do this and then hit
Enteragain to proceed.
Verify the installation by checking the installed 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
MySQL was installed to the
Another important directory is
/var/lib/mysqlwhich is where the database data files are stored.
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
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
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
-hflag, but it will default to
localhostso we can omit it.
If the connection has worked and you will see a welcome message followed by the
We can list the available databases (there will only be a few default ones at this stage):
Finally, exit the MySQL client by typing
exitto return to the normal bash prompt.
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
Uninstall MySQL in Ubuntu
If you ever need to completely uninstall MySQL I found this StackOverflow answer worked well: Removing MySQL 5.5 Completely.
That’s it for this quick article. In the next part we’ll install PHP in Ubuntu.