MongoDB初读-1
更新日期:
MongoDB初读
MongoDB安装
1 2 3 4 5 6 7 8 9 10 11 12 | 1 Download mongodb-linux-i686-2.4.9.tgz 2 tar xvf mongodb-linux-i686-2.4.9.tgz 3 mkdir -p /data/db/ //默认会在/data/db/存储数据库文件 4 chown `id -u` /data/db **5 chown -R mongodb:mongodb db/** 这个可重要了 6 ./mongod OR 1 sudo apt-get install mongodb-10gen Installing new version of config file /etc/mongodb.conf ... Installing new version of config file /etc/init/mongodb.conf ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | $mongod ./mongod --help for help and startup options Mon Mar 3 16:27:50.735 Mon Mar 3 16:27:50.738 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability. Mon Mar 3 16:27:50.739 Mon Mar 3 16:27:51.037 [initandlisten] MongoDB starting : pid=19039 port=27017 dbpath=/data/db/ 32-bit host=ubuntu Mon Mar 3 16:27:51.039 [initandlisten] Mon Mar 3 16:27:51.040 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. Mon Mar 3 16:27:51.041 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal). Mon Mar 3 16:27:51.042 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off. Mon Mar 3 16:27:51.042 [initandlisten] ** See http://dochub.mongodb.org/core/32bit Mon Mar 3 16:27:51.044 [initandlisten] Mon Mar 3 16:27:51.047 [initandlisten] db version v2.4.9 Mon Mar 3 16:27:51.047 [initandlisten] git version: 52fe0d21959e32a5bdbecdc62057db386e4e029c Mon Mar 3 16:27:51.047 [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 Mon Mar 3 16:27:51.048 [initandlisten] allocator: system Mon Mar 3 16:27:51.048 [initandlisten] options: {} Mon Mar 3 16:27:51.271 [FileAllocator] allocating new datafile /data/db/local.ns, filling with zeroes... Mon Mar 3 16:27:51.273 [FileAllocator] creating directory /data/db/_tmp Mon Mar 3 16:27:51.355 [FileAllocator] done allocating datafile /data/db/local.ns, size: 16MB, took 0.045 secs Mon Mar 3 16:27:51.357 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes... Mon Mar 3 16:27:51.432 [FileAllocator] done allocating datafile /data/db/local.0, size: 16MB, took 0.002 secs Mon Mar 3 16:27:51.510 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 reslen:37 239ms Mon Mar 3 16:27:51.520 [websvr] admin web console waiting for connections on port 28017 Mon Mar 3 16:27:51.522 [initandlisten] waiting for connections on port 27017 |
另起console
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $mongo MongoDB shell version: 2.4.9 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: Mon Mar 3 16:39:22.629 [initandlisten] Mon Mar 3 16:39:22.631 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. Mon Mar 3 16:39:22.631 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal). Mon Mar 3 16:39:22.632 [initandlisten] ** See http://dochub.mongodb.org/core/32bit Mon Mar 3 16:39:22.633 [initandlisten] > |
设置开机启动
以下这个是要自己写,要是应用apt-get install 的话是不需要的。
/etc/init.d/mongodb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #!/bin/sh ### BEGIN INIT INFO # Provides: mongodb # Required-Start: # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: mongodb # Description: mongo db server ### END INIT INFO . /lib/lsb/init-functions PROGRAM=/tools/mongodb-linux-i686-2.4.9/bin/mongod MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'` test -x $PROGRAM || exit 0 case "$1" in start) ulimit -n 3000 log_begin_msg "Starting MongoDB server" $PROGRAM --fork --quiet -journal -maxConns=2400 -rest --logpath /data/db/journal/mongdb.log log_end_msg 0 ;; stop) log_begin_msg "Stopping MongoDB server" if [ ! -z "$MONGOPID" ]; then kill -15 $MONGOPID fi log_end_msg 0 ;; status) ;; *) log_success_msg "Usage: /etc/init.d/mongodb {start|stop|status}" exit 1 esac exit 0 |
关闭/启动服务
1 2 3 4 5 | sudo service mongodb stop
sudo service mongodb start
`http://192.168.6.129:27017/`
You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number
|
手动粗暴启动mongodb server
1 2 3 4 | ./mongod -journal -maxConns=2400 -rest -journal 代表要写日志,-maxConns=2400代表mongodb 可以接受2400个tcp连接,-rest代表可以允许客户端通过rest API访问mongdb server. 还可以使用参数—quiet启动可以指定安静模式减少记录的项目数,注意使用该参数必须要同时指定日志路径,比如: —quiet —logpath /data/db/journal/mongdb.log |
修改系统允许的最大连接数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 上面的最大连接数目的限制原因是Linux系统默认一个进程最大文件打开数目为1024,用ulimit -a 命令检查,可以看到下面这行: open files (-n) 1024 修改/etc/security/limits.conf 配置文件。 使用命令:sudo gedit /etc/security/limits.conf 在文件中增加 * soft nofile 3000 * hard nofile 20000 root soft nofile 3000 root hard nofile 20000 * 表示该配置对所有用户均有效,root用户要特别加两行。 硬限制通常是根据系统硬件资源状况(主要是系统内存)计算出来的系统最多可同时打开的文件数量,软限制是在这个基础上进一步的限制。因此软限制数目要低于硬限制。 nofile表示 max number of open files 重新启动计算机,然后再用ulimit -a 命令查看: open files (-n) 3000 已经生效了。现在再启动mongodb server,问题解决 |
客户端登录服务器
启动日志如上,服务端启动如上,现在我们在另外一个终端测试服务器是否正常。 进入/usr/local/mongodb-linux-x86_64-2.0.2/bin,执行./mongo 出现
1 2 | MongoDB shell version: 2.0.2 connecting to: test |
执行
1 2 3 | $db.foo.save({1 : “Hello world”}) $db.foo.find(); { "_id" : ObjectId("4e4b395986738efa2d0718b9"), "1" : "hello world" } |
执行到这里恭喜你,成功安装好了mongodb
也可以通过下面这种方式连接远程的mongodb server,默认端口为27017,比如 ./mongo 192.168.6.129
创建数据库
如果没有mydb数据库的话,在客户端中使用命令: use mydb 将创建mydb数据库,而且当前数据库切换为mydb. 此时show dbs不显示该数据库名称。使用db.stats()命令检查当前数据库状态。
1 | $db 当前数据库
|
标准检查流程
- 1.首先检查 ulimit -a 查看open files (-n) 是否为设置的值
- 2.ps -def | grep mongod 查看该服务是否启动
- 3.cd /var/log/ cat mongdb.log 查看服务器是否正确
- 4.进入http://192.168.6.129:28017 看服务器是否启动正常
MONGOVUE
MongoVUE is an innovative MongoDB desktop application for Windows OS that gives you an elegant and highly usable GUI interface to work with MongoDB. Now there is one less worry in managing your web-scale data.
Connection Congfig
1 2 | Server: 192.168.6.129 Prot: 27017 |