{"id":10334,"date":"2023-08-31T14:41:33","date_gmt":"2023-08-31T19:41:33","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=10334"},"modified":"2023-09-06T09:01:22","modified_gmt":"2023-09-06T14:01:22","slug":"mongodb-setting-up-a-replica-set","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=10334","title":{"rendered":"MongoDB: Setting Up a Replica Set"},"content":{"rendered":"<p>On one server create a key file. Copy this key file to all other servers that will participate in the replica set<\/p>\n<pre>mkdir -p \/opt\/mongodb\/keys\/\r\nopenssl rand -base64 756 &gt; \/opt\/mongodb\/keys\/$(date '+%Y-%m-%d').key\r\nchmod 400 \/opt\/mongodb\/keys\/$(date '+%Y-%m-%d').key\r\nchown -R mongodb:mongodb \/opt\/mongodb\/keys\/$(date '+%Y-%m-%d').key\r\n<\/pre>\n<p>On each server, edit \/etc\/mongo.conf and add the keyfile to the security section <em>and<\/em> define a replica set<\/p>\n<pre>security:\r\n authorization: enabled\r\n keyFile:  \/etc\/mongodb\/keys\/mongo-key\r\n#replication:\r\nreplication:\r\n  replSetName: \"myReplicaSet\"<\/pre>\n<p>Restart MongoDB on each node.<\/p>\n<p>On one server, use mongosh to enter the MongDB shell.<\/p>\n<pre>rs.initiate(\r\n{\r\n_id: \"myReplicaSet\",\r\nmembers: [\r\n{ _id: 0, host: \"mongohost1.example.net\" },\r\n{ _id: 1, host: \"mongohost2.example.net\" },\r\n{ _id: 2, host: \"mongohost3.example.net\" }\r\n]\r\n})<\/pre>\n<p>Use <tt>rs.status()<\/tt> to view the status of the replica set. If it is stuck in STARTING &#8230; check connectivity. If the port is open, I ran into a snag with some replacement servers. They&#8217;ve got temporary hostnames. But you cannot add a host on itself &#8212; it ignores that you typed mongohost1.example.net &#8230; and it takes <em>it&#8217;s<\/em> <code>hostname<\/code> value. And then sends that value to the other servers in the replica set. If you cannot change the hostname to match what you want, there is a <a href=\"https:\/\/www.mongodb.com\/docs\/manual\/tutorial\/change-hostnames-in-a-replica-set\/\" target=\"_blank\" rel=\"noopener\">process to change the hostname in a replicaset<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On one server create a key file. Copy this key file to all other servers that will participate in the replica set mkdir -p \/opt\/mongodb\/keys\/ openssl rand -base64 756 &gt; \/opt\/mongodb\/keys\/$(date &#8216;+%Y-%m-%d&#8217;).key chmod 400 \/opt\/mongodb\/keys\/$(date &#8216;+%Y-%m-%d&#8217;).key chown -R mongodb:mongodb \/opt\/mongodb\/keys\/$(date &#8216;+%Y-%m-%d&#8217;).key On each server, edit \/etc\/mongo.conf and add the keyfile to the security section and &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1907],"tags":[429],"class_list":["post-10334","post","type-post","status-publish","format-standard","hentry","category-mongodb","tag-mongodb"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10334"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10334\/revisions"}],"predecessor-version":[{"id":10335,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/10334\/revisions\/10335"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}