8月29号,MongoDB发行了最新的数据库版本2.2.0.今天我来把它升级一下.下面记录下我的相关操作.
首先,我查看一下mongod服务有没有在运行.可以用下面两个命令查看: [root@mysql-master ~]# ps -ef | grep mongo root 3244 1 0 Aug15 ? 00:00:37 /usr/bin/mongod -f /etc/mongod.conf root 11480 11451 0 10:18 pts/0 00:00:00 grep mongo [root@mysql-master ~]# netstat -tpln | grep mongo tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 3244/mongod
tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 3244/mongod 上面显示我系统上的mongod服务是正常运行的,下面连上服务查看一下当前版本号及一些数据库信息: [root@mysql-master ~]# mongo MongoDB shell version: 2.0.7 connecting to: test > show dbs admin 0.203125GB jzf 0.203125GB local 0.203125GB test 0.203125GB > exit 升级前一定要关掉服务,停服务我们可以直接在shell里面调用db.shutdownServer()函数.或者用下面这条命令: /etc/init.d/mongod stop Stopping mongod: [ OK ] 服务器停掉后,我们可以将数据做一下备份.因为我这不是生产环境,这里就不做备份了.下面来升级MongoDB数据库软件 [root@mysql-master ~]# yum update -y mongo-10gen mongo-10gen-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: centos.ustc.edu.cn * extras: centos.ustc.edu.cn * updates: centos.ustc.edu.cn Setting up Update Process Resolving Dependencies --> Running transaction check ---> Package mongo-10gen.x86_64 0:2.2.0-mongodb_1 set to be updated ---> Package mongo-10gen-server.x86_64 0:2.2.0-mongodb_1 set to be updated --> Finished Dependency Resolution ...... Updated: mongo-10gen.x86_64 0:2.2.0-mongodb_1 mongo-10gen-server.x86_64 0:2.2.0-mongodb_1
Complete! 升级完成后,我们来试着启动下服务: [root@mysql-master mongo]# /etc/init.d/mongod start Starting mongod: all output going to: /var/log/mongo/mongod.log forked process: 11577 [FAILED] 糟糕,服务启不来,我们换下别的启动方法. [root@mysql-master mongo]# which mongod /usr/bin/mongod [root@mysql-master mongo]# /usr/bin/mongod -f /etc/mongod.conf forked process: 11590 all output going to: /var/log/mongo/mongod.log child process started successfully, parent exiting 我们来看一下,唉,这样是能正常启动的. [root@mysql-master mongo]# ps -ef | grep mongo root 11590 1 2 10:58 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf root 11601 11451 0 10:58 pts/0 00:00:00 grep mongo 既然服务启动了,我们来看一下能不能连接: [root@mysql-master mongo]# mongo MongoDB shell version: 2.2.0 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see Questions? Try the support group > use admin switched to db admin > db.auth("root","redhat") 1 > show dbs admin 0.203125GB jzf 0.203125GB local 0.203125GB test 0.203125GB > use jzf switched to db jzf > show collections blog food myself system.indexes system.users test test.test user > db.food.find() { "_id" : 1, "fruit" : [ "apple", "banana", "peach", "strawberry" ] } { "_id" : 2, "fruit" : [ "apple", "orange", "peach", "strawberry" ] } { "_id" : 3, "fruit" : [ "cherry", "orange", "peach", "strawberry" ] } { "_id" : 4, "fruit" : [ "cherry", "orange", "kumquat", "strawberry" ] } > exit bye 由上面可以看出,服务器版本上升了(原来的2.0.7升到了2.2.0).而且原来的数据都在!突然感觉备不备份都没差,当然生产环境中最好别偷懒. 可是为什么我们之前的第一种方式服务启不来呢,下面的操作是我试图再次用那种方式启动: [root@mysql-master mongo]# ps -ef | grep mongo root 11590 1 0 10:58 ? 00:00:01 /usr/bin/mongod -f /etc/mongod.conf root 11618 11451 0 11:26 pts/0 00:00:00 grep mongo [root@mysql-master mongo]# kill -2 11590 [root@mysql-master mongo]# ps -ef | grep mongo root 11621 11451 0 11:26 pts/0 00:00:00 grep mongo [root@mysql-master mongo]# /etc/init.d/mongod restart Stopping mongod: [FAILED] Starting mongod: all output going to: /var/log/mongo/mongod.log forked process: 11639 [FAILED] 还是启不来,我们来看看日志,看到底是什么原因 [root@mysql-master mongo]# vim /var/log/mongo/mongod.log Thu Aug 16 11:27:10 [initandlisten] opening db: test Thu Aug 16 11:27:10 [initandlisten] couldn't open /var/lib/mongo/test.ns errno:13 Permission denied Thu Aug 16 11:27:10 [initandlisten] error couldn't open file /var/lib/mongo/test.ns terminating Thu Aug 16 11:27:10 dbexit: Thu Aug 16 11:27:10 [initandlisten] shutdown: going to close listening sockets... Thu Aug 16 11:27:10 [initandlisten] shutdown: going to flush diaglog... Thu Aug 16 11:27:10 [initandlisten] shutdown: going to close sockets... Thu Aug 16 11:27:10 [initandlisten] shutdown: waiting for fs preallocator... Thu Aug 16 11:27:10 [initandlisten] shutdown: lock for final commit... Thu Aug 16 11:27:10 [initandlisten] shutdown: final commit... Thu Aug 16 11:27:10 [initandlisten] shutdown: closing all files... Thu Aug 16 11:27:10 [initandlisten] closeAllFiles() finished Thu Aug 16 11:27:10 [initandlisten] journalCleanup... Thu Aug 16 11:27:10 [initandlisten] removeJournalFiles Thu Aug 16 11:27:10 [initandlisten] flushing directory /var/lib/mongo/journal Thu Aug 16 11:27:10 [initandlisten] removeJournalFiles end Thu Aug 16 11:27:10 [initandlisten] shutdown: removing fs lock... Thu Aug 16 11:27:10 dbexit: really exiting now 打开日志一看,很明显的字眼映入眼帘:errno:13 Permission denied,这种错误信息太常见了!第一感觉当然是看数据库的目录权限咯: [root@mysql-master mongo]# ls -l /var/lib/mongo/ total 852856 -rw------- 1 root root 67108864 Aug 14 23:21 admin.0 -rw------- 1 root root 134217728 Aug 14 23:28 admin.1 -rw------- 1 root root 16777216 Aug 14 23:21 admin.ns drwxr-xr-x 2 mongod mongod 4096 Aug 16 11:34 journal -rw------- 1 root root 67108864 Aug 15 00:36 jzf.0 -rw------- 1 root root 134217728 Aug 15 02:45 jzf.1 -rw------- 1 root root 16777216 Aug 15 00:36 jzf.ns -rw------- 1 mongod mongod 67108864 Aug 14 12:50 local.0 -rw------- 1 mongod mongod 134217728 Aug 14 18:50 local.1 -rw------- 1 mongod mongod 16777216 Aug 14 12:50 local.ns -rwxr-xr-x 1 mongod mongod 0 Aug 16 11:34 mongod.lock drwxr-xr-x 2 root root 4096 Aug 15 02:35 test -rw------- 1 root root 67108864 Aug 15 02:35 test.0 -rw------- 1 root root 134217728 Aug 15 02:45 test.1 -rw------- 1 root root 16777216 Aug 15 02:35 test.ns 看到这里,我想大家应该明白,为什么后面那种方法能正常启动了.那么我们来改一下目录权限,看看前面那种启动方式能不能正常启动服务: [root@mysql-master mongo]# chown -R mongod. /var/lib/mongo/ [root@mysql-master mongo]# ls -l /var/lib/mongo/ total 852856 -rw------- 1 mongod mongod 67108864 Aug 14 23:21 admin.0 -rw------- 1 mongod mongod 134217728 Aug 14 23:28 admin.1 -rw------- 1 mongod mongod 16777216 Aug 14 23:21 admin.ns drwxr-xr-x 2 mongod mongod 4096 Aug 16 11:34 journal -rw------- 1 mongod mongod 67108864 Aug 15 00:36 jzf.0 -rw------- 1 mongod mongod 134217728 Aug 15 02:45 jzf.1 -rw------- 1 mongod mongod 16777216 Aug 15 00:36 jzf.ns -rw------- 1 mongod mongod 67108864 Aug 14 12:50 local.0 -rw------- 1 mongod mongod 134217728 Aug 14 18:50 local.1 -rw------- 1 mongod mongod 16777216 Aug 14 12:50 local.ns -rwxr-xr-x 1 mongod mongod 0 Aug 16 11:34 mongod.lock drwxr-xr-x 2 mongod mongod 4096 Aug 15 02:35 test -rw------- 1 mongod mongod 67108864 Aug 15 02:35 test.0 -rw------- 1 mongod mongod 134217728 Aug 15 02:45 test.1 -rw------- 1 mongod mongod 16777216 Aug 15 02:35 test.ns [root@mysql-master mongo]# /etc/init.d/mongod start Starting mongod: forked process: 11703 all output going to: /var/log/mongo/mongod.log child process started successfully, parent exiting [ OK ] 正常启动了.那么这两种方法倒底有什么不同的地方呢?我们来看一下mongod的启动脚本 [root@mysql-master ~]# vim /etc/init.d/mongod ...... MONGO_USER=mongod MONGO_GROUP=mongod ...... start() { echo -n $"Starting mongod: " daemon --user "$MONGO_USER" $NUMACTL $mongod $OPTIONS RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod } 原来,mongod启动脚本中服务启动的用户是mongod,而用后一种方法启动用户是root.那么服务启不了,报权限被拒绝也是很正常的事啦.下面我们来分别看下:
[root@mysql-master ~]# /etc/init.d/mongod start [root@mysql-master ~]# ps -ef | grep mongo mongod 11703 1 0 11:49 ? 00:00:12 /usr/bin/mongod -f /etc/mongod.conf root 11736 11451 0 13:57 pts/0 00:00:00 mongo
[root@mysql-master ~]# /usr/bin/mongod -f /etc/mongod.conf [root@mysql-master ~]# ps -ef | grep mongo root 11590 1 0 10:58 ? 00:00:01 /usr/bin/mongod -f /etc/mongod.conf
本文出自 “” 博客,请务必保留此出处