How to create a user and password in MySQL?

The following statement can be used in MySQL to create a user and password:

  1. Create a user:
  2. Add a new user with the username “username” at the localhost.
  3. This will create a user with the username ‘username’ that can only login from the local host (localhost). If you want to allow login from any host, you can use ‘%’ instead of ‘localhost’.
  4. Set the user password:
  5. Assign a password for the user ‘username’ on the localhost using the given password.
  6. This will set the password for the user with the username ‘username’ to ‘password’.
  7. Granting authority:
  8. Give ‘username’ full access to all privileges on the database named database_name when accessing from localhost.
  9. This will grant the user all permissions on the database called ‘database_name’. If you want to grant the user permissions on all databases, you can change ‘database_name.’ to ‘.*’.
  10. If you only want to grant specific permissions, you can use the following command:
  11. Give permissions for selecting, inserting, updating, and deleting data in the specified database to the user ‘username’ when they are accessing from ‘localhost’.
  12. This will only grant the user SELECT, INSERT, UPDATE, and DELETE permissions for the database named ‘database_name’.
  13. Refresh authorization.
  14. Refresh privileges.
  15. After creating, changing permissions, or deleting a user, you should execute this statement to refresh MySQL’s permission cache.

Note: The ‘username’ and ‘password’ in the above command should be replaced with the actual username and password.

Leave a Reply 0

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