【问题标题】:MongoDB service won't start after moving location (Centos 7.1.1503)移动位置后 MongoDB 服务不会启动 (Centos 7.1.1503)
【发布时间】:2015-08-06 21:52:35
【问题描述】:

以前 mongodb 位于/var/lib/mongod。我已将此移动到/home/mongod,并更新了/etc/mongod.conf 文件以指向此位置。文件夹的权限看起来是正确的:

drwxrwxr-x. 2 mongod mongod          6 Aug  6 10:37 admin
-rw-------. 1 mongod mongod   67108864 Aug  6 11:49 admin.0
-rw-------. 1 mongod mongod   16777216 Aug  6 11:49 admin.ns
drwxrwxr-x. 2 mongod mongod          6 Aug  6 10:40 journal
drwxrwxr-x. 2 mongod mongod          6 Aug  6 10:37 local
-rw-------. 1 mongod mongod   67108864 Aug  6 11:49 local.0
-rw-------. 1 mongod mongod   16777216 Aug  6 11:49 local.ns
-rwxr-xr-x. 1 mongod mongod          0 Aug  6 11:50 mongod.lock
-rwxrwxr-x. 1 mongod mongod          0 Aug  6 10:12 mongod.lock.bak
drwxrwxr-x. 2 mongod mongod          6 Aug  6 10:37 sphere
-rw-------. 1 mongod mongod   67108864 Aug  6 11:50 sphere.0
-rw-------. 1 mongod mongod  134217728 Aug  6 11:49 sphere.1
-rw-------. 1 mongod mongod  268435456 Aug  6 11:49 sphere.2
-rw-------. 1 mongod mongod  536870912 Aug  6 11:49 sphere.3
-rw-------. 1 mongod mongod 1073741824 Aug  6 11:50 sphere.4
-rw-------. 1 mongod mongod 2146435072 Aug  6 11:50 sphere.5
-rw-------. 1 mongod mongod   16777216 Aug  6 11:50 sphere.ns
drwxr-xr-x. 2 mongod mongod          6 Aug  6 11:50 _tmp

当我从命令行运行时,它可以工作:

[user@localhost home]$ sudo -u mongod mongod --dbpath /home/mongod
[sudo] password for user: 
2015-08-06T12:04:35.761+0100 [initandlisten] MongoDB starting : pid=10235 port=27017 dbpath=/home/mongod 64-bit host=localhost.localdomain
2015-08-06T12:04:35.762+0100 [initandlisten] db version v2.6.10
2015-08-06T12:04:35.762+0100 [initandlisten] git version: 5901dbfb49d16eaef6f2c2c50fba534d23ac7f6c
2015-08-06T12:04:35.762+0100 [initandlisten] build info: Linux build18.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-08-06T12:04:35.762+0100 [initandlisten] allocator: tcmalloc
2015-08-06T12:04:35.762+0100 [initandlisten] options: { storage: { dbPath: "/home/mongod" } }
2015-08-06T12:04:35.772+0100 [initandlisten] journal dir=/home/mongod/journal
2015-08-06T12:04:35.773+0100 [initandlisten] recover : no journal files present, no recovery needed
2015-08-06T12:04:36.865+0100 [initandlisten] waiting for connections on port 27017

但是运行 using systemctl 失败:

sudo service mongod restart
Restarting mongod (via systemctl):  Job for mongod.service failed. See 'systemctl status mongod.service' and 'journalctl -xn' for details.
                                                           [FAILED]

/etc/mongod.conf文件如下

logpath=/var/log/mongodb/mongod.log
logappend=true

# fork and run in background
fork=true

#port=27017

dbpath=/home/mongod

# location of pidfile
pidfilepath=/var/run/mongodb/mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces. 
bind_ip=127.0.0.1

/var/log/mongod/mongod.log中的输出如下:

2015-08-06T12:08:35.402+0100 ***** SERVER RESTARTED *****
2015-08-06T12:08:35.407+0100 [initandlisten] MongoDB starting : pid=10370 port=27017 dbpath=/home/mongod 64-bit host=localhost.localdomain
2015-08-06T12:08:35.407+0100 [initandlisten] db version v2.6.10
2015-08-06T12:08:35.407+0100 [initandlisten] git version: 5901dbfb49d16eaef6f2c2c50fba534d23ac7f6c
2015-08-06T12:08:35.407+0100 [initandlisten] build info: Linux build18.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-08-06T12:08:35.407+0100 [initandlisten] allocator: tcmalloc
2015-08-06T12:08:35.407+0100 [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1" }, processManagement: { fork: true, pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/home/mongod" }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } }
2015-08-06T12:08:35.407+0100 [initandlisten] exception in initAndListen std::exception: boost::filesystem::status: Permission denied: "/home/mongod/local.ns", terminating
2015-08-06T12:08:35.407+0100 [initandlisten] dbexit: 
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: going to close listening sockets...
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: going to flush diaglog...
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: going to close sockets...
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: waiting for fs preallocator...
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: lock for final commit...
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: final commit...
2015-08-06T12:08:35.407+0100 [initandlisten] shutdown: closing all files...
2015-08-06T12:08:35.407+0100 [initandlisten] closeAllFiles() finished
2015-08-06T12:08:35.407+0100 [initandlisten] dbexit: really exiting now

我已尝试删除锁定文件 /home/mongod/mongod.lock 并使用 sudo -u mongod mongod --dbpath /home/mongod --repair 运行修复,但错误仍然存​​在。这引起了严重的头痛!

【问题讨论】:

    标签: mongodb service centos


    【解决方案1】:

    原来问题出在SElinux上,解决方法和this question一样。

    这是与权限相关的,是由于 SELinux 安全上下文在 CentOS 上默认设置为强制执行的。

    sudo ausearch -m avc -ts today | audit2allow
    

    在我的机器上,我进行了一些与 mongo 相关的审计,如下所示:

    #============= mongod_t ============== 
    allow mongod_t home_root_t:file getattr; 
    allow mongod_t user_home_dir_t:dir search; 
    allow mongod_t var_lib_t:lnk_file read;
    allow mongod_t default_t:file getattr;
    

    要解决上述问题,请执行以下操作:

    sudo chcon -Rv --type=mongod_var_lib_t /path_to_your_mongo
    

    /path_to_your_mongo 是您的 mongod 数据文件所在的位置(在我的情况下是 /home/mongod,但通常是 /var/lib/mongo/data/db

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 2017-01-29
      • 1970-01-01
      • 2011-04-12
      相关资源
      最近更新 更多