MariaDB server is one of the most popular and open-source relational database servers in the world. It is backward-compatible and a replacement for MySQL. In this guide, you will learn how to install the MariaDB server on Ubuntu 20.04 or later.
First of all, you need to log in as a sudo user. Then, use the following command to install the MariaDB server:
> sudo apt install mariadb-server
Next, you can use the following command to check if the MariaDB service is enabled at system startup.
> sudo systemctl status mariadb
If it is not enabled at system startup, then you can enable it using the command:
> sudo systemctl enable mariadb
Also, you can start the service with this command:
> sudo systemctl start mariadb
Securing MariaDB Server
After installation, to secure it, you can run the following command:
> sudo mysql_secure_installation
Or, you may use this command (instead of underscores, there are dashes):
> sudo mariadb-secure-installation
Note that, “mariadb-secure-installation” is a symlink to “mysql_secure_installation” from MariaDB 10.4.6. This means, they would work the same and you can use any one of them. The command “sudo mariadb-secure-installation” would work for version 10.4.6 or later.
In order to log into MariaDB to secure it, you’ll need to enter the current password for the root user. If you’ve just installed the MariaDB server, and you haven’t set the root password yet, the password will be blank, so you can just press “Enter” here.
Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorization. You already have a root password set, so you can safely answer 'n' or you can press 'y' to change it. Change the root password? [Y/n] n ... skipping.
By default, the installation has an anonymous user which allows anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing. You can remove them before moving into a production environment.
Remove anonymous users? [Y/n] y ... Success!
Normally, the root should only be allowed to connect from “localhost”. This ensures that someone cannot guess 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 going into a production environment.
Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success!
Now, reload the privilege tables so, the changes can take place their 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.
Creating Database & Assign Users
In order to log into the MariaDB server to run SQL commands, you can run the command:
> sudo mariadb
Starting from version 11.0, the command “sudo mysql” would show a warning of “deprecated program name”. So, you can simply use “sudo mariadb” instead. Also, you can replace “mysql” with “mariadb” in most of the deprecated commands.
If the above command doesn’t work, then you can run this command instead:
> sudo mariadb -u root -p
Here, you will need to enter the MySQL or MariaDB root password. Once, you are logged into MariaDB, you can run the following command to create a database with a character set (optionally).
> mariadb> CREATE DATABASE exampledb DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Here, the database name is “exampledb”. Now, to show all the databases, you can run this command:
> mariadb> show databases;
To create a new database user and grant all permissions of this database (“exampledb”) to that user, you can use the command:
> mariadb> GRANT ALL PRIVILEGES ON exampledb.* TO "exampledbuser"@"localhost" IDENTIFIED BY "your_password";
Here, “exampledbuser” is the new user, and the password for that user is “your_password”. Also, you can simply assign an existing user “testdbuser” to this new database “exampledb” using the following command:
> mariadb> GRANT ALL PRIVILEGES ON exampledb.* TO "testdbuser"@"localhost";
Now, you will need to flush the privileges for changes to take effect.
> mariadb> FLUSH PRIVILEGES;
Finally, you can exit from MariaDB mode using the “exit” command:
> mariadb> exit;
Benefits of MariaDB Database Server
Here are some great benefits of using MariaDB as your database server.
- Improved caching and performance: It comes with enhanced caching mechanisms, resulting in faster query execution and overall better performance.
- Tighter security measures: It provides internal security and password checks, PAM and LDAP authentication, user roles, and encryption, ensuring better data protection.
- More storage engines: It offers a wider range of storage engines, including NoSQL support with Cassandra, allowing you to run SQL and NoSQL in a single database.
- Galera cluster integration: It includes built-in support for the Galera cluster, providing synchronous multi-master replication for high availability and scalability.
- Replication enhancements: It offers replication features like parallel replication, multi-source replication, and delayed replication, improving data redundancy and availability.
- Open-source and free: You can use it for free and benefit from ongoing community contributions and updates, ensuring continuous improvement of the database system.
- Monitoring capabilities: It offers advanced monitoring tools and features, allowing you to efficiently monitor and optimize the performance of your database.
In conclusion, the MariaDB server is a widely-used open-source relational database server and a popular replacement for MySQL. By following the steps outlined in this guide, you can easily install it and start building robust and scalable applications.