mysqldatabasecodeignitercreateuserdbforge

Codeigniter dbforge library - assign db user to current database and add grant privileges


I am trying to create a db user and assigned him to the new database what already has been created using dbforge library:

$this->dbforge->create_database( $oData->db_name );

My question is:

  1. What must do first - create user or create a database?
  2. How to assign user to current database and add grant privileges?

My code to create a database and user:

$this->load->dbforge();
// create user
$this->db->query("CREATE USER '". $data->db_user ."'@'localhost' IDENTIFIED BY '". $data->db_password ."';");
// create database
$this->dbforge->create_database( $data->db_name );

Solution

  • The answer of my own qestion found a is:

    1. What must do first - create user or create a database? -

      • First must create database!
    2. How to assign user to current database and add grant privileges? - Following this code I created a new user, with grant privileges, which can access to all databases:

    $db->query("CREATE USER '". $oData->db_user ."'@'localhost' IDENTIFIED BY '". $oData->db_password ."';");
    $db->query("GRANT CREATE, ALTER, DELETE, INSERT, SELECT, DROP, UPDATE  ON * . * TO '". $oData->db_user ."'@'localhost';");