0
votes

I have this php function:

function createDatabase($dbName,$dbPass) {
         $host = 'localhost';
         $user = 'myuser';
         $user_password = 'mypass';
         $db = new PDO("mysql:host=$host", $user, $user_password);
         $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
         try {
           doLog('Creating Database...');
           $count = $db->exec("CREATE DATABASE `$dbName`");
           if($count === false) {
             doLog('Could not create database');
             return false;
           }
           doLog('Creating User...');
           $count = $db->exec("CREATE USER '$dbName'@'localhost' IDENTIFIED BY '$dbPass'");
           if($count === false) {
             doLog('Could not create user');
             return false;
           }
           doLog('Granting User Privileges...');
           $count = $db->exec("GRANT ALL ON `$dbName`.* TO '$dbName'@'localhost'");
           if($count === false) {
             doLog('Could not grant user privileges');
             return false;
           }
           doLog('Flushing Privileges...');
           $count = $db->exec("FLUSH PRIVILEGES");
           if($count === false) {
             doLog('Could not flush privileges');
             return false;
           }

         } catch (PDOException $e) {
           doLog("DB ERROR: ". $e->getMessage());
           return false;
         }
         return true;
    }

I was working off another question here: Can I create a database using PDO in PHP

This code keeps getting hung up during the 3rd query with granting privileges with this error:

SQLSTATE[42000]: Syntax error or access violation: 1044 Access denied for user

The MySQL user in question has global permissions, including grant. What else could I be missing?

1
it's the full error message? anything else after Access denied for user?Muhammad Sumon Molla Selim
Yes it lists the user and says for the name of the new databaseHelto
What is the username?Jay Blanchard
Worked for me after removing \ from doLog('Creating User...'); \david strachan
That slash shouldn't be there, I'll remove it from post.Helto

1 Answers

-1
votes

The MySQL user in question has global permissions, including grant. What else could I be missing?

Noting but common sense.

If your database says the a user has not permissions then it is so.

This is the point of error messages: yo have to read them and fix them. As simple as that.