Skip to content

Instantly share code, notes, and snippets.

@Tiffceet
Last active April 15, 2023 03:04
Show Gist options
  • Save Tiffceet/bb77163427a7ce4defcfec070d6dcd59 to your computer and use it in GitHub Desktop.
Save Tiffceet/bb77163427a7ce4defcfec070d6dcd59 to your computer and use it in GitHub Desktop.
  1. Install mongo
  2. Setup replica set
replication:
  replSetName: "rs0"
  1. Initiate replica set
rs.initiate(
  {
    _id : "rs0",
    members: [
      { _id : 0, host : "<remote-ip>:<remote-port>" }
    ]
  }
)
  1. Bind to 0.0.0.0 for ALL remote connection, bindIp specify which ip is allowed to connect
net:
  port: 27017
  bindIp: 0.0.0.0
  1. Setup authentication
use admin;
db.createUser(
  {
    user: "<your-username>",
    pwd: "<your-password>",
    roles: [
      { role: "userAdminAnyDatabase", db: "admin" },
      { role: "readWriteAnyDatabase", db: "admin" }
    ]
  }
)
  1. Generate key file (required by replica set)
openssl rand -base64 756 > /etc/mongodb/keyFiles/mongo-key
chmod 400 /etc/mongodb/keyFiles/mongo-key
chown mongodb /etc/mongodb/keyFiles/mongo-key
  1. Enable authorization
security:
  authorization: "enabled"
  keyFile: /etc/mongodb/keyFiles/mongo-key
  1. Remote connection string
mongodb://<username>:<password>@<remote-ip>:<remote-port>/?authMechanism=DEFAULT&authSource=<auth-source>
  • username - refer step 5
  • password - refer step 5
  • remote-ip - refer step 3
  • remote-port - refer step 3
  • auth-source - refer step 5, which db did you createUser on

When in doubt, check system logs:

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment