As a representative of non-relational database, MongoDB is very suitable for storing some unstructured data. With the purpose of understanding and research, I tried to install it on Windows machine and found that there are still many points to share in the middle.

Installation of MongoDB

I thought the installation was a very simple task, but I got stuck in downloading the installation package during the execution. The download page keeps returning 404 when I select the corresponding version from the official download center: https://www.mongodb.com/download-center. I have no choice but to find another download address: https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl

After downloading, follow the instructions to install, but nothing happens after installation, MongoDB does not start by default, there is no desktop shortcut, and nothing is added to the Window menu, which makes you think that the installation is not successful.

MongoDB Installation False Death Problem

Problems with installing MongoDB on Windows usually occur, mainly because the installation process keeps falsely dying. The installation progress bar doesn’t move at all. Wait for an hour and nothing happens.

The progress displayed is: Status:Installing MongoDB Compass…(this may take a few minutes)

The probable reason is that MongoDB Compass is intercepted by SFW, resulting in not being downloaded. The solution is very simple, i.e. don’t choose to install MongoDB Compass during installation!

MongoDB startup configuration

After installing MongoDB, if you want to use it, you need to start it by yourself, the common way is to register it as a Windows service. The main process is as follows.

add environment variables

The main aspect is to execute mongodb command, you don’t need to switch to the folder every time, this step can be omitted

Variable name: MONGODB_HOME, value: C:\Program Files\MongoDB\Server\3.2 (the specific path is determined according to your installation path)

Variable name: PATH, append content: %MONGODB_HOME%\bin;

After finishing, you can execute MongoDB-related programs directly from the command line.

Install Windows services

In order to turn on and off MongoDB conveniently, you need to register Windows services, the method is relatively simple, as follows.

1
mongod --dbpath "C:\MongoDB\data" --logpath "C:\MongoDB\log\mongodb.log" --install

The above command calls the mongod.exe file in the C:\Program Files\MongoDB\Server\3.2\bin directory, and specifies the database file storage address and log file storage address. mongod’s main parameters are: (you can set them according to your own situation)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
##基本配置
--quiet    # 安静输出
--port arg    # 指定服务端口号,默认端口27017
--bind_ip arg    # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP
--logpath arg    # 指定MongoDB日志文件,注意是指定文件不是目录
--logappend    # 使用追加的方式写日志
--pidfilepath arg    # PID File 的完整路径,如果没有设置,则没有PID文件
--keyFile arg    # 集群的私钥的完整路径,只对于Replica Set 架构有效
--unixSocketPrefix arg    # UNIX域套接字替代目录,(默认为 /tmp)
--fork    # 以守护进程的方式运行MongoDB,创建服务器进程
--auth    # 启用验证
--cpu    # 定期显示CPU的CPU利用率和iowait
--dbpath arg    # 指定数据库路径
--diaglog arg    # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb    # 设置每个数据库将被保存在一个单独的目录
--journal    # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里
--journalOptions arg    # 启用日志诊断选项
--ipv6    # 启用IPv6选项
--jsonp    # 允许JSONP形式通过HTTP访问(有安全影响)
--maxConns arg    # 最大同时连接数 默认2000
--noauth    # 不启用验证
--nohttpinterface    # 关闭http接口,默认关闭27018端口访问
--noprealloc    # 禁用数据文件预分配(往往影响性能)
--noscripting    # 禁用脚本引擎
--notablescan    # 不允许表扫描
--nounixsocket    # 禁用Unix套接字监听
--nssize arg (=16)    # 设置信数据库.ns文件大小(MB)
--objcheck    # 在收到客户数据,检查的有效性,
--profile arg    # 档案参数 0=off 1=slow, 2=all
--quota    # 限制每个数据库的文件数,设置默认为8
--quotaFiles arg    # number of files allower per db, requires --quota
--rest    # 开启简单的rest API
--repair    # 修复所有数据库run repair on all dbs
--repairpath arg    # 修复库生成的文件的目录,默认为目录名称dbpath
--slowms arg (=100)    # value of slow for profile and console log
--smallfiles    # 使用较小的默认文件
--syncdelay arg (=60)    # 数据写入磁盘的时间秒数(0=never,不推荐)
--sysinfo    # 打印一些诊断系统信息
--upgrade    # 如果需要升级数据库

##Replicaton 参数
--fastsync    # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步
--autoresync    # 如果从库与主库同步数据差得多,自动重新同步,
--oplogSize arg    # 设置oplog的大小(MB)

##主/从参数
--master    # 主库模式
--slave    # 从库模式
--source arg    # 从库 端口号
--only arg    # 指定单一的数据库复制
--slavedelay arg    # 设置从库同步主库的延迟时间

## Replica set(副本集)选项:
--replSet arg    # 设置副本集名称

##Sharding(分片)选项
--configsvr    # 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb
--shardsvr    # 声明这是一个集群的分片,默认端口27018
--noMoveParanoia    # 关闭偏执为moveChunk数据保存

Set up auth authentication/permissions (optional)

By default, mongo has no password to enter, so if you need to set a username and password you need to do three steps.

  • Create administrator
  • Authorize authentication
  • Add a user to the database you are using
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
C:\WINDOWS\system32>mongo//打开cmd输入mongo开启服务
//使用admin数据库
> use admin 
    switched to db admin
//添加用户名和密码
> db.createUser({user:"admin",pwd:"admin",roles:["root"]})
    Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
//登录认证
> db.auth("admin","admin")
    1
//新建 test数据库
> use test
    switched to db test
//添加用户权限和角色
> db.createUser({user:'root',pwd:'123456',roles:[{role:'dbOwner',db:'test'}]})
    Successfully added user: {
            "user" : "root",
            "roles" : [
                    {
                            "role" : "dbOwner",
                            "db" : "test"
                    }
            ]
    }
> mongod --config "D:\Program Files\MongoDB\mongo.config" --auth
    2018-07-03T16:13:24.477+0800 E QUERY    [thread1] SyntaxError: missing ; before statement @(shell):1:9

Other related instructions.

1
2
3
net start MongoDB #启动 MongoDB 服务
net stop MongoDB #停止 MongoDB 服务
mongod --remove #删除 MongoDB 服务

Alternatively you can start or stop the operation from the Windows Service Management page.

Using MongoDB

Related tools.

  • Robomongo: Robomongo a.k.a. Robo 3T, tried and tested, is considered the best (free, open source) among mongodb visualization tools.
  • Studio 3T: A paid version of the above Robo 3T with more powerful features.
  • RockMongo, a PHP-based mongodb management tool
  • NoSQLBooster, a visualization tool for Windows.