Mysqli on WAMP,错误连接尝试失败 [英] Mysqli on WAMP, error - connection attempt failed
问题描述
添加的信息:我尝试新安装的codeigniter,只需添加 $ this-> load-> database();
到默认控制器触发相同的错误。
我检查了我的phpinfo并安装了mysqli。我检查它与以下代码,它是工作。
仍然当我打开我的一个Codeigniter项目时,它显示错误:连接尝试失败,为什么? b
这是否意味着Codeigniter有问题?
<?php
$ mysqli = new mysqli(localhost,root,,myDatabase);
/ *检查连接* /
if($ mysqli-> connect_errno){
printf(Connect failed:%s\\\
,$ mysqli-> connect_error);
exit();
}
/ *检查服务器是否活着* /
if($ mysqli-> ping()){
printf( \\\
);
} else {
printf(Error:%s\\\
,$ mysqli-> error);
}
/ * close connection * /
$ mysqli-> close();
?>
/ ***结果:我们的连接确定! *** /
遇到PHP错误
严重性:警告
讯息:mysqli :: real_connect():( HY000 / 2002):连接尝试
失败,因为关联方在
时间段后没有正确响应,或者由于连接的
主机未能响应而建立连接失败。
文件名:mysqli / mysqli_driver.php
行号:135
b $ b
这是Question的数据库代码,codeigniter系统文件位置是:system / database /drivers/mysqli/mysqli_driver.php
// ---------------- -------------------------------------------------- -
/ **
*数据库连接
*
* @param bool $ persistent
* @return object
* @todo SSL支持
* /
public function db_connect($ persistent = FALSE)
{
//我们有一个套接字路径吗?
if($ this-> hostname [0] ==='/')
{
$ hostname = NULL;
$ port = NULL;
$ socket = $ this-> hostname;
}
else
{
//持久连接支持已在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;
}
配置文件 b
$ 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
);
我想出来,
由于我的服务器运行缓慢,我正在寻找解决方案,我尝试了很多东西,但没有什么工作,然后我更改了主机从 localhost
到 127.0.0.1
的数据库连接的名称,它的工作。此更改使 mysqli 工作,并使我的服务器更快
现在, codeigniter工程如下:
$ db ['default'] = array(
'dsn'=> '',
'hostname'=>'127.0.0.1',//查看更改
'username'=>'root',
'password'=> ,
'database'=>'myProject',
'dbdriver'=>'mysqli',//查看更改
'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
);
Added Info : I tried it with the fresh install of the codeigniter, simply adding $this->load->database ( );
to the default controller triggers the same error.
I checked my phpinfo and the mysqli is installed. I checked it with the following code and it is working.
Still when I open one of my Codeigniter project, it shows error : A connection attempt failed, why ?
Does all this means there is something wrong with 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! ***/
A PHP Error was encountered
Severity: Warning
Message: mysqli::real_connect(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Filename: mysqli/mysqli_driver.php
Line Number: 135
This is the database code in Question, the codeigniter system file location is : 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;
}
Config File
$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
);
I figured it out and I hope the solution will help someone facing the same problem as me.
Since my server was running slow, I was searching for solutions, I tried a lot of thing but nothing worked, then I changed host name for the database connection from localhost
to 127.0.0.1
and it worked. This change made mysqli work and it made my server a lot faster
So now the new configuration settings for the codeigniter project looks like this :
$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
);
这篇关于Mysqli on WAMP,错误连接尝试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!