MySQL MariaDB

How to Create MariaDB User

“MariaDB is an open-source, relational database management system (RDBMS). The MariaDB client allows you to add new users and grant them different degrees of privileges with ease.

To learn more about MariaDB, check this out:

https://mariadb.org/

Today, we will explore how to create a MariaDB user and grant them full privileges. Make sure you have a running instance of MariaDB where you have a user account with sudo privileges.”

Guide

Step 1: Access MariaDB Server
First, we need to access the MariaDB server client shell. To do that, execute the following command:

sudo mysql -u root

If you have set the password for your root account, run the following command and then enter the password to get access.

sudo mysql -u root -p

If you have not created any database yet, you can create one using the following command:

CREATE DATABASE 'yourDB';

To see the list of the existing databases, issue the following command:

SHOW DATABASES;

This will show you the database you just created.

Step 2: Create a New MariaDB User

In this step, we will create a new MariaDB user. To do that, run the following command:

CREATE USER 'DBuser'@localhost IDENTIFIED BY 'password123';

Make sure to enter your username and password in place of DBuser and password123.

You can check the status of the user by running the following command:

SELECT User FROM mysql.user;

The output will present you with a list of all the existing users.

Step 3: Grant Privileges to MariaDB User

The user you created doesn’t have privileges to interact with databases. We need to grant it all privileges with the help of the following command:

GRANT ALL PRIVILEGES ON *.* TO 'DBuser'@localhost IDENTIFIED BY 'password123';

If you wish to grant privileges only to your database, run this command:

GRANT ALL PRIVILEGES ON 'yourDB'.* TO 'DBuser'@localhost;

Refresh the privileges with this command:

FLUSH PRIVILEGES;

To verify the right permissions you just granted to the user, run this command:

SHOW GRANTS FOR 'DBuser'@localhost;

Step 4: Remove MariaDB User Account

In case you want to remove a user, you can use the following command:

DROP USER 'user1'@localhost;

After running this command, your user will no longer have access to the database.

Conclusion

In today’s guide, we saw in detail how to create a MariaDB user and grant them full user privileges. We explored multiple MariaDB commands to interact with the database. In the end, we also had a look at how to remove a user.

We hope you liked the tutorial.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.