How is the grant command used in MySQL?
The GRANT command in MySQL is used to give specific permissions to users or roles. Its syntax is as follows:
Grant permissions on a specific database table to a user at a specific host by providing their credentials.
Privileges are the permissions that need to be granted, and they can be specific permission keywords (such as SELECT, INSERT, UPDATE, etc.), or ALL PRIVILEGES representing all permissions.
In the syntax database.table, you specify the database and table that you want to grant authorization to. This can be a specific database and table name, or you can use wildcard characters (such as * to represent all databases or tables).
user@host represents the user and hostname to be authorized, it can be a specific username and hostname, or wildcards can be used (such as ‘%’ representing all hosts).
‘IDENTIFIED BY ‘password’ is an optional setting used to establish a user’s password.
The GRANT command also has some other options and parameters, such as WITH GRANT OPTION, which allows a user to grant permissions that they have to other users.
For example, to grant user ‘john’ SELECT and INSERT permissions on table ‘table1’ in database ‘mydb’ and specify the password as ‘password123’, you can use the following command:
Give ‘john’@’localhost’ permission to select and insert data into the table1 of mydb using the password ‘password123’.
Note: In order to use the GRANT command, you need to have sufficient privileges, usually requiring the GRANT OPTION privilege or SUPER privilege.