How to Install MariaDB on Ubuntu 18.04

In this article, we will show you two different methods on how to install MariaDB on your Ubuntu 18.04 Cloud VPS.

MariaDB is an open-source database server that is a fork of MySQL and can easily serve as a drop-in replacement.
In the first installation method, we will use the official Ubuntu repository, while the second installation method we will install the latest version of MariaDB from the official MariaDB repositories.

Before proceeding with our installation methods, make sure you are logged in as root or a user with sudo privileges.

Step 1: Log in via SSH on the Ubuntu server:

ssh root@<span style="color: #ff0000;">Server_IP_Address</span> -p <span style="color: #ff0000;">Port_number</span>

Make sure to replace “Server_IP_Address” and “Port_number” with your server’s public IP address and SSH port number (if necessary).

Step 2: Update all packages

The first thing to do when you are logged in is to make sure that all the installed packages are up to date:

sudo apt update
sudo apt upgrade

Step 3: Installing MariaDB

Method 1: Installing MariaDB on Ubuntu 18.04 from the Ubuntu repositories

At the time of writing this article, the latest version of MariaDB available from the official Ubuntu repositories is MariaDB version 10.1.34.

Once our system is up to date, we will install MariaDB from the Ubuntu repositories with the following steps.

Step 1: Install MariaDB by running the following command:

sudo apt install mariadb-server

Step 2: The MariaDB service after the installation will start automatically. We will check the status by typing:

sudo systemctl status mariadb

Output:

● mariadb.service - MariaDB 10.1.34 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2019-01-26 02:50:17 CST; 22s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Main PID: 10214 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 27 (limit: 2320)
CGroup: /system.slice/mariadb.service
└─10214 /usr/sbin/mysqld

Step 3: We will check the MariaDB version with this command:

mysql -V

Output:

mysql Ver 15.1 Distrib 10.1.34-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Method 2: Installing MariaDB on Ubuntu 18.04 from the MariaDB Repositories

At the time of writing this article, the latest version of MariaDB available from the MariaDB repositories is version 10.3. You can always check for if the new version is available by visiting the MariaDB Repository page.

To install MariaDB version 10.3 on your Ubuntu 18.04 server, follow these steps:

Step 1: With the command below, we will add the MariaDB GPG key to our system:

sudo apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8

Output:

Executing: /tmp/apt-key-gpghome.8jprLzE53p/gpg.1.sh --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
gpg: key F1656F24C74CD1D8: 6 signatures not checked due to missing keys
gpg: key F1656F24C74CD1D8: public key "MariaDB Signing Key <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1

Step 2: We will add the MariaDB repository by executing the following command:

sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://ftp.utexas.edu/mariadb/repo/10.3/ubuntu bionic main'

Step 3: We will now update the list of packages so we can install the latest version of MariaDB from the repository we added in the previous step.

sudo apt update

Step 4: Considering that the repository is now added, we will run the following command to install the MariaDB package:

sudo apt install mariadb-server

Step 5: To verify that MariaDB service is running, we will run the command:

sudo systemctl status mariadb

Output:

● mariadb.service - MariaDB 10.3.12 database server
Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf
Active: active (running) since Sat 2019-01-26 03:12:52 CST; 10s ago
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 12496 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 12493 ExecStartPost=/etc/mysql/debian-start (code=exited, status=0/SUCCESS)
Process: 12387 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
Process: 12385 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 12384 ExecStartPre=/usr/bin/install -m 755 -o mysql -g root -d /var/run/mysqld (code=exited, status=0/SUCCESS)
Main PID: 12462 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 31 (limit: 2320)
CGroup: /system.slice/mariadb.service
└─12462 /usr/sbin/mysqld

Step 6: We can also print the MariaDB version by typing:

mysql -V

Output:

mysql Ver 15.1 Distrib 10.3.12-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

Securing MariaDB

At this point of this article, we have MariaDB installed on the server, and now by running the mysql_secure_installation command, we can improve the security of the MariaDB server.

sudo mysql_secure_installation

After the execution of the command, the script will prompt you to set up the root user password, remove the anonymous user, restrict root user access to the local machine and remove the test database.

Output:

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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 environment.

Remove anonymous users? [Y/n] y
... Success!

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? [Y/n] y
... Success!

By default, MariaDB 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 environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

In the upper output, we can see that all steps are explained in detail and it is recommended to answer “Y” (yes) to all questions.

Restart your MariaDB server when done:

sudo systemctl restart mariadb.service

Configuring the MariaDB Database Server

If we want to make any changes in order to optimize the database server performance, we can edit the MariaDB default configuration file located at /etc/mysql/mariadb.conf.d/50-server.cnf

sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

After making any changes, you need to restart the database server:

sudo systemctl restart mariadb.service

MariaDB Administration Commands

In the last section of this article, we will discuss some useful MariaDB commands. These commands are some of the basic commands that will help you get started with using MariaDB.

Connect to MariaDB from the Command Line

The first and most basic command is the command used to log into the MariaDB command line interface:

mysql -u root -p

After running the command above, you will get prompted for the root password. Enter the password to log in to the session.

Output:

Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 46
Server version: 10.3.12-MariaDB-1:10.3.12+maria~bionic mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Checking the Version of your MariaDB Installation

Check a detailed view of the current version of your MariaDB installation.

mysqladmin -u root version

Output:

mysqladmin Ver 9.1 Distrib 10.3.12-MariaDB, for debian-linux-gnu on x86_64
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Server version 10.3.12-MariaDB-1:10.3.12+maria~bionic
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/run/mysqld/mysqld.sock
Uptime: 6 min 43 sec

Threads: 7 Questions: 460 Slow queries: 0 Opens: 184 Flush tables: 1 Open tables: 31 Queries per second avg: 1.141

Showing All Databases

You can list all the databases that your MariaDB currently has with this simple command:

show databases;

List All Users in MariaDB

Type the following query at the MariaDB [(none)]> prompt to see/list the users in a MySQL database:

SELECT User, Host FROM mysql.user;
Output:

+------------------+-----------+
| User             | Host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| root             | localhost |
+------------------+-----------+
2 rows in set (0.001 sec)

Creating New Databases

If we want to create a new database in MariaDB, we can run the command below:

CREATE DATABASE our_database;

Where our_database is the name of the new database created.

These are just some of the basic commands that let you manipulate your databases. The capabilities of MariaDB are far greater than this, so we recommend reading some of the official documentation to help you get a grip on how to effectively use this excellent database management platform.


Of course, if you are one of our Cloud VPS Hosting customers, you don’t have to install MariaDB on your Ubuntu 18.04 Cloud VPS – simply ask our admins, sit back and relax. Our admins will install MariaDB on Ubuntu 18.04 for you immediately.

PS.If you liked this post about how to install MariaDB on an Ubuntu 18.04 Cloud VPS, please share it with your friends on the social networks using the share shortcuts, or simply leave a comment in the comments section. Thanks.

Leave a Comment