Skip to content

Instantly share code, notes, and snippets.

@zol
Last active January 22, 2016 22:20
Show Gist options
  • Save zol/3f34d7f4870991f0c3be to your computer and use it in GitHub Desktop.
Save zol/3f34d7f4870991f0c3be to your computer and use it in GitHub Desktop.
Setting up mongodb with oplog tailing

Setting up replicaset

  • Log into mongodb1
  • Configure the replicaset (with a secondary and arbiter)
rs.initiate()
rs.add('ip-internal-ip-of-mongodb2')
rs.addArb('ip-internal-ip-of-mongodb3')
  • To check the replicaset config do rs.conf(), to check the status do rs.status()

Setting up users

To use authentication with a replicaSet, in the ansible host config you must set a shared key such as:

auth_key: doesnt-matter-what-this-is-as-you-cant-guess-it

Authentication is set per db, with an admin db for db-wide auth.

First, create a super admin user

use admin
db.addUser( { user: "percolate", pwd: "foobar", roles: [ "userAdminAnyDatabase", "readWriteAnyDatabase", "clusterAdmin", "dbAdminAnyDatabase" ] } )

To show the configured users, do

db.system.users.find()

To add a user for e.g the atmosphere db, do

use atmosphere
db.addUser( { user: "atmosphere", pwd: "foobar", roles: ["readWrite", "dbAdmin"]})

To add a read-only user for e.g the atmosphere db, do

use atmosphere
db.addUser( { user: "reader", pwd: "$PASSWORD", roles: ["read"]})

To add an oplogger user for meteor, do

Mongodb 2.4

use admin
db.addUser({user: "oplogger", pwd: "foobar", roles: [], otherDBRoles: {local: ["read"]}})

Mongodb 2.6

use admin
db.createUser({user: "oplogger", pwd: "foobar", roles: [{role: "read", db: "local"}]})

The env vars for meteor become

MONGO_URL=mongodb://atmosphere:$PASSWORD@db1.percolatestudio.com,db2.percolatestudio.com/atmosphere
MONGO_OPLOG_URL=mongodb://oplogger:$PASSWORD@db1.percolatestudio.com,db2.percolatestudio.com/local?authSource=admin

To login to the mongo shell as the admin user do mongo -u percolate -p "$PASSWORD" --authenticationDatabase admin

Cloning a database

  1. Connect to the target db e.g mongo db1-staging.versoapp.com/verso
  2. Drop the database db.dropDatabase();
  3. Clone e.g db.cloneDatabase("db1.versoapp.com")

Unfortunately You must delete the db before you clone, so be careful!

Upgrading 2.4->2.6

  1. Connect to 2.4 database with a 2.6 shell, run db.upgradeCheckAllDBs()
  2. Upgrade mongo package on the server by adding the mongodb_version: 2.6 variable into inventory/group_vars/tag_Mongodb.yaml and running the gcontrol/ansible/playbooks/provision_mongodb.yaml playbook.
  • [NOTE AT THIS POINT YOU CANNOT RESTART THE METEOR APP WITHOUT UPDATING TO 1.1]
  1. Update authorization schema:
  • connect to 2.6 server, admin db
  • run db.getSiblingDB("admin").runCommand({authSchemaUpgrade: 1 });
  • run db.runCommand({grantRolesToUser: 'oplogger', roles: [{role: "read", db: "local"}]})
  1. Restart Meteor application.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment