Code Monkey home page Code Monkey logo

mongodb-replica-set's Introduction

MongoDB replica set

Simple MongoDB replica set docker compose file.

In order to use Transaction function in MongoDB, you need a set of a replicated databases named MongoDB replica set.

This project makes it easy to build a MongoDB replica set circumstances by using docker container.

Requirements

This project uses a Docker container for virtualizing Database circumstances. Therefore, Docker must be installed

To set localhost host environment, Direnv must be installed

how to install direnv?

install with brew

brew install direnv

set bash configuration(if you use bash) set zsh configuration(if you use zsh)

vi ~/.bashrc or vi ~/.zshrc

# add below configuration
eval "$(direnv hook bash)" or eval "$(direnv hook zsh)"

# apply
source ~/.bashrc or source ~/.zshrc

Installation

Pull all the necessary docker image from docker hub and set up replica set configuration

docker-compose up

Set host name so that localhost can connect to docker container's internal ip with given mongodb configuration setting

direnv allow

Set host for connecting to docker inner network

Changing directory to /dev triggers direnv setting so that hostname mongo-rs0-1, mongo-rs0-2, mongo-rs0-3 will be registered in you pc

cd dev

If you have Err: direnv: error .envrc is blocked Run direnv allow to approve its content.

direnv allow

Check which server is Primary node

docker exec mongodb-replica-set_mongo-rs0-1_1 bash -c 'mongo --eval "rs.status()"'

result Result

In members field you can find member who has "PRIMARY" in "stateStr" field, and that is a Primary Node

Connect to replica set

mongo 'mongodb://test:test1234@mongo-rs0-1:27017,mongo-rs0-2:27018,mongo-rs0-3:27019/test?replicaSet=rs0'

Connect to primary node

mongo --host `mongo mongo-rs0-1:27017 --quiet --eval "db.isMaster()['primary']"`

Create Account

Create Admin Account

mongo
use admin
db.createUser({user: "root", pwd: "test1234", roles: [{ role: "root", db: "admin" }] });

Create Database and matching account

mongo
use testdb
db.createUser(
 { user: "test",
 pwd: "test1234",
 roles: ['readWrite','userAdmin'] } )

mongodb-replica-set's People

Contributors

effy-coding avatar frontalnh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mongodb-replica-set's Issues

"Connect to the replica set" not working

Hello, thank you for your work and share!
I followed the step and when I was doing this step.
mongo 'mongodb://test:test1234@mongo-rs0-1:27017,mongo-rs0-2:27018,mongo-rs0-3:27019/test?replicaSet=rs0'
I cannot connect to the set and got error as below:
MongoDB shell version v3.6.3 connecting to: mongodb://mongo-rs0-1:27017,mongo-rs0-2:27018,mongo-rs0-3:27019/test?replicaSet=rs0 2020-10-19T15:13:50.780+0200 I NETWORK [thread1] Starting new replica set monitor for rs0/mongo-rs0-1:27017,mongo-rs0-2:27018,mongo-rs0-3:27019 2020-10-19T15:13:50.785+0200 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:50.787+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:50.788+0200 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:50.789+0200 W NETWORK [ReplicaSetMonitor-TaskExecutor-0] Unable to reach primary for set rs0 2020-10-19T15:13:50.789+0200 I NETWORK [ReplicaSetMonitor-TaskExecutor-0] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row. 2020-10-19T15:13:51.293+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:51.297+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:51.300+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:51.301+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:51.301+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 2 checks in a row. 2020-10-19T15:13:51.805+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:51.808+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:51.811+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:51.811+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:51.811+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 3 checks in a row. 2020-10-19T15:13:52.315+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:52.320+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:52.324+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:52.324+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:52.324+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 4 checks in a row. 2020-10-19T15:13:52.829+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:52.833+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:52.837+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:52.837+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:52.837+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 5 checks in a row. 2020-10-19T15:13:53.341+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:53.344+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:53.346+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:53.346+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:53.346+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 6 checks in a row. 2020-10-19T15:13:53.850+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:53.854+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:53.857+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:53.857+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:53.857+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 7 checks in a row. 2020-10-19T15:13:54.361+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:54.362+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:54.364+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:54.364+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:54.364+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 8 checks in a row. 2020-10-19T15:13:54.869+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:54.874+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:54.877+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:54.877+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:54.877+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 9 checks in a row. 2020-10-19T15:13:55.380+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:55.381+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:55.395+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:55.395+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:55.395+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 10 checks in a row. 2020-10-19T15:13:55.899+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:55.902+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:55.905+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:55.905+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:55.905+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 11 checks in a row. 2020-10-19T15:13:56.409+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:56.411+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:56.417+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:56.417+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:56.931+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:56.933+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:56.937+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:56.937+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:57.439+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:57.441+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:57.443+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:57.443+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:57.947+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:57.951+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:57.953+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:57.953+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:58.457+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:58.459+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:58.461+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:58.461+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:58.963+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:58.965+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:58.966+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:58.966+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:59.470+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:59.473+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:59.475+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:59.476+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:13:59.979+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:13:59.982+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:13:59.985+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:13:59.985+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:00.493+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:00.499+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:00.504+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:00.505+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:01.008+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:01.011+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:01.012+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:01.012+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:01.012+0200 I NETWORK [thread1] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 21 checks in a row. 2020-10-19T15:14:01.515+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:01.517+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:01.518+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:01.518+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:02.022+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:02.025+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:02.029+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:02.029+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:02.534+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:02.537+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:02.541+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:02.541+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:03.044+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:03.046+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:03.047+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:03.047+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:03.551+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:03.555+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:03.559+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:03.559+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:04.062+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:04.065+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:04.067+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:04.067+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:04.572+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:04.575+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:04.579+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:04.579+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:05.084+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:05.088+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:05.091+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:05.091+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:05.595+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-2") failed: No address associated with hostname 2020-10-19T15:14:05.599+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-1") failed: No address associated with hostname 2020-10-19T15:14:05.602+0200 I NETWORK [thread1] getaddrinfo("mongo-rs0-3") failed: No address associated with hostname 2020-10-19T15:14:05.602+0200 W NETWORK [thread1] Unable to reach primary for set rs0 2020-10-19T15:14:05.602+0200 E QUERY [thread1] Error: connect failed to replica set rs0/mongo-rs0-1:27017,mongo-rs0-2:27018,mongo-rs0-3:27019 : connect@src/mongo/shell/mongo.js:251:13 @(connect):1:6 exception: connect failed

Can you kindly take a look and give some insights?
Thank you : )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.