Create a New User and Grant Permissions in MySQL
log in with this command in terminal:
mysql -u [username] -p
making a new user within the MySQL shell
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Therefore, the first thing to do is to provide the user with access to the information they will need.
GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
FLUSH PRIVILEGES;
How To Grant Different User Permissions
Here is a short list of other common possible permissions that users can enjoy. ALL PRIVILEGES- as we saw previously, this would allow a MySQL user full access to a designated database (or if no database is selected, global access across the system) CREATE- allows them to create new tables or databases DROP- allows them to them to delete tables or databases DELETE- allows them to delete rows from tables INSERT- allows them to insert rows into tables SELECT- allows them to use the SELECT command to read through databases UPDATE- allow them to update table rows GRANT OPTION- allows them to grant or remove other users’ privileges To provide a specific user with a permission, you can use this framework:
To GRANT ALL
privileges to a user, allowing that user full control over a specific database, use the following syntax:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
GRANT type_of_permission ON database_name.table_name TO 'username'@'localhost';
revoke a permission, the structure is almost identical to granting it:
REVOKE type_of_permission ON database_name.table_name FROM 'username'@'localhost';
review a user’s current permissions by running the following:
SHOW GRANTS FOR 'username'@'localhost';
delete databases with DROP, you can use DROP to delete a user altogether:
DROP USER 'username'@'localhost';
log out by typing:
quit
by გიორგი ბაკაშვილი
4 years ago
Linux
MySQL
2
Pro tip: use ```triple backticks around text``` to write in code fences