Yii Framework Forum

Mongodb master slave connection

(yatinmistry5130@gmail.com) #1

Hi YiiTeam,

How to connect mongodb with master slave config ?

My db will be on different servers.

If master is down then it will be switch between server and i have to select my master db using PHP. Please guide how to do in yii2-mongodb extension ?


Yatin Mistry

(yatinmistry5130@gmail.com) #2

For my solution i have set mongodb replica set. It worked

Below is connection string (i have refered here)

// MongoDB

$config["components"]['mongodb'] = [

    'class' => '\yii\mongodb\Connection',

    'dsn' => 'mongodb://,,',


(Arihant Jain) #3

Hi @yatinmistry5130

I just wanted to check you, if you used read-preferences as well. When I use reapreference as Secondary, I am seeing errors when I read from secondary.