Skip to content

Instantly share code, notes, and snippets.

@zubair1024
Last active January 28, 2020 22:06
Show Gist options
  • Save zubair1024/236a2f9acf4255ba056bd0cbb026a5a4 to your computer and use it in GitHub Desktop.
Save zubair1024/236a2f9acf4255ba056bd0cbb026a5a4 to your computer and use it in GitHub Desktop.
storage:
dbPath: /srv/mongodb/arb1/
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/arb1/arb1.log
net:
port: 27720
bindIp: localhost, 0.0.0.0
#processManagement:
# timeZoneInfo: /usr/share/zoneinfo
operationProfiling:
mode: "slowOp"
slowOpThresholdMs: 50
security:
#authorization: enabled
keyFile: /srv/mongodb/mongo-key
replication:
replSetName: "razrnodes"
[Unit]
Description=An object/document-oriented database
Documentation=man:mongod(1)
After=network.target
[Service]
User=mongodb
Group=mongodb
RuntimeDirectory=mongodb
RuntimeDirectoryMode=0755
EnvironmentFile=-/etc/default/mongodb
Environment=CONF=/etc/arb1.conf
Environment=SOCKETPATH=/run/mongodb
ExecStart=/usr/bin/mongod --config ${CONF} $DAEMON_OPTS
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target

Setup MongoDB Replica set on Ubuntu

  • Update repo

sudo apt-get update

  • Install MongoDB

sudo apt-get install mongodb

  • Make directories for data and logs

mkdir -p /srv/mongodb/node1 /srv/mongodb/node2 /srv/mongodb/arb1

mkdir -p /var/log/mongodb/node1 /var/log/mongodb/node2 /var/log/mongodb/arb1

  • Install OpenSSL key and give necessary permissions:

openssl rand -base64 741 > /srv/mongodb/mongo-key

sudo chmod mongodb /srv/mongodb/mongo-key

sudo chown mongod:mongod /srv/mongodb/mongo-key

chmod 400 /srv/mongodb/mongo-key

  • Copy and paste the .conf files into /etc/

  • Enable permission for the data folders and log folders

chmod 777 -R /var/log/mongodb/

chmod 777 -R /srv/mongodb/node1/

chmod 777 -R /srv/mongodb/node2/

chmod 777 -R /srv/mongodb/arb1/

  • Connect to primary and init a replica set
var config = {
    _id: "razrnodes", members: [
        { _id: 0, host: "localhost:27717" },
        { _id: 1, host: "localhost:27718" },
        { _id: 2, host: "localhost:27719" }
    ]
}

rs.initiate(config);

rs.addArb('localhost:27720');
rs.addArb('localhost:27721');
  • Create necessary users
var admin = db.getSiblingDB("admin")
admin.createUser(
  {
    user: "rootAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "root", db: "admin" } ]
  }
)

var admin = db.getSiblingDB("admin")
admin.createUser(
  {
    user: "userAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

var admin = db.getSiblingDB("admin")
admin.createUser(
  {
    user: "clusterAdmin",
    pwd: passwordPrompt(), // or cleartext password
    roles: [ { role: "clusterAdmin", db: "admin" } ]
  }
)
  • Now enable authorization and you're done!
storage:
dbPath: /srv/mongodb/node1/
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/node1/node1.log
net:
port: 27717
bindIp: localhost, 0.0.0.0
#processManagement:
# timeZoneInfo: /usr/share/zoneinfo
operationProfiling:
mode: "slowOp"
slowOpThresholdMs: 50
security:
#authorization: enabled
keyFile: /srv/mongodb/mongo-key
replication:
replSetName: "razrnodes"
[Unit]
Description=An object/document-oriented database
Documentation=man:mongod(1)
After=network.target
[Service]
User=mongodb
Group=mongodb
RuntimeDirectory=mongodb
RuntimeDirectoryMode=0755
EnvironmentFile=-/etc/default/mongodb
Environment=CONF=/etc/node1.conf
Environment=SOCKETPATH=/run/mongodb
ExecStart=/usr/bin/mongod --config ${CONF} $DAEMON_OPTS
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
storage:
dbPath: /srv/mongodb/node2/
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/node2/node2.log
net:
port: 27718
bindIp: localhost, 0.0.0.0
#processManagement:
# timeZoneInfo: /usr/share/zoneinfo
operationProfiling:
mode: "slowOp"
slowOpThresholdMs: 50
security:
#authorization: enabled
keyFile: /srv/mongodb/mongo-key
replication:
replSetName: "razrnodes"
[Unit]
Description=An object/document-oriented database
Documentation=man:mongod(1)
After=network.target
[Service]
User=mongodb
Group=mongodb
RuntimeDirectory=mongodb
RuntimeDirectoryMode=0755
EnvironmentFile=-/etc/default/mongodb
Environment=CONF=/etc/node2.conf
Environment=SOCKETPATH=/run/mongodb
ExecStart=/usr/bin/mongod --config ${CONF} $DAEMON_OPTS
LimitFSIZE=infinity
LimitCPU=infinity
LimitAS=infinity
LimitNOFILE=64000
LimitNPROC=64000
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment