MariaDB is a powerful, open-source relational database management system that serves as a popular drop-in replacement for MySQL. Known for its robust performance, enhanced security features, and active community development, 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 and securing MariaDB on your Ubuntu 24.04 server.
Before you begin, ensure you have an Ubuntu 24.04 server with a non-root user configured with `sudo` privileges and access to the internet. While MariaDB is highly compatible with MySQL, it offers several improvements and features that make it a compelling alternative. Let’s dive into the installation process.
Step 1: Update System Packages
It’s always a good practice to update your system’s package list and upgrade existing packages to their latest versions before installing new software. This ensures you have the most current security patches and dependencies.
“`bash
sudo apt update
sudo apt upgrade -y
“`
Step 2: Install MariaDB Server
Ubuntu 24.04 includes MariaDB in its default repositories, making the installation straightforward. Use the `apt` package manager to install the MariaDB server and client packages.
“`bash
sudo apt install mariadb-server mariadb-client -y
“`
Once the installation is complete, the MariaDB service will automatically start. You can verify its status using the following command:
“`bash
sudo systemctl status mariadb
“`
You should see output indicating that the service is `active (running)`.
Step 3: Secure MariaDB Installation
After installation, it’s crucial to secure your MariaDB server. MariaDB provides a security script that helps you improve the security of your installation by setting the root password, removing anonymous users, disallowing remote root login, and removing the test database.
“`bash
sudo mysql_secure_installation
“`
The script will prompt you with several questions:
* **Enter current password for root (enter for none):** Press Enter, as there’s no password set by default for the root user.
* **Set root password? [Y/n]:** Type `Y` and press Enter, then enter a strong password twice.
* **Remove anonymous users? [Y/n]:** Type `Y` and press Enter.
* **Disallow root login remotely? [Y/n]:** Type `Y` and press Enter (recommended for most setups).
* **Remove test database and access to it? [Y/n]:** Type `Y` and press Enter.
* **Reload privilege tables now? [Y/n]:** Type `Y` and press Enter.
Step 4: Manage MariaDB Service
You can manage the MariaDB service using `systemctl` commands:
* **To start MariaDB:** `sudo systemctl start mariadb`
* **To stop MariaDB:** `sudo systemctl stop mariadb`
* **To restart MariaDB:** `sudo systemctl restart mariadb`
* **To enable MariaDB to start on boot:** `sudo systemctl enable mariadb`
* **To disable MariaDB from starting on boot:** `sudo systemctl disable mariadb`
Step 5: Connect to MariaDB
After securing your installation, you can connect to the MariaDB server using the `mysql` client as the root user with the password you set during the security script.
“`bash
mysql -u root -p
“`
Enter the root password when prompted. You will then be presented with the MariaDB monitor prompt, indicating a successful connection.
“`sql
MariaDB [(none)]>
“`
To exit the MariaDB monitor, type `exit;` and press Enter.
(Optional) Create a New User and Database
For enhanced security and better practice, it’s advisable to create a dedicated user and database for your applications instead of using the root user. Here’s how you can do it:
First, connect to MariaDB as the root user:
“`bash
mysql -u root -p
“`
Then, execute the following SQL commands. Replace `your_database_name`, `your_username`, and `your_password` with your desired values:
“`sql
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 connect to MariaDB using your new user and database:
“`bash
mysql -u your_username -p -D your_database_name
“`
Enter your new user’s password when prompted.
Congratulations! You have successfully installed, secured, and configured MariaDB on your Ubuntu 24.04 server. You now have a robust database system ready to power your applications. Remember to keep your server and MariaDB installation updated for optimal performance and security.
