我在yiisoft / yii2-mongodb扩展程序中遇到问题。我正在使用三台mongodb服务器,一个主服务器和两个辅助服务器,但是从辅助服务器获取数据时出现以下错误。
服务器给出以下错误 “ name”:“ MongoDB异常”, “消息”:“不是主”, “代码”:10107, “类型”:“ yii \ mongodb \ Exception
my connection is as below
‘components’ => [
‘mongodb’ => [
‘class’ => ‘\yii\mongodb\Connection’,
‘dsn’ => ‘mongodb://xxx:27017,xxx:27017,xxx:27017/?replicaSet=p&connectTimeoutMS=480000&socketTimeoutMS=520000&readPreference=secondary’,
],
]
答案 0 :(得分:-2)
尝试像这样向配置中添加选项:
'mongodb' => [
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://xxx:27017,xxx:27017,xxx:27017/?replicaSet=p&connectTimeoutMS=480000&socketTimeoutMS=520000&readPreference=secondary',
'options' => [
'replicaSet' => 'p'
]
]
此外,您可以直接在选项数组中添加诸如 connectTimeoutMS 之类的连接选项