MongoDB 起動エラー「Failed to set up sockets during startup.」「Failed to set up listener: InternalError: Failed to set up sockets」

MongoDB の起動時のエラーに対処する彼是。

# 環境

* AWS AMI MongoDB 3.4
    * https://aws.amazon.com/marketplace/pp/B01N5HGPHT?ref=cns_srchrow

# MongoDB 起動のエラー

```shell
$ restart mongodb
/jet/etc/init/mongodb: not running (no pid-file)
/jet/etc/init/mongodb: no process started

$ start mongodb
/jet/etc/init/mongodb: no process started
```

起動、再起動どちらも不可。

# ログを確認

```shell
$ cat mongodb.log.2018-07-05T05-59-44
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] MongoDB starting : pid=4021 port=27017 dbpath=/jet/prs/mongodb/db 64-bit host=ip-10-0-10-119
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] db version v3.4.4
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2h  3 May 2016
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] modules: none
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] build environment:
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten]     distarch: x86_64
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2018-07-05T05:59:41.533+0000 I CONTROL  [initandlisten] options: { config: "/jet/etc/mongodb/mongodb.conf", net: { bindIp: "0.0.0.0", port: 27017, unixDomainSocket: { pathPrefix: "/jet/run/mongodb" } }, storage: { dbPath: "/jet/prs/mongodb/db" }, systemLog: { destination: "file", path: "/jet/log/mongodb/mongodb.log" } }
2018-07-05T05:59:41.579+0000 E NETWORK  [initandlisten] listen(): bind() failed Address already in use for socket: 0.0.0.0:27017
2018-07-05T05:59:41.580+0000 E NETWORK  [initandlisten]   addr already in use
2018-07-05T05:59:41.580+0000 E NETWORK  [initandlisten] Failed to set up sockets during startup.
2018-07-05T05:59:41.580+0000 E STORAGE  [initandlisten] Failed to set up listener: InternalError: Failed to set up sockets
2018-07-05T05:59:41.580+0000 I NETWORK  [initandlisten] shutdown: going to close listening sockets...
2018-07-05T05:59:41.580+0000 I NETWORK  [initandlisten] shutdown: going to flush diaglog...
2018-07-05T05:59:41.580+0000 I CONTROL  [initandlisten] now exiting
2018-07-05T05:59:41.580+0000 I CONTROL  [initandlisten] shutting down with code:48
```

`Failed to set up sockets during startup.` `Failed to set up listener: InternalError: Failed to set up sockets` この辺りが怪しい。ソケットが上手く設定できていない模様。

# 原因

MongoDB が使用するポートが既に使用されていること原因で、以前のゴミが残ってしまっている状態。これを消せばおk。

`ps`コマンドでプロセスを確認後、`kill`コマンドでプロセスを削除。

```shell
$ ps aux | grep mongo
ec2-user  3201  0.2  5.2 303824 53672 ?        Sl   05:48   0:02 mongod --config /jet/etc/mongodb/mongodb.conf --logpath /jet/log/mongodb/mongodb.log --dbpath /jet/prs/mongodb/db --unixSocketPrefix /jet/run/mongodb --port 27017 --bind_ip 0.0.0.0
ec2-user  4083  0.0  0.0  10856   944 pts/0    D+   06:01   0:00 grep --color=auto mongo
```

```shell
$ kill -9 3201
```

# 参考記事

* https://stackoverflow.com/questions/34709062/failed-to-set-up-sockets-during-startup-dbexit-rc-48-error-in-mongodb/37954282