MongoDBの起動で、"code:9001"、"SocketException"、"Address already in use"とエラーが出た。

##エラー内容

以下のコマンドで、MongoDBサーバを起動しようとした。

```bash:terminal
$ mongod
```

すると以下のような出力が出た。
>
{"t":{"$date":"2021-10-27T06:52:57.241+00:00"},"s":"I",  "c":"CONTROL",  "id":23285,   "ctx":"main","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}
{"t":{"$date":"2021-10-27T06:52:57.243+00:00"},"s":"W",  "c":"ASIO",     "id":22601,   "ctx":"main","msg":"No TransportLayer configured during NetworkInterface startup"}
{"t":{"$date":"2021-10-27T06:52:57.243+00:00"},"s":"I",  "c":"NETWORK",  "id":4648601, "ctx":"main","msg":"Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize."}
{"t":{"$date":"2021-10-27T06:52:57.244+00:00"},"s":"I",  "c":"STORAGE",  "id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":354,"port":27017,"dbPath":"/data/db","architecture":"64-bit","host":"685120251d1c"}}
{"t":{"$date":"2021-10-27T06:52:57.244+00:00"},"s":"I",  "c":"CONTROL",  "id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"4.4.10","gitVersion":"58971da1ef93435a9f62bf4708a81713def6e88c","openSSLVersion":"OpenSSL 1.1.1f  31 Mar 2020","modules":[],"allocator":"tcmalloc","environment":{"distmod":"ubuntu2004","distarch":"aarch64","target_arch":"aarch64"}}}}
{"t":{"$date":"2021-10-27T06:52:57.244+00:00"},"s":"I",  "c":"CONTROL",  "id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Ubuntu","version":"20.04"}}}
{"t":{"$date":"2021-10-27T06:52:57.244+00:00"},"s":"I",  "c":"CONTROL",  "id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{}}}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"E",  "c":"STORAGE",  "id":20568,   "ctx":"initandlisten","msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"REPL",     "id":4784900, "ctx":"initandlisten","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":10000}}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"COMMAND",  "id":4784901, "ctx":"initandlisten","msg":"Shutting down the MirrorMaestro"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"SHARDING", "id":4784902, "ctx":"initandlisten","msg":"Shutting down the WaitForMajorityService"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"NETWORK",  "id":4784905, "ctx":"initandlisten","msg":"Shutting down the global connection pool"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"NETWORK",  "id":4784918, "ctx":"initandlisten","msg":"Shutting down the ReplicaSetMonitor"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"SHARDING", "id":4784921, "ctx":"initandlisten","msg":"Shutting down the MigrationUtilExecutor"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"CONTROL",  "id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"STORAGE",  "id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"STORAGE",  "id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"-",        "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"FTDC",     "id":4784926, "ctx":"initandlisten","msg":"Shutting down full-time data capture"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"CONTROL",  "id":20565,   "ctx":"initandlisten","msg":"Now exiting"}
{"t":{"$date":"2021-10-27T06:52:57.245+00:00"},"s":"I",  "c":"CONTROL",  "id":23138,   "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":48}}

本来なら、コマンドを打てない状態になるはずだが、プロンプト`$`が表示されてしまう。
最後を見ると、シャットダウンされている。そりゃ動かんわな。。
どこからシャットダウンが始まっているのか調べるために、上の方を見てみると次のエラー文がある。
`"msg":"Error setting up listener","attr":{"error":{"code":9001,"codeName":"SocketException","errmsg":"Address already in use"}}}`
"アドレスをすでに使っている"、つまり、"MongoDBのプロセスがすでにある"と怒られている。

##　解決策

とりあえず、プロセスがあることを確認してみる。

```bash:terminal
$ ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1   3744  2968 pts/0    Ss+  05:51   0:00 bash
root         9  0.0  0.1   3880  3000 pts/1    Ss   05:51   0:00 bash
mongodb    238  0.9  4.9 1509388 101232 ?      Ssl  05:52   1:43 /usr/bin/mongod --config /etc/mongod.conf
root       361  0.0  0.1   5472  2284 pts/1    R+   08:49   0:00 ps -aux
```

おった。mongodbのプロセスIDがわかったので、そいつをキルしてやる。

```bash:terminal
$ kill <PID>
$ kill 238  #今回の場合、PIDが238なので
```

すると、`mongod`コマンドがうまく実行できるようになる。