LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 数据库 >

修复MongoDB因Unclean Shutdown导致服务不能启动的问题

时间:2013-07-03 11:36来源:wubiaoblog.com/archives/683 编辑:吴飚 点击:
mongodb发现服务启不来 [root@biao ~]# mongod -f /etc/mongod.conf forked process: 19465 all output going to: /mongodb/log/mongodb.log [root@biao ~]# service mongod status mongod is stopped 打开log看了下,日志太多了,密密麻麻
mongodb发现服务启不来
[root@biao ~]# mongod -f /etc/mongod.conf
forked process: 19465
all output going to: /mongodb/log/mongodb.log
[root@biao ~]# service mongod status
mongod is stopped
打开log看了下,日志太多了,密密麻麻的,用tail查看又怕看得不全,索性重新指定个log文件,让日志记录到新的日志文件中:
[root@biao ~]# mongod -f /etc/mongod.conf -logpath /mongodb/log/mongodb_new.log
forked process: 20310
all output going to: /mongodb/log/mongodb_new.log
[root@biao ~]# service mongod status
mongod is stopped


查看log:

# more /mongodb/log/mongodb_new.log
Wed Apr  3 23:02:40
Wed Apr  3 23:02:40 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Wed Apr  3 23:02:40
Wed Apr  3 23:02:40 [initandlisten] MongoDB starting : pid=20310 port=27017 dbpath=/mongodb/data 32-bit host=biao.oracle.com
Wed Apr  3 23:02:40 [initandlisten]
Wed Apr  3 23:02:40 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Wed Apr  3 23:02:40 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Wed Apr  3 23:02:40 [initandlisten] **       with --journal, the limit is lower
Wed Apr  3 23:02:40 [initandlisten]
Wed Apr  3 23:02:40 [initandlisten] db version v2.2.3, pdfile version 4.5
Wed Apr  3 23:02:40 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Wed Apr  3 23:02:40 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 20
08 i686 BOOST_LIB_VERSION=1_49
Wed Apr  3 23:02:40 [initandlisten] options: { config: "/etc/mongod.conf", dbpath: "/mongodb/data", fork: "true", logappend:
"true", logpath: "/mongodb/log/mongodb_new.log", pidfilepath: "/var/run/mongodb/mongod.pid" }
Wed Apr  3 23:02:40 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator con
structor: No such file or directory: "/mongodb/data/journal"
**************
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repair for recovery instructions.
*************
Wed Apr  3 23:02:40 [initandlisten] exception in initAndListen: 12596 old lock file, terminating
Wed Apr  3 23:02:40 dbexit:
Wed Apr  3 23:02:40 [initandlisten] shutdown: going to close listening sockets...
Wed Apr  3 23:02:40 [initandlisten] shutdown: going to flush diaglog...
Wed Apr  3 23:02:40 [initandlisten] shutdown: going to close sockets...
Wed Apr  3 23:02:40 [initandlisten] shutdown: waiting for fs preallocator...
Wed Apr  3 23:02:40 [initandlisten] shutdown: closing all files...
Wed Apr  3 23:02:40 [initandlisten] closeAllFiles() finished
Wed Apr  3 23:02:40 dbexit: really exiting now

发现其中有一段,也是最中要的一段日志:

**************
Unclean shutdown detected.
Please visit http://dochub.mongodb.org/core/repairfor recovery instructions.
*************

日志中说发现有unclean shutdown,还给了个指导链接,真人性化!上去研究了下。服务器断电、系统异常关闭以及直接killall命令导致服务终止的情况都可能会被mondodb认为是unclean shutdown,因为unclean shutdown可能会导致数据不一致性或者数据损坏,所以必须要手动修复后才能继续提供服务。

mongodb修复unclean shutdown的方式有:
1、从journal修复,journal就是类似于Oracle数据库中的redo log,工作机制也是类似的都是write-ahead的。
2、从集群中其他节点的副本中修复。
3、如果服务不是运行在集群环境中,也没有启用journal,那必须在再次启动服务时使用–repair或者–repair加上–repairpath修复。

带与不带–repairpath的区别在于,来一段官方的解释,英文不好,翻译得费劲:
Use the –repair optionin conjunction with the –repairpath option. mongod will read the existing datafiles, and write the existing data to new data files. This does not modify oralter the existing data files.
You do not need to removethe mongod.lock file before using this procedure.
Use the –repair option.mongod will read the existing data files, write the existing data to new filesand replace the existing, possibly corrupt, files with new files.
You must remove themongod.lock file before using this procedure.

很明显我的mongodb跑的是单节点,也没有启用journal,journal配置据说在2.0以下的版本以及32位系统上默认是不启用的,据说是因为耗内存。。。

本机的mongod版本为2.2.3:

[root@biao ~]# mongod --version
db version v2.2.3, pdfile version 4.5
Thu Apr  4 01:14:14 git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08

本机是32位系统:

[root@biao ~]# getconf LONG_BIT
 32
[root@biao ~]# getconf WORD_BIT
32
[root@biao ~]# uname -a
Linux biao.oracle.com 2.6.18-348.3.1.el5 #1 SMP Mon Mar 11 19:40:14 EDT 2013 i686 i686 i386 GNU/Linux

在mongod –help发现有启用journal的参数,但是在默认配置文件中的没有发现有启用journal的参数,只有禁用的参数,但是想想的话,我这个情况应该是没有启用的,否则就自动修复了。

# Disables write-ahead journaling
# nojournal = true

这里我使用–repair修复,因为本来也就没啥有用的数据,替不替换都无所谓。
查看dbpath下的mogod.lock文件:

[root@biao ~]# ll /mongodb/data/
total 4
-rwxr-xr-x 1 root root 6 Jan 25 02:11 mongod.lock

官方指导说可以直接删了,我还是先移走吧,免得回头需要:

[root@biao ~]# mv /mongodb/data/mongod.lock  ~/.

执行修复,必须要指定dbpath:

[root@biao ~]# mongod --dbpath /mongodb/data --repair
Thu Apr  4 00:08:07
Thu Apr  4 00:08:07 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Apr  4 00:08:07
Thu Apr  4 00:08:07 [initandlisten] MongoDB starting : pid=23196 port=27017 dbpath=/mongodb/data 32-bit host=biao.oracle.com
Thu Apr  4 00:08:07 [initandlisten]
Thu Apr  4 00:08:07 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Apr  4 00:08:07 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Apr  4 00:08:07 [initandlisten] **       with --journal, the limit is lower
Thu Apr  4 00:08:07 [initandlisten]
Thu Apr  4 00:08:07 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Apr  4 00:08:07 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Thu Apr  4 00:08:07 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Apr  4 00:08:07 [initandlisten] options: { dbpath: "/mongodb/data", repair: true }
Thu Apr  4 00:08:07 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/mongodb/data/journal"
Thu Apr  4 00:08:07 [initandlisten] finished checking dbs
Thu Apr  4 00:08:07 dbexit:
Thu Apr  4 00:08:07 [initandlisten] shutdown: going to close listening sockets...
Thu Apr  4 00:08:07 [initandlisten] shutdown: going to flush diaglog...
Thu Apr  4 00:08:07 [initandlisten] shutdown: going to close sockets...
Thu Apr  4 00:08:07 [initandlisten] shutdown: waiting for fs preallocator...
Thu Apr  4 00:08:07 [initandlisten] shutdown: closing all files...
Thu Apr  4 00:08:07 [initandlisten] closeAllFiles() finished
Thu Apr  4 00:08:07 [initandlisten] shutdown: removing fs lock...
Thu Apr  4 00:08:07 dbexit: really exiting now

也没看到有是否修复成功的提示
直接启动下看看:

[root@biao ~]# mongod --dbpath /mongodb/data/
Thu Apr  4 00:12:06
Thu Apr  4 00:12:06 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Thu Apr  4 00:12:06
Thu Apr  4 00:12:06 [initandlisten] MongoDB starting : pid=23395 port=27017 dbpath=/mongodb/data/ 32-bit host=biao.oracle.com
Thu Apr  4 00:12:06 [initandlisten]
Thu Apr  4 00:12:06 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Thu Apr  4 00:12:06 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Thu Apr  4 00:12:06 [initandlisten] **       with --journal, the limit is lower
Thu Apr  4 00:12:06 [initandlisten]
Thu Apr  4 00:12:06 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Apr  4 00:12:06 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Thu Apr  4 00:12:06 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49
Thu Apr  4 00:12:06 [initandlisten] options: { dbpath: "/mongodb/data/" }
Thu Apr  4 00:12:06 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/mongodb/data/journal"
Thu Apr  4 00:12:06 [initandlisten] waiting for connections on port 27017
Thu Apr  4 00:12:06 [websvr] admin web console waiting for connections on port 28017

如上启动貌似是交互模式的,直接就定格在那了。。。

重开个窗口查看下状态:

[root@biao ~]# service mongod status
mongod (pid 23395) is running...
[root@biao ~]# ps aux|grep mongod
root     23395  0.1  0.7 119404 22800 pts/9    Sl+  00:12   0:01 mongod --dbpath /mongodb/data/
root     24417  0.0  0.0   4032   724 pts/11   S+   00:33   0:00 grep mongod
[root@biao ~]# netstat -aux|grep mongod
unix  2      [ ACC ]     STREAM     LISTENING     402385 /tmp/mongodb-27017.sock

可以看到mongod服务启动成功,监听端口为默认的27017。
重启一下再看看,记得用–shutdown关闭:

[root@biao ~]# mongod --shutdown
There doesn't seem to be a server running with dbpath: /data/db/
[root@biao ~]# mongod -f /etc/mongod.conf --shutdown
killing process with pid: 23395
[root@biao ~]# service mongod status
mongod is stopped
[root@biao ~]# mongod -f /etc/mongod.conf
forked process: 24592
all output going to: /mongodb/log/mongodb.log
child process started successfully, parent exiting

再检查下:

[root@biao ~]# service mongod status
 mongod (pid 24592) is running...
[root@biao ~]# ps aux|grep mongod
root     24592  0.6  0.6 118388 21060 ?        Sl   00:36   0:00 mongod -f /etc/mongod.conf
root     24631  0.0  0.0    292    52 pts/11   R+   00:36   0:00 grep mongod
[root@biao ~]# netstat -aux|grep mongod
unix  2      [ ACC ]     STREAM     LISTENING     404328 /tmp/mongodb-27017.sock
可以看到mongod服务依然坚挺的启动成功了,监听端口依然坚挺的为默认的27017。至此,修复成功。
心得:关闭服务时,一定要注意,最好就是用–shutdown命令关闭,既方便又安全。可以的话还是要启用journal功能,至于journal有多耗内存,还需后续进一步研究。

转载请保留固定链接: https://linuxeye.com/database/1802.html

------分隔线----------------------------
标签:mongodb
栏目列表
推荐内容