MongoDBの学習中、MongoDBを初めて起動する際にエラーが発生しMongoDBが起動できなかった場合の対処法をメモ。
設定後mongod
コマンドでエラー発生
起動に必要な/data/db
ディレクトリを作成後、PATHを通してmongod
コマンドを実行すると、下記のようなエラーが発生した。
wildmouse$ mongod
2018-11-18T12:31:11.896+0900 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] MongoDB starting : pid=5172 port=27017 dbpath=/data/db 64-bit host=noguchitsukasanoMacBook-Pro.local
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] db version v4.0.4
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] allocator: system
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] modules: none
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] build environment:
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] distarch: x86_64
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] target_arch: x86_64
2018-11-18T12:31:11.909+0900 I CONTROL [initandlisten] options: {}
2018-11-18T12:31:11.909+0900 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-11-18T12:31:11.909+0900 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2018-11-18T12:31:11.909+0900 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2018-11-18T12:31:11.910+0900 I CONTROL [initandlisten] now exiting
2018-11-18T12:31:11.910+0900 I CONTROL [initandlisten] shutting down with code:100
エラーの原因と解決方法
最後のエラー行を見てみると、shutting down with code:100
と書かれているので公式のエラーコードを参照してみる。
error_code("UnsatisfiableWriteConcern", 100)
と書かれており、書き込みに問題がある模様。
思い返すと/data/db
directoryの作成時にsudoを使っていたため、一般ユーザーでmongod
を使うと書き込みができないことが分かる。
そのため/data/db
の権限をchown
コマンドを使って書き換えた、sudo mongod
でもよかったかもしれない。