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.
Check out the previous article where we installed Apache HTTP Server.
Update Ubuntu package list
First, make sure your Ubuntu packages list is up to date.
sudo apt update
Install MySQL in Ubuntu
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:
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
MySQL will be downloaded and installed. This may take a few minutes depending on your internet connection.
Verify the installation by checking the installed MySQL version:
The command should return something like this:
mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper
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.
Run the MySQL security script:
First, you’ll be prompted if you want to use the
VALIDATE PASSWORD PLUGINto test your password strength. Hit
Yif you are security conscious, otherwise hit any other key.
If you hit
Yyou 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.
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 hit
Enteragain 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.
Proceed through the remaining prompts, selecting either
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
Yfor 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
Starting and stopping the MySQL service
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
Using the MySQL client
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
-hflag if needed.
If the connection has worked 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 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
That’s it for this quick article. In the next part we’ll install PHP in Ubuntu.