How can a user be created and granted permissions in MySQL?

To create a user and grant permissions in MySQL, follow these steps:

  1. Open the MySQL command line interface or use a MySQL client tool, and log in to the MySQL server as the root user.
  2. To create a new user, use the following command:
  3. Create a user with the username ‘username’ on the localhost server and set their password to ‘password’.
  4. 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.
  5. Grant users appropriate permissions. You can use the following command to grant a user all permissions on all databases:
  6. Give ‘username’@’localhost’ all permissions on all databases.
  7. Alternatively, you can specify the specific database and permissions, for example:
  8. Authorize the user ‘username’ at localhost to have permission to select, insert, update, and delete data on the database named database_name.
  9. In this case, ‘database_name’ is the specific name of the database, and ‘SELECT, INSERT, UPDATE, DELETE’ are the permissions to be granted.
  10. Refresh permissions to make the changes take effect.
  11. Update user privileges;

Note: In practical applications, it is important to allocate appropriate permissions based on need rather than giving users excessive permissions.

Leave a Reply 0

Your email address will not be published. Required fields are marked *