MySQL Tutorial

MySQL Installation and Configuration

MySQL Database Operations

Database Design

MySQL Data Types

MySQL Storage Engines

MySQL Basic Operations of Tables

MySQL Constraints

MySQL Operators

MySQL Function

MySQL Manipulate Table Data

MySQL View

MySQL Indexes

MySQL Stored Procedure

MySQL Trigger

MySQL Transactions

MySQL Character Set

MySQL User Management

MySQL Database Backup and Recovery

MySQL Log

MySQL Performance Optimization

MySQL create user Tutorial

To create a new user in MySQL, you use the CREATE USER statement. Here are the steps to create a new user:

Step 1: Login to MySQL

First, you need to login to MySQL using the mysql command in your terminal or command prompt:

mysql -u root -p

You will be prompted to enter the password for the root user.

Step 2: Create a New User

Once you are logged in, you can create a new user with the CREATE USER command:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';

Replace 'newuser' with the username for the new account, and replace 'password' with the password for the new account.

Note:

  • The localhost part after the @ symbol means that this user will be able to connect to the MySQL server only from the localhost (i.e., the same machine the MySQL server is installed on). If you want to allow the user to connect from any host, use the '%' wildcard character: 'newuser'@'%'.

  • It's recommended to always specify a password for a new account. For passwordless accounts, you can omit the IDENTIFIED BY 'password' part, but this is generally not recommended due to security reasons.

Step 3: Grant Privileges

By default, the new user has no permissions to do anything with the databases. In fact, the new user can't even login to MySQL. Therefore, you need to grant the user permissions. Here is an example of how to grant all privileges on a database:

GRANT ALL PRIVILEGES ON database_name.* TO 'newuser'@'localhost';

Replace database_name with the name of your database.

Step 4: Flush Privileges

Whenever you change the grant tables, it's a good idea to tell the server to reload them:

FLUSH PRIVILEGES;

Step 5: Exit MySQL

You can exit the MySQL shell by typing:

EXIT;

Remember:

Always ensure that your MySQL server is secure. Only grant the necessary permissions that a user needs to perform their tasks. Don't share passwords and always use strong, unique passwords for your user accounts.

  1. Creating a new user in MySQL:

    • Description: Creating a new user involves defining a username and associated password for authentication purposes.
    • Code:
      CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
      
  2. How to add a user in MySQL:

    • Description: Adding a user is synonymous with creating a new user account in MySQL.
    • Code:
      CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
      
  3. MySQL user creation command:

    • Description: The command to create a new user in MySQL, specifying the username and authentication details.
    • Code:
      CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
      
  4. Granting privileges to a user in MySQL:

    • Description: After creating a user, you need to grant specific privileges to define what actions the user can perform.
    • Code:
      GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost';
      FLUSH PRIVILEGES;
      
  5. Setting up user accounts in MySQL:

    • Description: The overall process of creating user accounts in MySQL, including setting passwords and granting privileges.
    • Code:
      CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
      GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost';
      FLUSH PRIVILEGES;
      
  6. MySQL add user and grant privileges:

    • Description: Combining the user creation and privilege granting steps into a single operation.
    • Code:
      CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
      GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost';
      FLUSH PRIVILEGES;
      
  7. Creating MySQL users and assigning roles:

    • Description: In addition to privileges, you can also assign roles to users for more structured access control.
    • Code:
      CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
      GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost';
      GRANT role_name TO 'newuser'@'localhost';
      FLUSH PRIVILEGES;