Initiate a replica set
The Debezium MongoDB connector can capture changes from a single MongoDB replica set. In production replica sets it is recommended to have at least three members, the connector can work with fewer members, it doesn’t care how many members are in the replica set.
To use the Debezium MongoDB connector with a replica set, simply provide the addresses of one or more replica set servers as seed addresses via the connector’s mongodb.hosts property. The connector will use these seeds to connect to the replica set, and then once connected will get from the replica set the complete set of members and which member is primary. The connector will start a task to connect to the primary and capture the changes from the primary’s oplog. When the replica set elects a new primary, the task will automatically switch over to the new primary.
With at least one MongoDB server running, start a container with this image to verify if the replica set is initiated. If it is not initiated, initiate the replica set and add all of the servers to it.
Start the container with the name of the replica set in the $REPLICASET
environment variable, using links named like MONGO_n
(where n=1,2,3, etc.) for each of the MongoDB servers that are to be in the replica set.
docker run -it --name mongo-init --rm -e REPLICASET=rs0 --link data1:mongo1 --link data2:mongo2 --link data3:mongo3 debezium/mongo-initiator
Initiate a sharded cluster
A MongoDB sharded cluster consists of:
- a separate replica set that acts as the cluster’s configuration server
- one or more shards, each deployed as a replica set
- one or more routers (mongos) to which clients connect and that routes requests to the appropriate shards
To use the Debezium MongoDB connector with a sharded cluster:
- Configure the connector with the host addresses of the configuration server replica set.
The container can be added to the replica set as a shard to one or more MongoDB routers. For example, consider three MongoDB servers running in containers shardA1
, shardA2
, and shardA3
, and two MongoDB routers running in containers router1
and router2
. The following command will ensure that shardA1
, shardA2
, and shardA3
are properly initiated as replica set shardA, and that the shardA
replica set is added as a shard to the routers router1 and router2.
docker run -it --name mongo-init --rm -e REPLICASET=shardA --link shardA1:mongo1 --link shardA2:mongo2 --link shardA3:mongo3 --link router1 --link router2 debezium/mongo-initiator
- More shard replica sets can be added by running more containers. For example:
docker run -it --name mongo-init --rm -e REPLICASET=shardB --link shardB1:mongo1 --link shardB2:mongo2
When the connector connects to this replica set, it acts as the configuration server for a sharded cluster and discovers the information about each replica set used as a shard in the cluster
The connector starts up a separate task to capture the changes from each replica set. If new shards are added to the cluster or existing shards removed, the connector will automatically adjust its tasks accordingly.