基于mongo3.0,和2.x的版本有些地方会不大一样。
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
apt-get update
apt-get install -y mongodb-org
mac 下面默认环境变量会导致登陆不上,报如下错误:
root@iZ28ywqw7nhZ:~# mongo
Failed global initialization: BadValue Invalid or no user locale set. Please ensure LANG and/or LC_* environment variables are set correctly.
设置下环境变量就好了:
export LC_ALL=C
mongo
脚本的方法:
/etc/init.d/mongod start
/etc/init.d/mongod stop
命令行:
从配置文件启动:
nohup mongod -f /etc/mongod.conf &
停止:
mongo
use admin
db.shutdownServer()
脚本:
service mongod stop
命令行:
use admin
db.shutdownServer()
如果以上都无效的话用
killall -15 mongod
注意不用用kill -9 ,-9会导致数据库损坏。
很简单,启动时加上–repair参数
mongod –dbpath /data/db –repair
非正常重启或者kill -9 均可导致mongodb数据库损坏。
pecl install mongo
apt-get install python-pip
pip install pymongo
root@iZ28ywqw7nhZ:~# python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import pymongo
能正确import 就说明pymongo已经安装成功
mongo索引和其他数据库类似的,数据结构上面用的是b树。
db.inventory.ensureIndex({"item":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
db.inventory.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.inventory"
},
{
"v" : 1,
"key" : {
"item" : 1
},
"name" : "item_1",
"ns" : "test.inventory"
}
]
db.inventory.dropIndex({"item":1})
{ "nIndexesWas" : 2, "ok" : 1 }
db.test.ensureIndex({"item":1},{"unique":true})
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
db.inventory.find().explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.inventory",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [ ]
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"$and" : [ ]
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "iZ28ywqw7nhZ",
"port" : 27017,
"version" : "3.0.2",
"gitVersion" : "6201872043ecbbc0a4cc169b5482dcf385fc464f"
},
"ok" : 1
}
stage 是COLLSCAN 说明没有走索引,走索引的话会显示 IXSCAN
root@iZ28ywqw7nhZ:~/backup# mongodump
2015-04-26T21:31:34.095+0800 writing admin.system.indexes to dump/admin/system.indexes.bson
2015-04-26T21:31:34.096+0800 writing admin.system.users to dump/admin/system.users.bson
2015-04-26T21:31:34.113+0800 writing admin.system.users metadata to dump/admin/system.users.metadata.json
2015-04-26T21:31:34.115+0800 done dumping admin.system.users
2015-04-26T21:31:34.115+0800 writing admin.system.version to dump/admin/system.version.bson
2015-04-26T21:31:34.116+0800 writing admin.system.version metadata to dump/admin/system.version.metadata.json
2015-04-26T21:31:34.117+0800 done dumping admin.system.version
2015-04-26T21:31:34.117+0800 writing test.inventory to dump/test/inventory.bson
2015-04-26T21:31:34.118+0800 writing test.inventory metadata to dump/test/inventory.metadata.json
2015-04-26T21:31:34.119+0800 done dumping test.inventory
2015-04-26T21:31:34.119+0800 writing test.system.indexes to dump/test/system.indexes.bson
root@iZ28ywqw7nhZ:~/backup# mongorestore --drop
2015-04-27T09:03:22.391+0800 using default 'dump' directory
2015-04-27T09:03:22.394+0800 building a list of dbs and collections to restore from dump dir
2015-04-27T09:03:22.397+0800 reading metadata file from dump/test/inventory.metadata.json
2015-04-27T09:03:22.397+0800 restoring test.inventory from file dump/test/inventory.bson
2015-04-27T09:03:22.398+0800 restoring indexes for collection test.inventory from metadata
2015-04-27T09:03:22.398+0800 finished restoring test.inventory
2015-04-27T09:03:22.399+0800 restoring users from dump/admin/system.users.bson
2015-04-27T09:03:22.402+0800 done
cd ~/testmongobackup
mongorestore -d blog –drop
cd ~/testmongobackup
mongorestore -d blog -c posts –drop
目前支持三种格式
比如:
$mongoimport -d blog -c tagcloud –type csv –headerline < csvimportfile.csv
也是支持3种格式,和import一致。
mongoexport -d blog -c posts -q {} -f _id,Title,Message,Author –csv >blogposts.csv
use admin
db.createUser({user : "admin", pwd: "pass", roles: [ "readWrite", "dbAdmin" ] })
db.system.users.find()
具体含义请见 http://docs.mongodb.org/v3.0/reference/built-in-roles/
在mongo配置文件中加入,之后重启生效:
auth=true
service mongodb restart