Install MySQL

Another important step towards a web hosting system is a backend database. On Linux many databases systems are available, but the one most webhosts use is MySQL. Besides MySQL, Postgres and MariaDB also deserve mentioning.

The installation is not more complicated than Apache server installation (did you read the Install Apache how-to?)

Start a terminal and type the following commands:

sudo apt-get -y update 
sudo apt-get -y install mysql-server 
MySQL Installation Results

After the installation it is a good idea to secure MySQL Database. For this run:

 sudo mysql_secure_installation 

A series of questions will be asked. The first question is about the PASSWORD PLUGIN:

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:

Press Y or y and enter.

The next question is about password complexity. I personally prefer complex passwords, even though they are harder to remember (they are also harder to guess).

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary           file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 

I usually choose 2 here but you can choose either option. However, if you choose 2, the password needs to contain alpha-numeric, case and special characters. It will also be checked against a dictionary so it does not match words.

Then you are asked to enter a new password:

New password: 

Enter the password you want for your database. You should choose a strong password here as this is the password that is used with the main MySQL database account. After you enter the password you will be asked to confirm it. Next you will be given an estimated strength for your password and you will be asked if you wish to continue using it. If the strength is less than 100 I would suggest that you say no and you choose another password. Choose Y to accept the password.

The next question is about an anonymous user and if you wish to remove it. Answer Y.

By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production

Remove anonymous users? (Press y|Y for Yes, any other key for No) : 

The next question asks if we want to disable remote root access. In fact, we do not want anyone to remotely access the database. The Apache server is running locally on the same server, so we do not need remote access at all. Answer Yes to this question also.

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y

The next question is about the default test database and if we wish to remove it. We do not need this database and we will create our own. Answer Yes to this question also.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production

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

The final question is if you want to reload the privilege table. This means if you want to apply the configurations you just changed. Answer Yes.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

That completes the installation. One thing you want to make sure is that the MySQL server will restart with every reboot. For this type the following command:

 sudo systemctl enable mysql

You should get a message that says:

Synchronizing state of mysql.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable mysql

Finally reboot your server:

sudo reboot

Testing the installation

Once your server has rebooted, login using your username and password. Start a terminal and become root using:

sudo su

than type:


You should get a prompt saying mysql> like in the image below.

Testing MySQL – We connected using user root

Any other result means something did not work during the installation. Please post a comment and I will do my best to help you out and get MySQL up and running for you.

Further reading

SQL: Learn SQL (using MySQL) in One Day and Learn It Well. SQL for Beginners with Hands-on Project. (Learn Coding Fast with Hands-On Project)
Learn MySQL in Plain English: A Beginner’s Guide to MySQL
MySQL: MySQL Tutorials for Beginners Basic to Advanced MySQL Languages


Posted in How to, MySQL.

Leave a Reply

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