安装

在 macOS 下使用 Homebrew 安装 MongoDB。但在 Homebrew 的核心库中,我们无法找到 MongoDB,于是需要先运行 brew tap,帮助我们扩大可安装软件的选择范围。

The tap command allows Homebrew to tap into another repository of formulae. Once you’ve done this you’ve expanded your options of installable software.

$ brew tap mongodb/brew

之后,运行如下命令安装 MongoDB:

$ brew install mongodb-community@4.2

启动

使用 brew 命令运行 MongoDB:

$ brew services start mongodb-community@4.2

建立连接

启动成功后,使用如下命令与 MongoDB 建立连接:

$ mongo

运行命令后发现连接失败,错误如下:

MongoDB shell version v4.2.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2020-01-31T19:24:23.752+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:341:17
@(connect):2:6
2020-01-31T19:24:23.768+0800 F - [main] exception: connect failed
2020-01-31T19:24:23.768+0800 E - [main] exiting with code 1

报错排查

看报错信息总之就是连接失败了,这么一大段除了 Error connecting 也没啥有用的信息了。为了获取具体的错误信息,我们可以查看 MongoDB 的日志文件,日志在 /usr/local/var/log/mongodb 目录中,日志信息如下:

$ cat /usr/local/var/log/mongodb/mongo.log
2020-01-31T20:20:11.840+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] MongoDB starting : pid=44665 port=27017 dbpath=/usr/local/var/mongodb 64-bit host=Jalan-JiangdeMacBook-Pro.local
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] db version v4.2.2
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] allocator: system
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] modules: none
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] build environment:
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] distarch: x86_64
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] target_arch: x86_64
2020-01-31T20:20:11.875+0800 I CONTROL [initandlisten] options: { config: "/usr/local/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, storage: { dbPath: "/usr/local/var/mongodb" }, systemLog: { destination: "file", logAppend: true, path: "/usr/local/var/log/mongodb/mongo.log" } }
2020-01-31T20:20:11.876+0800 I STORAGE [initandlisten]
2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** WARNING: Support for MMAPV1 storage engine has been deprecated and will be
2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** removed in version 4.2. Please plan to migrate to the wiredTiger
2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** storage engine.
2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/deprecated-mmapv1
2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten]
2020-01-31T20:20:11.877+0800 I STORAGE [initandlisten] Detected data files in /usr/local/var/mongodb created by the 'mmapv1' storage engine, so setting the active storage engine to 'mmapv1'.
2020-01-31T20:20:11.878+0800 I STORAGE [initandlisten] exception in initAndListen: Location18656: Cannot start server with an unknown storage engine: mmapv1, terminating
2020-01-31T20:20:11.878+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2020-01-31T20:20:11.878+0800 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2020-01-31T20:20:11.878+0800 I - [initandlisten] Stopping further Flow Control ticket acquisitions.
2020-01-31T20:20:11.879+0800 I CONTROL [initandlisten] now exiting
2020-01-31T20:20:11.879+0800 I CONTROL [initandlisten] shutting down with code:100

列出关键信息:WARNING: Support for MMAPV1 storage engine has been deprecated and will be removed in version 4.2. Please plan to migrate to the wiredTiger

翻译一下就是:4.2 版本已不再支持 MMAPV1 引擎,请迁移至 wiredTiger 引擎

Compatibility Changes in MongoDB 4.0 中我们可以看到,MongoDB 从 4.0 版本开始就不再支持 MMAPV1 引擎了(Deprecate MMAPv1),因此,我们需要把引擎切换成 wiredTiger。

错误处理

首先,启动要切换引擎的 MongoDB:

$ brew services start mongodb-community@4.2

如果这个数据库中还有你所需要的数据,需要使用 mongodump 对数据库进行备份。

然后,创建一个新的目录,用于 wiredTiger 引擎的数据存储。我创建的目录为:/usr/local/varmongodb-w

最后,使用 wiredTiger 引擎启动 MongoDB:

$ mongod --storageEngine wiredTiger --dbpath /usr/local/varmongodb-w

其中,--dbpath 后填写你刚才创建的新目录。

如果你刚才备份过数据,还需要使用 mongorestore 将数据重新导入。

大功告成啦~

参考资料