How to create a new user and database in MySQL?

In MySQL, you can create a new user and database by following these steps:

  1. Open a command-line tool (such as MySQL Shell, MySQL Command Line, etc.) or use a graphical interface tool (such as phpMyAdmin) to connect to the MySQL database server.
  2. Log in to the MySQL server using the administrator account. Typically, the administrator account is root and requires entering the corresponding password.
  3. Create a new database. You can use the following command to create a database named mydatabase:
CREATE DATABASE mydatabase;
  1. my username
  2. – the password for my account
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

In the command above, myuser is the username to be created, localhost indicates that the user can only access the database through a local connection, and mypassword is the password for this user.

  1. The user belongs to me.
  2. The database that belongs to me
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';

In the above command, “mydatabase.*” means that the user “myuser” has permission to access all tables and perform all operations in the “mydatabase” database.

  1. Refresh permissions. You can use the following command to refresh the MySQL permissions settings, so that new users and permissions take effect immediately:
FLUSH PRIVILEGES;

After completing the above steps, a new MySQL user and database have been successfully created.

bannerAds