MongoDB MongoDB 修改数据库保存位置无法启动?

OrderSun · 2018年03月20日 · 最后由 OrderSun 回复于 2018年03月20日 · 4439 次阅读

环境ubuntu16.04,项目使用mongodb,由于数据量较大,想把数据库保存到其他目录,修改如下:

默认的数据存储目录是/var/lib/mongodb,假设我们的目的路径是/home/mongodb。

关闭数据库:

sudo service mongod stop

在配置文件中将storage.dbPath项设置为目的路径,修改后如下:

storage:
  dbPath: /home/mongodb

将原目录下的文件复制到新目录中:

sudo cp -r /var/lib/mongodb/* /home/mongodb/

将mongod.lock文件删除后启动数据库:

sudo rm /home/mongodb/mongod.lock
sudo service mongod start

看起来一切都是那么美好~~~~

查看启动状态:

sudo service mongod status
● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2018-03-20 09:35:41 CST; 4s ago
     Docs: https://docs.mongodb.org/manual
  Process: 17283 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=100)
 Main PID: 17283 (code=exited, status=100)

Mar 20 09:35:41 smart systemd[1]: Started High-performance, schema-free document-oriented database.
Mar 20 09:35:41 smart systemd[1]: mongod.service: Main process exited, code=exited, status=100/n/a
Mar 20 09:35:41 smart systemd[1]: mongod.service: Unit entered failed state.
Mar 20 09:35:41 smart systemd[1]: mongod.service: Failed with result 'exit-code'.

那么问题来了,为什么会启动失败呢? 通过查询日志文件得知:

Attempted to create a lock file on a read-only directory: /home/mongodb, terminating

查看/home/mongodb的权限

drwxr-xr-x 4 root root 4.0K Mar 20 10:55 mongodb

这里的文件所属者不正确,mongodb这个目录的所有者必须是mongodb,使用以下命令修改

# /home
sudo chown -R mongodb:mongodb *

修改后权限为

drwxr-xr-x 4 mongodb mongodb 4.0K Mar 20 10:55 mongodb

重启服务,大功告成。

共收到 6 条回复

直接执行 mongod --config /etc/mongod.conf 看错误日志

如果配置里面错误日志没有直接输出,请阅读 /etc/mongod.conf 找到日志文件,就可以找到错误原因。

huacnlee 回复

执行这个没有反应,卡住不动。

OrderSun 回复

看我第二句...

huacnlee 回复

日志内容是这样的,没看出来哪里出错了,请指教🙏

2018-03-20T10:09:21.433+0800 I CONTROL  [main] ***** SERVER RESTARTED *****
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] MongoDB starting : pid=17726 port=27017 dbpath=/home/mongodb 64-bit host=smart
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] db version v3.4.13
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] git version: fbdef2ccc53e0fcc9afb570063633d992b2aae42
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] modules: none
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] build environment:
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten]     distmod: ubuntu1604
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-03-20T10:09:21.436+0800 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/home/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2018-03-20T10:09:21.460+0800 I -        [initandlisten] Detected data files in /home/sunyudong/database/mongodb created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-03-20T10:09:21.460+0800 I STORAGE  [initandlisten]
2018-03-20T10:09:21.460+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-03-20T10:09:21.460+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-03-20T10:09:21.460+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=3406M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),
2018-03-20T10:09:21.674+0800 I STORAGE  [initandlisten] WiredTiger message [1521511761:674256][17726:0x7f4c98877d00], txn-recover: Main recovery loop: starting at 11/5632
2018-03-20T10:09:21.760+0800 I STORAGE  [initandlisten] WiredTiger message [1521511761:760006][17726:0x7f4c98877d00], txn-recover: Recovering log 11 through 12
2018-03-20T10:09:21.857+0800 I STORAGE  [initandlisten] WiredTiger message [1521511761:857612][17726:0x7f4c98877d00], txn-recover: Recovering log 12 through 12
2018-03-20T10:09:22.359+0800 I CONTROL  [initandlisten] 
2018-03-20T10:09:22.360+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-03-20T10:09:22.360+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-03-20T10:09:22.360+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-03-20T10:09:22.360+0800 I CONTROL  [initandlisten]
2018-03-20T10:09:22.365+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/home/sunyudong/database/mongodb/diagnostic.data'
2018-03-20T10:09:22.366+0800 I NETWORK  [thread1] waiting for connections on port 27017
2018-03-20T10:10:48.106+0800 I CONTROL  [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2018-03-20T10:10:48.106+0800 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2018-03-20T10:10:48.107+0800 I NETWORK  [signalProcessingThread] closing listening socket: 7
2018-03-20T10:10:48.107+0800 I NETWORK  [signalProcessingThread] closing listening socket: 8
2018-03-20T10:10:48.107+0800 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2018-03-20T10:10:48.107+0800 I NETWORK  [signalProcessingThread] shutdown: going to flush diaglog...
2018-03-20T10:10:48.107+0800 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2018-03-20T10:10:48.112+0800 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2018-03-20T10:10:48.502+0800 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2018-03-20T10:10:48.502+0800 I CONTROL  [signalProcessingThread] now exiting
2018-03-20T10:10:48.502+0800 I CONTROL  [signalProcessingThread] shutting down with code:0
OrderSun 回复

你要用 sudo service mongod start 的方式启动,然后看那个日志文件。

这个明显是权限的问题,你直接执行 mongod --config /etc/mongod.conf 没问题,不代表 sudo service mongod start 也没问题,两种执行方式权限是不一样的。

huacnlee 回复

多谢,搞定了,是文件权限问题,mongodb/这个目录的所有者必须是mongodb,可以使用这个命令修复:

sudo chown -R mongodb:mongodb *

权限改成这样:

drwxr-xr-x 4 mongodb mongodb 4.0K Mar 20 10:55 mongodb
huacnlee 关闭了讨论 03月20日 16:17
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册