How to create a user and password in MySQL?
The following statement can be used in MySQL to create a user and password:
- Create a user:
- Add a new user with the username “username” at the localhost.
- 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’.
- Set the user password:
- Assign a password for the user ‘username’ on the localhost using the given password.
- This will set the password for the user with the username ‘username’ to ‘password’.
- Granting authority:
- Give ‘username’ full access to all privileges on the database named database_name when accessing from localhost.
- 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 ‘.*’.
- If you only want to grant specific permissions, you can use the following command:
- Give permissions for selecting, inserting, updating, and deleting data in the specified database to the user ‘username’ when they are accessing from ‘localhost’.
- This will only grant the user SELECT, INSERT, UPDATE, and DELETE permissions for the database named ‘database_name’.
- Refresh authorization.
- Refresh privileges.
- 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.