Getting setup with MongoDB
MongoDB Local installation
NOTE: MongoDB is NOT installed on the department servers, and without SysAdmin privileges neither can you.
Installing with a download
- Find the download page using a search engine
- Choose the tab corresponding to the Community Server and then select your system (be careful with Linux)
- Unpack/untar the downloaded file if necessary
- find the
bin
directory and then do one of these: - move everything in
bin
there to some directory on yourPATH
(such as/usr/local/bin
), OR - copy/link everything in
bin
to some directory on yourPATH
, OR - add the full path to that
bin
directory permanently to yourPATH
.
MAC-Specific instructions
The MongoDB server (
mongod
) puts all its data in/data/db
, so we have to create that. If not using that as the root, use a command line option to choose somewhere else.
$ sudo bash
# mkdir -p /data/db
# chmod 777 /data
# chmod 777 /data/db
# exit
$
Note: Instructions for installing with Homebrew (Mac) are also available on the MongoDB download page.
Instructions for Windows and Linux are also on the download page.
You will not be able to install mongo on one of the CS systems.
What are the MongoDB executables?
| Mongo executable | Use |
| --------------------------- | ------------------------------------------------------------------------------------------ |
| /usr/local/bin/mongo | shell |
| /usr/local/bin/mongod | server |
| usr/local/bin/mongodump | dump (backup) your work into `.bson` |
| /usr/local/bin/mongoexport | Export data from MongoDB in CSV or JSON format. |
| /usr/local/bin/mongofiles | gridfs |
| /usr/local/bin/mongoimport | Import CSV, TSV or JSON data into MongoDB. |
| /usr/local/bin/mongooplog | Poll operations from the replication oplog of one server, <br/> and apply them to another. |
| /usr/local/bin/mongoperf | Performance testing |
| usr/local/bin/mongoreplay | traffic capture and replay tool |
| /usr/local/bin/mongorestore | restore a dump from `.bson` |
| /usr/local/bin/mongos | shard manager |
| /usr/local/bin/mongostat | monitor basic MongoDB server statistics |
| /usr/local/bin/mongotop | monitor basic usage statistics for collections |
Now test the setup by running mongod
:
Swamp: [~] $ mongod
2017-10-26T20:49:48.280-0400 I CONTROL [initandlisten] MongoDB starting : pid=38300 port=27017 dbpath=/data/db 64-bit host=Swamp.local
2017-10-26T20:49:48.280-0400 I CONTROL [initandlisten] db version v3.4.4
2017-10-26T20:49:48.280-0400 I CONTROL [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 0.9.8zh 14 Jan 2016
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] allocator: system
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] modules: none
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] build environment:
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] distarch: x86_64
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] target_arch: x86_64
2017-10-26T20:49:48.281-0400 I CONTROL [initandlisten] options: {}
2017-10-26T20:49:48.282-0400 I - [initandlisten] Detected data files in /data/db created by the 'mmapv1' storage engine, so setting the active storage engine to 'mmapv1'.
2017-10-26T20:49:48.289-0400 I JOURNAL [initandlisten] journal dir=/data/db/journal
2017-10-26T20:49:48.290-0400 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2017-10-26T20:49:48.309-0400 I JOURNAL [durability] Durability thread started
2017-10-26T20:49:48.309-0400 I JOURNAL [journal writer] Journal writer thread started
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten]
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten]
2017-10-26T20:49:49.674-0400 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2017-10-26T20:49:49.677-0400 I NETWORK [thread1] waiting for connections on port 27017
This will keep running until you ctrl-c
to exit it. Switching to another bash
shell, we now can run the mongo shell. Here’s what it looks like on my system
(yours will very likely look a little different):
Swamp: [~] $ mongo
MongoDB shell version v3.4.9
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.4
Server has startup warnings:
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten]
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-10-26T20:49:48.309-0400 I CONTROL [initandlisten]
> show databases
CS 0.078GB
blog 0.078GB
census 0.078GB
fruit 0.078GB
local 0.078GB
m101 0.078GB
mongomart 0.078GB
reddit 0.078GB
school 1.953GB
students 1.953GB
test 0.078GB
video 0.078GB
>
Connecting to ATLAS
Installing locally is fine for learning and some development, but to do bigger applications and collaborate with other developers, you need a shared database. We will be using ATLAS from MongoDB. The lowest level of database there is free!
You shouldn’t need to login to ATLAS.MONGODB.COM , because we’ll use the mongo
shell or our own software to access ATLAS.
Each member of the class has a database on ATLAS of the form “
MongoDB Cluster
We’re using a cluster of three machines for our class databases. To access them
you would use your netid and password in the following mongo
shell
command:
mongo "mongodb://<NETID>:<PASSWORD>@cluster0-shard-00-00-ppp7l.mongodb.net:27017,cluster0-shard-00-01-ppp7l.mongodb.net:27017,cluster0-shard-00-02-ppp7l.mongodb.net:27017/<DATABASE>?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin"
Replace <NETID>
with your netid, <PASSWORD>
with your password,
and <DATABASE>
with the name of the your database.
Demonstrate using a test userid/pw/db.
Python connection example
client = pymongo.MongoClient("mongodb://<userid>:<password>@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin")
db = client.test
Use Python 2.7.9+ and use a Python driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Python driver and MongoDB, see the MongoDB compatibility matrix.