MongoDB 基础系列一:安装使用

前言

此篇博文是 Mongdb 基础系列之一;主要介绍如何安装,运行;

本文为作者的原创作品,转载需注明出处;

安装

mac

1
$ brew update
1
$ brew install mongodb

安装最新版的 mongodb

1
$ brew install mongodb --devel

安装成功以后的日志如下,

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
$ brew install mongodb
Updating Homebrew...
==> Installing dependencies for mongodb: openssl
==> Installing mongodb dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2l.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2l.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
/usr/local/etc/openssl/certs

and run
/usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have this software first in your PATH run:
echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

==> Summary
🍺 /usr/local/Cellar/openssl/1.0.2l: 1,709 files, 12.2MB
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.6.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.6.sierra.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.4.6: 18 files, 266.9MB

启动 MongoDB

默认情况下,mongodb 进程 mongod 使用的是 /data/db 来作为数据存储的路径,如果,你想要用其他的路径来取代,那么在通过 mongod 启动 mongodb 的时候,需要通过 dbpath 选项来指定;

1
$ mongod --dbpath <path to data directory>

需要注意的是<path to data directory>必须存在并且具有合适的访问权限;

1
$ mkdir ~/data | mkdir ~/data/mongodb

使用自定义的数据目录来执行 Mongodb,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
macdeMacBook-Pro:~ mac$ mongod --dbpath ~/data/mongodb
2017-07-21T16:26:16.600+0800 I CONTROL [initandlisten] MongoDB starting : pid=4154 port=27017 dbpath=/Users/mac/data/mongodb 64-bit host=macdeMacBook-Pro.local
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] db version v3.4.6
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2l 25 May 2017
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] allocator: system
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] modules: none
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] build environment:
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] distarch: x86_64
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-07-21T16:26:16.601+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/Users/mac/data/mongodb" } }
2017-07-21T16:26:16.602+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7680M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-07-21T16:26:16.967+0800 I CONTROL [initandlisten]
2017-07-21T16:26:16.967+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-07-21T16:26:16.967+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-07-21T16:26:16.967+0800 I CONTROL [initandlisten]
2017-07-21T16:26:17.059+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/Users/mac/data/mongodb/diagnostic.data'
2017-07-21T16:26:17.259+0800 I INDEX [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2017-07-21T16:26:17.259+0800 I INDEX [initandlisten] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-07-21T16:26:17.271+0800 I INDEX [initandlisten] build index done. scanned 0 total records. 0 secs
2017-07-21T16:26:17.271+0800 I COMMAND [initandlisten] setting featureCompatibilityVersion to 3.4
2017-07-21T16:26:17.272+0800 I NETWORK [thread1] waiting for connections on port 27017

使用默认的数据路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ mongod
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] MongoDB starting : pid=3934 port=27017 dbpath=/data/db 64-bit host=macdeMacBook-Pro.local
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] db version v3.4.6
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2l 25 May 2017
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] allocator: system
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] modules: none
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] build environment:
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] distarch: x86_64
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] options: {}
2017-07-21T16:19:59.841+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2017-07-21T16:19:59.841+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-07-21T16:19:59.841+0800 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] now exiting
2017-07-21T16:19:59.841+0800 I CONTROL [initandlisten] shutting down with code:100

可见,启动的时候默认使用的是/data/db;不过,因为本地尚未创建过该路径,所以报错,终止;

如果像上面这样使用的是默认的方式,那么默认使用的文件路径就是/data/db

关闭

Reference

https://www.tutorialspoint.com/mongodb/
https://docs.mongodb.com/manual/introduction/
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/