Integrated monitoring<\/strong> using microsecond precision and extended user statistics.<\/li>\n<\/ol>\n\n\n\nIn this guide, we will demonstrate how to install and secure MariaDB on RHEL 9.<\/p>\n\n\n\n
Step 1: Upgrade Software Packages<\/h2>\n\n\n\n To get started, log into your server as a sudo user via SSH. Next, upgrade all the packages and refresh the repositories as follows:<\/p>\n\n\n\n
$ sudo dnf update<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nThe MariaDB Server package is provided by the official AppStream repositories. You can confirm this by searching for the package on the repositories as shown:<\/p>\n\n\n\n
$ sudo dnf search mariadb-server<\/code><\/p>\n\n\n\nThe following output confirms that MariaDB is hosted on the default repositories.<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 2: Install MariaDB Server on RHEL 9<\/h2>\n\n\n\n The next step is to install the MariaDB Server. To do so, run the following command:<\/p>\n\n\n\n
$ sudo dnf install mariadb-server -y<\/code><\/p>\n\n\n\nThe command installs the MariaDB server alongside other dependencies and additional packages required by the database server.<\/p>\n\n\n\n <\/figure>\n\n\n\n <\/figure>\n\n\n\nOnce the installation is complete, confirm that MariaDB is installed using the following command:<\/p>\n\n\n\n
$ rpm -qi mariadb-server<\/code><\/p>\n\n\n\nRunning this command displays comprehensive details about the MariaDB Server package including the name, version, architecture, installation date, and installed size to name a few.<\/p>\n\n\n\n <\/figure>\n\n\n\nStep 3: Start and Enable MariaDB Server<\/h2>\n\n\n\n Up to this point, we have successfully installed the MariaDB Server. By default, the MariaDB service does not start automatically. As such you need to start it by running the following command:<\/p>\n\n\n\n
$ sudo systemctl start mariadb<\/code><\/p>\n\n\n\nIn addition, set it to start automatically on system startup.<\/p>\n\n\n\n
$ sudo systemctl enable mariadb<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nTo verify that MariaDB is up and running, run the command:<\/p>\n\n\n\n
$ sudo systemctl status mariadb<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nMariaDB listens on TCP port 3306. You can confirm this using the command:<\/p>\n\n\n\n
$ sudo ss -pnltu | grep mariadb<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nStep 4: Secure MariaDB Server<\/h2>\n\n\n\n The default settings for the MariaDB database server are considered weak and not robust in the face of a breach or intrusion. As such, you need to go an extra step and secure the database server. To do this, run the mysql_secure_installation<\/code> script as shown:<\/p>\n\n\n\n$ sudo mysql_secure_installation<\/code><\/p>\n\n\n\nRunning the script will present you with a series of prompts.<\/p>\n\n\n\n
First, you will be required to provide the root password. Next, switch to unix_socket authentication<\/strong> which allows the user to use operating system credentials when connecting to the MariaDB database server.<\/p>\n\n\n\nYou can then decide to change the root user or let it remain exactly the way it is.<\/p>\n\n\n\n <\/figure>\n\n\n\nFor the remaining prompts, press \u201cY\u201d in order to secure MariaDB to the recommended standards. This does the following:<\/p>\n\n\n\n
\nRemoves anonymous users from the database server.<\/strong> This prevents the risk of having anyone log into MariaDB without having a user account.<\/li>\n\n\n\nDisallows remote root login.<\/strong> This ensures that only the root user is allowed to connect from \u2018localhost\u2019 or the server on which MariaDB is installed. This prevents brute-force attacks using the root user password.<\/li>\n\n\n\nRemoves a test database<\/strong> called test which can be accessed by anyone and is only used for testing. Its removal is recommended before transitioning to a producing environment.<\/li>\n\n\n\nReloads the privilege tables.<\/strong> Hence, saves all the changes made.<\/li>\n<\/ol>\n\n\n\n <\/figure>\n\n\n\nMariaDB is now secured using the recommended security standards after installation.<\/p>\n\n\n\n
Step 5: Log Into MariaDB Server<\/h2>\n\n\n\n To log in to the MariaDB database server, run the command:<\/p>\n\n\n\n
$ sudo mysql -u root -p<\/code><\/p>\n\n\n\nProvide the root password for MariaDB and press ENTER. This ushers you to the MariaDB shell.<\/p>\n\n\n\n <\/figure>\n\n\n\nTo check the version of MariaDB installed, run the command:<\/p>\n\n\n\n
SELECT VERSION();<\/code><\/p>\n\n\n\nFrom the output, you can see that we are running MariaDB 10.5.16.<\/p>\n\n\n\n <\/figure>\n\n\n\nTo list all the databases, run the command:<\/p>\n\n\n\n
SHOW DATABASES;<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nStep 6: Create Database and Database User (Optional)<\/h2>\n\n\n\n This step illustrates how to create a database and a database user.<\/p>\n\n\n\n
To create a database in the MariaDB Server, run the following command where test_db <\/code>is the database name:<\/p>\n\n\n\nCREATE DATABASE test_db;<\/code><\/p>\n\n\n\nNext, create a database user on the system with a password. Here, test_user <\/code>is the name of the database user and P@ssword321@ <\/code>is the user\u2019s password. Be sure to provide a stronger password for your user.<\/p>\n\n\n\nCREATE USER ‘test_user’@’localhost’ IDENTIFIED BY ‘Password321@’;<\/code><\/p>\n\n\n\nNext, grant privileges to the database user on the database. This determines the rights that the user has on the database, e.g., ALTER, CREATE, DELETE, DROP, SELECT, UPDATE, etc. This command will grant user rights to the database.<\/p>\n\n\n\n
GRANT ALL ON test_db.* TO ‘test_user’@’localhost’ WITH GRANT OPTION;<\/code><\/p>\n\n\n\nLastly, reload the grant tables in order to save the changes made as follows:<\/p>\n\n\n\n
FLUSH PRIVILEGES<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nTo confirm the creation of the database, again, run the following SQL query:<\/p>\n\n\n\n
SHOW DATABASES;<\/code><\/p>\n\n\n\nThis time around, an additional database named test_db <\/code>appears on the list.<\/p>\n\n\n\n <\/figure>\n\n\n\nTo view a list of all the users in the database server, run the following query:<\/p>\n\n\n\n
SELECT User, Host FROM mysql.user;<\/code><\/p>\n\n\n\n <\/figure>\n\n\n\nConclusion<\/h2>\n\n\n\n In this guide, you learned how to install and secure the MariaDB database server on RHEL 9. For more information about MariaDB, check out the official documentation<\/a>.<\/p>\n\n\n\nLooking for more ways to secure your Linux servers and devices? Learn how to improve Linux security posture<\/a> with JumpCloud’s MDM policies. <\/p>\n\n\n\n\n