How to create a MySQL database and set privileges

If you don’t have a GUI interface for your database server – like phpMySQL for example – you can create MySQL/MariaDB users and databases from the command line.

1. Authenticate as the MySQL root/admin user

# mysql -u root -p

2. Create the desired database

mysql> create database DATABASE_NAME

3. Allow user access to the MySQL server

mysql> grant usage on *.* to USER_NAME@localhost identified by 'USER_PASSWORD';

4. Grant privileges to the user

mysql> grant all privileges on DATABASE_NAME.* to USER_NAME@localhost;

Example on a test MariaDB server:

[root@web ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4180008
Server version: 10.3.28-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database plothost_database1;
Query OK, 1 row affected (0.002 sec)

MariaDB [(none)]> grant usage on *.* to plothost@localhost identified by 'plothostpassword';
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> grant all privileges on plothost_database1.* to plothost@localhost;
Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> quit
Bye
[root@web ~]#



Related articles:
Top five software applications to access MySQL/MariaDB servers

Leave a Reply