Mac側のmongoが起動しなくなったらdbpathを疑う

ある日突然mongoが繋がらなくなりました。

```
$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
2018-08-03T23:11:23.982+0900 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
2018-08-03T23:11:23.987+0900 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed
```

…もう…MySQLといい…Mac側にデータベース持つとこういう不具合多くないですか…。

# （結論）mongod.confで指定してるdbpathが`/data/db`以外になってないかを確認する

あくまで可能性の一つですが、自分の場合はmongodを起動するときに読みに行く`dbpath`が合っていないことが原因でした。

#### 1. ひとまずmongodを実行してみる。

```
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten] build environment:
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten]     distarch: x86_64
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten]     target_arch: x86_64
2018-08-03T23:13:20.920+0900 I CONTROL  [initandlisten] options: {}
2018-08-03T23:13:20.923+0900 I STORAGE  [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-08-03T23:13:20.923+0900 I CONTROL  [initandlisten] now exiting
2018-08-03T23:13:20.923+0900 I CONTROL  [initandlisten] shutting down with code:100
```

`NonExistentPath: Data directory /data/db not found` と言われてシャットダウンしてしまっています。`/data/db`なんてパスはありませんでした。ので、一体どこにデータなんてあるんだい！と思っていろいろ探していたら、`/usr/local/etc/mongod.conf`を見つけました。

#### 2. `vim /usr/local/etc/mongod.conf`を実行してみる

```
$ vim /usr/local/etc/mongod.conf
  1 systemLog:
  2   destination: file
  3   path: /usr/local/var/log/mongodb/mongo.log
  4   logAppend: true
  5 storage:
  6   dbPath: /usr/local/var/mongodb
  7 net:
  8   bindIp: 127.0.0.1
```

`storage:`にデータの保存先が書かれています。なので、mongodを起動する時にこのパスを明示的に指定してあげればOKです。

#### 3. `mongod --dbpath /usr/local/var/mongodb/` を実行してみる

```
$ mongod --dbpath /usr/local/var/mongodb/
...(省略)...
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten] build environment:
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten]     distarch: x86_64
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten]     target_arch: x86_64
2018-08-03T23:40:02.087+0900 I CONTROL  [initandlisten] options: { storage: { dbPath: "/usr/local/var/mongodb/" } }
2018-08-03T23:40:02.091+0900 I -        [initandlisten] Detected data files in /usr/local/var/mongodb/ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
...(省略)...
2018-08-03T23:40:03.477+0900 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/usr/local/var/mongodb/diagnostic.data'
2018-08-03T23:40:03.478+0900 I NETWORK  [initandlisten] waiting for connections on port 27017
```

#### 4. `mongo` をぶっ叩く

```
$ mongo
MongoDB shell version v3.6.4
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.4
Server has startup warnings:
2018-08-03T23:40:03.453+0900 I CONTROL  [initandlisten]
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten]
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2018-08-03T23:40:03.454+0900 I CONTROL  [initandlisten]
>
```

はいこんにちは。やっと入れましたよ…。