Mysqli on WAMP, error - connection attempt failed

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

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 );

回答1:

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 );


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!