Mysql create new user
To create a new user in MySQL, you can use the following command:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
Here:
username
is the name of the new user.host
is the host from which the user can connect to the database. For example,%
means any host,localhost
means only from the local machine, and192.168.1.100
means only from the machine with IP address192.168.1.100
.password
is the password for the new user.
For example, to create a new user named john
who can connect from any host with the password mypassword
, you would use the following command:
CREATE USER 'john'@'%' IDENTIFIED BY 'mypassword';
You can also specify additional options when creating a new user, such as:
-
GRANT
privileges to the user, for example:CREATE USER 'john'@'%' IDENTIFIED BY 'mypassword' GRANT SELECT, INSERT, UPDATE, DELETE ON *.* TO 'john'@'%';
This grants the user
john
the privileges to select, insert, update, and delete data on all tables in all databases. -
REQUIRE
a specific SSL/TLS certificate or key, for example:CREATE USER 'john'@'%' IDENTIFIED BY 'mypassword' REQUIRE SSL;
This requires the user
john
to use an SSL/TLS connection to connect to the database. -
WITH
a specific role or privilege, for example:CREATE USER 'john'@'%' IDENTIFIED BY 'mypassword' WITH MAX_QUERIES_PER_HOUR 0;
This sets the maximum number of queries per hour for the user
john
to 0.
You can also use the CREATE USER
statement with the INFILE
clause to create a new user from a file, for example:
CREATE USER 'john'@'%' IDENTIFIED BY 'mypassword' INFILE '/path/to/file.txt';
This creates a new user john
with the password mypassword
and grants the privileges specified in the file /path/to/file.txt
.
Note that you need to have the CREATE USER
privilege to create a new user in MySQL.