How to install MariaDB on CentOS

Reference Server

In this tutorial we will learn to install MariaDB on CentOS server.

What is MariaDB?

MariaDB is a community-developed fork of the MySQL relational database management system (RDBMS) intended to remain free under the GNU GPL.
mariadb.com
mariadb.org

So, lets get started with the installation process. Open terminal and login to your server as root user.

Step 1: Clean up and update yum

Its a best practice to first clean up and update yum and then perform installation.

Clean up yum by typing the following in terminal.

# sudo yum clean all

Now perform the update

# sudo yum -y update

Once the update is done we are ready to install mariadb.

Step 2: Install MariaDB

To install MariaDB use the following command.

# sudo yum install mariadb-server mariadb

Step 3: Start MariaDB

After installing MariaDB we have to start it. Use the following command.

# sudo systemctl start mariadb.service

Note!

To stop MariaDB use the following command.

# sudo systemctl stop mariadb.service

To make MariaDB start at boot use the following command.

# sudo systemctl enable mariadb.service

Step 4: Set password MariaDB

Use the following command to create a new login password.

# sudo /usr/bin/mysqladmin -u root password 'your-new-password-for-root'

Step 5: Login to MySQL Shell and create database

Use the following command to login to MySQL Shell.

# mysql -u root -p
Enter password: "TYPE IN ROOT USER PASSWORD"

mysql>

Once you have successfully logged in its time to create a database. For this use the following SQL command.

mysql> CREATE DATABASE mydb;

In the above example mydb is the name of the database created.

To check the created databases type the following SQL command.

mysql> SHOW DATABASES;

And you will get a similar output.


+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
+--------------------+
3 rows in set (0.00 sec)

Step 6: Create a new User for the database

You can create a new user that can later be used by your application to access the database.

To create a new user type in the following command.

mysql> CREATE USER 'mydbuser'@'localhost' IDENTIFIED BY 'mydbuserpassword';

A new user mydbuser is created having password mydbuserpassword.

Step 7: Grant Privileges to the user

Use the following command to grant privileges to the the new user created in the above step.

mysql> GRANT ALL PRIVILEGES ON mydb.* to mydbuser@localhost;

Now use the following command to make the privileges take effect.

mysql> FLUSH PRIVILEGES;

Note!

Use the following command to check the grant of a user.

mysql> SHOW GRANTS FOR 'mydbuser'@'localhost';
+--------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for mydbuser@localhost                                                                                                              |
+--------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'mydbuser'@'localhost' IDENTIFIED BY PASSWORD '*8F767905A3234ABC20EBACD9112E4AAB2A9227C9' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'mydbuser'@'localhost' WITH GRANT OPTION                                                                           |
+--------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

To revoke (or take back) grants from a user use the following command.

mysql> REVOKE ALL ON mydb.* FROM mydbuser@localhost;

Conclusion

Congrats! you have installed MariaDB and created a new user. Have fun coding!