Skip to content

Instantly share code, notes, and snippets.

@jaygaha
Created May 19, 2023 08:48
Show Gist options
  • Save jaygaha/03642ddbae2142f312986cab69d09a08 to your computer and use it in GitHub Desktop.
Save jaygaha/03642ddbae2142f312986cab69d09a08 to your computer and use it in GitHub Desktop.
How To Create a New User and Grant Permissions in MySQL8

How To Create a New User and Grant Permissions in MySQL 8

Use the grant permissions command.

If your database name is "newdb" and user is "newuser" the command to grant all privileges on all the tables contained within would be:

Create A New User

mysql> CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
#if user needs to grant access from the remote machine
mysql> CREATE USER 'newuser'@'8.8.8.8' IDENTIFIED BY 'password';

Grant Privileges

mysql> GRANT ALL PRIVILEGES ON newdb.* TO 'newuser'@'localhost';
#if selective permissions need to grant
mysql> GRANT SELECT, INSERT, DELETE ON newdb.* TO newuser@'localhost';

Show Granted Privileges

mysql> SHOW GRANTS FOR 'newuser'@'localhost';

Refresh Privileges

mysql> FLUSH PRIVILEGES;

Drop User

mysql> DROP USER 'newuser'@'localhost';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment