Mysql: Create User

Quick note, create user in MySQL CLI.

Login into MySQL:

mysql -u root -p
#remote
mysql -u root -h <server_hostname> -p
CREATE DATABASE <database>;
CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON <database>.* TO '<username>'@'localhost';
FLUSH PRIVILEGES;
#show users
SELECT User FROM mysql.user;
#show tables
SHOW databases;

If you need to delete a user:

DROP USER '<username>'@'localhost';

To revoke permissions:

REVOKE <permission> ON <database>.<table> FROM '<username>'@'localhost';

Test new user:

#exit mysql console
\q

Login again with the new user:

mysql -u <username> -p

User Permissions

  • CREATE Create new tables or databases
  • DROP Delete tables or databases
  • DELETE Delete rows from tables
  • INSERT Insert rows into tables
  • SELECT Select command to read through databases
  • UPDATE Update table rows
  • GRANT OPTION Manage other users' privileges
  • ALL PRIVILEGES