illuminate/database 库如何设置数据库连接超时时间
用illuminate/database做数据库连接,DB::connection('db')->getPdo()
, 当ip不存在或连不上等情况,超时需要等待的时间过长
查了不是pdo
的说明,经测试,以下方法设置超时时间
-
mysql
$config = [ 'driver' => 'mysql', 'host' => $this->host, 'port' => $this->port, 'username' => $this->username, 'password' => $this->password, 'database' => $this->database, 'charset' => $this->encoded, 'prefix' => '', 'strict' => false, 'engine' => null, 'options' => [ // mysql连接3s超时设置 \PDO::ATTR_TIMEOUT => 3 ] ];
-
oracle
的oci8
(用的是yajra/laravel-oci8)$config = [ 'driver' => 'oracle', 'tns' => "(DESCRIPTION = (CONNECT_TIMEOUT=1)(RETRY_COUNT=1) (ADDRESS = (PROTOCOL = TCP)(HOST = $this->host)(PORT = $this->port)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = $this->service_name) ) )", 'host' => $this->host, 'port' => $this->port, 'database' => $this->mode, 'username' => $this->username, 'password' => $this->password, 'charset' => $this->encoded, 'prefix' => '', 'prefix_schema' => '', ];
3mssql
找到解决方案,不但是不知道怎么通过laravel
的 connection
配置
https://stackoverrun.com/cn/q/9972324
目前临时解决方案,根据调试源码得出的偏方
$config = [
'driver' => 'sqlsrv',
'host' => $this->host,
'port' => $this->port,
'username' => $this->username,
'password' => $this->password,
// 附在database,加入超时限制
'database' => $this->database . ';LoginTimeout=2',
'charset' => $this->encoded,
'prefix' => '',
];
各位猿友有没有知道更好的解决方案,提点一下
推荐文章: