How To Create a New User?

We have seen how to connect to the mysql server and create a new schema, now we will see how we can create a new user and grant permissions to that user.

Creating user using the MySQL workbench:

  1. Open the MySQL workbench and log in with the root user.
  2. Go to Server → Users and Privileges option.
  1. We will get the window as shown in the following image. Click on Add Account button as shown in highlighted part.
  1. We will get the window as follows. From the Login tab, we can add the user name in the Login Name option, Authentication type as Standard, host as localhost (if any other machine host then gives that), and, password.
  1. The second tab is for limiting the particular operations for that user. 0 means no limit. We can add any limit based on the requirements. For example, if we want to limit the user connection to 5 then we can add Max. connections as 5.
  1. In the Administrative Roles tab, we can select which privileges the user can have. If we don’t want to give all privileges to the user then we can add custom privileges by selecting required privileges from the Global privileges section as shown in the following image.
  1. We can also select the schema privileges for that users as follows. Selecting All Schemas will give access to the user for all database schemas available on that server.
  1. Once we select the option on the above image and click on Ok, we will get the following options where % will be added as schema and we can select the rights of the user on that schema from the Object Rights section.
  1. If nothing is selected then all objects will be selected by default.
  2. Once every configuration is done for that user click on Apply. The user will get created successfully once we click on apply.

Log in to the database with the new user using the workbench.

  1. Go to the home and click on add connection as shown below.
  2. Add the connect name. (We can give any name since this will be for our reference)
  3. Give the username as the user we created earlier which is ugt1.
  4. Click on Test connection and provide the same password added earlier for that user. Also, tick the checkbox to save the password in a vault so that no need to add the password again and again.
  1. Once the password is added, click on Ok. For a successful connection, we will get the success window popup as shown in the below image.
  1. Once the connection is successful click on Ok, and we will get the new option added for that user as shown in the below image. Double-click on that and we can able to connect to the database and perform operations like select/insert.

Delete the user by using the workbench

  1. For deleting the user, go to the same Users and Privileges option and select the user that needs to be deleted.
  2. Note that you have to log in with the root user or user who has the privilege to delete the user.
  3. Once we select the user we have the ‘Delete’ option as shown in the below image.

Creating user using the MySQL toolkit:

  1. Go to the command window and log in to the root user by using the following command.
mysql -u root -p
  1. The MySQL toolkit will be opened as shown in the following image.
  1. Once the toolkit is opened run the following command to create a user.
CREATE USER 'ugt2'@'localhost' IDENTIFIED BY 'Welcome@123';
  1. ‘ugt2’ is the user name, ‘localhost’ is the host for which we are creating the user and ‘Welcome@123’ is the password. We can change these things accordingly based on our requirements.
  1. Once the user is created we can check the privileges he has by using the following command.
SHOW GRANTS FOR 'ugt2'@'localhost';
  1. We can change the privileges of that user according to the requirements. To give all rights to the user we can run the following command.
GRANT ALL PRIVILEGES ON *.* TO 'ugt2'@'localhost' WITH GRANT OPTION;
  1. From the following image, we can see the all privileges given to the user.
  1. The Following command will revoke all the privileges from the user.
REVOKE ALL PRIVILEGES ON *.* FROM 'ugt2'@'localhost';
  1. We can also give custom privileges to the user by using the command as follows.
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'ugt2'@'localhost' WITH GRANT OPTION;
  1. We can add privilege options separated by a comma and grant those privileges to the user.
  1. If we want to Revoke any particular privilege option we can use the following command
REVOKE RELOAD ON *.* FROM 'ugt2'@'localhost';
  1. As shown in the following image, before revoking, the user has RELOAD privilege access, but after revoking, this option is removed from the privileges.

Log in to the database with the new user using the toolkit.

  1. To log in with the newly created user, we can exit from the toolkit using ‘exit;’ and re-login with the user using the following command.
mysql -u ugt2 -p
  1. Here ‘ugt2’ is the user name. Once we use the command and enter it will ask for the password. Give the same password which was provided while creating the user.
  2. Once we are created successfully we can do the select/ insert operations based on the user privileges.

Delete the user by using the toolkit

  1. To delete the user from the server we can use the following command.
DROP USER 'ugt2'@'localhost';

-A blog by Shwetali Khambe

Related Posts