SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In MySQL, the CREATE USER
statement is used to create a new user account in the database server. By default, a new user doesn't have any privileges to do anything (like querying or updating tables). After creating a user, you typically need to explicitly grant permissions to this user.
CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';
username
: Name of the user you want to create.hostname
: Name of the host from which the user can connect to the MySQL server. To allow connection from any host, you can use the wildcard %
.password
: Password for the user.Creating a User:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password123';
This creates a user named 'newuser' who can only connect from 'localhost' and has the password 'password123'.
Allowing User to Connect from Any Host:
CREATE USER 'newuser'@'%' IDENTIFIED BY 'password123';
This allows 'newuser' to connect from any host.
After creating a user, you might want to grant some privileges to it. Here's an example:
GRANT SELECT, INSERT, UPDATE ON mydatabase.* TO 'newuser'@'localhost';
This grants SELECT
, INSERT
, and UPDATE
privileges on all tables of mydatabase
to 'newuser'@'localhost'.
After executing GRANT
, always execute the following to make sure the privileges are reloaded:
FLUSH PRIVILEGES;
CREATE USER
for account management features, like setting password expiration policies, account locking, and more.Creating a New User:
CREATE USER
to create a new MySQL user without any privileges.CREATE USER 'new_user'@'localhost';
Granting Privileges to a User with CREATE USER
in MySQL:
GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';
Managing MySQL Users using CREATE USER
Statement:
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';
Setting Password for a New User in MySQL:
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
Creating Users with Specific Host Access in MySQL:
CREATE USER 'new_user'@'specific_host' IDENTIFIED BY 'password';
MySQL CREATE USER
vs GRANT
Statement:
CREATE USER
to create a user, and GRANT
to assign privileges.CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';
Adding Multiple Users with CREATE USER
in MySQL:
CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1', 'user2'@'localhost' IDENTIFIED BY 'pass2';