How to Install MariaDB on Ubuntu 24.04, 22.04, or 20.04

MariaDB is a robust, open-source relational database management system that is a popular alternative to MySQL. It offers high performance, scalability, and a rich feature set, making it suitable for a wide range of applications, from small personal projects to large-scale enterprise environments. MariaDB provides long-term support (LTS) releases, which ensure stability and reliability over extended periods, as well as short-term releases that introduce new features and improvements more frequently.

On Ubuntu 24.04, 22.04, or 20.04, you can install MariaDB using the official apt repository provided by mariadb.org. This repository includes the latest builds for long-term releases of MariaDB, including versions 10.5, 10.6, 10.11, and 11.4. If you prefer to use short-term releases, the same installation method applies, allowing you to choose the version that best fits your needs. This guide will walk you through the process of setting up MariaDB on your Ubuntu system using the official apt repository, ensuring you have access to the latest features and security updates.

MariaDB Pre-Installation Steps

Update Ubuntu Before MariaDB Installation

Before installing MariaDB, it’s crucial to update your Ubuntu system. This step ensures that all existing packages are current, minimizing potential conflicts.

Run the following command to update the package list:

sudo apt update

After updating the list, proceed to upgrade any outdated packages. This step is vital for system stability and security:

sudo apt upgrade

Install Initially Required Packages

MariaDB installation requires specific dependencies. These are essential packages that MariaDB needs to function correctly. Install them with the following command:

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https curl -y

Import MariaDB APT Repository

The next step involves importing the MariaDB APT repository containing the MariaDB packages you’ll install. Different branches are available: long-term support (LTS), recommended for production environments due to their stability, and short-term releases, suitable for users who regularly update their systems, such as content management systems like WordPress.

Importing the GPG Key

First, import the GPG key to verify the authenticity of the MariaDB packages:

curl -fsSL http://mirror.mariadb.org/PublicKey_v2 | sudo gpg --dearmor | sudo tee /usr/share/keyrings/mariadb.gpg > /dev/null

Select MariaDB Version Repository

Select the appropriate MariaDB version based on your requirements:

Option 1: MariaDB 10.5 (LTS)

End-of-life date: 24th June 2025.

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.5/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
Option 2: MariaDB 10.6 (LTS)

End-of-life date: July 2026.

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.6/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
Option 3: MariaDB 10.11 (LTS)

End-of-life date: February 2028.

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/10.11/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list
Option 4: MariaDB 11.4 (Long-term release)

End-of-life date: February 2030.

echo "deb [arch=amd64,arm64,ppc64el signed-by=/usr/share/keyrings/mariadb.gpg] http://mirror.mariadb.org/repo/11.4/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/mariadb.list

Refresh APT Package Index After MariaDB Repository Import

Once you’ve chosen and imported the desired repository version, refresh your APT repository to recognize the new source:

sudo apt update

This step ensures the system recognizes the newly added MariaDB repository and that it is ready for installation.

Finalize MariaDB Installation

Install MariaDB Server and Client

To set up MariaDB on your system, you must install both the server and client packages. This installation integrates the database server, which manages the data, and the client, which provides an interface to interact with the server.

Execute the following command to install these packages:

sudo apt install mariadb-server mariadb-client -y

Verifying MariaDB Installation

After installation, it’s essential to confirm that MariaDB is correctly installed. Check the installed version and build of MariaDB using this command:

mariadb --version

If the installation is successful, you will see an output similar to this:

mariadb  Ver x.xx Distrib xx.xx.x-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

Enable MariaDB Service

Checking MariaDB Service Status

Post-installation, verify the status of the MariaDB service. This command checks if MariaDB is active and running:

systemctl status mariadb

Starting MariaDB Service

While MariaDB typically activates automatically after installation, you can manually start it if needed:

sudo systemctl start mariadb

Stopping MariaDB Service

To stop the MariaDB service, use the following command:

sudo systemctl stop mariadb

Enabling MariaDB on System Startup

For MariaDB to start automatically upon system boot:

sudo systemctl enable mariadb

Disabling MariaDB on System Startup

To prevent MariaDB from starting automatically:

sudo systemctl disable mariadb

Restarting MariaDB Service

Restarting the service is often required after configuration changes. Use this command to restart MariaDB:

sudo systemctl restart mariadb

Run MariaDB Security Script

Importance of Running the MariaDB Security Script

When setting up MariaDB, it’s essential to strengthen its default configurations, which are generally considered insecure. This step is crucial in safeguarding the database against unauthorized access and vulnerabilities. Running the mysql_secure_installation script is a widely recognized industry practice during the MariaDB installation process. It enhances the security of your MariaDB installation and mitigates risks associated with potential malicious attacks.

Executing the Security Script

To initiate this security enhancement process, execute the mysql_secure_installation script. This script performs several security-related operations, ensuring a more secure environment for your MariaDB installation:

sudo mysql_secure_installation

Configuring Security Settings

After running the script, you will be guided through several steps to improve the security of your MariaDB installation:

  • Setting the Root Password: You’ll be prompted to create a strong password for MariaDB’s root user. This step is critical as it prevents unauthorized database access.
  • Disallowing Remote Root Access: The script will ask if you want to disable remote root logins, a recommended practice to limit access to local users only.
  • Removing Anonymous User Accounts: These accounts are accessible without a password, posing a security risk. Removing them is a vital step in securing your database.
  • Deleting the Test Database: MariaDB has a default test database accessible to anyone. Removing this database reduces potential points of entry for unauthorized users.

Following these steps significantly improves the security posture of your MariaDB installation on Ubuntu.

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] Y <---- Type Y then press the ENTER KEY.
Enabled successfully!
Reloading privilege tables..
 ... Success!


You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] Y <---- Type Y then press the ENTER KEY.
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


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 <---- Type Y then press the ENTER KEY.
 ... 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 <---- Type Y then press the ENTER KEY.
 ... 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 <---- Type Y then press the ENTER KEY.
 - 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 <---- Type Y then press the ENTER KEY.
 ... Success!

Cleaning up...

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

Thanks for using MariaDB!

Managing MariaDB

Run MariaDB Database Tables Upgrade Tool

If you’ve upgraded MariaDB to a newer version (e.g., from 10.5 to 10.10), ensuring your database tables are compatible with the new version is crucial. The “mariadb-upgrade” tool is designed for this purpose. It checks and updates the tables to align with the upgraded version’s requirements.

To start this process or to check the current status of your database tables, run:

sudo mariadb-upgrade

Running this command is advisable even if you believe the upgrade has been successful. The tool will confirm this in cases where it has already been executed. Otherwise, it will provide a detailed report on the tables examined and any necessary updates.

Remove MariaDB

To completely remove MariaDB from your system, use the following command:

sudo apt autoremove mariadb-server mariadb-client --purge -y

Be aware that using the “–purge” flag will delete not only MariaDB but also all associated database data. If you intend to retain the data, omit the “–purge” flag.

After removing MariaDB, it’s important also to remove the repository and GPG key to prevent any future updates from the MariaDB repository. This can be done with the following command:

sudo rm /etc/apt/sources.list.d/mariadb.list /usr/share/keyrings/mariadb.gpg

Conclusion

By installing MariaDB on Ubuntu via the official apt repository, you ensure that your database system is up-to-date with the latest builds, whether you choose a long-term release or opt for a short-term version. This method provides flexibility and security, allowing you to tailor your database environment to your specific requirements. Regular updates from the mariadb.org repository will keep your installation secure and optimized, providing a reliable foundation for your data-driven applications.

Useful Links

Here are some valuable links related to using MariaDB:

  • MariaDB Official Website: Visit the official MariaDB website for information about the database, features, and download options.
  • MariaDB Knowledge Base: Access the MariaDB Knowledge Base for comprehensive documentation, tutorials, and user guides.
  • MariaDB Foundation: Explore the MariaDB Foundation website for information about the MariaDB community, development, and the latest news.
  • MariaDB Foundation Documentation: Find detailed documentation on using and configuring MariaDB provided by the MariaDB Foundation.
  • MariaDB GitHub Repository: You can access the MariaDB GitHub repository to view the source code, report issues, and contribute to development.

Leave a Comment