Install MariaDB on Ubuntu

Install MariaDB on Ubuntu

MariaDB is a high performance community-developed database engine that’s commonly used by big systems. we will install the latest version of MariaDB on Ubuntu 20.04

1. Before you in Install MariaDB on Ubuntu, update and upgrade the repositories by the below two commands:

root@iRedMail-doesnt-exist:~# apt update
root@iRedMail-doesnt-exist:~# apt upgrade

Sometimes during the upgrade process apt upgrade command, you get popups like the one below about updating the packages configuration, don’t change anything, just hit Enter key and keep it as default. Do this for any similar popup.

Apt Upgrade Popup

2. Install the software-properties-common package, we already installed it before installing the PHP, so you don’t need to do anything if you already installed the PHP.

root@iRedMail-doesnt-exist:~# apt install software-properties-common

3. Import MariaDB gpg key by running the command:

root@iRedMail-doesnt-exist:~# apt-key adv --fetch-keys ''

4. Add the APT repository for MariaDB. Until writing this article, 10.5 is the latest. This step will allow the MariaDB version to be upgraded automatically when you upgrade the packages on your server (when running: apt upgrade).

root@iRedMail-doesnt-exist:~# add-apt-repository 'deb [arch=amd64] focal main'

For Ubuntu 22.04 LTS, the latest stable version on June 4, 2023 is: 10.11, so run this command:

root@iRedMail-doesnt-exist:~# add-apt-repository 'deb [arch=amd64] jammy main'
Add APT Repository for MariaDB

To remove the repository we’ve just added, run the below command, you can use this command in case you added wrong repository.

root@iRedMail-doesnt-exist:~# add-apt-repository -r 'deb [arch=amd64] focal main'

Or enter the file which contains the repositories by running the command and delete it from there:

root@iRedMail-doesnt-exist:~# nano /etc/apt/sources.list

5. Update the APT repository:

root@iRedMail-doesnt-exist:~# apt update

6. Now, let’s install the MariaDB server, by running the command:

root@iRedMail-doesnt-exist:~# apt install mariadb-server

You will be asked to confirm the installation, type: y (or: Y) then hit Enter key. If you don’t want to proceed with the installation, type: n, then hit Enter key.

Install MariaDB Server Confirmation on Ubuntu

You will see the below progress:

Install MariaDB Server Progress on Ubuntu

And here once it’s done:

Install MariaDB Server Done on Ubuntu

7. Let’s now check the version installed of MariaDB, run any of the following commands, they are same.

root@iRedMail-doesnt-exist:~# mariadb -V
root@iRedMail-doesnt-exist:~# mysql -V
Check MariaDB Version on Ubuntu

Assign Password to Root User

8. Let’s add password to root user, first login to MySQL console by running:

root@iRedMail-doesnt-exist:~# mysql
First Login to MariaDB From Root User

Run the below MySQL command to assign password to root user. We will assign the password: abcdefgh as an example, you should assign so strong password.

ALTER USER 'root'@'localhost' IDENTIFIED BY 'abcdefgh';
Assign Password to Root User

Note that if you want to clean the MariaDB console, just press Ctrl + L.

9. Quit MariaDB Console by pressing: Ctrl + c, or type: quit or exit. Now your next login to MariaDB console again needs password not like before, run the below command to login:

 root@iRedMail-doesnt-exist:~# mysql -u root -p

Enter the root password to login as you see below.

Login to MariaDB Console from Root User Using Password

Use CNF File to Login to MariaDB on Ubuntu Without a Password

10. If you want to login to MySQL console without entering the root password everytime, we need to create CNF file in the base directory of your server and add the password there, so next time you login to MySQL console by just typing: mysql as before when we had no password.

To proceed, open CNF file by running the below command:

root@iRedMail-doesnt-exist:~# nano .my.cnf

Add the user and the password inside the file as below, use a strong password please.

My CNF File For Quick Access

Close and save the file and now try to login to MySQL console via the command:

root@iRedMail-doesnt-exist:~# mysql

Super! Now you can access MySQL console easily and you don’t need to run the command: mysql -u root -p nor to type the password every time you login to your MySQL console.

If you want to remove MariaDB completely with all related databases, just run the below command and when you aget a prompt, choose yes to delete everything:

root@iRedMail-doesnt-exist:~# apt purge mariadb-*

Our next step is to create new MySQL database for the new WordPress website that we will start.