CODEIGNITER 1.7.2:无法根据提供的设置连接到数据库

Posted

技术标签:

【中文标题】CODEIGNITER 1.7.2:无法根据提供的设置连接到数据库【英文标题】:CODEIGNITER 1.7.2: unable to connect to db based on provided settings 【发布时间】:2018-06-18 23:18:10 【问题描述】:

这是我的数据库配置文件:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost:8080',
    'username' => 'root',
    'password' => '',
    'database' => 'some_db',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => FALSE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

我的确切错误信息是这样的:

A Database Error Occurred

Unable to connect to your database server using the provided settings.

【问题讨论】:

尝试将 'localhost' 更改为 '127.0.0.1'。您有可以连接的 SQL 客户端吗?这是在您的开发机器上运行还是在远程系统上运行? 可能升级到最新版本 @wolfgang1983 到目前为止,这不是一个选项。 尝试不使用端口,例如'hostname' => 'localhost', mysql 的典型端口是3606,但您不需要hostname 中的端口。 【参考方案1】:

在 CI 1.7 中,您需要为 mysqli 使用“端口”配置。

$db['default']['port'] = "3306";
$db['default']['hostname'] = "localhost";

默认的DB_driver.php 设置是$port = ''(在第 43 行附近)。

对于mysqli_driver.php,db_connect() 函数使用此空端口并引发错误。

您会在下面看到驱动程序方法的区别,(文件分别位于 system/database/drivers/mysql 或 system/database/drivers/mysqli)

mysql_driver.php:

function db_connect()

    if ($this->port != '')
    
        $this->hostname .= ':'.$this->port;
    

    return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);

与 mysqli_driver.php:

function db_connect()

    if ($this->port != '')
    
        return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database, $this->port);
    
    else
    
        return @mysqli_connect($this->hostname, $this->username, $this->password, $this->database);
    


来源:https://forum.codeigniter.com/archive/index.php?thread-12577.html

【讨论】:

【参考方案2】:
$db_obj=$CI->load->database($config, TRUE);
if($db_obj->conn_id) 
    //do something
 else 
    echo 'Unable to connect with database with given db details.';

        or


You can check for the conn_id on the $db_obj

if ($db_obj->conn_id === false) 
    $config['db_debug'] = true;
    $config['hostname'] = "myMasterDatabase.com";
    $db_obj=$CI->load->database($config, TRUE);

This should work.

【讨论】:

以上是关于CODEIGNITER 1.7.2:无法根据提供的设置连接到数据库的主要内容,如果未能解决你的问题,请参考以下文章

上游太大 - nginx + codeigniter

无法使用提供的设置连接到您的数据库服务器。 (Codeigniter) 和 MySQL® 服务器当前处于脱机状态。 (面板)

CodeIgniter 错误:无法使用提供的设置使用 Mongodb 数据库连接到您的数据库服务器

Codeigniter 根据选择的日期从数据库中填充下拉值

Codeigniter 无法连接到外部 SQL Server

无法使用 php codeigniter 插入批量记录