MySQL create database and user

The topic is already well-described in the official documentation. Below you can find just yet another example. Assuming a connection to MySQL database is established, we will start with creating a new database named "wortschatz":

 create database wortschatz;

You can make sure if the database is created by using command

 show databases;

Now we can create a new user. Let's name the user "some_user" and create a password "some_password":

 create user 'some_user'@'%' identified by 'some_password';

Please note, @ character is followed by host name surrounded with quotation marks. In our example it is % character --this is wildcard  which means that user can from any host.

The last step is to grant permissions to the new user. Let's allow the user to run only SELECT queries:

 grant select on wortschatz.* TO 'some_user'@'%';

To sum up: we created a new database, added a user with a password and granted the user permissions to run SELECT queries on the create database. 

Comments