Mysql User Creation: Setting Up a New MySQL User Account

How do I create a user account on MySQL database server?

When you try to access MySQL database server from client such as mysql or even programming language such as php or perl you need a user account. MySQL has sophisticated user management system that controls who can access server and from which client system. It uses special tables in mysql database. In order to create a new user account you need a MySQL root account password. You need to use the GRANT SQL command to set up the MySQL user account.
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 10m
Finally, use the account’s name and password to make connections to the MySQL server.

Please note that MySQL root user account is different from UNIX/Linux root login account. For example, the MySQL root user and the Linux/Unix root user are separate and have nothing to do with each other, even though the username is the same in each case.

Setup a root user password

To setup root password for first time, use mysqladmin command at shell prompt as follows:
$ mysqladmin -u root password NEWPASSWO
If you want to change or update a root user password, then you need to use the following command:
$ mysqladmin -u root -p'oldpassword' password newpass

Procedure for setting up a MySQL user account

Login in as mysql root user. At shell prompt type the following command:

$ mysql -u root -p


$ mysql -u root -h your-mysql-server-host-name -p

Create a new mysql database called demo. Type the following command at mysql> prompt:

mysql> CREATE DATABASE demo;

Create a new user called user1 for database called demo:

mysql> GRANT ALL ON demo.* TO user1@localhost IDENTIFIED BY 'mypassword';

How do I connect to MySQL database demo using user1 account?

User user1 can connect to demo database using the following shell command:

$ mysql -u user1 -p demo


$ mysql -u user1 -h  your-mysql-server-host-name-here -p demo


  • -u user1 : MySQL Username
  • -h : MySQL server name (default is localhost)
  • -p : Prompt for password
  • demo: demo is name of mysql database (optional)
See also
  • Man pages: mysqladmin(1)
This entry is 3 of 5 in the CentOS Linux MySQL Server Tutorial series. Keep reading the rest of the series:
  1. CentOS install Mysql database server
  2. CentOS install MySQL client only
  3. MySQL Create a user accounts
  4. MySQL Create a database & tables and data
  5. Reinstall MySQL On Linux

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 17 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
17 comments… add one
  • amol Sep 27, 2008 @ 10:25

    how to install & configure PHP on RHEL 4

  • herc Apr 17, 2010 @ 16:59

    Thanks! Simple and clear

  • sisila Nov 21, 2011 @ 4:22

    its cool .v good

  • RK Feb 1, 2012 @ 10:14

    Its very easy and clear

  • saber Apr 1, 2012 @ 7:51

    hi , i want to configure a policy for users that expire password off users in specific time any one can help me ? (im using ubuntu 10.04 and mysql)

  • Betrand Njibamum May 30, 2012 @ 22:54

    Please am trying to set up a mysql account to use in installing joomla to no avail . Please can someone help me with that ?

  • krishna Jun 12, 2012 @ 9:38

    thanx yar.its a simple installation

  • Sanjib Jul 2, 2012 @ 8:39

    Its very simple and valueable. Wroks fine for me. Thanks a lot.

  • yash Sep 18, 2012 @ 15:25

    nice man :)

  • benariba Feb 11, 2013 @ 11:46

    very fine, thank you very match;

  • deepak Feb 12, 2013 @ 6:55

    Hi I am new to mysql,
    When I try to connect from command line. I need to enter my root password. I have created the demo database, but when I type “mysql -u user1 -p demo”, It shows me ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
    corresponds to your MySQL server version for the right syntax to use near ‘mysql
    -u user1 -p demo’ at line 1. Please help, how can I connect with this user1 to demo database in command prompt.

    • Jia Ye Mar 28, 2013 @ 12:54

      you need to grant the privilege of operating the database’demo’ to the user in the root model, like this: grant all on demo(name of your database) to user1(username)@ “localhost ” identified by “password”(your account’s password).

  • Jia Ye Mar 28, 2013 @ 13:09

    if you don’t have the primaryl syntax error,it should be a problem of your MySql version,which may be too old,then you can change a new release.

  • Chase May 2, 2013 @ 8:15

    Excellent, quick how-tos. Thank you very Much!

  • praveenkumar Jul 3, 2013 @ 4:18

    it too clear to understand… i want full tutorial for mysql. pleaze help me

  • Sreenivasa M Aug 5, 2013 @ 4:41

    thanks , its cool and good

  • ismarvs Nov 24, 2015 @ 12:31

    i dont get it

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @