当前位置:Gxlcms > 数据库问题 > mongodb常用操作

mongodb常用操作

时间:2021-07-01 10:21:17 帮助过:4人阅读

--fork

2.创建mongodb认证

host=$MONGODB_HOST:$MONGODB_PORT
mongo $host/admin --eval "db.system.version.remove({});"
mongo $host/admin --eval "db.system.version.insert({‘_id‘:‘authSchema‘,‘currentVersion‘:3});"
mongo $host/admin --eval "db.createRole({role:‘sysadmin‘,roles:[],privileges:[{resource:{anyResource:true},actions:[‘anyAction‘]}]});"
mongo $host/admin --eval "db.createUser({user:‘$MONGODB_USER‘,pwd:‘$MONGODB_PASS‘,roles:[‘sysadmin‘]})"

3.启动认证mongodb

numactl --interleave=all /usr/local/mongodb/bin/mongod --port %d --master --dbpath %s --logpath %s --pidfilepath %s --logappend --auth  --keyFile=mongodb_key --oplogSize=240 --fork

4.做mongodb从库

numactl --interleave=all /usr/local/mongodb/bin/mongod --source 主库ip:主库端口 --port %d --slave --dbpath %s --logpath %s --pidfilepath %s --logappend --auth --keyFile=mongodb_key  --nohttpinterface --autoresync  --fork

5.外部执行mongodb语句

echo ‘db.gl.scene.drop(); | /usr/local/mongodb/bin/mongo 127.0.0.1:27317/sid${server_id} -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin

6.导出某条件的数据【此处不开启认证】

/usr/local/mongodb/bin/mongoexport -h ${source_ip} --port ${source_port} -d sid${server_id} -c gl.player_secretary -q {pi:${pi}} -o gl.player_secretary_${pi}.json

7.删除某条件的数据

echo db.gl.player_secretary.remove({\"pi\":${pi}}); | /usr/local/mongodb/bin/mongo 127.0.0.1:27317/sid${server_id} -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin

8.导入json数据

/usr/local/mongodb/bin/mongoimport -h 127.0.0.1 --port=27317  -uxxx -pxxxx --authenticationMechanism=MONGODB-CR --authenticationDatabase=admin -d sid${server_id} -c gl.player_secretary --upsert gl.player_secretary_${pi}.json

 

mongodb常用操作

标签:slave   secret   span   actions   imp   current   OLE   mongo   code   

人气教程排行