MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
MySQL's privilege system ensures that all users may perform only the operations allowed to them. As a general rule, you should grant only the privileges that are necessary for a given user to perform their duties.
Here is a brief tutorial on controlling privileges in MySQL:
1. Login to MySQL Server:
First, you need to login to MySQL using the mysql
command in your terminal or command prompt:
mysql -u root -p
You will be prompted to enter the password for the root
user.
2. Create User:
To create a new user, you use the CREATE USER
statement:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
3. Grant Privileges:
To grant privileges to a user, you use the GRANT
statement. For example, to grant all privileges on a database:
GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';
Or to grant specific privileges:
GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'newuser'@'localhost';
4. View User's Privileges:
To see what privileges a user has, you can use the SHOW GRANTS
statement:
SHOW GRANTS FOR 'newuser'@'localhost';
5. Revoke Privileges:
If you want to remove privileges from a user, you can use the REVOKE
statement. For example:
REVOKE SELECT, INSERT ON database_name.* FROM 'newuser'@'localhost';
6. Delete User:
To delete a user, use the DROP USER
statement:
DROP USER 'newuser'@'localhost';
7. Flush Privileges:
Whenever you change the grant tables, it's a good idea to tell the server to reload them:
FLUSH PRIVILEGES;
8. Exit MySQL:
You can exit the MySQL shell by typing:
EXIT;
Remember:
Always ensure that your MySQL server is secure. Only grant the necessary permissions that a user needs to perform their tasks. Don't share passwords and always use strong, unique passwords for your user accounts. Be careful when granting privileges or deleting users, as these actions can have significant effects on database security and functionality.
Granting and revoking privileges in MySQL:
GRANT privilege_type ON database_name.table_name TO 'username'@'hostname'; FLUSH PRIVILEGES;
REVOKE privilege_type ON database_name.table_name FROM 'username'@'hostname'; FLUSH PRIVILEGES;
Setting up privileges with MySQL:
GRANT privilege_type ON database_name.table_name TO 'username'@'hostname'; FLUSH PRIVILEGES;
Granting specific privileges in MySQL:
GRANT SELECT, INSERT, UPDATE ON database_name.table_name TO 'username'@'hostname'; FLUSH PRIVILEGES;
MySQL GRANT and REVOKE commands:
GRANT
command is used to give privileges to users, while the REVOKE
command removes those privileges.GRANT privilege_type ON database_name.table_name TO 'username'@'hostname'; FLUSH PRIVILEGES;
REVOKE privilege_type ON database_name.table_name FROM 'username'@'hostname'; FLUSH PRIVILEGES;