[MySQL] How to create a new database, database user and grant privileges to a user

| | 1 min read

This article lists some of the basic MySQL commands how to create a new database, a new database user and granting privileges to a user.

How to create a new database from terminal.

The first step here is to log in as the MySQL root from terminal using the following command.

mysql -u root -p 

Now this will prompt for the MySQL root password.

Enter password : 

After entering the MySQL root password we will be able to log in to the MySQL prompt.

The next step is to create the new database.

 CREATE DATABASE database-name; 

To create a new database user

Create user 'new-username'@'localhost' identified by 'password-for-new-account';

To grant privileges for that user follow:

GRANT ALL PRIVILEGES ON ''database-name''.* TO "''username''"@"''host-name''" IDENTIFIED BY "''password''"; 

To flush the privileges:

FLUSH PRIVILEGES;