Introduction to MySQL Users
The following article provides an outline for MySQL Users. MySQL user is a row in the table nameduser in the database of the MySQL server which gives us the information for attributes like login, account permissions, and for MySQL user account. It is crucial to make a user in table for various purposes like in accessing and management of the databases. To create new users in the database we use create user statement. It is used to provide validation to check the resource-limit, user’s role, and also in password management of the new user.
It also controls the initially blocked or unblocked accounts. It is mandatory to have a overall permissions to create user in the table or to have the insert permissions for the schema, if create user statement is used. If we try to create a user that is already existing in the table it gives us a technical error message however, if we add if not exists clause in the create user statement, it will give us a warning message for each existing user in place of an error message.
Syntax to create user:
Create user [if not exists] account_name identified by 'password';
The account_name in the create statement has two portions. Both the part is separated by @. The part before @ is the name given to the new user and the part after @ is the hostname through which the created user will get connected with the server which in my case is my localhost.
The hostname in the account_name is optional. It means the user will get connected to any host from the various options present on the server. Account name lacking hostname can also be written as [email protected]%.
Example of Create User Statement
Given below is the example mentioned:
Steps to create user in MySQL server:
Step 1: Login into the MySQL server from command line as root user.
mysql -u root –p
Where ,’u’ is the root username and ‘p’ is the root password.
Step 2: Check for all existing user in the present working server.
SELECT user from mysql.user;
Where, MySQL is the name of database and user is the name of table.
We can also write above statement as:
Step 1:
use mysql;
Step 2:
select user from user;
Output of list of users:
Step 3: Create a new user.
CREATE USER [email protected] IDENTIFIED BY ‘abc123’;
Step 4: Now, we will use the IF NOT EXISTS clause with the CREATE USER statement.
CREATE USER IF NOT EXISTS [email protected] IDENTIFIED BY ‘abc123’;
List of Privileges provided by Server
Given below are the list of privileges provided by the server:
- ALL PRIVILEGES: It gives all privileges to a new user.
- CREATE: It gives the user privilege to create databases and tables.
- DROP: It gives the user privilege to drop databases and tables.
- DELETE: It gives the user privilege to delete rows from a specific table.
- INSERT: It gives the user privilege to insert rows into a specific table.
- SELECT: It gives the user privilege to read a database.
- UPDATE: It gives the user privilege to update table rows.
Syntax of Grant Privileges to the User
Given below are the syntax of grant privileges to the user:
GRANT ALL PRIVILEGES ON *.* TO ‘newuser’@’localhost’;
- Given below is the query, if we want to give all privileges to a user.
Code:
GRANT ALL PRIVILEGES ON * . * TO [email protected];
‘*’ is used to provide the user the access to read, edit, execute and perform all tasks in all the databases and tables.
- If you want to give specific privileges to a newly created user, execute the following command.
Code:
GRANT CREATE, SELECT, INSERT ON * . * TO [email protected];
- Given below is the query to flush all the privileges of a user.
Code:
FLUSH PRIVILEGES;
- If we want to see the existing privileges for the user, execute the following command.
Code:
SHOW GRANTS for username;
- Given below is the query to lists fields of the user table.
Code:
desc mysql.user;
Output:
Code:
select user,host,account_locked from mysql.user;
Output:
- Given below is the query to see current user.
Code:
SELECT user();
Output:
Or
Code:
SELECT current_user();
Output:
Code:
SELECT User, host FROM mysql.user;
Output:
- If you want to display only unique usernames.
Code:
SELECT DISTINCT User FROM mysql.user;
Output:
- Given below is the query to Delete a user.
Code:
Drop user ‘user’@’localhost’;
- Given below is the query to Revoke privileges from user.
Code:
Revoke all privileges on database_name from ‘user’@’localhost’;
- Given below is the query to view a list of all users.
Code:
select user from mysql.user GROUP BY user;
Output:
- Given below is the query to delete a specific user.
Code:
Delete from mysql.user where user =’username’;
Where, username is the name of the user which we want to delete.
Output:
So, after deletion of the root user we are left with only 3 users.
Conclusion – MySQL Users
In this article, we saw how to create new users and delete user. We have also seen about how to grant privileges to users and revoke privileges and different types of privileges granted to the users in a MySQL database. We have also seen about how to list of users in the present server.
Recommended Articles
This is a guide to MySQL Users. Here we discuss the introduction to MySQL Users, example to create user statement and list of privileges provided by server. You may also have a look at the following articles to learn more –
12 Online Courses | 10 Hands-on Projects | 92+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses