Yii2 Mongodb 数据库报错:remote connect close

我使用了mongodb的复制集,报错remote connect close

Yii2中我更改下了配置如下:

'mongodb_erp' => [
            'class' => 'yii\mongodb\Connection',
            'dsn' => 'mongodb://192.168.220.105:27017/erp,192.168.220.106:27017/erp?replicaSet=rs0&readPreference=secondaryPreferred', # primaryPreferred |  secondaryPreferred
      'options' => [
        'socketTimeoutMS' => 300000,  # 设置超时时间
        'connectTimeoutMS'=> 600000,  # 设置连接超时时间
      ],
],

 

将读改成副本读优先,对于读的优先级的设置解释如下:

/**
        const string RP_PRIMARY = "primary" ;  						# 始终用主读
        const string RP_PRIMARY_PREFERRED = "primaryPreferred" ;	# 主读优先
        const string RP_SECONDARY = "secondary" ;					# 始终用副本读
        const string RP_SECONDARY_PREFERRED = "secondaryPreferred" ;# 副本读优先
        const string RP_NEAREST = "nearest" ;						# 主和副本随机读
      */

另外,长连接可能存在失效问题,在连接mongodb的时候,需要循环一下:

Yii2 随机发生 php mongo报错:’Failed to connect to: XXXXX: Remote server has closed the connection’的解决方法