MongoDB学习笔记【1】-- 安装启动

大部分内容根据MongoDB官方手册整理:http://docs.mongodb.org/manual/contents/

环境CentOS5.7 x86_64

 

下载:

[root@slayer Pkgs]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.2.3.tgz

解压并复制到/opt/mongo/目录

[root@slayer bin]# tar zxvf mongodb-linux-x86_64-2.2.3.tgz -C /tmp/


[root@slayer ~]#mkdir /opt/mongo


[root@slayer ~]# cp -R /tmp/mongodb-linux-x86_64-2.2.3/* /opt/mongo/

给mongodb的可执行文件建立软连接放到/usr/bin目录

[root@slayer ~]# for i in `ls /opt/mongo/bin`
> do
> ln -s /opt/mongo/bin/$i /usr/bin/$i
> done
[root@slayer ~]#

 

建立数据库目录,这里我不准备root来运行,所以不需要更改目录所有权

[root@slayer ~]# mkdir /data/db/
[root@slayer ~]# chmod 777 /data/db/

 

vi /etc/mongodb.conf建立配置文件并写入以下内容

fork = true
bind_ip = 127.0.0.1
port = 27017
quiet = true
dbpath = /data/db
logpath = /var/log/mongodb/mongod.log
logappend = true
journal = true

 

 启动mongodb

[root@slayer ~]# mongod --config /etc/mongodb.conf
can't open [/var/log/mongodb/mongod.log] for log file: errno:2 No such file or directory

log文件不存在?建立一下再启动

[root@slayer ~]# mkdir -p /var/log/mongodb/ && mongod --config /etc/mongodb.conf
all output going to: /var/log/mongodb/mongod.log
forked process: 27030
[root@slayer ~]#

然后打开MongoDB Shell看看

[root@slayer ~]# mongo
MongoDB shell version: 2.2.3
connecting to: test
Thu Dec 13 11:29:13 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed

连接失败,看看log文件是什么问题 cat /var/log/mongodb/mongod.log

[root@slayer ~]# cat /var/log/mongodb/mongod.log
Thu Dec 13 11:26:34 [initandlisten] MongoDB starting : pid=27030 port=27017 dbpath=/data/db 64-bit host=postgresql
Thu Dec 13 11:26:34 [initandlisten] db version v2.2.3, pdfile version 4.5
Thu Dec 13 11:26:34 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08
Thu Dec 13 11:26:34 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Thu Dec 13 11:26:34 [initandlisten] options: { bind_ip: "127.0.0.1", config: "/etc/mongodb.conf", dbpath: "/data/db", fork: "true", journal: "true", logappend: "true", logpath: "/var/log/mongodb/mongod.log", port: 27017, quiet: "true" }
Thu Dec 13 11:26:34 [initandlisten] journal dir=/data/db/journal
Thu Dec 13 11:26:34 [initandlisten] recover : no journal files present, no recovery needed
Thu Dec 13 11:26:34 [initandlisten]
Thu Dec 13 11:26:34 [initandlisten] ERROR: Insufficient free space for journal files
Thu Dec 13 11:26:34 [initandlisten] Please make at least 3379MB available in /data/db/journal or use --smallfiles
Thu Dec 13 11:26:34 [initandlisten]
Thu Dec 13 11:26:34 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Thu Dec 13 11:26:34 dbexit:
Thu Dec 13 11:26:34 [initandlisten] shutdown: going to close listening sockets...
Thu Dec 13 11:26:34 [initandlisten] shutdown: going to flush diaglog...
Thu Dec 13 11:26:34 [initandlisten] shutdown: going to close sockets...
Thu Dec 13 11:26:34 [initandlisten] shutdown: waiting for fs preallocator...
Thu Dec 13 11:26:34 [initandlisten] shutdown: lock for final commit...
Thu Dec 13 11:26:34 [initandlisten] shutdown: final commit...
Thu Dec 13 11:26:34 [initandlisten] shutdown: closing all files...
Thu Dec 13 11:26:34 [initandlisten] closeAllFiles() finished
Thu Dec 13 11:26:34 [initandlisten] journalCleanup...
Thu Dec 13 11:26:34 [initandlisten] removeJournalFiles
Thu Dec 13 11:26:34 [initandlisten] shutdown: removing fs lock...
Thu Dec 13 11:26:34 dbexit: really exiting now

可用控件不足。。照log提示加--smallfiles再启试试

[root@slayer ~]# mongod --config /etc/mongodb.conf --smallfiles
all output going to: /var/log/mongodb/mongod.log
forked process: 27054
child process started successfully, parent exiting
[root@slayer ~]# mongo
MongoDB shell version: 2.2.3
connecting to: test
>

成功进入MongoDB shell

 

MongoDB学习笔记【1】-- 安装启动

MongoDB学习笔记【2】-- 试用

MongoDB学习笔记【3】-- MongoDB C驱动使用

MongoDB学习笔记【4】-- MongoDB Java驱动使用

posted @ 2013-03-01 11:17  Leo Forest  阅读(1287)  评论(0编辑  收藏  举报