0

I am create user using bellow script

$conn = mysqli_connect('host', 'root', 'password');
$dbName = "userdb";
$dbUser = "username";
$dbPass = "password";

$queries = array(
        "CREATE DATABASE `$dbName` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci",
        "CREATE USER '$dbUser'@'localhost' IDENTIFIED BY '$dbPass'",
        "GRANT USAGE ON * . * TO '$dbUser'@'localhost' IDENTIFIED BY '$dbPass' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0",
        "GRANT CREATE, SELECT , INSERT , UPDATE, DELETE ON `$dbName` . * TO '$dbUser'@'localhost'",
        "GRANT ALL PRIVILEGES ON *.* TO '$dbUser'@'%' IDENTIFIED BY '$dbPass'",
        "FLUSH PRIVILEGES"
    );

    foreach($queries as $query) {
        $rs = mysqli_query($conn, $query);          
    }

Using this Script user create successfully .

How to change this user password in future using php script?

1 Answer 1

3

You can use the following PHP script to change the password on your MySQL database:

$conn = mysqli_connect('host', 'root', 'password');
$dbUser = "username"; # same username as in your example
$dbPass = "new_password"; # new password

$queries = array(
  "USE mysql;", # switch to the 'mysql' database
  "SET PASSWORD FOR '$dbUser'@'localhost' = PASSWORD('$dbPass');"
);

foreach($queries as $query) {
    $rs = mysqli_query($conn, $query);          
}

Note that there is nothing magical happening here. We are changing the user password as you normally would from the MySQL prompt, except we are doing it from PHP instead.

Sign up to request clarification or add additional context in comments.

1 Comment

@JayeshVekariya You need to replace the values in my example with those corresponding to your actual database setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.