Skip to main content

Commands

Command syntax will vary between MySQL Versions

====================================================================================

User Management

Show all users and hosts
SELECT user, host FROM mysql. user;

-------------------------------------------------------------------------------------------------------

Add User
CREATE USER 'name'@'localhost' IDENTIFIED BY 'password.';

-------------------------------------------------------------------------------------------------------

Granting Privileges

Available privileges:

GRANT

CREATE,ALTER,DROP,INSERT,
CREATE Allows UPDATE,the DELETE,user SELECT,to REFERENCES,create new databases and tables.
ALTERAllows the user to alter (modify) existing tables.
DROPAllows the user to drop (delete) databases and tables.
INSERTAllows the user to insert new rows into tables.
UPDATEAllows the user to update existing rows in tables.
DELETEAllows the user to delete rows from tables.
SELECTAllows the user to read (select) data from tables.
REFERENCESAllows the user to create foreign key constraints when defining tables.
RELOAD

Allows the user to execute the FLUSH statement, which reloads various server configurations and clears or refreshes caches.

Grant specific privilege ( or multiple comma separated):

GRANT PRIVILEGEPRIVILEGETYPE ON database.table TO 'username'@'host';
FLUSH PRIVILEGES;

or

Grant all:

GRANT ALL ON database.table TO 'username'@'host';
FLUSH PRIVILEGES;

------------------------------------------------------------------------------------------------------------------------------------------------

Delete a user
DROP USER 'name'@'host';

------------------------------------------------------------------------------------------------------------------------------------------------

Set user password

Change user password

ALTER USER 'user'@'host' IDENTIFIED BY 'NewPassword';
FLUSH PRIVILEGES;


====================================================================================

Database Management

Create a database
CREATE DATABASE name;

------------------------------------------------------------------------------------------------------------------------------------------------

Delete a database
DROP DATABASE name;

====================================================================================