光のカナダ留学blog

30歳で会社を退職。現在カナダ留学中です。

MongoDB起動時のAttempted to create a lock file on a read-only directoryというエラーと対処法

スポンサーリンク

MongoDB起動時のエラーとその対処法についてです。

MongoDBをmongodコマンドで起動させようとした所、下記のエラーが出て起動できず。

exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating

エラーによると/data/dbがread-onlyになっていてファイル作成が出来ないようなので、下記リンクを参考にchmodコマンドを実行()。

The problem is that the directory you created, /data/db is owned by and only writable by the root user, but you are running mongod as yourself. There are several ways to resolve this, but ultimately, you must give the directory in question the right permissions.

引用:MongoDB: exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating - Stack Overflow

sudo chmod -R go+w /data/db   (Rオプションはディレクトリとその配下のファイル全てのモードを変更)

ls -lコマンドで/data/dbディレクトリのパーミッションを確認すると、次のように書き変わりました。グループ及びその他のユーザーにw(書き込み)が追加されています。

drwxr-xr-x(元)
drwxrwxrwx(chmod後)

ここでもう一度mongodコマンドを実行したところ、今度はエラー無く起動できるようになりました。

)なおchmodの代わりにchownコマンドでディレクトリのオーナーを該当ユーザーに変更することでも可能です。

sudo chown -R $USER /data/db