illuminate/database 库如何设置数据库连接超时时间

illuminate/database做数据库连接,DB::connection('db')->getPdo(), 当ip不存在或连不上等情况,超时需要等待的时间过长

查了不是pdo的说明,经测试,以下方法设置超时时间

  1. 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
        ]
    ];
  2. oracleoci8(用的是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找到解决方案,不但是不知道怎么通过laravelconnection配置
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' => '',
];

各位猿友有没有知道更好的解决方案,提点一下

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 6

老铁,撞脸了

5年前 评论

@klinson 没事没事,背景还是有点区别的,哈哈哈

5年前 评论

暂时找到偏方,勉强解决。。。

5年前 评论

我测了下 好像没生效呢
'options' => [
// mysql连接3s超时设置
\PDO::ATTR_TIMEOUT => 3
]
这个配置

5年前 评论

大佬有mongo的配置吗?

2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!