已添加信息:我尝试使用全新安装的codeigniter,只需将$this->load->database ( );
添加到默认控制器即可触发相同的错误。
我检查了我的phpinfo并安装了mysqli。我用以下代码检查了它,它正在工作。
当我打开我的一个Codeigniter项目时,它显示错误:连接尝试失败,为什么?
这一切是否意味着Codeigniter出现了问题?
<?php
$mysqli = new mysqli("localhost", "root", "", "myDatabase");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* check if server is alive */
if ($mysqli->ping()) {
printf ("Our connection is ok!\n");
} else {
printf ("Error: %s\n", $mysqli->error);
}
/* close connection */
$mysqli->close();
?>
/*** Results in : Our connection is ok! ***/
遇到PHP错误
严重性:警告
消息:mysqli :: real_connect():( HY000 / 2002):连接尝试 失败,因为关联方在a之后没有正确回应 一段时间,或建立的连接失败,因为连接 主持人没有回复。
文件名:mysqli / mysqli_driver.php
行号:135
这是问题中的数据库代码,codeigniter系统文件的位置是:system / database / drivers / mysqli / mysqli_driver.php
// --------------------------------------------------------------------
/**
* Database connection
*
* @param bool $persistent
* @return object
* @todo SSL support
*/
public function db_connect($persistent = FALSE)
{
// Do we have a socket path?
if ($this->hostname[0] === '/')
{
$hostname = NULL;
$port = NULL;
$socket = $this->hostname;
}
else
{
// Persistent connection support was added in PHP 5.3.0
$hostname = ($persistent === TRUE && is_php('5.3'))
? 'p:'.$this->hostname : $this->hostname;
$port = empty($this->port) ? NULL : $this->port;
$socket = NULL;
}
$client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
$mysqli = mysqli_init();
$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
if ($this->stricton)
{
$mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode="STRICT_ALL_TABLES"');
}
return $mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags)
? $mysqli : FALSE;
}
配置文件
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'myProject',
'dbdriver' => 'mysql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
答案 0 :(得分:3)
我想通了,我希望解决方案可以帮助遇到与我相同问题的人。
由于我的服务器运行缓慢,我正在寻找解决方案,我尝试了很多但没有任何效果,然后我将数据库连接的主机名从localhost
更改为127.0.0.1
并且它工作正常。此更改使 mysqli 工作,使我的服务器更快
现在,codeigniter项目的新配置设置如下所示:
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1', // See the change
'username' => 'root',
'password' => '',
'database' => 'myProject',
'dbdriver' => 'mysqli', // See the change
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);