This guide walks you through installing and securing MariaDB on Ubuntu 20.04.
A reliable, open-source relational database system that can be used in place of MySQL is called MariaDB. Because of its improved stability, performance, and compatibility with MySQL SQL syntax, it is ideal for production database management.
Prerequisites
- Ubuntu 20.04 server
- SSH access with a non-root user having administrative privileges.
- Updated system packages (sudo apt update && sudo apt upgrade -y)
Step 1: Install MariaDB
You can install MariaDB straight from the default APT repositories in Ubuntu 20.04. Once MariaDB is installed, you can verify that it is operating properly on your server by checking the system service.
- Update the package index:
sudo apt update
- Install the MariaDB server:
sudo apt install mariadb-server -y
- Verify the installation:
mariadb --version
Step 2: Manage the MariaDB Service
After installing MariaDB, you can control the service using “systemd” commands to start, stop, restart, or check the status, ensuring it’s running correctly on your server.
- Enable MariaDB to start at boot:
sudo systemctl enable mariadb
- Start the MariaDB service:
sudo systemctl start mariadb
- Check the service status:
sudo systemctl status mariadb
Step 3: Secure the MariaDB Installation
MariaDB employs the “root” account by default, which is first password-free and restricts access to elevated privilege system users. To increase security, it is suggested to remove anonymous users, create a root password, and restrict remote access for the root account.
- Launch the MariaDB Security Script
Run the command below to begin the security configuration:sudo mysql_secure_installation
- Follow the on-screen prompts:
- Press Enter to leave the current root password empty.
- Type N to skip setting a root password at this stage.
- Type Y to remove anonymous users.
- Type Y to disable remote root login.
- Type Y to remove the test database.
- Type Y to reload the privilege tables.
- Log in to MariaDB as root:
sudo mysql
- Set a strong root password
Inside the MariaDB shell, run: ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_strong_password';
- Exit the MariaDB shell
EXIT;
- Restart the MariaDB service
sudo systemctl restart mariadb
Step 4: Access and Use MariaDB
- Log in to the MariaDB shell:
mariadb -u root -p
- Create a new database:
CREATE DATABASE exampledb;
- List databases:
SHOW DATABASES;
- Create a new user and grant privileges:
CREATE USER ‘example_user’@‘localhost’ IDENTIFIED BY ‘secure_password’; GRANT ALL PRIVILEGES ON exampledb.* TO ‘example_user’@‘localhost’; FLUSH PRIVILEGES; EXIT;
This way, MariaDB is now installed and secured on your Ubuntu 20.04 server. You can begin using it for development or production environments, either through the command line or GUI tools like phpMyAdmin.
Want to monitor your MariaDB more effectively? Learn How to Enable or Configure the Error Log in MySQL® or MariaDB