The CREATE USER statement in MySQL is used to create a new user account that can connect to the MySQL server.
It is part of DCL (Data Control Language) and is usually followed by GRANT statements to provide permissions.
? Secure user management command
'john'@'localhost') used to connect to MySQL.localhost or % (any host).CREATE USER, GRANT, REVOKE).
-- Syntax for creating a user in MySQL
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
localhost or % for any host).
-- Create a user 'john' who can connect only from localhost
CREATE USER 'john'@'localhost' IDENTIFIED BY 'john123';
root account.CREATE USER 'john'@'localhost' IDENTIFIED BY 'john123';
mysql.user).'john'@'localhost' can now attempt to log in with password john123.So CREATE USER only creates the login identity. To actually work with databases, you must use GRANT afterwards.
'username'@'%', but avoid this in production unless really needed.GRANT command.Imagine you have a database called EmployeeDB. Perform the following tasks:
employee_user with a secure password.
-- Create a new user for EmployeeDB
CREATE USER 'employee_user'@'localhost' IDENTIFIED BY 'StrongP@ssw0rd!';
EmployeeDB.
-- Give full access on EmployeeDB to employee_user
GRANT ALL PRIVILEGES ON EmployeeDB.* TO 'employee_user'@'localhost';
-- Reload privilege tables (older MySQL versions)
FLUSH PRIVILEGES;
-- Check all users and their hosts
SELECT User, Host FROM mysql.user;