Dont know if anyone gives a damn but if you guys are stoned like me atm then you can watch pretty much all of the new series on instaflixxer. Have been binge watching with my brother for the last few days =)
I just found this video, i couldve saved hours from the things i did which is only 12 minutes in this videos creating a replica set with 3 instances of mongdb. Wow! im gonna watch the whole series now. Thank you so much!
what would be connecting string if we wanted to connect to this same replica set using the mongsqld commands i used './mongosqld --mongo-uri "mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017,mongo-2.mongo:27017/?replicaSet=rs0&readPreference=secondary"' and i am getting the error current topology: Type: ReplicaSetNoPrimary, unable to discover
Hey Bro You forgot an experiment in this Lab mongodb replica set with 3 Nodes will be very good but if 1 of 3 Nodes die then there is no problem. But if 2 out of 3 nodes, an even number, die, you know the consequences. Without a Primary Node, what will you do now? And the next thing is that the Primary's job is divided among the Slave Nodes. But the main responsibility is still the Primary Node. Suppose at some point your Primary Node is Full Disk and your system resources cannot be scaled vertically, what do you plan to do with your MongoDB Cluster?. Please give me your answer as soon as possible. Thanks!!!
Thanks for the video Venkat. But something wrong when i have connect to replica Sets like this: "client = MongoClient("mongodb://mongodb0:27017,mongodb1:27017,mongodb2:27017/?replicaSet=rs0")" and error below: "ServerSelectionTimeoutError errno 11001 getaddrinfo failed python" seem the Primary did not connect to Secondary for reading data. Could you help?
Why I got "writeConcern is not allowed within a multi-statement transaction" when trying the "multi-document transaction". I ran in on primary node. Please help!
Hi Nirmal, thanks for watching. You can do that. In fact you can set up a replicaset with nodes running anywhere. But you have to bear in mind the latency and other stuff. To be able to setup a replicaset, all you need is mongo instances running. It can run anywhere. You have to take into account firewall, latency and security. If the nodes can talk to each other on port 27017, then you can hook them up.
At 10:46 when you execute the cat command, I cannot see the IP Address of my 3 mongodb's, and all my 3 machines are running, can you tell how can I add this data?
Hi, using the local server with three instances without issue, but if have three separate servers, facing node not found error. Are you able to advice? Thank you.
Great video, quick question. I used docker and not lxc. If i put all three "mongoDb servers" on three dockers, it works fine. If I use two dockers, and my local machine, it does not work and it says network is unreachable. Any idea why this might be the case? I've exposed ports, and mapped them correct `docker run -p 27017:27017` A bit confused as to why the dockers can interact with each other, but my host machine cannot interact with any of the containers?
@@justmeandopensource yes! rs.initiate() doesn't work. Then I put my "host machine mongoDb" on to a docker as well, and works with 3 dockers beautifully.
@@david19993 Are you defining a separate network for your docker containers in your docker-compose file? I haven't actually tried that setup to be honest.
@@justmeandopensource well, my dockers have `172.17.0.*` as IPs, and so I just use my host machines IP address. It may be something to do with my firewall. Anyway, this is not a question about mongoDb but more docker. Thanks for the video, the response, and just being a great experience! Subscribed!
I have to execute rs.slaveOk() everytime on the secondary nodes to read databases. How can I avoid executing this everytime when connecting to the secondary.
Hi Varun, I don't think there is a way to set the rs.slaveOk() in a permanent way. rs.slaveOk() is set per connection session. This is needed only when you are directly connecting to a secondary member of the replicaset. Why do you want to connect directly to a secondary member. You can still connect to the full replicaset and direct the reads to secondary members by passing readPreference option in the connection string. For example, if your replicaset name is myrepl and your hosts are mongo-0, mongo-1 & mongo-2, then you can use below connection string. $ mongo mongodb://mongo-0:27017,mongo-1:27017,mongo-2:27017/admin?replicaSet=myrepl&readPreference=secondary This will do any reads from secondary and writes will happen on primary. Cheers.
lets say I make some changes in primary (create database test-db, and db.tester.insert() some values) Without doing rs.slaveOk() how can I simply view the changes in secondary? So when I update primary, I don't want to update the values. But how can I do a .find() in my secondary servers without explicitly entering `rs.slaveOk()`?
@@david19993 The replica set will take care of the replication between its members. May I ask you why you want to connect directly to a secondary member? If you want to directly connect to a secondary member, then there is no way other than running rs.slaveOk() to interact with the database.
Thanks for very helpful video. I have one confusion When you connected to first mongo instance and did rs.initiate() In mongo prompt appears that is secondary but after you rs.status() it shows that node is primary?
Hi Venkat, Thanks for nice video. I did mongodb replica set by seeing your vidoe, however noticed secondary node asks to run rs.slaveOk() command before run any command when i connects to its(secondary node) mongo shell. Could you please let me know is this expected beheaviour.
what is the difference between rs.initiate() vs rs.initiate( { _id : "rs0", members: [ { _id: 0, host: "localhost:27017" }, { _id: 1, host: "localhost:27018" }, { _id: 2, host: "localhost:27019" } ] }) what is the condition of both cases.
Hi Atul, thanks for watching. rs.initiate() requires replicaset configuration. You either add the configuration beforehand and run rs.initiate() or pass the replicaset configuration within this method.
Is there anyway of doing a multi master set-up in mongodb like mysql. I have a multi-cloud replica setup on digitalocean and linode where my primary is on digitalocean . I would like to have one-more master on linode so that in case the communication is cut between the two environments I would still want my application on linode to perform write operations.
HI Varun, thanks for watching. The master node in MongoDB cluster is called Primary. No master/slave concept. It is Primary/Secondary. By design, you can't have two primaries in a cluster. What you can do is spread your replicaset across different providers DO and Linode. If one of the provider has problem, then a secondary from another provider can take the primary role.
Hi Abhimanyu, thanks for watching. Not just Mongodb cluster, generally in any cluster you will have odd number of nodes. In mongodb replicaset, the minimum nodes recommended is 3. You can however run just two nodes with one primary and one secondary. As long as the master node is running fine, you won't see any problem. But if master dies, the only secondary node will have to take the primary role but it might not have catched up with the master. So loss of data might happen. If there are two secondaries, there will be election to find out which can become the primary.
Hi venkat I am from India..the explanation was really great but I have one doubt: I have converted my standalone to replica sets in windows lastday but today after restarting my system 1.I am unable to Rerun my replica sets(getting this error: connect failed to replica set myReplicaSet/localhost:27017,localhost:27027,localhost:27037). 2.when I run my primary node(27017)..it is telling me that " "errmsg" : "This node was not started with the replSet option" when i typed rs.status() and also giving throwing as "Document(s) exist in 'system.replset', but started without --replSet." PLEASE HELP ME VENKAT
@@justmeandopensource When I installed MongoDb in Windows it showed showed as Standalone cluster(mongo compass UI) hosted with port number: 27017 then..in shell I used the below command to make it as replica set. mongod --dbpath "C:\Program Files\MongoDB\Server\4.4\data" --logpath "C:\Program Files\MongoDB\Server\4.4\log\mongod.log" --port 27017 --storageEngine=wiredTiger --journal --replSet myReplicaSet
Dear, How to use/set `/etc/hosts` for difference ip? Does you do this on your local machine? 10.92.25.103 mongo0 10.92.250.227 mongo1 10.92.250.62 mongo2 What difference with 3 docker container in difference port? ports: - 40001:27017 ports: - 40002:27017 ports: - 40003:27017
Hi Theara, Thanks for watching this video. Are you running these three mongodb containers on the same machine or on a separate virtual machines? If you want to run all 3 mongo containers on the same machine using docker-compose, you need to map them to different ports on your host machine. If you are running each of the mongo containers in its own virtual machine, then you can just run them on 27017 the default port. Thanks.
Hi Theara, I just responded to the github issue. Please use this RU-vid chat section for all your queries. This will help other viewers as well. Thanks.
Hi, thanks for watching. This below video will give you some idea. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-SY_9zwb29LA.html Basically you bring up a replicaset with authentication enabled on all nodes either through a config file or through a command line option "--auth" and then set up users/roles on primary which will get replicated.
hi Sir, I am getting some different message of the command rs.status() rs.status() { "ok" : 0, "errmsg" : "not running with --replSet", "code" : 76, "codeName" : "NoReplicationEnabled" } What is the meaning of this ?
Hello.....I am back again with one question... So, I deployed a replica set of MongoDB and I have three machines (Mongo1:127.0.0.1(Primary), Mongo2:127.0.0.2(sec), Mongo3:127.0.0.3(sec)). Now we know if the mongo1(primary) will die then one of the secondary will be primary and when it will b back it will be one of the secondary nodes. Suppose I am using the mongo1 IP in all my scripts and application which is 127.0.0.1 and as we know all read and write are on primary so suppose if it will die then I have to go and change the primary IP in all my script. How I can handle this problem.
Hi Abhi, thanks for watching. Whenever you are connecting to a replicaset from a mongo shell or from any client application, you will have to use the replicaset uri and not the ip address of a single mongo node. In your case, you will have to replicaset uri like "mongodb://mongo1:27017,mongo2:27017,mongo3:27017/database?replicaSet=replicaset-name". By using this URI format, the connection will be made to any available mongo node in the cluster. Cheers.
@@abhimanyukumar4185 By using that connection string, it will connect to any available instance in the replicaset. In a replicaset if the master node goes down, one of the secondaries will become the primary. You will have to implement some form of monitoring to get alerted when a node goes down and bring it as soon as possible.
@@justmeandopensource I got that. Suppose mongo1 is master and it goes down and mongo3 will be next master so it will go to that one. If i am getting it right.
thank you for this very explanatory tutorial. I have a question: can we just use 2 machines for replication or 3 machines is the minimum? I can't find anything about it in the documentation Thanks.
Hi Chatti, thanks for watching. I would always advise to go with a minimum of 3 node replicaset with all of them containing the data. If a node goes down, then you will only be left with one node if you had just 2 node replicaset. If you don't want to replicate the data on 3 nodes, you can just have 2 nodes that contains data and an arbiter node that only takes part in election and doesn't hold the actual data. docs.mongodb.com/manual/core/replica-set-architecture-three-members/
Hi, thanks for watching. I have a separate series covering LXC/LXD concepts. If you are interested. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-CWmkSj_B-wo.html
@@justmeandopensource Error: Failed container creation: Create LXC container: LXD doesn't have a uid/gid allocation. In this mode, only privileged containers are supported
Hi James, What do you mean by read replicas? You can just add additional secondary members to your replicaset if your read traffic is increasing. All your reads will happen on secondary members when you pass the "readPreference=secondary" to the mongodb connection string when connecting. Cheers.
There does not seem to be mongo services deployed on docker swarm , just separate mongodb containers on different hosts. Mapping in /etc/hosts also seems counter productive , because it implies you always want mongo0 on hostA , mongo1 on hostB and mongo2 on hostC. That is not how services deployed on docker swarm work - mongo0 , mongo1 and mongo2 as services could be deployed randomly on hostA or hostB or hostC.
Hey hi, thanks for the such a great video. I have question, i have 3 slave and 1 master node mongodb replicaset in kubenates. I used the replicaset name to connect inside the cluster as you shown in video. Now i want to connect outside the cluster, how can i do that. Thank in advance.
Hi Rajesh, thanks for watching this video. I actually have this in my list. The video will be about deploying mongodb as a statefulset inside a Kubernetes cluster. Only if you are deploying mongodb replicaset outside the k8s cluster you will have to think about traditional architecture and accessing the replicaset uri, by passing all the replicas in the uri. Whereas if you are deploying inside a kubernetes cluster, you can just deploy them as a normal kubernetes replicaset. And expose the deployment as a LoadBalancer or nodeport service. I haven't tried any of these. But will soon record a video for it. Thanks.
Thanks for your reply, When i exposed as LoadBlaancer or Node Port, it will switch b/w primary and secondary(works in round robbin .).My question is how can handle in the application. Should i use Drivers or any think else. Please helm me here. Thanks in advance.
@@RAJESHNAYAK-wg5li You don't deploy them as a mongodb replicaset with primary and secondary inside the cluster. Its like any other deployment with number of replicas. So all of them are same and no primary/secondary concept. Thanks.
Thank you so much for this tutorial. It's great! I have a problem, it would be great if you can help! (i've searched a lot online, but no luck) i did the exact replicaset over 2 servers: Server A in docker: db1, db2 & Server B in docker: db3 if db1 was primary & i stop the container db1, db2=> will be set to primary, and everything will keep on working fine same story for db2 however if i stop db1 and db2 (or if i shutdown the whole server of db1 and db2), db3 is staying forever secondary! any idea?
Hi Theara, mongos router is very light-weight and acts as an interface/gateway to your shards. From your applications you connect to one of the mongos router and do the db operations. Mongos will read/write data to the appropriate shard. Config servers are deployed as replicaset which stores information about the shards in your cluster. mongos router retrieves these information from config servers and uses it to find the data in the shard. You don't need to deploy mongos as a replicaset. You can run as many mongos router as you want on any machine. Typically a mongos router will be configured on the server where your client application reside. For more information you can check the official mongos documentation in the below link. docs.mongodb.com/manual/core/sharded-cluster-query-router/ Thanks.
Yes. It can be deployed anywhere on any machine, preferably on the server itself where your client application is running. Once you create the mongos router anywhere, you connect to it and add the shards that you want. Thanks.
Thanks for the awesome video Venkat :) I have a small question. -> I am setting up 3 mongo docker containers in the same centos machine. but I am not able to change the default port. I don't want to use docker-compose file, do u have any solution for Dockerfile? where I can change the default MongoDB port? mongodb0 docker run --hostname mongodb0 -d -p 25000:27017 -v /data/mongodb0:/data/db/ --network='database' --name mongodb0 mongo:latest --replSet "rs0" mongodb1 docker run --hostname mongodb1 -d -p 26000:27017 -v /data/mongodb1:/data/db/ --network='database' --name mongodb1 mongo:latest --replSet "rs0" mongodb2 docker run --hostname mongodb2 -d -p 27000:27017 -v /data/mongodb2:/data/db/ --network='database' --name mongodb2 mongo:latest --replSet "rs0"
Hi Abhishek, thanks for watching. Your docker run commands seems to right. Since you are running all these containers on the same host machine, you will have to bind them to different ports like what you did. I am not entirely sure what you are after?
I have a Question. How can we configure authentication for client connections on a replica set? One other thing, have been following your new AWS series and I am really learning a lot.
Hi Varun, thanks for watching and for your interest in this series. I have done a video on configuring authentication in Mongo DB which you can watch it at ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-SY_9zwb29LA.html Thanks.
Hi Theara, there is no specific answer to this question. It depends on a lot of factors like - how many shards you need - how big you expect your database to grow - what OS you are going to use - whether you are going to run mongo as docker containers or install directly on droplets For production use case, consider reading the below article www.mongodb.com/blog/post/capacity-planning-and-hardware-provisioning-mongodb-ten-minutes For just learning purpose, you can do with just one Digital Ocean droplet and run as many mongodb containers as you want in as many shards as you like. I have done couple videos on sharding, hope you watched them as well. If not here are the links. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-7Lp6R4CmuKE.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-LGERGvEaPW0.html ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-Rwg26U0Zs1o.html Thanks
thanks for your amazing videos. Quick question . Can I have my own cluster of mongodb ? , If yes why I need to pay amazong and other companies to do that . I am thinking that I'm missing something very important. For example I'm never going to use docker to run my relational database . Can you please help me to understand it in a better way
Hi bro great video i followed the steps and I created the three mongodb instance but when i am using read preference its not connecting via spring boot can you advice getting this error" No server chosen by com.mongodb.client.internal.MongoClientDelegate"
Hi Leo, thanks for watching. Can you check if you can access the replicaset from a mongo shell first? And what is the connection string you are using from your spring boot application?
@@justmeandopensource I can connect to replica set without read preference but when read preference is given its not going to shell ,but replication is happening when i create some collections in primary node
Hi Atul, thanks for watching. There are different ways to add members to a replicaset. What I have shown in this video is one method. If you want to add individual members later, you can follow your approach. Cheers.
Thanks for your video. I base on Mac. I tried to run 3 docker container that difference by port (Not 3 host machine). I have problem with mongo connection ``` mongo 'mongodb://mongo1:27018,mongo2:27019:27020/?replicaSet=rs0 --- no primary found in replicaset or invalid replica set name ``` cat /etc/hosts ``` 127.0.0.1 mongo1 127.0.0.1 mongo2 127.0.0.1 mongo3 ```
Hi Theera, Thanks for watching this video. I am not sure how you are starting the 3 docker containers for mongodb. Basically you run the same docker mongodb container in the default port 27017 but map to different host port on your mac. And don't have 127.0.0.1 entries in your /etc/hosts file. That will cause other problems. I am not sure if you have watched my other mongodb videos around sharding concept. I have docker-compose.yaml file ready for you to use. Check the below docker-compose file. github.com/justmeandopensource/learn-mongodb/blob/master/sharding/shard1/docker-compose.yaml Download it and run "docker-compose up -d". It will start 3 mongodb containers. All these containers use the default port 27017 but they are mapped to host machine on 50001, 50002 and 50003. Now you need to connect to one of the instance and initialize the replicaset. Check the below doc under shard1 servers. github.com/justmeandopensource/learn-mongodb/blob/master/sharding/00-setup-sharding-doc.md Now you can connect to the replicaset with the ip address of your mac machine. For example, if you mac's ip is 192.168.1.5, you can use the below command to connect. mongo mongodb://192.168.1.5:50001,192.168.1.5:50002,192.168.1.5:50003/?replicaSet=rs0 Use the replicaset name in the above command as what you defined while initializing the replicaset. Hope this makes sense. Thanks
@@justmeandopensource Thanks for your quick reply. Yes I tried with `docker-composer.yml`, and mapped to host machine on difference port. Ex: 27018:27017, 27019:27017, 27020:27017 But don't use `--shardsvr`. I don't understand about `--shardsvr` option?. Oh I connect to mongo with `localhost:27018,localhost:27019, localhost:27020/?.....` I will try with your doc...
At 14:50 when you run this command I am getting this error - MongoNetworkError: getaddrinfo ENOTFOUND mongodb0 Can you help me in resolving this issue?