To create a new user and grant permissions in MySQL, you can use the following steps:
- Connect to the MySQL server as the root user:
mysql -u root -p
- Create a new user with a password by using the following command:
CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
- Grant the new user specific permissions on a specific database using the GRANT command. For example, to grant all privileges on the database named "mydb":
GRANT ALL PRIVILEGES ON mydb.* TO 'newuser'@'localhost';
- To grant all privileges on all databases
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
- To flush the privileges, so the server reloads the grant tables and put new privileges into effect.
FLUSH PRIVILEGES;
- Exit MySQL:
exit;
It is important to note that these commands must be executed as the MySQL root user, and the user will only have the granted privileges when connecting from localhost.