How to create new mysql user

To create a new MySQL user, you can use the following steps:

Using the MySQL Command-Line Client

  1. 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 and p with the password.

  2. 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), and password with the desired password.

  3. 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.

  4. To make the changes permanent, use the following command:

    FLUSH PRIVILEGES;

Using the MySQL Workbench

  1. Open the MySQL Workbench and connect to your MySQL server.
  2. Click on the "Users" tab in the left-hand menu.
  3. Click on the "Create User" button.
  4. Enter the desired username, password, and hostname or IP address of the machine that the user will be connecting from.
  5. Click on the "Apply" button to create the new user.
  6. To grant privileges to the new user, click on the "Privileges" tab and select the desired privileges.
  7. Click on the "Apply" button to apply the changes.

Using a SQL Script

  1. 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;
  2. Run the SQL script using the following command:
    mysql -u root -p < create_user.sql

    Replace root with the root username and p 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.