MariaDB and MySQL are relational database management systems (RDBMS) used to store and manage structured data. MariaDB is a fork of MySQL and is designed to be compatible, allowing easy migration between the two.
Both MySQL and MariaDB can be installed on Ubuntu, one of the most widely used Linux distributions. Using apt, the installation process for both systems is similar, making deployment straightforward.
This guide provides the steps to install either MySQL or MariaDB on Ubuntu. The process covers installing, securing, and configuring the database server for immediate use.
Steps to install MariaDB or MySQL on Ubuntu:
- Launch the terminal.
- Update the apt package list.
$ sudo apt update [sudo] password for user: Hit:1 http://ports.ubuntu.com/ubuntu-ports kinetic-security InRelease Hit:2 http://us.ports.ubuntu.com/ubuntu-ports kinetic InRelease Hit:3 http://us.ports.ubuntu.com/ubuntu-ports kinetic-updates InRelease Hit:4 http://us.ports.ubuntu.com/ubuntu-ports kinetic-backports InRelease Reading package lists... Done Building dependency tree... Done Reading state information... Done
- Install mariadb-server or mysql-server package using apt.
$ sudo apt install mariadb-server Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libaio1 libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7a libevent-pthreads-2.1-7 libfcgi-bin libfcgi-perl libfcgi0ldbl libhtml-template-perl libmecab2 libprotobuf-lite23 mecab-ipadic mecab-ipadic-utf8 mecab-utils mariadb-client mariadb-common mariadb-server-core 0 upgraded, 20 newly installed, 0 to remove and 10 not upgraded. Need to get 25 MB of archives. After this operation, 240 MB of additional disk space will be used.
Replace the package name with mysql-server to install MySQL instead.
- Ensure the service is started automatically.
The installed service will start automatically and is enabled on boot.
Related: How to manage MySQL or MariaDB service in Linux - Set the password for the root user.
$ sudo mysql_secure_installation Securing the MariaDB server deployment. Enter current password for root (enter for none): Set root password? [Y/n]: Y New password: Re-enter new password:
If no password is set, you may encounter errors in subsequent steps.
- Run the secure installation script.
$ sudo mysql_secure_installation
This script improves the security of your installation by removing insecure defaults.
- Enable password validation.
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. Would you like to setup VALIDATE PASSWORD component? Press Y for Yes, any other key for No: Y
- Select the password validation policy level.
There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters, and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1
- Keep the current root password or change it if necessary.
Estimated strength of the password: 50 Change the password for root? (Press Y for Yes, any other key for No): N
- Remove the anonymous users.
Remove anonymous users? (Press Y for Yes, any other key for No): Y
Removing anonymous users enhances security by preventing unauthorized access.
- Restrict root user login to the local system.
Disallow root login remotely? (Press Y for Yes, any other key for No): Y
- Delete the test database.
Remove test database and access to it? (Press Y for Yes, any other key for No): Y
The test database is only for testing purposes and should be removed for production environments.
- Reload the privilege tables.
Reload privilege tables now? (Press Y for Yes, any other key for No): Y
- Test the connection by logging in as the root user.
$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 27 Server version: 8.0.32-0ubuntu0.22.10.2 (Ubuntu) Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
This guide is tested on Ubuntu:
Version | Code Name |
---|---|
22.04 LTS | Jammy Jellyfish |
23.10 | Mantic Minotaur |
24.04 LTS | Noble Numbat |

Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
Comment anonymously. Login not required.