For example, you want to create a user named knowhow
with password secret
and the user will access your database from the same host. Query to create this user is next:
CREATE USER 'knowhow'@'localhost' IDENTIFIED BY 'secret';
If you want this user to access your database from the remote host - you need to replace localhost
with the IP address of your remote host. For example, your backend server has IP 123.11.12.13
, the query will be next:
CREATE USER 'knowhow'@'123.11.12.13' IDENTIFIED BY 'secret';
The next step is granting privileges to a table or whole database. To allow access to all tables use the next query:
GRANT ALL PRIVILEGES ON *.* TO 'knowhow'@'localhost';
Note that it needs to specify host here too, rules are the same as for creating user.
Also, you can grant access only to only one database, blog
for example:
GRANT ALL PRIVILEGES ON `blog`.* TO 'knowhow'@'localhost';
You can grant access even only to one table:
GRANT ALL PRIVILEGES ON `blog`.`posts` TO 'knowhow'@'localhost';
After all, you need to apply changes using the next query:
FLUSH PRIVILEGES;