Granting privileges lets you give users permission to do specific actions in the database. This keeps your data safe and organized.
Granting privileges in MySQL
GRANT privilege_type ON database_name.table_name TO 'username'@'host';
privilege_type can be SELECT, INSERT, UPDATE, DELETE, ALL PRIVILEGES, etc.
database_name.table_name specifies where the privilege applies. Use * for all databases or tables.
GRANT SELECT ON mydb.* TO 'alice'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'bob'@'%';
GRANT INSERT, UPDATE ON shop.products TO 'carol'@'192.168.1.10';
This creates a new user 'dave' with a password, grants him permission to read and add data in the 'orders' table of 'testdb', and then shows his privileges.
CREATE USER 'dave'@'localhost' IDENTIFIED BY 'password123'; GRANT SELECT, INSERT ON testdb.orders TO 'dave'@'localhost'; SHOW GRANTS FOR 'dave'@'localhost';
After granting privileges, sometimes you need to run FLUSH PRIVILEGES; to apply changes immediately.
Be careful granting ALL PRIVILEGES; it gives full control which can be risky.
Use specific privileges to follow the principle of least privilege for better security.
Granting privileges controls what users can do in the database.
You specify the user, the allowed actions, and where those actions apply.
Always grant only the permissions needed to keep data safe.
Practice
GRANT SELECT ON database.* TO 'user'@'host'; command do in MySQL?Solution
Step 1: Understand the GRANT command
The GRANT command is used to give specific permissions to a user on a database or table.Step 2: Analyze the SELECT privilege
SELECT privilege allows reading data but not modifying it.Final Answer:
Allows the user to read data from all tables in the specified database. -> Option AQuick Check:
GRANT SELECT = read permission [OK]
- Confusing SELECT with UPDATE or DELETE privileges
- Thinking GRANT creates a user
- Assuming it deletes data
shop database to user 'alice' connecting from any host?Solution
Step 1: Check the database and user format
The database must be followed by .* to specify all tables, and user must include host.Step 2: Verify the correct order of clauses
The syntax is: GRANT privileges ON database.* TO 'user'@'host';Final Answer:
GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; -> Option AQuick Check:
Correct syntax = GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; [OK]
- Omitting .* after database name
- Placing TO clause incorrectly
- Not specifying host with user
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost'; SHOW GRANTS FOR 'bob'@'localhost';
What will the
SHOW GRANTS output include?Solution
Step 1: Understand the GRANT command given
The user 'bob' is granted SELECT and INSERT privileges on all tables in the sales database.Step 2: Interpret SHOW GRANTS output
SHOW GRANTS displays the exact privileges granted, so it will list SELECT and INSERT on sales.* for bob.Final Answer:
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost' -> Option CQuick Check:
SHOW GRANTS matches granted privileges [OK]
- Assuming ALL PRIVILEGES are granted
- Ignoring INSERT privilege
- Thinking SHOW GRANTS returns no rows
GRANT UPDATE, DELETE ON * TO 'carol'@'localhost';
Solution
Step 1: Check the ON clause syntax
The ON clause requires a database or table name, e.g., database.* or database.table.Step 2: Identify the missing part
The command uses * alone, which is invalid syntax in MySQL for ON.Final Answer:
Missing database or table name after ON -> Option DQuick Check:
ON must specify database or table [OK]
- Using * alone without database name
- Using wrong quotes for user
- Assuming localhost is invalid host
'dave' to create new tables only in the inventory database but not modify data. Which GRANT command is correct?Solution
Step 1: Identify the needed privilege
To create tables, the CREATE privilege is required; INSERT or ALL PRIVILEGES would allow data changes.Step 2: Scope the privilege to the inventory database
Granting on inventory.* limits the permission to that database only.Step 3: Check user and host format
User 'dave'@'localhost' is correctly specified.Final Answer:
GRANT CREATE ON inventory.* TO 'dave'@'localhost'; -> Option BQuick Check:
CREATE only on inventory = GRANT CREATE ON inventory.* TO 'dave'@'localhost'; [OK]
- Granting ALL PRIVILEGES instead of just CREATE
- Including INSERT which allows data changes
- Granting on *.* instead of specific database
Ready to prove it?
Pass the test and this pattern moves unknown
