mogondb

2019-05-06  本文已影响0人  就是这么任性_ac9c

Mongodb副本集副本集是什么

– MongoDB

复制是将数据同步在多个服务器的过程。

复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。

复制还允许您从硬件故障和服务中断中恢复数据

1)

清空从库数据

[root@host53 etc]# rm -rf /usr/local/mongodb/data/db/*

2

)启动副本集(都要配置)启动服务时,指定主机所在副本集名称

副本集成员间使用相同的副本集名称

– --replSet rs1 //

指定副本集名称

[root@host53 etc]# rm -rf /usr/local/mongodb/data/db/*

[root@host53 etc]# vim mongodb.conf

[root@host53 etc]# tail -1 mongodb.conf

replSet=rs1   ###S

大写

3

)创建副本集(任意一台进入mongodb创建)

config={

_id:"rs1",

members:[

{_id:0,host:"192.168.4.51:27051"},

{_id:1,host:"192.168.4.52:27052"},

{_id:2,host:"192.168.4.53:27053"},

]

};

######{_id:0,host:"192.168.4.51:27051",priority:10},##priority:10

指定优先级,优先级越高的做主

{ ###

这是输出

    "_id" :"rs1",

    "members" : [

        {

            "_id" : 0,

            "host" :"192.168.4.51:27051"

        },

        {

            "_id" : 1,

            "host" :"192.168.4.52:27052"

        },

        {

            "_id" : 2,

            "host" :"192.168.4.53:27053"

        }

    ]

}

初始化 Replica Sets 环境

执行如下命令

– >rs.initiate(config)

{

    "ok" : 1,

    "operationTime" :Timestamp(1531101358, 1),

    "$clusterTime" : {

        "clusterTime" :Timestamp(1531101358, 1),

        "signature" : {

            "hash" :BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),

            "keyId" :NumberLong(0)

        }

    }

}

######

想让水成为master就在谁上执行创建集群的命令

查看副本集信息

查看状态信息

– > rs.status( )

查看是否是 master 库

– > rs .isMaster( )

测试副本集

验证副本集配置

同步数据验证

rs1:PRIMARY> use gamedb

switched to db gamedb

rs1:PRIMARY> db

gamedb

rs1:PRIMARY> db.c1.save({name:"bob",age:19})

WriteResult({ "nInserted" : 1 })

rs1:PRIMARY> db.c1.find()

{ "_id" : ObjectId("5b42c904f84e7d5e5f048b7f"),"name" : "bob", "age" : 19 }

rs1:PRIMARY>

– >db.getMongo( ).setSlaveOk( )

允许从库查看数据 从库不输入这个命令不能操作的

rs1:SECONDARY> use gamedb

switched to db gamedb

rs1:SECONDARY> show tables

c1

rs1:SECONDARY> db.c1.find()

{ "_id" : ObjectId("5b42c904f84e7d5e5f048b7f"),"name" : "bob", "age" : 19 }

rs1:SECONDARY>

down

掉51测试集群高可用

rs1:SECONDARY> rs.isMaster()

{

    "hosts" : [

        "192.168.4.51:27051",

        "192.168.4.52:27052",

        "192.168.4.53:27053"

    ],

    "setName" :"rs1",

    "setVersion" : 1,

    "ismaster" : true,

    "secondary" : false,

    "primary" :"192.168.4.52:27052",    ####52

为主

    "me" :"192.168.4.52:27052",

save( )

格式

– db.

集合名 .save({ key:“ 值”,

key:” 值” })

注意

集合不存在时创建集合,后插入记录

– _id

字段值 已存在时 修改文档字段值 ####与insert区别

– _id

字段值 不已存在时 插入文档

insert()

格式

– db.

集合名 .insert({key:“ 值”,

key:” 值” })

注意

集合不存在时创建集合,后插入记录

– _id

字段值 已存在时放弃插入

– _id

字段值 不已存在时 插入文档

save insert

一次只能插入一条记录

> db.c5.save({_id:17,name:"bob",age:55})####

指定了ID,id相同才算同一条纪律

WriteResult({ "nMatched" : 0, "nUpserted" : 1,"nModified" : 0, "_id" : 17 })

> db.c5.save({_id:17,name:"bob",age:44})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.c5.find()

{ "_id" : ObjectId("5b42d871db9ecee350432c16"),"name" : "bob", "age" : 66 }

{ "_id" : ObjectId("5b42d879db9ecee350432c17"),"name" : "bob", "age" : 55 }

{ "_id" : 17, "name" : "bob", "age" :44 }

> db.c5.save({_id:17,name:"bob",age:33})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.c5.find()

{ "_id" : ObjectId("5b42d871db9ecee350432c16"),"name" : "bob", "age" : 66 }

{ "_id" : ObjectId("5b42d879db9ecee350432c17"),"name" : "bob", "age" : 55 }

{ "_id" : 17, "name" : "bob", "age" :33 }

> db.c5.insert({_id:17,name:"bj",age:33}) ###

只认id字段值

WriteResult({

    "nInserted" : 0,

    "writeError" : {

        "code" : 11000,

        "errmsg" : "E11000duplicate key error collection: test.c5 index: _id_ dup key: { : 17.0}"

    }

> db.c6.insertMany([{name:'tom',age:18},{name:'xie',school:'tarena'}])

一次插入多条纪律,注意有中括号

##############

{

    "acknowledged" : true,

    "insertedIds" : [

        ObjectId("5b42da40db9ecee350432c18"),

        ObjectId("5b42da40db9ecee350432c19")

    ]

}

查询语法

显示所有行,默认一次只输出 20 行 输入 it 显示后续的行

– db.

集合名.find()

显示第 1 行

– > db.

集合名.findOne()

指定查询条件并指定显示的字段

– > db.

集合名 .find ( { 条件 },{定义显示的字段} )

– > db.user.find({},{_id:0,name:1,shell:1})

– 0

不显示 1 显示

> db.c3.find({uid:1})

{ "_id" : ObjectId("5b408b7343bc8ff8b489ac08"),"name" : "bin", "password" : "x","uid" : 1, "gid" : 1, "comment" :"bin", "homedir" : "/bin", "shell" :"/sbin/nologin" }

> db.c3.find({uid:1},{_id:0})

{ "name" : "bin", "password" : "x","uid" : 1, "gid" : 1, "comment" :"bin", "homedir" : "/bin", "shell" :"/sbin/nologin" }

> db.c3.find({uid:2},{_id:0})

{ "name" : "daemon", "password" : "x","uid" : 2, "gid" : 2, "comment" :"daemon", "homedir" : "/sbin", "shell": "/sbin/nologin" }

> db.c3.find({uid:2},{name:1,uid:1})

{ "name" : "daemon", "uid" : 2 }

行数显示限制

• limit(

数字 ) // 显示前几行

> db.

集合名.find().limit(3)

skip(

数字)

//

跳过前几行

– > db.

集合名.find().skip(2)

• sort(

字段名 ) // 排序

– > db.

集合名.find().sort(age:1|-1)     1:升序 ,-1:降序

– > db.user.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:

1,shell:1}).skip(2).limit(2)

> db.c3.find({},{_id:0,name:1,uid:1}).sort({uid:-1})

{ "name" : "yaya", "uid" : 888888 }

{ "name" : "nfsnobody", "uid" : 65534 }

{ "name" : "lisi", "uid" : 1000 }

{ "name" : "polkitd", "uid" : 999 }

– db.

集合名 .find({key:” 值”})

– db.

集合名 .find({key:” 值”,

keyname:” 值” })

> db.c3.find({name:"root"},{_id:0,name:1})

{ "name" : "root" }

> db.c3.find({name:"root",uid:0},{_id:0,name:1})

两个条件同时成立

{ "name" : "root" }

范围比较

– $in

在...里

– $nin

不在...里

– $or

– > db.user.find({uid:{$in:[1,6,9]}})

– > db.user.find({uid:{$nin:[1,6,9]}})

– > db.user.find({$or: [{name:"root"},{uid:1} ]})

> db.c3.find({ uid:{$in: [1,6,9]} },{_id:0,uid:1})

{ "uid" : 1 }

{ "uid" : 6 }

>

> db.c3.find({name:{$in:['root','daemon','bin']}},{_id:0,name:1})

{ "name" : "root" }

{ "name" : "bin" }

{ "name" : "daemon" }

>db.c3.find({shell:{$nin:['/bin/bash','/sbin/nologin']}},{_id:0,name:1,shell:1})

{ "name" : "sync", "shell" :"/bin/sync" }

{ "name" : "shutdown", "shell" : "/sbin/shutdown"}

{ "name" : "halt", "shell" :"/sbin/halt" }

{ "name" : "mysql", "shell" :"/bin/false" }

>

> db.c3.find({$or:[{name:'root'},{name:'mysql'}]},{_id:0,name:1})

{ "name" : "root" }

{ "name" : "mysql" }

>

正则匹配

– > db.user.find({name: /^a/ })

数值比较

– $lt $lte $gt $gte $ne

   <   <=   >    >= !=

– db.user.find( { uid: { $gte:10,$lte:40} } , {_id:0,name

:1,uid:1})

– db.user.find({uid:{$lte:5,}})

> db.c3.find({name:/^a/},{name:1,_id:0})

{ "name" : "adm" }

{ "name" : "abrt" }

{ "name" : "avahi" }

{ "name" : "apache" }

> db.c3.find({uid:{$gte:10,$lte:40}},{_id:0,name:1,uid:1})

{ "name" : "operator", "uid" : 11 }

{ "name" : "games", "uid" : 12 }

{ "name" : "ftp", "uid" : 14 }

{ "name" : "rpc", "uid" : 32 }

{ "name" : "rpcuser", "uid" : 29 }

{ "name" : "ntp", "uid" : 38 }

{ "name" : "mysql", "uid" : 27 }

>db.c3.find({uid:{$gte:10,$lte:40}},{_id:0,name:1,uid:1}).sort({uid:1})

{ "name" : "operator", "uid" : 11 }

{ "name" : "games", "uid" : 12 }

{ "name" : "ftp", "uid" : 14 }

{ "name" : "mysql", "uid" : 27 }

{ "name" : "rpcuser", "uid" : 29 }

{ "name" : "rpc", "uid" : 32 }

{ "name" : "ntp", "uid" : 38 }

>db.c3.find({uid:{$gte:10,$lte:40}},{_id:0,name:1,uid:1}).sort({uid:1}).limit(1)

{ "name" : "operator", "uid" : 11 }

匹配 null , 也可以匹配没有的字段

– > db.user.save({name:null,uid:null})

> db.c3.save({name:null,uid:null})

WriteResult({ "nInserted" : 1 })

> db.c3.find({name:null})

{ "_id" : ObjectId("5b430a6cdb9ecee350432c1a"),"name" : null, "uid" : null }

> db.c3.find({name:null},{_id:0}) 

也可以匹配没有的字段

{ "name" : null, "uid" : null }

> db.c3.find({gid:null},{_id:0})

{ "name" : null, "uid" : null }

update()

– > db.

集合名 .update({ 条件 },{修改的字段} )

注意:把文件的其他字段都删除了,只留下了 password 字段

,

且只修改与条件匹配的第 1 行!!!

$set

条件匹配时,修改指定字段的值

– db.user.update({

条件 },$set: { 修改的字段})

> db.c3.find({uid:{$lte:3}},{_id:0})

{ "name" : "root", "password" : "x","uid" : 0, "gid" : 0, "comment" :"root", "homedir" : "/root", "shell" :"/bin/bash" }

{ "name" : "bin", "password" : "x","uid" : 1, "gid" : 1, "comment" :"bin", "homedir" : "/bin", "shell" :"/sbin/nologin" }

{ "name" : "daemon", "password" : "x","uid" : 2, "gid" : 2, "comment" :"daemon", "homedir" : "/sbin", "shell": "/sbin/nologin" }

{ "name" : "adm", "password" : "x","uid" : 3, "gid" : 4, "comment" :"adm", "homedir" : "/var/adm", "shell": "/sbin/nologin" }

> db.c3.update({uid:{$lte:3}},{password:"AAA"})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.c3.find({uid:{$lte:3}},{_id:0})

{ "name" : "bin", "password" : "x","uid" : 1, "gid" : 1, "comment" :"bin", "homedir" : "/bin", "shell" :"/sbin/nologin" }

{ "name" : "daemon", "password" : "x","uid" : 2, "gid" : 2, "comment" :"daemon", "homedir" : "/sbin", "shell": "/sbin/nologin" }

{ "name" : "adm", "password" : "x","uid" : 3, "gid" : 4, "comment" :"adm", "homedir" : "/var/adm", "shell": "/sbin/nologin" }

> db.c3.find({password:'AAA'})

{ "_id" : ObjectId("5b408b7343bc8ff8b489ac07"),"password" : "AAA" }

语法格式:默认只更新与条件匹配的第 1 行

– > db.user.update({

条件 },{$set:{ 修改的字段}} ,false,

true )

> db.c3.update({uid:{$lte:3}},{$set:{password:"AAA"}},false,true)

WriteResult({ "nMatched" : 3, "nUpserted" : 0,"nModified" : 3 })

> db.c3.find({uid:{$lte:3}},{_id:0})

{ "name" : "bin", "password" : "AAA","uid" : 1, "gid" : 1, "comment" :"bin", "homedir" : "/bin", "shell" :"/sbin/nologin" }

{ "name" : "daemon", "password" :"AAA", "uid" : 2, "gid" : 2, "comment": "daemon", "homedir" : "/sbin","shell" : "/sbin/nologin" }

{ "name" : "adm", "password" : "AAA","uid" : 3, "gid" : 4, "comment" :"adm", "homedir" : "/var/adm", "shell": "/sbin/nologin" }

>db.c3.update({uid:{$lte:3}},{$set:{password:"AAA",shell:"/bin/bash"}},false,true)

WriteResult({ "nMatched" : 3, "nUpserted" : 0,"nModified" : 3 })

> db.c3.find({uid:{$lte:3}},{_id:0})

{ "name" : "bin", "password" : "AAA","uid" : 1, "gid" : 1, "comment" :"bin", "homedir" : "/bin", "shell" :"/bin/bash" }

{ "name" : "daemon", "password" :"AAA", "uid" : 2, "gid" : 2, "comment": "daemon", "homedir" : "/sbin","shell" : "/bin/bash" }

{ "name" : "adm", "password" : "AAA","uid" : 3, "gid" : 4, "comment" :"adm", "homedir" : "/var/adm", "shell": "/bin/bash" }

>

$unset

删除与条件匹配文档的字段

– db.

集合名 .update({ 条件},{$unset:{key:values}})

> db.c3.update({uid:1},{$unset:{password:'F'}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

$inc

条件匹配时,字段值自加或自减

– Db.

集合名 .update({ 条件},{$inc:{ 字段名 : 数字}})

正整数自加 负整数自减!!

> db.c3.update({name:'bin'},{$inc:{uid:2}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.c3.find({name:'bin'},{_id:0})

{ "name" : "bin", "uid" : 3, "gid" : 1,"comment" : "bin", "homedir" : "/bin","shell" : "/bin/bash" }

> db.c3.update({name:'bin'},{$inc:{uid:-2}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.c3.find({name:'bin'},{_id:0})

{ "name" : "bin", "uid" : 1, "gid" : 1,"comment" : "bin", "homedir" : "/bin","shell" : "/bin/bash" }

$push / $addToSet

• $push

向数组中添加新元素

– db.

集合名 .update({ 条件},{$push:{ 数组名 :“ 值” }})

– db.user.insert({name:"bob",likes:

["a","b","c","d","e","f"]})

– db.user.update({name:“bob”},{$push:{likes:“w"}})

> db.user.find()

{ "_id" : ObjectId("5b4314b5db9ecee350432c1b"),"name" : "bob", "likes" : [ "a","b", "c", "d", "e", "f","w" ] }

• $addToSet

避免重复添加

– db.

集合名 .update({ 条件},{$addToSet:{ 数组名:”

值” }})

db.user.update({name:"bob"},{$addToSet:{likes:“f"}})

> db.user.update({name:'bob'},{$addToSet:{likes:'w'}})

####

命令会执行单不会添加

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 0 })

> db.user.find()

{ "_id" : ObjectId("5b4314b5db9ecee350432c1b"),"name" : "bob", "likes" : [ "a","b", "c", "d", "e", "f","w" ] }

$pop /$pull

• $pop

从数组头部删除一个元素

– db.

集合名 .update({ 条件},{$pop:{ 数组名 : 数字}})

– db.user.update({name:"bob"},{$pop:{likes:1}})

– db.user.update({name:"bob"},{$pop:{likes:-1}})

1

删除数组尾部元素 -1 删除数组头部元素

> db.user.update({name:'bob'},{$pop:{likes:1}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.user.find()

{ "_id" : ObjectId("5b4314b5db9ecee350432c1b"),"name" : "bob", "likes" : [ "a","b", "c", "d", "e", "f" ]}

> db.user.update({name:'bob'},{$pop:{likes:-1}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.user.find()

{ "_id" : ObjectId("5b4314b5db9ecee350432c1b"),"name" : "bob", "likes" : [ "b","c", "d", "e", "f" ] }

• $pull

删除数组指定元素

– db.

集合名 .update({ 条件},{$pull:{ 数组名 : 值}})

– db.user.update({name:"bob"},{$pull:{likes:"b"}})

> db.user.update({name:'bob'},{$pull:{likes:'e'}})

WriteResult({ "nMatched" : 1, "nUpserted" : 0,"nModified" : 1 })

> db.user.find()

{ "_id" : ObjectId("5b4314b5db9ecee350432c1b"),"name" : "bob", "likes" : [ "b","c", "d", "f" ] }

$drop/$remove

• $drop

删除集合的同时删除索引

– db.

集合名.drop( )

– db.user.drop( )

• remove()

删除文档时不删除索引

– db.

集合名 .remove({}) // 删除所有文档

– db.

集合名 .remove({ 条件 }) // 删除与条件匹配的文档

– db.user.remove({uid:{$lte:10}})

– db.user.remove({})

�P�L�ֈ

上一篇下一篇

猜你喜欢

热点阅读