How to Create a User and Grant Permissions in MySQL


How to create a user

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

How to delete a user

DROP USER 'myuser'@'localhost';

How to grant permissions

GRANT ALL PRIVILEGES ON * . * TO 'myuser'@'localhost';

How to apply those permissions

FLUSH PRIVILEGES;

Going a bit deeper

Different types of Grants

ALL PRIVILEGES : Full access to everything, globally unless restricted to a database

CREATE : Create databases and tables

DROP : Delete databases and tables

DELETE : Delete rows from tables

INSERT : Insert rows in tables

SELECT : Read data from tables

UPDATE : Update rows in tables

GRANT OPTION : Grant or remote other user’s permissions

How to use them

GRANT permission_type ON database_name.table_name TO 'myuser'@'localhost';

How to revoke them

REVOKE permission_type ON database_name.table_name FROM 'myuser'@'localhost';

How to show them

SHOW GRANTS myuser;