How to Install MySQL/MariaDB on Ubuntu


Share this

The second part of our LAMP tutorial series: how to install MySQL (or MariaDB) on an Ubuntu server.

This tutorial is intended for Ubuntu servers, the instructions should work on any LTS release of Ubuntu, including Ubuntu 14.04, Ubuntu 16.04, Ubuntu 18.04, even non-LTS releases like Ubuntu 17.10 and other Ubuntu-based distros. We tested this on an Ubuntu 16.04 server.

For the first part of our LAMP series, go to our How to Install and Optimize Apache on Ubuntu tutorial.

Before we begin installing MySQL/MariaDB

Some requirements and other notes:

  • MySQL and MariaDB are almost identical when it comes to basic usage in a LAMP stack. Most commands are the same, even the installation is similar. Choose one and install it for your LAMP stack, we’ll include instructions for both.
  • You’ll need an Ubuntu server to run MySQL/MariaDB on. We recommend Vultr, they offer a $2.5 per month instance which is more than enough for a simple LAMP stack. You can compare other cloud server providers too.
  • You’ll need the root user or a user with sudo access to the server. The commands below are all executed by a root user, so we didn’t have to append ‘sudo’ to each command. You’ll likely have to if you use a non-root user.
  • You’ll need SSH enabled if you use Ubuntu or an SSH client like MobaXterm if you use Windows.
  • MySQL/MariaDB may already be installed on your server. You can check if they’re installed by entering “mysql” or “mariadb” and you should know based on the output.





That’s it for now. Let’s move onto our tutorial.

How to install MySQL on Ubuntu

We’ll start with MySQL. If you want to install MariaDB, skip to the MariaDB instructions.

Update Ubuntu

First of all, as always, before you do anything else, update your Ubuntu server by running the following command:

apt-get update && apt-get upgrade

Install MySQL

Then, install MySQL by running the following command:

apt-get install mysql-server

This command will install both the MySQL server and client. You’ll get a prompt to enter a password for your root user.

That’s it. MySQL is installed. Now, you need to secure and configure it.

Secure MySQL

You should run the mysql_secure_installation script that will help you secure your MySQL.

Start the script with the following command:

mysql_secure_installation

And respond to the prompts. You can respond with the default to each prompt.

Optimize MySQL (advanced users only)

To optimize your MySQL, you can use the MySQLTuner script. It does NOT do all the work for you. The script will only give you recommendations on how you can improve and optimize your MySQL.

Download and run the script with the following command:

curl -L http://mysqltuner.pl/ | perl

And check the recommendations. Do some research and use google for each recommendation. If you don’t know what you’re doing, contact someone else and let them do it for you or just skip this.

You can also use mysqlcheck to repair your databases.  You can repair all your databases with a single command:

mysqlcheck -A --auto-repair -u root -p

There are other optimizations you can do on your server and databases, so do your own research if you want to further optimize MySQL.


How to install MariaDB on Ubuntu

Now for our MariaDB installation instructions.

Update Ubuntu

First, update your Ubuntu server:

apt-get update && apt-get upgrade

Add the MariaDB repository

Before you can install MariaDB, you need to add the MariaDB repository.

Depending on your Ubuntu release, you may need to run different commands, so go to the official MariaDB repository page, select your distro and choose a mirror closest to your server’s location. Then, copy the commands you’ll get on the page. We use Ubuntu 16.04.1 and we chose a US mirror, so we’ll run these commands to add the repository:

apt-get install software-properties-common
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
add-apt-repository 'deb [arch=amd64,i386,ppc64el] https://mirrors.evowise.com/mariadb/repo/10.1/ubuntu xenial main'

Next, you need to update your package list again:

apt-get update

And move onto installing MariaDB.

Install MariaDB

After you’ve added the MariaDB repository, you can install it by running the following command:

apt-get install mariadb-server

And that’s it. You’ve installed MariaDB on your server.

Secure MariaDB

It’s the same process as with MySQL. Run the security script with the following command:

mysql_secure_installation

And follow the prompts. You can enter the default for each prompt. Of course, use a strong password.

Optimize MariaDB (advanced users only)

Again, same with MySQL, you can use MySQLTuner to check your MariaDB and get recommendations on how to improve it. It does NOT do all the work for you. The script will only give you recommendations on how you can improve and optimize your MariaDB.

Run the script with:

curl -L http://mysqltuner.pl/ | perl

And check the recommendations. Do some research and use google for each recommendation. If you don’t know what you’re doing, contact someone else and let them do it for you or just skip this.

Mysqlcheck works with MariaDB too, so to optimize all your MariaDB databases at once, run the following command:

mysqlcheck -A --auto-repair -u root -p

There are other optimizations you can do on your server and databases, so do your own research if you want to further optimize MariaDB


Share this

Leave a comment

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