MySQL User Administration - Creating new users, logging in with them , and dropping users.

Learn how to create a new user and explore multiple methods to connect to mysql with that user. Additionally, discover the steps to delete a user when no loner needed.


Let's look at the steps to create a MySQL user:

Step 1: First let's check what options are available when creating a user.

    help create user;


Step 2: To create user execute this command:

    create user if not exists 'user_name' identified by 'password' password expire never account unlock;


Step 3: Now let's grant the permissions to the user. In this case we are granting all the privileges to the user.

    grant all privileges on *.* to 'junior' with grant option;

Step 4: Flush the privileges.

    flush privileges;

    (To reload the privileges, ensuring any changes made to the account take effect immediately)

Step 5: To confirm whether all privileges are granted:

    show grants for user_name;


Now the question arises: how do we connect to mysql with the new user which we have just created through the terminal?

We can do this by executing the following query:

    mysql --host=<your-ip-address> --user=user_name --password



We can also connect to MySQL with the help of MySQL Shell.

    mysqlsh --host=<your-ip-address> --user=user_name --password


Make sure you have mysql-shell installed on your system.


Now let's look at how to delete a user.


First let's check which users are present on the mysql server. We can do so by using the default mysql database.

    select host,user from user;



Now let's drop a user, lets say john.

    drop user 'john';

Note: You can only drop one user if you have the privilege to drop a user.

To confirm whether the user has been dropped, let's execute the same command:

    select host,user from user;



Great! We have successfully deleted a user.



Great! We have successfully created a user, learned different ways to connect to MySQL with with the new user, and explored how to drop the users when needed. This covers the essential aspects of MySQL user administration.🎉






Comments

Popular posts from this blog

MySQL Configuration File - Updating and changing the default location of my.cnf

MySQL config editor , Executing MySQL commands from the terminal, mysqlimport and mysqlcheck