mongodb学习笔记

来源:互联网 发布:女孩子湿有多夸张知乎 编辑:程序博客网 时间:2024/06/06 18:05

一:安装和启动

1.第一种定义配置文件,带配置文件启动

./mongod -f ../etc/mongo.conf


./mongod -f  ../etc/mongo.conf(带配置文件启动) 或./mongod,服务器默认端口27017

关闭mongodb
1)./mongod --shutdown或./mongod -f ../etc/mongo.conf --shutdown

2)用killall

killall mongod
[root@iZ94eveq0q4Z bin]# killall mongod[root@iZ94eveq0q4Z bin]# !psps -ef | grep mongodroot     10806 10126  0 22:13 pts/2    00:00:00 grep mongod[root@iZ94eveq0q4Z bin]# 


mongodb.conf

mongodb配置文件

自定义启动参数

logpath=../log/mongodb.loglogappend=falsedbpath=/data/kehu/mongo/data/dbfork=truerest=true

如果不写配置文件,默认是安装在/data/db下的

配置文件rest=true,打开web控制台
默认端口28017
访问url: http://ip:28017

查看mongodb进程是否启动:

pstree -p | grep mongod


第二种:直接在命令行定义启动参数

./mongod --dbpath=/data/kehu/mongo/data/db/ --logpath=/data/kehu/mongo/mongodb-linux-x86_64-2.4.2/log/mongodb.log --fork




将命令加入开机自启动

vi /etc/rc.local 

[root@iZ94eveq0q4Z bin]# more /etc/rc.local #!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch /var/lock/subsys/local/data/kehu/mongo/mongodb-linux-x86_64-2.4.2/bin/mongod --dbpath=/data/kehu/mongo/data/db/ --logpath=/data/kehu/mongo/mongodb-linux-x86_64-2.4.2/log/mongodb.log --fork

mongo命令窗口

  输入./mongo命令

> show tables;> show conllections;2015-05-02T20:18:27.501-0700 E QUERY    Error: don't know how to show [conllections]    at Error (<anonymous>)    at shellHelper.show (src/mongo/shell/utils.js:733:11)    at shellHelper (src/mongo/shell/utils.js:524:36)    at (shellhelp2):1:1 at src/mongo/shell/utils.js:733> db.c1.insert({name="user1"});2015-05-02T20:19:33.251-0700 E QUERY    SyntaxError: Unexpected token => db.c1.insert({name:"user1"});WriteResult({ "nInserted" : 1 })> show dbs;local  0.078GBtest   0.078GB> dbtest> show tables;c1system.indexes> db.c1.find();{ "_id" : ObjectId("554593d21a75735eb89645c4"), "name" : "user1" }> 


控制台下基本命令:

http://www.itokit.com/2012/0417/73611.html


mongodb中insert和save的区别

mongodb的save和insert函数都可以向collection里插入数据,但两者是有两个区别:

一、使用save函数里,如果原来的对象不存在,那他们都可以向collection里插入数据,如果已经存在,save会调用update更新里面的记录,而insert则会忽略操作

二、insert可以一次性插入一个列表,而不用遍历,效率高, save则需要遍历列表,一个个插入


0 0