TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Create a New User in MySQL with Password
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

Create a New User in MySQL with Password

Last updated: May 22, 2023 12:52 pm
By Meenakshi Agarwal
Share
6 Min Read
MySQL Create User with Password
SHARE

This tutorial explains how to create a user with a password in MySQL. We’ll use the MySQL Create User command and describe it with the help of examples.

Contents
SyntaxCreating users in MySQL – ExamplesMySQL Create User with PasswordGrant Privileges to New UsersSummary – MySQL CREATE USER with PASSWORD

By using this command, you can create a new MySQL user and grant privileges. Usually, you might be using the root user to access a database. The root user has complete control and can’t be restricted.

Hence. It is mandatory to create users depending on the type of access s/he needs. And that’s where this command is going to help.

MySQL Create User with Password

The MySQL Create User statement can be used to create new users with a password and can be assigned permissions on a need basis.

Syntax

It comes in the following forms:

-- MySQL CREATE USER with PASSWORD
CREATE USER [IF NOT EXISTS] [User_Name]
    IDENTIFIED [BY/WITH] ['Password'];

Here, the User_Name field has two elements: The actual username and the machine hostname.

user_name@host_name
e.g. john@myhost.com

Please note the following:

  • The user_name is the name of the logged-in user whereas the host_name is the name of the machine running the MySQL DB.
  • The host_name subfield is optional. If you remove it, even then the user can establish a connection.
  • Both the user_name  and host_name can include special chars. You need to quote them accurately: ‘user_name’ or ‘host_name.’

You must supply a password for the user right after the IDENTIFIED BY clause

Also, by using the IF NOT EXISTS option, you can make sure a new user is created when s/he is not available.

You must remember that MySQL CREATE USER command creates a new user who doesn’t have any privileges. Hence, you need to grant permissions to the new user.

We feel that the following posts would guide you even more along with this tutorial:

  • MySQL Optimize Table
  • MySQL Aggregate Functions

Creating users in MySQL – Examples

First of all, you must have an active connection with the MySQL DB. So, if it is not the case, then run the following MySQL client command:

mysql -u root -p

If you would have set a root password, then provide the same.

Enter Password : XXXXXXXX

Once the connection is successful, you can list down the available users.

mysql> SELECT  user from  MYSQL.user;

It will give the below result:

+------------------+
| Users            |
+------------------+
| mysql.sys        |
| root             |
+------------------+

MySQL Create User with Password

Now, try to define a new user known as ‘john’:

mysql> CREATE user  john@localhost IDENTIFIED by 'xxYYYxx';

Run the below command to display all users.

mysql> select user from mysql.user;
+------------------+
| Users            |
+------------------+
| mysql.sys        |
| john             |
| root             |
+------------------+

You have now successfully a new user called John. Now, start another MySQL session and log in as john.

mysql -u john -p

Supply the password for john and press the Enter key:

Enter Password : xxYYYxx

In the next step, you have to list down the databases that john can access.

mysql> SHOW   DATABASES;

You might see something like:

Databases
-------------------
information_schema

There is no database that john can use. So, we’ll now create one for him.

mysql> CREATE DATABASE johndb;

Now, select the database you created.

mysql> use johndb;

The database is empty, so create a dummy table named ‘test_table.’

mysql> CREATE TABLE test_table(
-> id INT AUTO_INCREMENT PRIMARY KEY,
-> name VARCHAR(30) not null,
-> married bool default false);

Grant Privileges to New Users

Now, assign the required privileges on the ‘johndb’ to ‘john’:

mysql> GRANT ALL PRIVILEGES on johndb.* to john@localhost;

You can read more about MySQL Grant Privileges here.

Now again go to the console and run the command to show the databases.

mysql> SHOW DATABASES;

This time, john should be able to list the johndb:

Databases
-------------------
johndb
information_schema

Now, select the johndb and check the available tables.

mysql> USE johndb;
mysql> SHOW  TABLES;

You can now confirm that john can list the test_table.

Tables_in_johndb
-------------------
test_table

Let’s now see if john can add some records to the test_table.

mysql> INSERT  INTO test_table(name) values('John Travolta');
mysql> SELECT  * from test_table;

The above statements would give the below result:

Records in test_table
-------------------------
1, John Travolta, 0

Now, you can assert that the user john can run any operation on the johndb database.

Now, it’s time to disconnect from the MySQL DB and close the running sessions.

Summary – MySQL CREATE USER with PASSWORD

Trust us that you can now easily create a user in MySQL with a password, and grant privileges. However, you can take up more examples and practice.

While you are on a learning spree, more information is what you need. So, don’t miss on to the below posts.

  • Quick Steps to Install MySQL on Windows 7
  • Easy Steps to Install MySQL on Ubuntu 16.04
  • MySQL Date and Date Functions

And that’s it for this tutorial, we hope you found it helpful and informative!

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

The Difference between UPSERT & Insert

SQL Programming Test in 2024

SQL Table Creation: The Missing Manual

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Switch between activities in Android How to Switch between Activities in Android
Next Article Convert Python String to Int & Int to String with Examples Python String to Int and Back to String

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x