SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

MySQL | CREATE USER Statement

In MySQL, the CREATE USER statement is used to create a new user account in the database server. By default, a new user doesn't have any privileges to do anything (like querying or updating tables). After creating a user, you typically need to explicitly grant permissions to this user.

Syntax:

CREATE USER 'username'@'hostname' IDENTIFIED BY 'password';
  • username: Name of the user you want to create.
  • hostname: Name of the host from which the user can connect to the MySQL server. To allow connection from any host, you can use the wildcard %.
  • password: Password for the user.

Examples:

  1. Creating a User:

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

    This creates a user named 'newuser' who can only connect from 'localhost' and has the password 'password123'.

  2. Allowing User to Connect from Any Host:

    CREATE USER 'newuser'@'%' IDENTIFIED BY 'password123';
    

    This allows 'newuser' to connect from any host.

Granting Privileges:

After creating a user, you might want to grant some privileges to it. Here's an example:

GRANT SELECT, INSERT, UPDATE ON mydatabase.* TO 'newuser'@'localhost';

This grants SELECT, INSERT, and UPDATE privileges on all tables of mydatabase to 'newuser'@'localhost'.

After executing GRANT, always execute the following to make sure the privileges are reloaded:

FLUSH PRIVILEGES;

Notes:

  1. Always be cautious while creating users and granting privileges. Only provide the necessary privileges that the user requires.
  2. Always choose strong passwords for new users to ensure database security.
  3. Remember to manage users properly by revoking unnecessary privileges, updating passwords regularly, and deleting users who no longer require access.
  4. In newer versions of MySQL (>= 5.7.6), it's possible to use more options with CREATE USER for account management features, like setting password expiration policies, account locking, and more.
  1. Creating a New User:

    • Use CREATE USER to create a new MySQL user without any privileges.
    CREATE USER 'new_user'@'localhost';
    
  2. Granting Privileges to a User with CREATE USER in MySQL:

    • Grant specific privileges to the user.
    GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';
    
  3. Managing MySQL Users using CREATE USER Statement:

    • Create a user and set privileges in a single statement.
    CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
    GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';
    
  4. Setting Password for a New User in MySQL:

    • Set a password for the new user.
    CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
    
  5. Creating Users with Specific Host Access in MySQL:

    • Specify the host from which the user can connect.
    CREATE USER 'new_user'@'specific_host' IDENTIFIED BY 'password';
    
  6. MySQL CREATE USER vs GRANT Statement:

    • Use CREATE USER to create a user, and GRANT to assign privileges.
    CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';
    GRANT SELECT, INSERT ON database_name.* TO 'new_user'@'localhost';
    
  7. Adding Multiple Users with CREATE USER in MySQL:

    • Create multiple users in a single statement.
    CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1',
                'user2'@'localhost' IDENTIFIED BY 'pass2';