在CodeIgniter 3中设置数据库连接超时 [英] Set database connection timeout in CodeIgniter 3
问题描述
我们正在使用2个数据库,我们的本地数据库和一个外部数据库.但是现在我们的外部数据库已关闭(我们仍在开发中,因此很高兴遇到了此问题),它现在尝试连接到外部数据库30秒钟,如何将数据库的连接超时更改为类似1-2秒?我将Codeigniter与数据库上的PDO驱动程序一起使用.是否有人可以解决此问题?
We are working with 2 databases, our local database and an external database. But now our external database is down (we're still under development so it's good we came across this issue) and it now tries to connect to the external database for 30 seconds, how can I change the connection timeout of the Database to something like 1 - 2 seconds? I am using Codeigniter with the PDO drivers on my databases. Is there anyone with a clean solution for this problem?
推荐答案
这不是文档功能,但是您可以通过添加options
设置从数据库配置文件(application/config/database.php
)中进行此操作,例如:
It is not a documented feature, but you can do this from the database config file (application/config/database.php
) by adding options
setting e.g.:
$db['default']['options'] = array(PDO::ATTR_TIMEOUT => 5);
使用相同内部机制的其他设置(例如,使用$db['default']['stricton']
设置的PDO::MYSQL_ATTR_INIT_COMMAND
和通过$db['default']['compress']
设置的PDO::MYSQL_ATTR_COMPRESS
)不受此影响.
The other settings which use the same internal mechanism (e.g. PDO::MYSQL_ATTR_INIT_COMMAND
set with $db['default']['stricton']
and PDO::MYSQL_ATTR_COMPRESS
set with $db['default']['compress']
) are not affected by this.
如果要深入研究或检查设置了哪些选项,可以在system/database/drivers/pdo/pdo_driver.php
的db_connect
功能中登录$this->options
并检查database/drivers/pdo/subdrivers/pdo_mysql_driver.php
.
If you want to dig deeper or check which options are set, you can log $this->options
in db_connect
function in system/database/drivers/pdo/pdo_driver.php
and also check database/drivers/pdo/subdrivers/pdo_mysql_driver.php
.
这篇关于在CodeIgniter 3中设置数据库连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!