MySql User Role Privileges Reference
🗂️ MySQL User, Role, and Privilege Management Reference
Section titled “🗂️ MySQL User, Role, and Privilege Management Reference”This guide introduces the syntax and purpose of MySQL statements used to create and manage databases, users, roles, and privileges.
It provides general explanations — not specific examples from any exercise.
🧱 1. Creating and Using Databases
Section titled “🧱 1. Creating and Using Databases”Create a database
Section titled “Create a database”CREATE DATABASE database_name;Creates a new database. The name must be unique on the server.
Select a database to work with
Section titled “Select a database to work with”USE database_name;Switches the active database context to database_name.
🧩 2. Creating Tables
Section titled “🧩 2. Creating Tables”Create a table
Section titled “Create a table”CREATE TABLE table_name ( column_name data_type [constraints], ...);Defines a new table structure within the current database.
data_typespecifies what kind of data is stored (e.g.,INT,VARCHAR(100),DATE).constraintscan define rules likePRIMARY KEY,NOT NULL, orFOREIGN KEY.
🧮 3. Inserting Data
Section titled “🧮 3. Inserting Data”Insert rows into a table
Section titled “Insert rows into a table”INSERT INTO table_name (column1, column2, ...)VALUES (value1, value2, ...);Adds one or more rows to a table.
Multiple rows can be inserted by separating sets of values with commas.
👥 4. Managing Users
Section titled “👥 4. Managing Users”Create a user
Section titled “Create a user”CREATE USER 'username'@'host' IDENTIFIED BY 'password';Creates a new MySQL account.
'host'defines where the user can connect from ('localhost'or'%'for any host).'password'sets the user’s authentication password.
View existing users
Section titled “View existing users”SELECT User, Host FROM mysql.user;🧑🤝🧑 5. Managing Roles
Section titled “🧑🤝🧑 5. Managing Roles”Create a role
Section titled “Create a role”CREATE ROLE role_name;Creates a named role, which can hold privileges that can later be assigned to users.
Grant a role to a user
Section titled “Grant a role to a user”GRANT role_name TO 'username'@'host';Assigns one or more existing roles to a user.
Set default roles
Section titled “Set default roles”SET DEFAULT ROLE ALL TO 'username'@'host';Ensures that the granted roles are active automatically upon login.
🔐 6. Granting Privileges
Section titled “🔐 6. Granting Privileges”Grant privileges to a user or role
Section titled “Grant privileges to a user or role”GRANT privilege_type ON database_name.table_name TO 'username'@'host';Allows a user or role to perform certain actions.
Common privileges include:
SELECT— Read data from tablesINSERT— Add dataUPDATE— Modify existing dataDELETE— Remove dataALL PRIVILEGES— Grants all available permissions on the object
You can grant privileges on:
- A specific table
- An entire database
- Or even all databases (using
*.*)
🚫 7. Revoking Privileges
Section titled “🚫 7. Revoking Privileges”Revoke privileges
Section titled “Revoke privileges”REVOKE privilege_type ON database_name.table_name FROM 'username'@'host';Removes specific privileges from a user or role.
🧾 8. Viewing Granted Privileges
Section titled “🧾 8. Viewing Granted Privileges”Show privileges of a user or role
Section titled “Show privileges of a user or role”SHOW GRANTS FOR 'username'@'host';Displays all privileges and roles granted to a user or role.
🧹 9. Dropping Users, Roles, or Databases
Section titled “🧹 9. Dropping Users, Roles, or Databases”Drop a user
Section titled “Drop a user”DROP USER 'username'@'host';Drop a role
Section titled “Drop a role”DROP ROLE role_name;Drop a database
Section titled “Drop a database”DROP DATABASE database_name;Removes the specified object permanently. Use with caution!
⚙️ 10. Additional Tips
Section titled “⚙️ 10. Additional Tips”- Always use least privilege principle: grant only the access a user or application truly needs.
- Use roles to manage permissions for groups of users efficiently.
- Regularly check privileges with
SHOW GRANTSto maintain secure configurations.