Skip to content

Instantly share code, notes, and snippets.

@dsamarin
Last active October 29, 2019 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsamarin/ec79149407d5877534e4cf0ff0e2461f to your computer and use it in GitHub Desktop.
Save dsamarin/ec79149407d5877534e4cf0ff0e2461f to your computer and use it in GitHub Desktop.
MySQL new database, user, grant permissions cheatsheet
-- =========
-- = Users =
-- =========
-- Create new user
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
-- Delete user
DROP USER 'username'@'localhost';
-- List users
SELECT CONCAT('\'', User, '\'@\'', Host, '\'') AS user FROM mysql.user;
-- =============
-- = Databases =
-- =============
-- Create new utf8 database
CREATE DATABASE database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Drop database
DROP DATABASE database;
-- List databases
SHOW DATABASES;
-- ==========
-- = Grants =
-- ==========
-- Grant permissions
GRANT ALL PRIVILEGES ON database.* TO 'username'@'localhost';
GRANT SELECT ON database.* TO 'username'@'localhost'; -- Read only
GRANT SELECT, INSERT, UPDATE, DELETE ON database.* TO 'username'@'localhost'; -- CRUD
-- Revoke permissions
REVOKE ALL PRIVILEGES ON database.* FROM 'username'@'localhost';
-- Show permissions
SHOW GRANTS FOR 'username'@'localhost';
-- Flush permissions
FLUSH PRIVILEGES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment