MariaDB is a powerful, open-source relational database management system (RDBMS) that is a popular drop-in replacement for MySQL. Known for its performance, robust feature set, and strong community support, MariaDB is an excellent choice for a wide range of applications, from web development to enterprise-level data management. This Tutorial will guide you through the process of installing MariaDB on your Ubuntu 24.04 server, ensuring a secure and functional database environment.
Before you begin, it’s always a good practice to update your system’s package list and upgrade any existing packages to their latest versions. This ensures you have access to the most recent software and security patches.
First, open your terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
Install MariaDB Server
Ubuntu 24.04 repositories include the MariaDB server package, making the installation straightforward. To install MariaDB, execute the following command:
sudo apt install mariadb-server -y
This command will download and install the MariaDB server and its dependencies. Once the installation is complete, the MariaDB service will start automatically.
Verify MariaDB Service Status
To confirm that the MariaDB service is running correctly, you can check its status using systemctl:
sudo systemctl status mariadb
You should see output indicating that the service is ‘active (running)’. Press q to exit the status view.
Secure MariaDB Installation
Immediately after installation, it’s crucial to secure your MariaDB server. MariaDB provides a script specifically for this purpose, which helps set the root password, remove anonymous users, disallow remote root login, and remove the test database.
Run the security script with the following command:
sudo mysql_secure_installation
You will be prompted to answer a series of questions:
- Enter current password for root (enter for none): Press
Enteras there’s no password set initially. - Set root password? [Y/n]: Type
Yand pressEnter. Then, enter a strong password for the MariaDB root user and confirm it. - Remove anonymous users? [Y/n]: Type
Yand pressEnter. - Disallow root login remotely? [Y/n]: Type
Yand pressEnter. This is recommended for security, especially if your database is not intended for remote administration. - Remove test database and access to it? [Y/n]: Type
Yand pressEnter. - Reload privilege tables now? [Y/n]: Type
Yand pressEnter.
After completing these steps, your MariaDB installation will be significantly more secure.
Access MariaDB Shell
You can now log into the MariaDB shell using the root user and the password you just set:
sudo mariadb -u root -p
When prompted, enter the root password you configured during the security script. Once logged in, you’ll see the MariaDB prompt. You can exit the shell by typing exit; and pressing Enter.
Create a New Database and User (Optional)
For most applications, it’s better to create a dedicated database and user with specific privileges instead of using the root user. Here’s how you can do it:
Log in to the MariaDB shell as root:
sudo mariadb -u root -p
Then, execute the following SQL commands. Replace your_database_name, your_username, and your_password with your desired values:
CREATE DATABASE your_database_name;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Now you can log in with the new user:
mariadb -u your_username -p
Conclusion
You have successfully installed and secured MariaDB on your Ubuntu 24.04 server. You now have a robust and reliable database system ready to power your applications. Remember to regularly back up your databases and keep your system updated to maintain optimal performance and security.
