How to Create a New MySQL/MariaDB Database User

In this article, I am going to show you how to create a new MySQL/MariaDB database, create a new MySQL/MariaDB user, and grant the new user access to the newly created database.

Connect to the Database

To access the MySQL/MariaDB server as root user, run the command below. When prompted, type in your MySQL root password and press <Enter>.

sudo mysql -u root -p

Create a new MySQL/MariaDB Database

To create a new database magento_dev (let’s say), run the following SQL statement:

mysql> CREATE DATABASE magento_dev;

If you want to create a MySQL/MariaDB database that uses a specific character set and collation, check this article.

Create a New MySQL/MariaDB User

To create a new MySQL/MariaDB database user magento_dev and set the password ohf3zieN, run the following SQL statement:

mysql> CREATE USER 'magento_dev'@'localhost' IDENTIFIED BY 'ohf3zieN';

If you want to allow remote database access to the magento_dev user, run the following SQL statement instead:

mysql> CREATE USER 'magento_dev'@'%' IDENTIFIED BY 'ohf3zieN';


Allow MySQL/MariaDB User Access to Database

To grant the user magento_dev all type of privileges/access to the database magento_dev, run the following SQL statement:

 mysql> GRANT ALL PRIVILEGES ON magento_dev.* TO 'magento_dev'@'localhost';

If you’re created a remote database user, run the following SQL statement instead:

mysql> GRANT ALL PRIVILEGES ON magento_dev.* TO 'magento_dev'@'%';

Reload MySQL/MariaDB Privileges

After granting the magento_dev user access to the magento_dev database, you must reload the MySQL/MariaDB privileges to apply the changes.

To reload the MySQL/MariaDB privileges, run the following SQL statement:

mysql> FLUSH PRIVILEGES;

References

  1. MySQL Reference Manual – Creating and Selecting a Database
  2. MySQL Reference Manual – CREATE USER Statement
  3. MySQL Reference Manual – GRANT Statement
  4. MySQL Reference Manual – FLUSH Statement