MySQL Create User

To create a user in MySQL, you can use the CREATE USER statement. Here’s the basic syntax for creating a new user:

CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';

In this syntax, ‘username’ represents the name of the user you want to create, ‘localhost’ represents the host from which the user can connect to the MySQL server (in this case, it’s the same machine where the MySQL server is running), and ‘password’ represents the password for the user.

You can also specify a different host if you want to allow the user to connect from a different machine. For example, to allow the user to connect from any host, you can use ‘%’:

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

Once you’ve created the user, you can grant privileges to the user using the GRANT statement. For example, to grant all privileges on a specific database to the user, you can use the following syntax:

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'localhost';

This grants the user all privileges on all tables in the specified database.

Note that you need to have the necessary privileges to create a user and grant privileges. The MySQL root user has all privileges, so you can use that account to create new users and grant privileges. However, it’s generally a good practice to create a separate user with fewer privileges for everyday use.

Related Articles

Leave a Reply

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

Check Also
Back to top button