之前配置过redis,本地线上都没问题,但是这次本地没问题,线上却死活连不上
报错信息甚至都是"Failed to open redis DB connection (xxx): - "
坑爹啊,居然没有显示错误编码和错误错误信息!
查看yii-redis Connection源码,找到redis连接方法:
public function open()
{
if ($this->_socket !== false) {
return;
}
$connection = ($this->unixSocket ?: $this->hostname . ':' . $this->port) . ', database=' . $this->database;
\Yii::trace('Opening redis DB connection: ' . $connection, __METHOD__);
$this->_socket = @stream_socket_client(
$this->unixSocket ? 'unix://' . $this->unixSocket : 'tcp://' . $this->hostname . ':' . $this->port,
$errorNumber,
$errorDescription,
$this->connectionTimeout ? $this->connectionTimeout : ini_get('default_socket_timeout'),
$this->socketClientFlags
);
if ($this->_socket) {
if ($this->dataTimeout !== null) {
stream_set_timeout($this->_socket, $timeout = (int) $this->dataTimeout, (int) (($this->dataTimeout - $timeout) * 1000000));
}
if ($this->password !== null) {
$this->executeCommand('AUTH', [$this->password]);
}
if ($this->database !== null) {
$this->executeCommand('SELECT', [$this->database]);
}
$this->initConnection();
} else {
\Yii::error("Failed to open redis DB connection ($connection): $errorNumber - $errorDescription", __CLASS__);
$message = YII_DEBUG ? "Failed to open redis DB connection ($connection): $errorNumber - $errorDescription" : 'Failed to open DB connection.';
throw new Exception($message, $errorDescription, $errorNumber);
}
}
调试了半天,也没能打印出errorDescription;
最后突然注意到$this->_socket = @stream_socket_client()
晕。。。@符把stream_socket_client()函数错误信息给屏蔽了
去掉@符号,显示是因为php.ini把stream_socket_client()函数给禁用了,修改php.ini重启,redis可以用了==!
一个弱智的问题浪费了2个小时,以后遇到问题一定要多看底层源码,注意细节