aid

Monday, 13 April 2015

how to create mysql user

how to create mysql user
Now a database has been created we want to create a user for utr created database so we need to create a user in mysql using php, create a user in mysq than the root user needs to be able to work with it.
This means a MySQL user, not a system user, needs to be created and given the appropriate permissions to be able to work with data. We will create a user with all permissions on the periodic_table database. If you want real security, you can limit the permissions a user has to just SELECT, thus limitting possible injection attacks. The topic of PHP/MySQL security will be covered in another article. For now, we will GRANT ALL privileges to the new user, except GRANT. Once again we need to have root access to mysql to do this. The code itself should look very familiar if you have read the previous section as it is almost identical. The same principles and connections apply, the only thing that changes is the SQL statement itself.
Create a user with mysql.
As mentioned, this is identical to creating a database, the only change is the SQL statement, we have reproduced the code here for the benifit of familiarity.
<?php


$hostname = 'localhost';
$username = 'root';
$password = 'rootpassword';
$link = @mysql_connect($hostname, $username, $password);
if(is_resource($link))
    {
     echo 'Connected successfully<br />';

      $sql = "GRANT ALL ON periodic_table.* TO username@localhost IDENTIFIED BY 'password'";    if(mysql_query($sql, $link))
        {
        echo 'New user created successfully<br />';
        }
    else
        {
        echo 'Unable to create user: <br />' . $sql .'<br />' . mysql_error();
        }
        mysql_close($link);
    }
else
    {
        echo 'Unable to connect';
    }
?>

No comments:

Post a Comment