How to create a new MySQL (MariaDB) Database, user, and grant it privileges

To create a new database table and user by the command line, you have to enter this command:

mysql -u root -p

(The -u root = login as root user. The -p part = enter a password (it'll prompt you for the password - it isn't a good idea to type passwords directly into a command))

You should now be in the mysql prompt.

Let's pretend we want to set up the following:

  • Database name new_database
  • New user new_username
  • And the password for the new user pAs5w0rd

At the mysql prompt enter the following 4 sql statements.

Remember to add a ; at the end of every sql statement.

CREATE USER  'new_username'@'localhost' IDENTIFIED BY 'pAs5w0rd';
CREATE DATABASE new_database;
GRANT ALL PRIVILEGES ON new_database.* TO 'new_username'@'localhost' ;
FLUSH PRIVILEGES;
exit;

And you should be all done!

Comments How to create a new MySQL (MariaDB) Database, user, and grant it privileges