How to create new mysql user
To create a new MySQL user, you can use the following steps:
Using the MySQL Command-Line Client
-
Open a terminal or command prompt and connect to your MySQL server using the following command:
mysql -u root -p
Replace
root
with the root username andp
with the password. -
Once connected, use the following command to create a new user:
CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';
Replace
newuser
with the desired username,@'%'
with the hostname or IP address of the machine that the user will be connecting from (e.g.@'localhost'
for local connections only), andpassword
with the desired password. -
To grant privileges to the new user, use the following command:
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%';
This grants the user all privileges on all databases.
-
To make the changes permanent, use the following command:
FLUSH PRIVILEGES;
Using the MySQL Workbench
- Open the MySQL Workbench and connect to your MySQL server.
- Click on the "Users" tab in the left-hand menu.
- Click on the "Create User" button.
- Enter the desired username, password, and hostname or IP address of the machine that the user will be connecting from.
- Click on the "Apply" button to create the new user.
- To grant privileges to the new user, click on the "Privileges" tab and select the desired privileges.
- Click on the "Apply" button to apply the changes.
Using a SQL Script
- Create a new SQL file (e.g.
create_user.sql
) with the following contents:CREATE USER 'newuser'@'%' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'%'; FLUSH PRIVILEGES;
- Run the SQL script using the following command:
mysql -u root -p < create_user.sql
Replace
root
with the root username andp
with the password.
Note: Make sure to replace newuser
and password
with the desired values for your new user. Also, be careful when granting privileges to ensure that the user only has the necessary permissions to perform their tasks.