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
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.
Creating a new user in MySQL:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
How to add a user in MySQL:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
MySQL user creation command:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
Granting privileges to a user in MySQL:
GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost'; FLUSH PRIVILEGES;
Setting up user accounts in MySQL:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost'; FLUSH PRIVILEGES;
MySQL add user and grant privileges:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost'; FLUSH PRIVILEGES;
Creating MySQL users and assigning roles:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'; GRANT SELECT, INSERT, UPDATE ON database.* TO 'newuser'@'localhost'; GRANT role_name TO 'newuser'@'localhost'; FLUSH PRIVILEGES;