How can a user be created and granted permissions in MySQL?
To create a user and grant permissions in MySQL, follow these steps:
- Open the MySQL command line interface or use a MySQL client tool, and log in to the MySQL server as the root user.
- To create a new user, use the following command:
- Create a user with the username ‘username’ on the localhost server and set their password to ‘password’.
- In this case, ‘username’ is the name to be created, ‘localhost’ is the hostname or IP address allowed for connection, and ‘password’ is the user’s password.
- Grant users appropriate permissions. You can use the following command to grant a user all permissions on all databases:
- Give ‘username’@’localhost’ all permissions on all databases.
- Alternatively, you can specify the specific database and permissions, for example:
- Authorize the user ‘username’ at localhost to have permission to select, insert, update, and delete data on the database named database_name.
- In this case, ‘database_name’ is the specific name of the database, and ‘SELECT, INSERT, UPDATE, DELETE’ are the permissions to be granted.
- Refresh permissions to make the changes take effect.
- Update user privileges;
Note: In practical applications, it is important to allocate appropriate permissions based on need rather than giving users excessive permissions.