02 - Mongodb - Install
02 - Mongodb - Install
Install MongoDB
Community Edition
Install MongoDB Community Edition
https://www.mongodb.com/download-center
3
Install MongoDB Community Edition
Supported Platforms
https://www.mongodb.com/download-center
4
Install MongoDB Community Edition
Supported Platforms
https://www.mongodb.com/download-center
5
Install MongoDB Community Edition
Since MongoDB 3.0, commercial support is no longer provided for MongoDB on 32-bit
platforms (Linux and Windows).
• 32-bit builds disable journaling by default because journaling further limits the maximum amount of
data that the database can store.
• When running a 32-bit build of MongoDB, the total storage size for the server, including data and
indexes, is 2 gigabytes. For this reason, do not deploy MongoDB to production on 32-bit machines.
If you’re running a 64-bit build of MongoDB, there’s virtually no limit to storage size. For
production deployments, 64-bit builds and operating systems are strongly recommended.
6
Install MongoDB Community Edition
Install on Windows
https://support.microsoft.com/en-us/kb/2731284
MongoDB Community Edition requires Windows Server 2008 R2, Windows Vista, or later.
The .msi installer includes all other software dependencies and will automatically upgrade any
older version of MongoDB installed using an .msi file.
7
Install MongoDB Community Edition
Install on Windows
Determine which MongoDB build you need
To find which version of Windows you are running, enter the following commands in
the Command Prompt or Powershell:
> wmic os get caption
Caption
Microsoft Windows 10 Home
8
Install MongoDB Community Edition
Install on Windows
Download MongoDB for Windows
Download the latest production release of MongoDB from the MongoDB downloads page.
Ensure you download the correct version of MongoDB for your Windows system.
The 64-bit versions of MongoDB do not work with 32-bit Windows.
9
https://www.mongodb.com/download-center
Install MongoDB Community Edition
Install on Windows
Interactive Installation
In Windows Explorer, locate the downloaded MongoDB .msi file and double-click the .msi file.
A set of screens will appear to guide you through the installation process.
You may specify an installation directory if you choose the “Custom” installation option.
MongoDB is self-contained and does not have any other system dependencies. You can run
MongoDB from any folder you choose. You may install MongoDB in any folder
10
Install MongoDB Community Edition
Install on Windows
MongoDB archives
The defaults installation folder for MongoDB is
C:\Program Files\MongoDB\Server\3.2
11
Install MongoDB Community Edition
Install on Windows
MongoDB Tools
Component Set Binaries
Server
Router
Client
MonitoringTools
ImportExportTools
MiscellaneousTools
12
Install MongoDB Community Edition
Install on Windows
Unattended Installation
You may install MongoDB Community unattended on Windows from the command line as
Administrator using msiexec.exe.
You can specify the installation location for the executable by modifying the value of parameter
INSTALLLOCATION.
By default, this method installs all MongoDB binaries. To install specific MongoDB component
sets, you can specify them in the ADDLOCAL argument using a comma-separated list
including one or more of the following component sets.
13
Install MongoDB Community Edition
Install on Windows
MongoDB Tools
Component Set Binaries
Server
Router
Client
MonitoringTools
ImportExportTools
MiscellaneousTools
14
Install MongoDB Community Edition
Install on Windows
Unattended Installation
For instance, to install only the MongoDB utilities, invoke:
> msiexec.exe /q /i mongodb-win32-x86_64-2008plus-ssl-3.2.7-signed.msi ^
INSTALLLOCATION="C:\mongodb" ^
ADDLOCAL="MonitoringTools,ImportExportTools,MiscellaneousTools"
15
Run MongoDB
Community Edition
Run MongoDB Community Edition
Start MongoDB
To start MongoDB, run mongod.exe. For example, from the Command Prompt:
> mongod
The waiting for connections message in the console output indicates that the mongod.exe
process is running successfully.
Depending on the security level of your system, Windows may pop up a Security Alert dialog
box about blocking “some features” of mongod.exe from communicating on networks.
All users should select Private Networks, such as my home or work network and click Allow
access.
17
Run MongoDB Community Edition
Start MongoDB
> mongod
18
Run MongoDB Community Edition
Start MongoDB
You can see a listing of all mongod options by running the command:
> mongod --help
http://docs.mongodb.org/manual/reference/mongod/
By default mongod listens on port 27017. To have the process listen on another port run:
> mongod --port <portnum>
19
Run MongoDB Community Edition
Create this folder using the following commands from a Command Prompt:
> mkdir \data\db
You can specify an alternate path for data files using the --dbpath option to mongod.exe:
> mongod.exe --dbpath d:\test\mongodb\data
If your path includes spaces, enclose the entire path in double quotes, for example:
20
Run MongoDB Community Edition
You can have the server write the log to a file with the options:
> mongod --dbpath “…” --logpath “..\logs\mongod.log” --logappend
It’s strongly advisable to retain log files for some reasonable period of time, for forensics,
debugging, etc, …
21
Run MongoDB Community Edition
Configuring MongoDB
You can configure mongod instances at startup using a configuration file. The configuration
file contains settings that are equivalent to the mongod command-line options.
Using a configuration file makes managing mongod options easier, especially for large-scale
deployments. You can also add comments to the configuration file to explain the server’s
settings.
To start mongod using a config file, specify the config file with the --config option or the -f
option, as in the following examples:
> mongod --config /etc/mongod.conf
> mongod -f /etc/mongod.conf
http://docs.mongodb.org/manual/reference/configuration-options/
22
Run MongoDB Community Edition
Configuring MongoDB
MongoDB configuration files use the YAML format
http://yaml.org/
23
Run MongoDB Community Edition
Configuring MongoDB
The configuration file in version 2.6 is in YAML format. The following is an example of several
settings in a configuration file:
24
Run MongoDB Community Edition
Configuring MongoDB
Before version 2.6, you must declare all settings in this file using the following format:
Note: The 2.4 configuration file format remains for backward compatibility.
25
The mongo Shell
Install MongoDB Community Edition
27
Install MongoDB Community Edition
ImportExportTools
MiscellaneousTools
28
Install MongoDB Community Edition
When you run mongo without any arguments, the mongo Shell will attempt to connect to the
MongoDB instance running on the localhost interface on port 27017.
To specify a different host or port number, as well as other options, see examples of starting up
mongo and mongo reference which provides details on the available options.
29
Install MongoDB Community Edition
> exit
bye
> quit()
30
Install MongoDB Community Edition
31
Install MongoDB Community Edition
32
Install MongoDB Community Edition
At any time, issue the following operation at the mongo Shell to report the name of the current
database:
> db
test
From the mongo Shell, display the list of databases, with the following operation:
> show dbs
local 0.000GB
33
Install MongoDB Community Edition
34
Install MongoDB Community Edition
At this point, if you issue the show dbs operation again, it will not include the mydb database.
MongoDB will not permanently create a database until you insert data into that database.
35
Install MongoDB Community Edition
The following creates both the database myNewDatabase and the collection myCollection:
> use myNewDatabase
> db.myCollection.insert({name: “jordi”})
WriteResult({ "nInserted" : 1 })
36
Install MongoDB Community Edition
> db.getCollectionNames()
[ "myCollection", "system.indexes" ]
> db.getCollectionNames().forEach(function(e){print(e)})
37
Install MongoDB Community Edition
> db.myCollection.drop()
true
38
Install MongoDB Community Edition
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }
39
Install MongoDB Community Edition
This operation will rename the miColection collection to myCollection. If the target name
(i.e. myCollection) is the name of an existing collection, then the operation will fail.
40
Install MongoDB Community Edition
41
Install MongoDB Community Edition
42
Install MongoDB Community Edition
> db.getCollection(“3test”).find()
43
Install MongoDB Community Edition
44
Install MongoDB Community Edition
Furthermore, you can append the .help() method to some JavaScript methods, any cursor
object, as well as the db and db.collection objects to return additional help information.
> db.help()
> db.collection.help()
45
Install MongoDB Community Edition
Multi-line commands
You can enter a multi line javascript expression. If parens, braces, etc. are not
closed, you will see a new line
beginning with '...' characters. Type the rest of your expression. Press Ctrl-C to
abort the data entry if you get stuck.
https://docs.mongodb.com/manual/reference/program/mongo/
46
Install MongoDB Community Edition
If found, mongo interprets its content before displaying the prompt for the first time.
You can prevent .mongorc.js from being loaded by using the --norc option.
Users can define variables, customize the mongo shell prompt, or update information that they
would like updated every time they launch a shell
var prompt = function() {
var host = db.serverStatus().host;
var dbname = db.getName();
var time = new Date().toLocaleTimeString();
return sprintf("\n%s\\%s@%s> ", host, dbname, time.slice(0,-3));
}
DBQuery.prototype._prettyShell = true;
47
Install MongoDB Community Edition
If a user also has a .mongorc.js file located in the home directory, the mongo shell evaluates
the global /etc/mongorc.js file before evaluating the user’s .mongorc.js file.
The --norc option for mongo suppresses only the user’s .mongorc.js file.
48
Install MongoDB Community Edition
--verbose
Increases the verbosity of the output of the shell during the connection process.
--quiet
Silences output from the shell during the connection process..
--version
Returns the mongo release number.
--norc
Prevents the shell from sourcing and evaluating ~/.mongorc.js on start up..
49
Install MongoDB Community Edition
--port <port>
Default: 27017
Specifies the port where the mongod instance is listening. If --port is not specified, mongo
attempts to connect to port 27017.
50
Install MongoDB Community Edition
--shell
Enables the shell interface. If you invoke the mongo command and specify a JavaScript
file as an argument, or use --eval to specify JavaScript on the command line, the --shell
option provides the user with a shell prompt after the file finishes executing.
--eval <javascript>
Evaluates a JavaScript expression that is specified as an argument. mongo does not load
its own environment when evaluating code. As a result many options of the shell
environment are not available.
<file.js>
Specifies a JavaScript file to run and then exit.
51
Configure a Windows Service for
MongoDB Community Edition
Install MongoDB Community Edition
53
Install MongoDB Community Edition
Use multiple instances only when sufficient system resources exist and your system design
requires it.
54
Install MongoDB Community Edition
55
Install MongoDB Community Edition
sc.exe requires a space between “=” and the configuration values (eg “binPath= ”), and a “\” to
escape double quotes.
57
MongoDB Logs Messages
Install MongoDB Community Edition
Starting in version 3.0, Mongo DB includes the severity level and the component associated
with each log message.
For example:
2016-06-25T12:32:24.546+0200 I CONTROL [initandlisten] MongoDB starting : pid=10084 port=27017
dbpath=C:\data\db\ 64-bit host=DESKTOP-TGCRHCH
59
Install MongoDB Community Edition
The default format for the <timestamp> is iso8601-local. To modify the timestamp format, use
the --timeStampFormat runtime option or the systemLog.timeStampFormat config setting.
Value Description
ctime Displays timestamps as Wed Dec 31 18:17:54.811.
60
Install MongoDB Community Edition
The following table lists the severity levels associated with each log message:
Level Description
D Debug Messages (Verbosity Level > 0)
I Info Messages (Verbosity Level of 0)
W Warn Messages
E Error Messages
F Fatal Messages
You can specify the verbosity level of mongod.exe to determine the amount of Informational
and Debug messages MongoDB outputs.
61
Install MongoDB Community Edition
Log messages now include components, providing functional categorization of the messages:
Component Description
62
Install MongoDB Community Edition
Log messages now include components, providing functional categorization of the messages:
Component Description
FTDC Messages related to the diagnostic data, such as server statistics and status msg.
REPL Messages related to replica sets, such as initial sync and heartbeats.
SHARDING Messages related to sharding activities, such as the startup of the mongos.
63