5

I am working with laravel 5.1 with mongodb. For mongodb I am using jenssegers mongo configuration.

Now I have to make mongodb replica set and I want to make read operation only from slave & write operation at master.

In laravel's documentation I read that we can make separation in read & write like below:

'mysql' => [
    'read' => [
        'host' => '192.168.1.1',
    ],
    'write' => [
        'host' => '196.168.1.2'
    ],
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
],

Here is link for how to set configuration in laravel.

In jenssegers documentation here is configuration for replication:

'mongodb' => [
    'driver'   => 'mongodb',
    'host'     => ['server1', 'server2'],
    'port'     => env('DB_PORT', 27017),
    'database' => env('DB_DATABASE'),
    'username' => env('DB_USERNAME'),
    'password' => env('DB_PASSWORD'),
    'options'  => ['replicaSet' => 'replicaSetName']
],

So if I change this to:

'mongodb' => [
        'driver'   => 'mongodb',
        'read'     => ['host' => '192.168.1.1'],
        'write'    => ['host' => '192.168.1.2'],
        'port'     => env('DB_PORT', 27017),
        'database' => env('DB_DATABASE'),
        'username' => env('DB_USERNAME'),
        'password' => env('DB_PASSWORD'),
        'options'  => ['replicaSet' => 'replicaSetName']
    ],

So can I use the above configuration in jenssegers mongo db configuration?

チーズパン
  • 2,752
  • 8
  • 42
  • 63
Vishant dhandha
  • 495
  • 7
  • 19
  • 2
    Before you even do, you should really understand what you are asking. Firstly you need to forget the "Master -> Slave" relationship because that really does not apply here. Nor does the general concepts of "read" and "write" nodes apply in the same way as it does with SQL Relational databases. MongoDB replication is an entirely different animal, with different function. 90% of the time, you **will** actually want reads from the "PRIMARY", unless you actually know what you are doing. If think you need to read up on [Replication](https://docs.mongodb.org/manual/replication/) first. – Neil Lunn Apr 18 '16 at 07:48
  • There is https://docs.mongodb.org/manual/core/read-preference/ though. – Maximilian Riegler Apr 18 '16 at 09:26

0 Answers0