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

FLUSH PRIVILEGES;

exit;

And you should be all done!

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

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

webdevetc profile pic
webdevetc

I'm a late 20's web developer, specialising in Laravel, but work with other PHP frameworks and associated technologies (such as JS). I mostly write about PHP here. Contact me here. Need to hire or work with a contract PHP software developer in London, UK (or freelance)? Contact me and check my availability.

Leave a Comment