EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Users
Secondary Sidebar
MySQL Tutorial
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL?Table?Size
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

MySQL Users

By Priya PedamkarPriya Pedamkar

MySQL Users

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.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

[email protected]

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]%.

Note: A new user created has all access but if we want to give some specific privileges we have to specify grant statement in that.

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:

MySQL Users 1

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:

desc

Code:

select user,host,account_locked from mysql.user;

Output:

MySQL Users 3

  • Given below is the query to see current user.

Code:

SELECT user();

Output:

MySQL Users 4

Or

Code:

SELECT current_user();

Output:

MySQL Users 5

Code:

SELECT User, host FROM mysql.user;

Output:

MySQL Users 6

  • If you want to display only unique usernames.

Code:

SELECT DISTINCT User FROM mysql.user;

Output:

Distinct

  • 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:

MySQL Users 8

  • 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:

Delete

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 –

  1. Unique Key in MySQL
  2. MySQL Timestamp
  3. MySQL Constraints
  4. MySQL Outer Join
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (17 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more