在cPanel中动态创建数据库?

时间:2017-02-13 07:31:34

标签: php mysql codeigniter cpanel

database.php - config

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'sample',
    'password' => 'xxxxxxxxxxx',
    'database' => 'db_sample',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE

控制器:

<?php

class Test extends CI_Controller {
    public function index() {

        $this->load->model('Test_model');

        $sample = $this->Test_model->test();

        echo $sample;
    }

}
?>

模型

<?php

class Test_model extends CI_Model {

    public function test() {
        $sql = "SELECT name
        FROM company";

        $query = $this->db->query($sql);
        $num = $query->num_rows();

        $this->load->dbforge();
        if($this->dbforge->create_database('service_test')) {
            $this->db->query("Use service_test");
            $this->db->query("CREATE USER 'user1'@localhost IDENTIFIED BY 'admin123';");
            $this->db->query("GRANT ALL PRIVILEGES ON service_test.* TO user1@localhost identified by 'admin123';");
            return true;
        } else {
            return false;
        }


    }
}

?>

但是我一直收到这个错误:

Error Number: 1044

Access denied for user 'sample'@'localhost' to database 'service_test'

CREATE DATABASE `service_test` CHARACTER SET utf8 COLLATE utf8_general_ci

我是否缺少任何配置设置? 我已经挣扎了2天。我已经发送了一张帮助票,但他们只是告诉我有问题的特权用户和数据库必须在同一个cPanel帐户下(据我所知,它们都是)。 示例用户具有为其启用的所有权限。我们手动设置它。

0 个答案:

没有答案