How to create a user in the MySQL command line?
To create a user in MySQL command line, you can follow these steps:
- Open the MySQL command line client.
- Use the following command to create a user:
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
In this case, ‘username’ is the username to be created, ‘localhost’ is the hostname for accessing the MySQL server, and ‘password’ is the user’s password. If you want to allow users to access the server from any host, you can replace ‘localhost’ with ‘%’.
- Use the following command to grant users access to a specific database:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';
‘database_name’ is the name of the database that needs to be granted access.
- Refresh permissions to make changes take effect.
FLUSH PRIVILEGES;
Congratulations, you have now successfully created a new MySQL user. This user can access the MySQL server and authorized databases using their username and password.