Yii Framework Forum

How to read from secondary mongodb server?


(Vatsal235) #1

I am facing an issue in yiisoft/ yii2-mongodb extension I am using three mongodb servers one primary and two secondary but i am getting an error as below while fetching data from secondary servers.
The server gives below error
“name”: “MongoDB Exception”,
“message”: “not master”,
“code”: 10107,
“type”: “yii\mongodb\Exception”


(Alirz23) #2

you could add another component for reads something along these lines config/web.php

//...
'components' => [
    //...
    'readMongo' => [
              // your config goes here for secondary servers 
    ]
    //...
]
//..

Yii::$app->readMongo->createCommand(['listIndexes' => 'some_collection'])->execute();



(Vatsal235) #3

I am using this same way but didn’t worked.
‘components’ => [
‘mongodb’ => [
‘class’ => ‘\yii\mongodb\Connection’,
‘dsn’ => ‘mongodb://XXX:27017,XXX8:27017,XXX:27017/?replicaSet=p&connectTimeoutMS=480000&socketTimeoutMS=520000&readPreference=secondary’,
],
]
+++
{
“name”: “MongoDB Exception”,
“message”: “not master”,
“code”: 10107,
“type”: “yii\mongodb\Exception”,
“file”: “/home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Command.php”,
“line”: 192,
“stack-trace”: [
#0 /home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Command.php(357): yii\mongodb\Command->execute()”,
#1 /home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Collection.php(153): yii\mongodb\Command->createIndexes(‘auth_item’, Array)”,
#2 /home/ubuntu/panelapi/common/components/MongodbManager.php(59): yii\mongodb\Collection->createIndex(Array, Array)”,
#3 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/BaseObject.php(109): common\components\MongodbManager->init()”,
#4 [internal function]: yii\base\BaseObject->__construct(Array)”,
#5 /home/ubuntu/panelapi/vendor/yiisoft/yii2/di/Container.php(375): ReflectionClass->newInstanceArgs(Array)”,
#6 /home/ubuntu/panelapi/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build(‘common\componen…’, Array, Array)”,
#7 /home/ubuntu/panelapi/vendor/yiisoft/yii2/BaseYii.php(349): yii\di\Container->get(‘common\componen…’, Array, Array)”,
#8 /home/ubuntu/panelapi/vendor/yiisoft/yii2/di/ServiceLocator.php(137): yii\BaseYii::createObject(Array)”,
#9 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Module.php(742): yii\di\ServiceLocator->get(‘authManager’, false)”,
#10 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Application.php(599): yii\base\Module->get(‘authManager’, false)”,
#11 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/User.php(782): yii\base\Application->getAuthManager()”,
#12 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/User.php(792): yii\web\User->getAuthManager()”,
#13 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/User.php(736): yii\web\User->getAccessChecker()”,
#14 /home/ubuntu/panelapi/api/modules/v1/controllers/PanelistController.php(1616): yii\web\User->can(‘get-survey-pane…’)”,
#15 [internal function]: api\modules\v1\controllers\PanelistController->actionGetSurvey(‘225659’)”,
#16 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)”,
#17 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)”,
#18 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction(‘get-survey’, Array)”,
#19 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction(‘v1/panelist/get…’, Array)”,
#20 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))”,
#21 /home/ubuntu/panelapi/api/web/index.php(49): yii\base\Application->run()”,
#22 {main}”
],
“previous”: {
“name”: “Exception”,
“message”: “not master”,
“code”: 10107,
“type”: “MongoDB\Driver\Exception\CommandException”,
“file”: “/home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Command.php”,
“line”: 186,
“stack-trace”: [
#0 /home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Command.php(186): MongoDB\Driver\Manager->executeCommand(‘panelmanagement’, Object(MongoDB\Driver\Command), Object(MongoDB\Driver\ReadPreference))”,
#1 /home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Command.php(357): yii\mongodb\Command->execute()”,
#2 /home/ubuntu/panelapi/vendor/yiisoft/yii2-mongodb/src/Collection.php(153): yii\mongodb\Command->createIndexes(‘auth_item’, Array)”,
#3 /home/ubuntu/panelapi/common/components/MongodbManager.php(59): yii\mongodb\Collection->createIndex(Array, Array)”,
#4 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/BaseObject.php(109): common\components\MongodbManager->init()”,
#5 [internal function]: yii\base\BaseObject->__construct(Array)”,
#6 /home/ubuntu/panelapi/vendor/yiisoft/yii2/di/Container.php(375): ReflectionClass->newInstanceArgs(Array)”,
#7 /home/ubuntu/panelapi/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build(‘common\componen…’, Array, Array)”,
#8 /home/ubuntu/panelapi/vendor/yiisoft/yii2/BaseYii.php(349): yii\di\Container->get(‘common\componen…’, Array, Array)”,
#9 /home/ubuntu/panelapi/vendor/yiisoft/yii2/di/ServiceLocator.php(137): yii\BaseYii::createObject(Array)”,
#10 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Module.php(742): yii\di\ServiceLocator->get(‘authManager’, false)”,
#11 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Application.php(599): yii\base\Module->get(‘authManager’, false)”,
#12 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/User.php(782): yii\base\Application->getAuthManager()”,
#13 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/User.php(792): yii\web\User->getAuthManager()”,
#14 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/User.php(736): yii\web\User->getAccessChecker()”,
#15 /home/ubuntu/panelapi/api/modules/v1/controllers/PanelistController.php(1616): yii\web\User->can(‘get-survey-pane…’)”,
#16 [internal function]: api\modules\v1\controllers\PanelistController->actionGetSurvey(‘225659’)”,
#17 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)”,
#18 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)”,
#19 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction(‘get-survey’, Array)”,
#20 /home/ubuntu/panelapi/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction(‘v1/panelist/get…’, Array)”,
#21 /home/ubuntu/panelapi/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))”,
#22 /home/ubuntu/panelapi/api/web/index.php(49): yii\base\Application->run()”,
#23 {main}”
]
}
}