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

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

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' ;



And you should be all done!

Subscribe to my spam free newsletter for other Laravel and Vue updates like this

I never spam, and only email when I have a good in-depth post published on my site (mostly about Laravel and Vue). You can also follow me on social media to get updates.

webdevetc profile pic

I've been working as a software developer for many years (with the last few years specialising in Laravel and Vue). I mostly write about PHP and JavaScript/TypeScript on this site. Contact me here. Need to hire a contract software developer in London, UK (or freelance)? Contact me and check my availability.

Leave a Comment