How to create a new user and database in MySQL?
In MySQL, you can create a new user and database by following these steps:
- 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.
- Log in to the MySQL server using the administrator account. Typically, the administrator account is root and requires entering the corresponding password.
- Create a new database. You can use the following command to create a database named mydatabase:
CREATE DATABASE mydatabase;
- my username
- – 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.
- The user belongs to me.
- 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.
- 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.