0

Is there any ways or methods to start mongodb replication directly when mongod service start? I don't want to enter to shell and ON the replication? Thanks!

Swe Zin Phyoe
  • 149
  • 1
  • 2
  • 11

3 Answers3

0

You can create a mongod service which starts automatically when server starts. First you need to create a configuration file(mongodb.conf) which will include configuration settings such as replicaSet name etc. Then create a service and install it using following command

mongod -f c:\mongod.conf --install

Then start the service using

net start mongodb

Read about configuration file here and How to install mongo as service here

Anirudh Bagri
  • 2,346
  • 1
  • 21
  • 33
  • No! I have already installed 2 servers(master and slave) replication. But my problem is I always open 2 shells and type(mongo) to enter shell and when i insert data in master server then i go to type (rs.slaveOk()) in slave server to replicate! I want to know if any ways or commands to run replication even from sudo service mongod start ? I dun want to go to run from the above i said! Thanks – Swe Zin Phyoe Dec 02 '17 at 04:41
  • If your configuration is right, replication should start automatically. Make sure they belong to same replicaSet. If you insert data to master it should automatically be replicated to slave servers as well. Try rs.setSlaveOk() in secondary server, I'm not sure about it. – Anirudh Bagri Dec 02 '17 at 06:17
  • yes. i always command rs.setSlaveOk() in secondary after inserting some data in primary! Is it error? – Swe Zin Phyoe Dec 02 '17 at 07:43
  • Yes..looks like an error, because data must be replicated automatically if everything is configured correctly. – Anirudh Bagri Dec 02 '17 at 07:47
0
  • When you create a valid replica set in mongodb, your data will be asynchronously from the primary member to the secondary members in replica set

  • Having said that, you're not required to do extra efforts manually to get data replication done

  • When you do rs.slaveOk() on secondary, that allows you to query data from secondary members in the replica set.

  • It's a provision. It allows you to read from secondary provided that you're can tolerate the possible eventual consistent data. The replication does not happen when you do rs.slaveOk() on secondary

Atish
  • 4,277
  • 2
  • 24
  • 32
  • I have already installed 2 servers(master and slave) replication. But my problem is I always open 2 shells and type(mongo) to enter shell and when i insert data in master server then i go to type (rs.slaveOk()) in slave server to replicate! I want to know if any ways or commands to run replication even from sudo service mongod start ? I dun want to go to run from the above i said! Thanks – Swe Zin Phyoe Dec 02 '17 at 09:10
  • rs.slaveOk() doesn't do replication. Replication is automatically done. If you want to check replicated data on secondary then you have to do rs.slaveOk(). There is no other way around for reading data from secondary – Atish Dec 02 '17 at 09:46
  • :D so the only way i know whether the data is replicated or not is typing rs.slaveOk() in secondary? is there any other ways to know ? sir :d Thanks – Swe Zin Phyoe Dec 04 '17 at 03:04
0

I'm not sure to understand. Your question was about service start. On my part, I install mongo on ubuntu and the service is not started with replicatet mode. Finally, I disabled the first one and I created another service with the option --replSet myReplicat . When you have only 2 servers, there is a problem with majority votes. On my part, I had 2 secondary after I stopped the primary and it was difficult to comeback with 1 primary and 1 secondary. Effectively, the replication is always active. By default, all connections should go to the Primary. If you want to readonly from a secondary, you first enter the commande rs.slaveOk(). This command is active at session level. If you reconnect, you have to pass it again. It is not possible to put it at server side.