我正在使用Codeignitor框架创建一个休息API,该框架将在GAE中托管,并与云SQL数据库进行通信。
我已根据此处描述的步骤更改并配置了我的Codeignitor框架。 How do I connect to a Google Cloud SQL database using CodeIgniter?
但是当我打电话给$this->load->database();
时
在我的控制器中,它会导致以下错误。
Severity: Warning
Message: mysqli_connect(): (HY000/2002): No such file or directory
Filename: mysqli/mysqli_driver.php
Line Number: 120
请帮我解决这个问题,因为我已经浪费了很多时间来处理这个问题。
谢谢
答案 0 :(得分:0)
我不确定您使用的是哪个版本的Codeigniter,但是,从版本3.0开始,这是框架中db_connect函数的默认代码
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;
$this->_mysqli = mysqli_init();
$this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
if (isset($this->stricton))
{
if ($this->stricton)
{
$this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION sql_mode = CONCAT(@@sql_mode, ",", "STRICT_ALL_TABLES")');
}
else
{
$this->_mysqli->options(MYSQLI_INIT_COMMAND,
'SET SESSION sql_mode =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
@@sql_mode,
"STRICT_ALL_TABLES,", ""),
",STRICT_ALL_TABLES", ""),
"STRICT_ALL_TABLES", ""),
"STRICT_TRANS_TABLES,", ""),
",STRICT_TRANS_TABLES", ""),
"STRICT_TRANS_TABLES", "")'
);
}
}
if (is_array($this->encrypt))
{
$ssl = array();
empty($this->encrypt['ssl_key']) OR $ssl['key'] = $this->encrypt['ssl_key'];
empty($this->encrypt['ssl_cert']) OR $ssl['cert'] = $this->encrypt['ssl_cert'];
empty($this->encrypt['ssl_ca']) OR $ssl['ca'] = $this->encrypt['ssl_ca'];
empty($this->encrypt['ssl_capath']) OR $ssl['capath'] = $this->encrypt['ssl_capath'];
empty($this->encrypt['ssl_cipher']) OR $ssl['cipher'] = $this->encrypt['ssl_cipher'];
if ( ! empty($ssl))
{
if (isset($this->encrypt['ssl_verify']))
{
if ($this->encrypt['ssl_verify'])
{
defined('MYSQLI_OPT_SSL_VERIFY_SERVER_CERT') && $this->_mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, TRUE);
}
// Apparently (when it exists), setting MYSQLI_OPT_SSL_VERIFY_SERVER_CERT
// to FALSE didn't do anything, so PHP 5.6.16 introduced yet another
// constant ...
//
// https://secure.php.net/ChangeLog-5.php#5.6.16
// https://bugs.php.net/bug.php?id=68344
elseif (defined('MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT'))
{
$this->_mysqli->options(MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT, TRUE);
}
}
$client_flags |= MYSQLI_CLIENT_SSL;
$this->_mysqli->ssl_set(
isset($ssl['key']) ? $ssl['key'] : NULL,
isset($ssl['cert']) ? $ssl['cert'] : NULL,
isset($ssl['ca']) ? $ssl['ca'] : NULL,
isset($ssl['capath']) ? $ssl['capath'] : NULL,
isset($ssl['cipher']) ? $ssl['cipher'] : NULL
);
}
}
if ($this->_mysqli->real_connect($hostname, $this->username, $this->password, $this->database, $port, $socket, $client_flags))
{
// Prior to version 5.7.3, MySQL silently downgrades to an unencrypted connection if SSL setup fails
if (
($client_flags & MYSQLI_CLIENT_SSL)
&& version_compare($this->_mysqli->client_info, '5.7.3', '<=')
&& empty($this->_mysqli->query("SHOW STATUS LIKE 'ssl_cipher'")->fetch_object()->Value)
)
{
$this->_mysqli->close();
$message = 'MySQLi was configured for an SSL connection, but got an unencrypted connection instead!';
log_message('error', $message);
return ($this->db->db_debug) ? $this->db->display_error($message, '', TRUE) : FALSE;
}
return $this->_mysqli;
}
return FALSE;
}
从这段代码中,我可以看出你可以直接将socket作为数组参数传递到database.php文件中。你有没有尝试过?
此外,不建议修改框架的核心文件。这可能会导致其他问题。
修改强>: 此外,您可以使用默认参数
创建自定义库函数以手动连接到数据库