Creating MySQL Users

How to create MySQL users using the MySQL Console to start working on a database. (7/22/2009)


The first step to start working on MySQL thru PHP is creating a user and password. Is not a good idea to create a user without password because the lack of security.


To create a user, open the MySQL console and type the command: grant all privileges on *.* to user@domain identified by "password" with grant option;


  • user is the username to be used.
  • domain is the host that have the user.
  • password is the pass to be used to access the database.

    Here is an example below, I created a user "jose" with the password "pino":


    Creating MySQL user
    mysql> grant all privileges on *.* to jose@localhost
    identified by "pino" with grant option;
    Query OK, 0 rows affected (0.53 sec)
    mysql> _


    Now we can connect to MySQL using the user that we just created, here is an example of the code needed to connect:
    # get the Mysql username
    $user = "jose";
    # Connect to the database, Server: Localhost (this machine),
    #  User, password: pino
    $conn = mysql_connect("localhost", $user, "pino");
    # check if the connection was successfully done
    if ($conn) {
    # If the connection was done, Set welcome message
       $msg="Welcome $user, you are connected to the MySql database";
    # If no connection, set a different message
    else {
       $msg="Sorry $user, you have no access to the database";
    <title>Conecting to MySQL</title>
    <h3><?php echo ($msg); ?></h3>


    Here is the result:


    Welcome, you are connected


    In case the connection cannot be done, the message "Sorry $user, you have no access to the database" will appear.


     < Hello PHP!Homepage
    php Index
    Displaying Special Characters> 


    If any information, data, picture or design infringes a copyrighted material, please send me an e-mail asking to remove it along with the supporting data.
    Some features may not work with Google Chrome. © 2006-2010 Jose Pino - Powered by JPC Alpha