mongodb配置

Mongodb
1. 安装
2. CRUD
3. 索引
4. 副本及(replica sets)
5. 分片(sharding)


nosql
简单数据模型
元数据和应用数据分离
弱一致性

优势:
避免不必要的复杂性
高吞吐量
高水平扩展能力和低端硬件集群
不实用对象 - 关系映射

劣势
不支持ACID
功能简单


面向collection的数据库
数据库: 数据库无须创建
表:行 《----》 集合 :文档
集合无须事先定义:

[root@localhost data]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.9.tgz

groupadd -r mongodb
useradd -M -r -g mongodb -d /data/mongodb -s /bin/false mongodb

mkdir -p /data/mongodb
mkdir -p /data/mongodb/var/log/

chown mongodb.mongodb /data/mongodb

mongod --config /data/mongodb/etc/mongod.conf


db.user.insert({
name:"fengjian",
age:25,status:1,
groups:["news","sports"]
})

> db.user.find({age:{$gt:22}}).sort({age:1})
{ "_id" : ObjectId("57bb5ad01346de1830c26aa2"), "name" : "feng", "age" : 24, "status" : 1, "groups" : [ "news", "sports" ] }
{ "_id" : ObjectId("57bb5a4d1346de1830c26aa1"), "name" : "fengjian", "age" : 25, "status" : 1, "groups" : [ "news", "sports" ] }
> db.user.find({age:{$gt:22}}).sort({age:-1})
{ "_id" : ObjectId("57bb5a4d1346de1830c26aa1"), "name" : "fengjian", "age" : 25, "status" : 1, "groups" : [ "news", "sports" ] }
{ "_id" : ObjectId("57bb5ad01346de1830c26aa2"), "name" : "feng", "age" : 24, "status" : 1, "groups" : [ "news", "sports" ] }


> db.user.find({age:{$lt:25}}).limit(1)
db.user.find({age:{$lt:25}},{name:1,status:1}).limit(1)


mysql 与 mongodb 查询对比

mysql: select name,age from su where age<38

显示name 和 age 的字段,mongodb 先写查询条件
mongodb: db.su.find({age: {$lt: 38}},{name:1},{age: 1})

 

mysql 与 mongodb 插入对比

mysql: insert into user values("su",24,"boy")
mongodb: db.su.insert({name: "su",age: 24, sex: "boy"})

 

mysql 与 mongodb update对比
mysql: update su set name="feng" where age > 18
db.su.update({age: 33}},{$set: {su:"zhang"}},{multi: true}

{multi: true} 修改满足条件的所有数据

mysql 与 mongodb delete对比
mysql: delete from su where age = 18;
mongodb: db.su.remove({age: 33}) // db.su.remove({name: "su"})

添加创建99个用户
for(i=1;i<100;i++) db.su.insert({name: "User"+i,age:i,sex: "boy",book: ["book1","book2"]})

删除
db.su.remove({name: "User12"})

修改
> db.su.update({name: "User1"},{$set: {age:99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

显示name和age age等于20的
db.su.find({age: {$eq: 20}},{name: 1,age: 1})

查询年龄 age 大于 80 ,小于 90的数据
db.su.find({$and: [{age: {$gt:80}},{age: {$lt: 90}}]})

查询名字是User80 或者 age小于15的数据
db.su.find({$or: [{name: {$eq: "User80"}},{age: {$lt:15}}]})


查询名字是User80 或者 age小于15的数据,只显示name数据
db.su.find({$or: [{name: {$eq: "User80"}},{age: {$lt:15}}]},{name:1})


元素查询

如果要根据文档中是否存在某字段等条件来挑选文档,则需要用到元素运算
$exists: 根据制定字段的存在性 挑选文档,"{field: {$exists:<boolean>}}",指定 <boolean>的值为true,返回
存在指定字段的文档,false则返回不存在制定字段的文档:

$mod: 将指定字段的值进行取模运算,并返回其余数为指定值的文档:语法为 "{field: {$mod: [divisor,remainder]}}"

$type: 返回制定的值类型为指定类型的文档,语法格式“{field: {$type:<BSON type>}}”

 

全表扫描

索引的优点:
1、减少了服务器需要扫描的数据量
2、索引帮助服务器减少排序或临时表的使用
3、索引可以将随机I/O 转变成顺序I/O


索引:三星
一星: 索引如果能将相关的记录放置在一起
二星: 索引中数据的存储顺序与查询找标准中顺序一致
三星: 如果索引中包含查询中所需要的全部数据:(覆盖索引)


索引类别:
1. 顺序索引
2. 散列索引:将索引映射至散列桶上,映射是通过散列函数进行的

顺序索引的特性:
全值匹配: Name = “user12”
匹配最左前缀: Name LIKE “User1%” , 无效 Name LIKE "%User%"

组合索引:(age,name) age > 90 and name = "user12"

mongodb 创建索引

db.mycoll.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead

再name上创建索引
> db.su.ensureIndex({name:1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

查询索引
> db.su.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "testdb.su"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "testdb.su"
}
]

删除索引

> db.su.dropIndex("name")
{
"nIndexesWas" : 2,
"ok" : 0,
"errmsg" : "index not found with name [name]",
"code" : 27
}


创建唯一索引
> db.su.ensureIndex({name:1},{unique:true})

创建稀疏索引
> db.su.ensureIndex({name:1},{sparce:true})


显示详细信息
> db.su.find({name: "User19"}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "testdb.su",
"indexFilterSet" : false,
"parsedQuery" : {
"name" : {
"$eq" : "User19"
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"name" : 1
},
"indexName" : "name_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"name" : [
"[\"User19\", \"User19\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "3.2.9",
"gitVersion" : "22ec9e93b40c85fc7cae7d56e7d6a02fd811088c"
},
"ok" : 1
}


使用hint制定使用的索引
> db.su.find({name: "User18"}).hint({name:1}).explain()


创建组合索引
> db.su.ensureIndex({name:1,age:1})

{
"v" : 1,
"key" : {
"name" : 1,
"age" : 1
},
"name" : "name_1_age_1",
"ns" : "testdb.su"
}




Oplog Size
Storage Engine Default Oplog Size Lower Bound Upper Bound
In-Memory Storage Engine 5% of physical memory 50 MB 50 GB
WiredTiger Storage Engine 5% of free disk space 990 MB 50 GB
MMAPv1 Storage Engine 5% of free disk space 990 MB 50 GB


mongodb replica set 副本集 3台服务器

简单定义 配置文件

bind_ip = 0.0.0.0

fork = true

logappend=true

port = 27017

dbpath=/data/mongodb/data

pidfilepath=/data/mongodb/run/mongo.pid

logpath=/data/mongodb/log/mongod.log

replSet = testtrs0

[root@mongo~] # /data/mongodb/bin/mongod -f /data/mongodb/etc/mongo.cfg

> rs.help()

> rs.status()
{
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94
}

测试环境使用最简单的初始化
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "mongodb1:27017",
"ok" : 1
}

再查看状态
testtrs0:OTHER> rs.status()
{
"set" : "testtrs0",
"date" : ISODate("2016-09-17T11:44:25.991Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 548,
"optime" : {
"ts" : Timestamp(1474112614, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-09-17T11:43:34Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1474112613, 2),
"electionDate" : ISODate("2016-09-17T11:43:33Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}

 

添加新节点
testtrs0:PRIMARY> rs.add("192.168.1.103:27017")
{ "ok" : 1 }

testtrs0:PRIMARY> rs.add("192.168.1.104:27017")
{ "ok" : 1 }


设置某节点的优先级

testtrs0:PRIMARY> cfg=rs.conf()
{
"_id" : "testtrs0",
"version" : 3,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.1.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.1.104:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("57dd2c65a94722c11cf7e0d6")
}
}


testtrs0:PRIMARY> cfg.members[0].priority = 1
1
testtrs0:PRIMARY> cfg.members[1].priority = 3
3
testtrs0:PRIMARY> cfg.members[2].priority = 2
2

重新载入cfg
testtrs0:PRIMARY> rs.reconfig(cfg)
{ "ok" : 1 }

 

优先级已经改变
testtrs0:PRIMARY> rs.conf()
{
"_id" : "testtrs0",
"version" : 4,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "192.168.1.103:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 3,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "192.168.1.104:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"tags" : {

},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {

},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("57dd2c65a94722c11cf7e0d6")
}
}



mongodb sharding分片配置

mongos(router) //app server
|
| config server
|
sharding

 

mongos 192.168.1.106
config server 192.168.1.103
shard1 192.168.1.104
shard2 192.168.1.107


一. 配置config server

默认监听27019端口,可以使用如下命令启动mongod进程

# mongod --configsvr --dbpath <path> --port <port>

也可以直接编辑配置文件,如下:
dbpath = /somewhere
configsvr = true
/data/mongodb/bin/mongod -f /data/mongodb/etc/mongo.cfg --shutdown

二 配置mongos实例

mongos 属于轻量级应用,完全可以与其他服务运行于同一个节点:启动时,需要为mongos实例指明各config服务器的访问地址

默认情况下, mongos监听于27017端口,可以使用如下命令配置mongos实例

mongos --configdb <config server hostnames((IP|Hostname):Port)>

也可以直接编辑配置文件

1.注释dbpath指令
2.添加configdb指令,并制定config服务器地址:


bind_ip = 0.0.0.0

fork = true

logappend=true

#port = 27017

#dbpath=/data/mongodb/data

pidfilepath=/data/mongodb/run/mongo.pid

logpath=/data/mongodb/log/mongod.log

configdb=config:27019,config2:27019,config3:27019

 

而后使用如下命令启动mongos实例:

#mongos -f /etc/mongopd.conf



三 配置好各副本集或独立的mongod实例

[root@shard2 ~]# cat /data/mongodb/etc/mongo.cfg
bind_ip = 0.0.0.0

fork = true

logappend=true

port = 27017

dbpath=/data/mongodb/data

pidfilepath=/data/mongodb/run/mongo.pid

logpath=/data/mongodb/log/mongod.lo


/data/mongodb/bin/mongod -f /data/mongodb/etc/mongo.cfg

 

四、向分区集群中添加各shard服务器或副本集

4.1 添加方式

使用mongo命令连接mongos实例,命令如下格式:

#mongo --host <hostname of machine running mongos> --port <port mongos listens on>

在任意节点连接mongos服务器,所有操作必须通过mongos服务器操作

[root@shard2 bin]# ./mongo --host 192.168.1.106:27017

接着使用sh.addShard()方法添加各shard至集群中

如果添加的shard是副本集,需要使用如下格式:

sh.addShard("RS_NAME/RS_SERVER:Port")

例如: sh.addShard("rs0/mongodb0.magedu.com:27017")

如果添加的shard 是独立的mongod实例,则使用如下方式:

sh.addShard("RS_SERVER:Port")

暂时为做副本集
1.把192.168.1.104 加入到sharding中
mongos> sh.addShard("192.168.1.104:27017")
{ "shardAdded" : "shard0000", "ok" : 1 }

2.把192.168.1.104 加入到sharding中
mongos> sh.addShard("192.168.1.107:27017")
{ "shardAdded" : "shard0000", "ok" : 1 }

3.查看

mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("57dd416ed2336438497022cd")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.1.104:27017" }
{ "_id" : "shard0001", "host" : "192.168.1.107:27017" }
active mongoses:
"3.2.9" : 1
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:


4.数据库 启动分区功能,默认,不启动,都回保存到主sharding上
testdb 启动Sharding功能
mongos> sh.enableSharding("testdb")
{ "ok" : 1 }

 

5. 对collections进行顺序分区, 一个数据库中有多个collections,未必所有的collection都分区,
要想让collections使用分区功能,必须使用sh.sharCollection 功能简单,明确说明对那个表进行分区,并
指明分区的key

例: 对testdb.sutable(全名),name 和 Age 进行分区
sh.sharCollection("testdb.sutable",{name: 1, Age:1})


mongos> db.sutable.ensureIndex({age:1,name:1})
{
"raw" : {
"192.168.1.107:27017" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1
}
mongos> sh.shardCollection("feng.sutable",{age: 1,name: 1})
{ "collectionsharded" : "feng.sutable", "ok" : 1 }

 

use testdb
for(i=1;i<1000;i++) db.sutable.insert({name: "User"+i,age:i,sex: "boy",book: ["book1","book2"]})

posted @ 2016-08-29 09:07  fengjian1585  阅读(340)  评论(0编辑  收藏  举报