我正在使用 Codeigniter 运行开发 LAMP。但是,当我尝试加载页面时,数据库无法连接(数据库实际上在另一台服务器上)并且我收到此错误:
A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: core/Loader.php
Line Number: 346
我的 database.php 是这样的:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'myhost.com';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mytable';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
如果我将 'pconnect' 和/或 'db_debug' 更改为 false,我会收到相同的错误,或者它只是不起作用。我知道数据库已启动并正常运行,因为其他人能够连接和使用它。
但是,如果我将主机的 IP 地址放入 database.php 文件中,它就可以工作。我尝试刷新 DNS,甚至更改为 Google 的 DNS 服务器也无济于事。
一个简单的 mysql_connect 的结果是:
A PHP Error was encountered
Severity: Warning
Message: mysql_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Filename: config/database.php
Line Number: 67
A PHP Error was encountered
Severity: Warning
Message: mysql_connect(): [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known (trying to connect via tcp://myhost.com:3306)
Filename: config/database.php
Line Number: 67
A PHP Error was encountered
Severity: Warning
Message: mysql_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Filename: config/database.php
Line Number: 67
Could not connect: php_network_getaddresses: getaddrinfo failed: Name or service not known