MySQL - create database, add user, grant permissions

This guide will explain how to create a database in MySQL, add a new user to MySQL, and grant permissions on that database for the new user.

Login to MySQL

mysql -u root -p

You will be prompted for your password. If you did not set a MySQL password, you can omit the -p option.

Create the database

Execute the following command in your terminal and press enter:

create database myDatabase;

Add the new user to MySQL

CREATE USER 'theNewUser'@'localhost' IDENTIFIED BY 'password';

Grant database permissions to the new user

GRANT ALL PRIVILEGES ON myDatabase.* TO 'theNewUser'@'localhost';

Should you wish to grant permissions for this user to every database in MySQL, you could modify the above command as follows:

GRANT ALL PRIVILEGES ON . TO 'theNewUser'@'localhost';

This is generally not recommended, as anyone with this user’s credentials will have full read/write access to every database in MySQL.