Database
Overview
This is a tutorial on how to install MongoDB on a Mac.
BTW, AWS DynamoDB provides a service for this.
MongoDB in Docker container
-
Start the Docker daemon
sudo docker run -d -p 27017:27017 -v ~/data:/data/db mongo
-
Based on https://hub.docker.com/_/mongo/ *
docker pull mongo:latest
Install MongoDB using Homebrew
-
Install Mongodb, on a Mac (not the latest dev release):
brew update
brew install mongodbThe response (takes a long time):
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.2.8.el_capitan.bo ==> Pouring mongodb-3.2.8.el_capitan.bottle.tar.gz ==> Caveats To have launchd start mongodb now and restart at login: brew services start mongodb Or, if you don't want/need a background service you can just run: mongod --config /usr/local/etc/mongod.conf ==> Summary 🍺 /usr/local/Cellar/mongodb/3.2.8: 17 files, 264.1M
Start MongoDB service
brew services start mongodb
The response:
==> Tapping homebrew/services Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'... remote: Counting objects: 12, done. remote: Compressing objects: 100% (8/8), done. remote: Total 12 (delta 0), reused 7 (delta 0), pack-reused 0 Unpacking objects: 100% (12/12), done. Tapped 0 formulae (40 files, 54KB) ==> Successfully started `mongodb` (label: homebrew.mxcl.mongodb)
Verify MongoDB service
ps -al
Stop MongoDB service
brew services stop mongodb
Re-Start MongoDB service
brew services restart mongodb
Troubleshooting
By default docker-machine uses the virtualbox driver to create a local VM. However, Mongo uses mmap to turbo-charge access to files on disk. So tell docker to create a data volume in the container rather than in the host:
docker run –name my-local-mongo -v mongo-data:/data/db -p 27017:27017 -d mongo view raw
-
Get the IP of the VM:
docker-machine ls
Dependencies in Docker
When an application starts, it’s a problem if its dependencies are not available.
- Load configuration settings from a JSON encoded config file
- Access a working data directory
- Establish a connection to an external mysql database
Traditionally, the approach is to ensure the database is started before starting the applications that depend on it by using Puppet, Chef, Ansible, or other configuration management tool.
“This is nothing more then a band-aid covering up the larger problem.” Kelsey Hightower says and recommends having app code handle the dependency problem. His sample in Go:
https://github.com/kelseyhightower/12-fractured-apps/blob/master/v3/main.go
-
load configuration files if it exists, but fall back to sane defaults.
-
Read environment variables to override configuration settings.
-
Manage working directories inside the application. If they are missing create them.
-
Retry the database connection, using some sort of backoff, and log errors along the way so alerts can be sent out.
This “defensive programming” code is the “optimistic” approach. It’s usually a transient problem. At some point the database will come online.
TODO: Put startup-related code in a library for re-use.