To create a new user in MySQL, you can follow these steps:
Open your terminal and connect to your MySQL server with an account that has administrative privileges (like root
):
mysql -u root -p
Enter your root password when prompted.
Use the CREATE USER
statement with the following syntax:
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
'%'
instead.For example:
CREATE USER 'johndoe'@'localhost' IDENTIFIED BY 'SecureP@ssw0rd';
After creating the user, grant the necessary privileges. For instance, to allow the user to select, insert, update, and delete on a specific database, use:
GRANT SELECT, INSERT, UPDATE, DELETE ON your_database.* TO 'johndoe'@'localhost';
If you need to grant all privileges on a database:
GRANT ALL PRIVILEGES ON your_database.* TO 'johndoe'@'localhost';