MongoDB随笔

#导出库

./mongodump -h 10.205.20.8 -u gcadmin -p gcadmin-d gamecircle_logs -o /home/mongodb/

#恢复库

./mongorestore -h 10.205.20.8 -u gcadmin -p gcadmin -d gamecircle_logs --dir ./gamecircle_logs --drop

#导出表

./mongoexport -h 10.205.20.8 --port 27017 -u gcadmin -p gcadmin -d gamecircle_logs -c event -o /Users/tangwei/event.dat

#导入表

./mongoimport -h10.205.20.8 --port 27017 -u gcadmin -p gcadmin -d gamecircle_logs -c event /Users/tangwei/event.dat

#如果命令不存在

进入mongodb/bin目录执行

#修改用户权限

db.grantRolesToUser("gcadmin" , [ { role: "dbAdmin", db:"gamecircle_logs" } ])

#出现以下错误可能是磁盘空间不足

new file allocation failure

#阿里云mongo导出库

1.进入docker mongo容器

2.进入/usr/local/mongodb/bin目录

3.执行命令:

./mongodump --host dds-2ze5256fdc7bf2c41.mongodb.rds.aliyuncs.com--port 3717 -d gamecircle_logs -u gcadmin -p gcadmin

4.打包命令:

tar -zcvf ./dump/gamecircle_logs.tar.gz ./dump/gamecircle_logs

5.退出docker容器

6.从docker容器复制文件

docker cp b96:/usr/local/mongodb/bin/dump/gamecircle_logs.tar.gz

/data/backup/mongodb/gamecircle_logs_201605091346.tar.gz

7.上传到oss并下载到本地

/home1/ossupload.sh gamecircle_logs_201605091346.tar.gz

8.执行恢复库命令

mongorestore -h 10.205.20.8--port 27017-u gcadmin -p gcadmin -d gamecircle_logs --dir/Users/tangwei/gamecircle_logs –drop

#批量更新数据

db.point.find().forEach(

    function(item){

        db.point.update({"_id":item._id},{"$set":{"appVersion":item.version}},true)

    }

)

#删除字段

db.point.update({},{$unset:{"version":""}},{multi:true})

#启动镜像

docker run -d -p 27017:27017 -v /volume1/docker/mongodb/data:/data/db --name mongodb mongodb /usr/local/mongodb/bin/mongod --auth

#mongod.conf配置

systemLog:

destination: file

path: /usr/local/mongodb/logs/mongodb.log

logAppend: true

storage:

dbPath: /data/db

journal:

enabled: true

mmapv1:

smallFiles: true

processManagement:

fork: true

pidFilePath: /usr/local/mongodb/logs/mongod.pid

net:

port: 27017

security:

authorization: enabled

#创建用户

use admin;

db.createUser({

    user: "root",

    pwd: "ymcx97973",

    roles: [{

        role: "root", db: "admin"

    },{

        role: "dbAdmin", db: "admin"

    },{

        role: "clusterAdmin", db: "admin"

    },{

        role: "userAdminAnyDatabase", db: "admin"

    }]

});

use gamecircle_logs;

db.createUser({

    user: "gcadmin",

    pwd: "gcadmin",

    roles: [{

        role: "readWrite", db: "gamecircle_logs"

    }]

});

#创建副本集镜像,节点总数为奇数可以自动切换,为偶数时需要增加仲裁节点才能自动切换

docker run -d -p 27017:27017 -v /data/mongodb/replset/data1:/data/db -v /data/mongodb/backup/data1:/data/backup --name mongodb1 10.170.232.47:5000/mongodb306 /usr/local/mongodb/bin/mongod --keyFile /usr/local/mongodb/key --replSet replset

docker run -d -p 27018:27017 -v /data/mongodb/replset/data2:/data/db -v /data/mongodb/backup/data2:/data/backup --name mongodb2 10.170.232.47:5000/mongodb306 /usr/local/mongodb/bin/mongod --keyFile /usr/local/mongodb/key --replSet replset

docker run -d -p 27019:27017 -v /data/mongodb/replset/data3:/data/db -v /data/mongodb/backup/data3:/data/backup --name mongodb3 10.170.232.47:5000/mongodb306 /usr/local/mongodb/bin/mongod --keyFile /usr/local/mongodb/key --replSet replset

连接任意一台mongo,使用admin库并登录admin用户,执行以下命令

use admin;

rs.initiate({

    "_id":"replset",

    "members":    [{

        "_id": 0,

        "host": "10.170.232.47:27017"

    },

    {

        "_id": 1,

        "host": "10.170.232.47:27018"

    },

    {

        "_id": 2,

        "host":"10.170.232.47:27019"

    }]

});

在主服务器上进行副本集节点添加

rs.add("10.170.232.47:27019")

rs.conf();

在主服务器上进行副本集节点移除

rs.remove("10.170.232.47:27019")

rs.conf();

查看状态

rs.status();

你可能感兴趣的:(MongoDB随笔)