How to Install MySQL or MariaDB on Ubuntu Linux

This post shows students and new users steps to install MySQL or MariaDB on Ubuntu Linux. MariaDB and MySQL are twins. Both MySQL and MariaDB is an open-source, multi-threaded relational database management systems. MariaDB is a backward compatible replacement for MySQL.

You can uninstall MySQL and install MariaDB, and your applications may not even know the difference. Since these two databases are identical, we’re going to write a single post detailing how to install both on Ubuntu Linux.

MariaDB maintained and developed by the MariaDB Foundation while MySQL is owned by Oracle.

If you’re a student or new user learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.

To get started with installing both MariaDB and MySQL databases on Ubuntu Linux, follow the steps below.

How to install MariaDB on Ubuntu Linux

As mentioned above, MariaDB is backward compatible to MySQL. It is maintained and developed by the MariaDB Foundation.

MariaDB packages are available in Ubuntu repositories. So it can simply be installed using the apt package management.

To install MariaDB, run the commands below.

sudo apt update
sudo apt install mariadb-server

After installing, you can run the commands below to view MariaDB service status.

sudo systemctl status mariadb

After running the command above, it should output similar lines as shown below.

mariadb.service - MariaDB 10.3.31 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-09-15 16:40:20 CDT; 22s ago
       Docs: man:mysqld(8)
             
   Main PID: 3007 (mysqld)
     Status: "Taking your SQL requests now..."
      Tasks: 31 (limit: 4651)
     Memory: 65.6M
     CGroup: /system.slice/mariadb.service
             └─3007 /usr/sbin/mysqld

Sep 15 16:40:20 ubuntu2004 /etc/mysql/debian-start[3045]: mysql
Sep 15 16:40:20 ubuntu2004 /etc/mysql/debian-start[3045]: performance_schema

To check which versions of MariaDB is running, run the commands below.

mysql -V

That should output similar lines as below.

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

How to install latest versions and update MariaDB

The versions available in Ubuntu repositories for MariaDB are not the latest. To get the latest, you will want to add MariaDB repositories to Ubuntu. The repositories are maintained at the link below.

MariaDB – Setting up MariaDB Repositories – MariaDB

To there, select the version of Ubuntu you’re installing for, then select the latest version of MariaDB. As of this writing, the latest version of MariaDB is 10.6.

Run the commands to add the 10.6 version to Ubuntu 20.04.

sudo apt-key adv --fetch-keys '
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el]  focal main'

Then install MariaDB

sudo apt update
sudo apt install mariadb-server

How to install MySQL on Ubuntu Linux

MySQL is also available via Ubuntu default repositories. Because of that, one can simply run the commands below to install MySQL using the apt package management tool.

sudo apt update
sudo apt install mysql-server

After installing MySQL, you can check its service status by running the commands below.

sudo systemctl status mysql

That should output similar lines as below.

mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2021-09-15 16:46:30 CDT; 8s ago
   Main PID: 3040 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4651)
     Memory: 353.1M
     CGroup: /system.slice/mysql.service
             └─3040 /usr/sbin/mysqld

Sep 15 16:46:29 ubuntu2004 systemd[1]: Starting MySQL Community Server...
Sep 15 16:46:30 ubuntu2004 systemd[1]: Started MySQL Community Server.

How to install the latest version of MySQL on Ubuntu Linux

Although MySQL server packages are included with Ubuntu, they may not necessarily be the latest. If you want to always get the latest versions of MySQL server downloaded to your servers, you might want to add its repository.

The link below takes you to the repository file.

Visit the download page and look for Ubuntu / Debian (Architecture Independent), DEB Package. The click the Download button to get the repository package…

You can run the commands below, updating the version number (0.8.15-1) at time of this writing, with the latest from the file above.

cd /tmp
wget 
sudo dpkg -i mysql-apt-config_0.8.15-1_all.deb

When you run the commands above, you should get a config prompt

Simply select OK as shown in the image below.

MySQL 8 on Ubuntu

Now that the repository is installed, run the commands below to install

sudo apt update
sudo apt upgrade

How to secure MariaDB and MySQL

Both MariaDB and MySQL come with a script that allows you to perform some security operations.

Run the commands below to invoke the script and perform some recommended tasks to secure the database.

sudo mysql_secure_installation

Both MariaDB and MySQL servers come with the root user is set to use the auth_socket authentication method by default.

The auth_socket plugin authenticates users that connect from the localhost through the Unix socket file. This means that you can’t authenticate as a root by providing a password.

To logon to MariaDB and MySQL servers as root, simply run the command below. You don’t need a password since it’s using auth_socket method.

sudo mysql

The server console should come up.

elcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.26-0ubuntu0.20.04.2 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

That should do it!

Conclusion:

This post showed you how install MariaDB or MySQL on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.