目录
一、简介
二、应用场景
三、概念
四、安装部署
1、关闭防火墙和selinux
2、指定一个进程同一时间最多可开启的文件数
3、用户最多可开启的进程数目
4、安装版本下载地址
5、创建数据目录,日志文件及目录并创建相应配置文件
6、启动MongoDB数据库,-f指定配置文件
7、设置开机自动启动
8、连接数据库
五、配置管理
1、服务管理
2、基本操作
4、数据备份与恢复
六、MongoDB复制集集群部署及管理
1、MongoDB复制
2、MongoDB复制集ReplSet(副本集)
3、构建MongoDB复制集集群
4、复制集选举原理
七、部署分片服务器
1、分片简介
2、分片优势
3、分片应用环境
4、分片架构及组件
5、MongoDB分片集群部署
八、利用Nginx Stream代理路由实例
一、简介
MongoDB是由C++语言编写一个基于分布式文件存储的开源NoSQL数据库系统。
MongoDB提供了一个面向文档存储方式,操作起来比较简单和容易,可以存储比较复杂的数据类型。
是一个面向集合的,模式自由的文档型数据库。
二、应用场景
游戏场景
物流场景
社交场景
物联网场景
视频直播
三、概念
1.文档(document)是MongoDB中数据的基本单元,非常类似于关系型数据库系统中的行(记录)但是比行存储的数据要复杂的多。
2.集合(collection)就是一组文档组成,如果说MongoDB中的文档类似于关系型数据库中的行,那么集合就如同数据表。
3.MongoDB的单个实例可以容纳多个独立的数据库,每一个数据库都有自己的集合和权限。
四、安装部署
1、关闭防火墙和selinux
[root@mongodb ~]# iptables -F
[root@mongodb ~]# setenforce 0
[root@mongodb ~]# systemctl stop firewalld
2、指定一个进程同一时间最多可开启的文件数
[root@mongodb ~]# ulimit -n
1024
[root@mongodb ~]# ulimit -n 65535
[root@mongodb ~]# ulimit -n
65535
3、用户最多可开启的进程数目
[root@mongodb ~]# ulimit -u
7758
[root@mongodb ~]# ulimit -u 65535
[root@mongodb ~]# ulimit -u
65535
[root@mongodb ~]#cat /etc/rc.local
ulimit –SHn 102400
4、安装版本下载地址
[root@mongodb ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.6.tgz #下载源码包
[root@mongodb ~]# tar xf mongodb-linux-x86_64-rhel70-4.0.6.tgz #解压
[root@mongodb ~]# mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb #移动到/usr/local/mongodb下
[root@mongodb ~]# ln -s /usr/local/mongodb/bin/* /bin/ #创建命令链接
5、创建数据目录,日志文件及目录并创建相应配置文件
[root@mongodb ~]# mkdir -p /data/mongodb1 #数据目录
[root@mongodb ~]# mkdir -p /data/logs/mongodb #日志目录
[root@mongodb ~]# touch /data/logs/mongodb/mongodb1.log #日志文件
[root@mongodb ~]# cd /usr/local/mongodb/
[root@mongodb mongodb]# mkdir conf #配置文件目录
[root@mongodb mongodb]# vim conf/mongodb1.conf #配置文件
port=27017 #监听端口
dbpath=/data/mongodb1 #指定数据目录
logpath=/data/logs/mongodb/mongodb1.log #指定日志文件路径
logappend=on #允许写入日志
fork=true #允许创建子进程
maxConns=5000 #最大连接数
storageEngine=mmapv1 #存储引擎
6、启动MongoDB数据库,-f指定配置文件
[root@mongodb mongodb]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf #启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 1825
child process started successfully, parent exiting
[root@mongodb ~]# netstat -lnpt | grep mongod #查看服务是否启动
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 1874/mongod
[root@mongodb ~]# ps aux | grep mongod | grep -v grep #查看服务状态
root 1874 4.7 9.3 1502244 93728 ? Sl 11:55 0:02 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
7、设置开机自动启动
[root@mongodb mongodb]# vim /etc/rc.local
rm -f /data/mongodb1/mongod.lock
mongod -f /usr/local/mongodb/conf/mongodb1.conf
8、连接数据库
[root@mongodb ~]# mongo #登录数据库
> show dbs #查看所有数据库
admin 0.078GB
config 0.078GB
local 0.078GB
> exit #退出
bye
admin:从权限的角度来看,这是"root"数据库。要是将一个用户添加到这个数据库,这个用户自动继承所有数据库的权限。一些特定的服务器端命令也只能从这个数据库运行,比如列出所有的数据库或者关闭服务器。
local:这个数据永远不会被复制,可以用来存储限于本地单台服务器的任意集合。
config:当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息。
五、配置管理
1、服务管理
关闭MongoDB的方式:
方法一:
[root@mongodb ~]# mongo
> use admin #选择数据库
switched to db admin
> db.shutdownServer(); #关闭服务
> exit
bye
[root@mongodb ~]# netstat -anpt |grep mongod #查看数据库状态
方法二:
[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf #启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 2123
child process started successfully, parent exiting
[root@mongodb ~]# netstat -lnpt | grep mongod #查看服务状态
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2123/mongod
[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf --shutdown #停止服务
killing process with pid: 2123
[root@mongodb ~]# netstat -lnpt | grep mongod #查看服务状态
方法三:
[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf #启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 2159
child process started successfully, parent exiting
[root@mongodb ~]# ps aux | grep mongod #查看服务进程状态
root 2159 1.5 8.6 1500184 86340 ? Sl 12:14 0:00 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
root 2179 0.0 0.0 112720 984 pts/0 R+ 12:14 0:00 grep --color=auto mongod
[root@mongodb ~]# kill -9 2159 #杀死进程号
[root@mongodb ~]# netstat -lnpt | grep mongod #查看服务状态
MongoDB 多实例配置
[root@mongodb ~]# cd /usr/local/mongodb/conf/
[root@mongodb conf]# vim mongodb2.conf #编写配置文件
port=27018 #监听端口
dbpath=/data/mongodb2 #指定数据目录
logpath=/data/logs/mongodb/mongodb2.log #指定日志文件路径
logappend=on #允许写入日志
fork=true #允许创建子进程
maxConns=5000 #最大连接数
storageEngine=mmapv1 #储存引擎
[root@mongodb conf]# mkdir /data/mongodb2 #创建数据目录
[root@mongodb conf]# touch /data/logs/mongodb/mongodb2.log #创建日志文件
[root@mongodb ~]# /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf #启动服务
about to fork child process, waiting until server is ready for connections.
forked process: 63890
child process started successfully, parent exiting
[root@mongodb ~]# netstat -lnpt | grep mongod #查看状态
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 2159/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN 63890/mongod
编写启停脚本
[root@mongodb ~]# vim /etc/init.d/mongodb #编写脚本文件
#!/bin/bash
INSTANCE=$1
ACTION=$2
case "$ACTION" in
'start')
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
'stop')
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown;;
'restart')
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf --shutdown
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/"$INSTANCE".conf;;
esac
[root@mongodb ~]# chmod +x /etc/init.d/mongodb #授予执行权限
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop #停止服务
killing process with pid: 2159
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop
killing process with pid: 63890
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start #启动服务
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 start
[root@mongodb ~]# netstat -lnpt | grep mongod #查看服务状态
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 64008/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN 64030/mongod
2、基本操作
数据库(Database)操作
一个mongodb中可以建立多个数据库。
MongoDB的默认数据库为"db",该数据库存储在data目录中。
MongoDB的单个实例可以容纳多个独立的数据库,每一个都有自己的集合和权限,不同的数据库也放置在不同的文件中
[root@mongodb ~]# mongo
> show dbs #查看当前示例下的数据库列表,等同于show databases
admin 0.078GB
config 0.078GB
local 0.078GB
> show databases;
admin 0.078GB
config 0.078GB
local 0.078GB
执行 "db" 命令可以显示当前数据库对象或集合。
> db
test
运行"use"命令,可以连接到一个指定的数据库。
> use admin
switched to db admin
> db
admin
集合
集合就是 MongoDB 文档组,类似于 RDBMS (关系数据库管理系统:Relational Database Management System)中的表格。
集合存在于数据库中,集合没有固定的结构,这意味着你在对集合可以插入不同格式和类型的数据,但通常情况下我们插入集合的数据都会有一定的关联性。
比如,我们可以将以下不同数据结构的文档插入到集合中:
{"site":"www.baidu.com"}
{"site":"www.google.com","name":"Google","age":"30"}
文档(Document)操作
文档是多组键值(key-value)对(即 BSON)。MongoDB 的文档不需要设置相同的字段,并且相同的字段不需要相同的数据类型,这与关系型数据库有很大的区别,也是 MongoDB 非常突出的特点。
一个简单的文档例子如下:
{"site":"www.crushlinux.com", "name":"crushlinux"} 需要注意的是:
1.文档中的键/值对是有序的。
2.文档中的值不仅可以是在双引号里面的字符串,还可以是其他几种数据类型(甚至可以是整个嵌入的文档)。
3.MongoDB区分类型和大小写。
4.MongoDB的文档不能有重复的键。
5.文档的键是字符串。除了少数例外情况,键可以使用任意UTF-8字符。
> use local
switched to db local
> show collections #显示当前数据库中的集合,等同于show tables
startup_log
> show tables
startup_log
> use cloud
switched to db cloud
> db.user.insert({"id":1,"name":"Crushlinux"}); #创建一个名叫user的集合,写入一个文档
WriteResult({ "nInserted" : 1 })
> show dbs #查看当前示例下的数据库列表,等同于show databases
admin 0.078GB
cloud 0.078GB
config 0.078GB
local 0.078GB
> show collections
user
> db.user.find()
{ "_id" : ObjectId("5e14181b354871170c398ef4"), "id" : 1, "name" : "Crushlinux" }
> ctrl + d
bye
[root@mongodb ~]# ll -h /data/mongodb1/cloud.* #详细查看并显示文件路径
-rw------- 1 root root 64M 1月 7 13:33 /data/mongodb1/cloud.0
-rw------- 1 root root 16M 1月 7 13:33 /data/mongodb1/cloud.ns
如何查看帮助
[root@mongodb ~]# /usr/local/mongodb/bin/mongod --help #mongod命令帮助
[root@mongodb ~]# mongo #登录数据库
> help #查看命令帮助
db.help() help on db methods
db.mycoll.help() help on collection methods
sh.help() sharding helpers
rs.help() replica set helpers
help admin administrative help
help connect connecting to a db help
help keys key shortcuts
help misc misc things to know
help mr mapreduce
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
show logs show the accessible logger names
show log [name] prints out the last segment of log in memory, 'global' is default
use <db_name> set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
DBQuery.shellBatchSize = x set default number of items to display on shell
exit quit the mongo shell
> db.help() #db命令的使用
> db.version();
4.0.6
> db.stats();
{
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1
}
> use cloud
switched to db cloud
> show collections
user
> db.user.help()
数据类型:
> use study #选择数据库
switched to db study
> db.t1.insert({"id":1}); #写入数据
WriteResult({ "nInserted" : 1 })
> show collections #查看当前集合
t1
> show dbs #查看当前数据库列表
admin 0.078GB
cloud 0.078GB
config 0.078GB
local 0.078GB
study 0.078GB
> db
study
> db.t1.insert({"id":2,"name":"Tom","isadmin":true,"gender":null,"favorite":["apple","banana","orange",1,2,3],"regtime":new Date()});
WriteResult({ "nInserted" : 1 })
> db.t1.find() #查看t1集合中的所有数据
{ "_id" : ObjectId("5e141989286870b99ef9001e"), "id" : 1 }
{ "_id" : ObjectId("5e1419a2286870b99ef9001f"), "id" : 2, "name" : "Tom", "isadmin" : true, "gender" : null, "favorite" : [ "apple", "banana", "orange", 1, 2, 3 ], "regtime" : ISODate("2020-01-07T05:39:46.486Z") }
> db.t1.findOne({'id':2}); #查看t1表中id为2的数据
{
"_id" : ObjectId("5e1419a2286870b99ef9001f"),
"id" : 2,
"name" : "Tom",
"isadmin" : true,
"gender" : null,
"favorite" : [
"apple",
"banana",
"orange",
1,
2,
3
],
"regtime" : ISODate("2020-01-07T05:39:46.486Z")
}
> a = db.t1.findOne({"id":2});
{
"_id" : ObjectId("5e1419a2286870b99ef9001f"),
"id" : 2,
"name" : "Tom",
"isadmin" : true,
"gender" : null,
"favorite" : [
"apple",
"banana",
"orange",
1,
2,
3
],
"regtime" : ISODate("2020-01-07T05:39:46.486Z")
}
> typeof(a.id)
number
> typeof(a.name)
string
> typeof(a.isadmin)
boolean
> typeof(a.gender)
object
> typeof(a.favorite)
object
> typeof(a.regtime)
object
>db.t1.insert({"id":3,"salary":66666666666666666666666666666,"rx":1.88888888888888888888888});
WriteResult({ "nInserted" : 1 })
> db.t1.findOne({"id":3});
{
"_id" : ObjectId("57836a653e9d743af15fe37d"),
"id" : 3,
"salary" : 6.666666666666667e+28,
"rx" : 1.8888888888888888
}
> b = db.t1.findOne({"id":3});
{
"_id" : ObjectId("58578f16f5fc504f2ba20123"),
"id" : 3,
"salary" : 6.666666666666667e+28,
"rx" : 1.8888888888888888
}
> typeof(b.salary)
number
> typeof(b.rx)
number
发现mongodb中,没有整数与浮点数的区别,都是number类型
关于null 空:
> db.t1.find({"gender":null});
{ "_id" : ObjectId("5e141989286870b99ef9001e"), "id" : 1 }
{ "_id" : ObjectId("5e1419a2286870b99ef9001f"), "id" : 2, "name" : "Tom", "isadmin" : true, "gender" : null, "favorite" : [ "apple", "banana", "orange", 1, 2, 3 ], "regtime" : ISODate("2020-01-07T05:39:46.486Z") }{ "_id" : ObjectId("5e141a79286870b99ef90020"), "id" : 3, "salary" : 6.666666666666667e+28, "rx" : 1.8888888888888888 }
所有行都被找出来了,若要找有gender存在的行,需要执行如下命令:
> db.t1.find({"gender":{"$exists":true}});
{ "_id" : ObjectId("5e1419a2286870b99ef9001f"), "id" : 2, "name" : "Tom", "isadmin" : true, "gender" : null, "favorite" : [ "apple", "banana", "orange", 1, 2, 3 ], "regtime" : ISODate("2020-01-07T05:39:46.486Z") }
键与值操作注意事项:
> db.t2.insert({"id":1,"id":2}); #相同的键,输入不同的值,后者会覆盖前者
WriteResult({ "nInserted" : 1 })
> db.t2.find()
{ "_id" : ObjectId("5e141bdc286870b99ef90021"), "id" : 2 }
> db.t2.insert({"id":1,"Id":2}); #区分大小写
WriteResult({ "nInserted" : 1 })
> db.t2.find()
{ "_id" : ObjectId("5e141bdc286870b99ef90021"), "id" : 2 }
{ "_id" : ObjectId("5e141bf1286870b99ef90022"), "id" : 1, "Id" : 2 }
> db.t2.insert({"Id":2,"id":1}); #有顺序
WriteResult({ "nInserted" : 1 })
> db.t2.find()
{ "_id" : ObjectId("5e141bdc286870b99ef90021"), "id" : 2 }
{ "_id" : ObjectId("5e141bf1286870b99ef90022"), "id" : 1, "Id" : 2 }
{ "_id" : ObjectId("5e141c23286870b99ef90023"), "Id" : 2, "id" : 1 }
> db.t2.insert({"":3}); #键可以为空,但是一般不这样定义
WriteResult({ "nInserted" : 1 })
> db.t2.find()
{ "_id" : ObjectId("5e141bdc286870b99ef90021"), "id" : 2 }
{ "_id" : ObjectId("5e141bf1286870b99ef90022"), "id" : 1, "Id" : 2 }
{ "_id" : ObjectId("5e141c23286870b99ef90023"), "Id" : 2, "id" : 1 }
{ "_id" : ObjectId("5e141c39286870b99ef90024"), "" : 3 }
> db.t2.insert({"$id":3}); #不能以$开头
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 2,
"errmsg" : "Document can't have $ prefixed field names: $id"
}
})
> db.t2.insert({"i$d":3}); #$可以在中
WriteResult({ "nInserted" : 1 })
> db.t2.find()
{ "_id" : ObjectId("5e141bdc286870b99ef90021"), "id" : 2 }
{ "_id" : ObjectId("5e141bf1286870b99ef90022"), "id" : 1, "Id" : 2 }
{ "_id" : ObjectId("5e141c23286870b99ef90023"), "Id" : 2, "id" : 1 }
{ "_id" : ObjectId("5e141c39286870b99ef90024"), "" : 3 }
{ "_id" : ObjectId("5e141c6d286870b99ef90027"), "i$d" : 3 }
> db.t2.insert({"\0":4}); #键不能以\0命名
2020-01-07T13:51:57.562+0800 E QUERY [js] Error: JavaScript property (name) contains a null char which is not allowed in BSON. :
Bulk/addToOperationsList@src/mongo/shell/bulk_api.js:611:28
Bulk/this.insert@src/mongo/shell/bulk_api.js:654:20
DBCollection.prototype.insert@src/mongo/shell/collection.js:318:13
@(shell):1:1
> db..insert({"id":1}); #表名不能为空
2020-01-07T13:52:24.879+0800 E QUERY [js] SyntaxError: missing name after . operator @(shell):1:3
3、查询操作
[root@mongodb ~]# /usr/local/mongodb/bin/mongoimport -d cloud -c list -f id,name,age,dept --file list.csv --type csv 导入word表到数据库
2020-01-07T14:10:40.460+0800 connected to: localhost
2020-01-07T14:10:40.468+0800 imported 102 documents
[root@mongodb ~]# mongo #登录数据库
> show dbs #显示所有数据库
admin 0.078GB
cloud 0.078GB
config 0.078GB
local 0.078GB
study 0.078GB
> use cloud #选择数据库
switched to db cloud
> show collections #显示集合
list
user
> db.list.find() #显示表中所有数据
{ "_id" : ObjectId("57844b599ee461a95f07ab6b"), "id" : "?1", "name" : "孙美伶", "age" : 57, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab6c"), "id" : 2, "name" : "付建梅", "age" : 48, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab6d"), "id" : 3, "name" : "刘嘉", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab6e"), "id" : 4, "name" : "刘晓慧", "age" : 33, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab6f"), "id" : 5, "name" : "谢娜", "age" : 23, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab70"), "id" : 6, "name" : "翟迪", "age" : 27, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab71"), "id" : 7, "name" : "王新宇", "age" : 36, "dept" : "市场部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab72"), "id" : 8, "name" : "闫金花", "age" : 41, "dept" : "教师部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab73"), "id" : 9, "name" : "曹铮铮", "age" : 52, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab74"), "id" : 10, "name" : "杨雪飞", "age" : 44, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab75"), "id" : 11, "name" : "蔡明珠", "age" : 22, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab76"), "id" : 12, "name" : "刘海霞", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab77"), "id" : 13, "name" : "郭占芳", "age" : 45, "dept" : "教师部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab78"), "id" : 14, "name" : "刘笑", "age" : 24, "dept" : "市场部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab79"), "id" : 15, "name" : "晋红柳", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab7a"), "id" : 16, "name" : "赵明煊", "age" : 37, "dept" : "教师部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab7b"), "id" : 17, "name" : "张研", "age" : 45, "dept" : "教师部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab7c"), "id" : 18, "name" : "常莎", "age" : 52, "dept" : "会计部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab7d"), "id" : 19, "name" : "姜鸿梅", "age" : 36, "dept" : "市场部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab7e"), "id" : 20, "name" : "关跃", "age" : 44, "dept" : "教师部" }
Type "it" for more
> it #继续,下翻一页
{ "_id" : ObjectId("57844b599ee461a95f07ab7f"), "id" : 21, "name" : "吴秋月", "age" : 58, "dept" : "教师部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab80"), "id" : 22, "name" : "闻燕超", "age" : 34, "dept" : "教师部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab81"), "id" : 23, "name" : "武雪姣", "age" : 28, "dept" : "工程部" }
{ "_id" : ObjectId("57844b599ee461a95f07ab82"), "id" : 24, "name" : "周颖", "age" : 44, "dept" : "教师部" }
Type "it" for more
> db.list.count() #显示有多少行数据
102
> db.list.insert({"id":103,"name":"诸葛亮","age":1000,"dept":"行政部"}) #显示这个数据只有一个
WriteResult({ "nInserted" : 1 })
> db.list.find({"name":"诸葛亮"}) #显示名字是诸葛亮的信息
{ "_id" : ObjectId("5e1421737840d8a4c49dbf75"), "id" : 103, "name" : "诸葛亮", "age" : 1000, "dept" : "行政部" }
> db.list.update({"name":"诸葛亮"},{"$set":{"id":150}}); #修改名字是诸葛亮的id为150
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.list.find({"name":"诸葛亮"})
{ "_id" : ObjectId("5e1421737840d8a4c49dbf75"), "id" : 150, "name" : "诸葛亮", "age" : 1000, "dept" : "行政部" }
> db.list.remove({"name":"诸葛亮"}) #删除名字是诸葛亮的信息
WriteResult({ "nRemoved" : 1 })
> db.list.find({"name":"诸葛亮"})
> db.list.find().sort({age:1}).limit(10) #排序,按年龄从小到大(-1为从大到小),前十个
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42085"), "id" : 93, "name" : "贾宝珠", "age" : 15, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42080"), "id" : 88, "name" : "崔兴龙", "age" : 17, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42086"), "id" : 94, "name" : "阎君", "age" : 17, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42079"), "id" : 81, "name" : "王晨旭", "age" : 18, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208e"), "id" : 102, "name" : "靳璠", "age" : 18, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42052"), "id" : 42, "name" : "杨硕", "age" : 20, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205f"), "id" : 55, "name" : "于忠鑫", "age" : 20, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205a"), "id" : 50, "name" : "高慧颖", "age" : 21, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208c"), "id" : 100, "name" : "杨强", "age" : 21, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42033"), "id" : 11, "name" : "蔡明珠", "age" : 22, "dept" : "会计部" }
> db.list.find().sort({age:-1}).limit(10)
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42082"), "id" : 90, "name" : "杨志超", "age" : 67, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202b"), "id" : 3, "name" : "刘嘉", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207b"), "id" : 83, "name" : "张静", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207f"), "id" : 87, "name" : "孙洁", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4206c"), "id" : 68, "name" : "李佳", "age" : 60, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208a"), "id" : 98, "name" : "槐莎莎", "age" : 60, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4203d"), "id" : 21, "name" : "吴秋月", "age" : 58, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42046"), "id" : 30, "name" : "宋涛", "age" : 58, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42029"), "id" : 1, "name" : "孙美伶", "age" : 57, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42045"), "id" : 29, "name" : "齐琪", "age" : 56, "dept" : "市场部" }
> db.list.find().skip(10).limit(5) #跳过前10个,然后的5个
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42033"), "id" : 11, "name" : "蔡明珠", "age" : 22, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42034"), "id" : 12, "name" : "刘海霞", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42035"), "id" : 13, "name" : "郭占芳", "age" : 45, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42036"), "id" : 14, "name" : "刘笑", "age" : 24, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42037"), "id" : 15, "name" : "晋红柳", "age" : 35, "dept" : "会计部" }
> db.list.find({"age":20}) #只显示20的
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42052"), "id" : 42, "name" : "杨硕", "age" : 20, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205f"), "id" : 55, "name" : "于忠鑫", "age" : 20, "dept" : "客服部" }
> db.list.find({"age":20},{"name":1,"age":1}) #只显示年龄20跟名字
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42052"), "name" : "杨硕", "age" : 20 }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205f"), "name" : "于忠鑫", "age" : 20 }
> db.list.find({"age":{$gt:60}}) #年龄大于60的
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202b"), "id" : 3, "name" : "刘嘉", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207b"), "id" : 83, "name" : "张静", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207f"), "id" : 87, "name" : "孙洁", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42082"), "id" : 90, "name" : "杨志超", "age" : 67, "dept" : "市场部" }
> db.list.find({"age":{$lt:20}}) #年龄小于20的
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42079"), "id" : 81, "name" : "王晨旭", "age" : 18, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42080"), "id" : 88, "name" : "崔兴龙", "age" : 17, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42085"), "id" : 93, "name" : "贾宝珠", "age" : 15, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42086"), "id" : 94, "name" : "阎君", "age" : 17, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208e"), "id" : 102, "name" : "靳璠", "age" : 18, "dept" : "客服部" }
> db.list.find({"age":{$mod:[10,1]}}) #年龄 除以10余1的,即11 21 31 41…
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42030"), "id" : 8, "name" : "闫金花", "age" : 41, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42058"), "id" : 48, "name" : "顾洁", "age" : 41, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205a"), "id" : 50, "name" : "高慧颖", "age" : 21, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42069"), "id" : 65, "name" : "王君", "age" : 51, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208c"), "id" : 100, "name" : "杨强", "age" : 21, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208d"), "id" : 101, "name" : "杨譞", "age" : 31, "dept" : "客服部" }
> db.list.find({"dept":"会计部"}) #只显示会计部
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42029"), "id" : 1, "name" : "孙美伶", "age" : 57, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202a"), "id" : 2, "name" : "付建梅", "age" : 48, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202c"), "id" : 4, "name" : "刘晓慧", "age" : 33, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202d"), "id" : 5, "name" : "谢娜", "age" : 23, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202e"), "id" : 6, "name" : "翟迪", "age" : 27, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42031"), "id" : 9, "name" : "曹铮铮", "age" : 52, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42032"), "id" : 10, "name" : "杨雪飞", "age" : 44, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42033"), "id" : 11, "name" : "蔡明珠", "age" : 22, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42034"), "id" : 12, "name" : "刘海霞", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42037"), "id" : 15, "name" : "晋红柳", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4203a"), "id" : 18, "name" : "常莎", "age" : 52, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42044"), "id" : 28, "name" : "吴迪", "age" : 38, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4204c"), "id" : 36, "name" : "高雪", "age" : 46, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42054"), "id" : 44, "name" : "杨柳", "age" : 23, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42070"), "id" : 72, "name" : "肖瑞", "age" : 38, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42071"), "id" : 73, "name" : "许博远", "age" : 30, "dept" : "会计部" }
> db.list.find({"dept":{"$in":["会计部","客服部"]}}) #查找会计部和客服部
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42029"), "id" : 1, "name" : "孙美伶", "age" : 57, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202a"), "id" : 2, "name" : "付建梅", "age" : 48, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202c"), "id" : 4, "name" : "刘晓慧", "age" : 33, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202d"), "id" : 5, "name" : "谢娜", "age" : 23, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202e"), "id" : 6, "name" : "翟迪", "age" : 27, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42031"), "id" : 9, "name" : "曹铮铮", "age" : 52, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42032"), "id" : 10, "name" : "杨雪飞", "age" : 44, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42033"), "id" : 11, "name" : "蔡明珠", "age" : 22, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42034"), "id" : 12, "name" : "刘海霞", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42037"), "id" : 15, "name" : "晋红柳", "age" : 35, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4203a"), "id" : 18, "name" : "常莎", "age" : 52, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42044"), "id" : 28, "name" : "吴迪", "age" : 38, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4204c"), "id" : 36, "name" : "高雪", "age" : 46, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42054"), "id" : 44, "name" : "杨柳", "age" : 23, "dept" : "会计部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42057"), "id" : 47, "name" : "赵祎爽", "age" : 42, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42058"), "id" : 48, "name" : "顾洁", "age" : 41, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42059"), "id" : 49, "name" : "张才旺", "age" : 30, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205f"), "id" : 55, "name" : "于忠鑫", "age" : 20, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42064"), "id" : 60, "name" : "赵楠楠", "age" : 42, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42065"), "id" : 61, "name" : "王子龙", "age" : 33, "dept" : "客服部" }
Type "it" for more
> db.list.find({name:/^张/}) #只显示姓张的
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42039"), "id" : 17, "name" : "张研", "age" : 45, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42059"), "id" : 49, "name" : "张才旺", "age" : 30, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205d"), "id" : 53, "name" : "张萌萌", "age" : 22, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4206a"), "id" : 66, "name" : "张园", "age" : 32, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207b"), "id" : 83, "name" : "张静", "age" : 63, "dept" : "市场部" }
> db.list.find({name:/^[张王]/}).sort({name:1}) #姓张或姓王的,按姓名排序
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4206a"), "id" : 66, "name" : "张园", "age" : 32, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42059"), "id" : 49, "name" : "张才旺", "age" : 30, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42039"), "id" : 17, "name" : "张研", "age" : 45, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4205d"), "id" : 53, "name" : "张萌萌", "age" : 22, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207b"), "id" : 83, "name" : "张静", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42069"), "id" : 65, "name" : "王君", "age" : 51, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42065"), "id" : 61, "name" : "王子龙", "age" : 33, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42043"), "id" : 27, "name" : "王希远", "age" : 22, "dept" : "教师部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202f"), "id" : 7, "name" : "王新宇", "age" : 36, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42079"), "id" : 81, "name" : "王晨旭", "age" : 18, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42081"), "id" : 89, "name" : "王朋", "age" : 38, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207c"), "id" : 84, "name" : "王继蔚", "age" : 49, "dept" : "工程部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4204a"), "id" : 34, "name" : "王芳", "age" : 52, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42067"), "id" : 63, "name" : "王超鑫", "age" : 28, "dept" : "客服部" }
> db.list.find({$or:[{"age":{$gt:60}},{"age":{$lt:20}}]}).sort({age:1}) #按年龄排序,大于60或小于20,从小到大
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42085"), "id" : 93, "name" : "贾宝珠", "age" : 15, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42080"), "id" : 88, "name" : "崔兴龙", "age" : 17, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42086"), "id" : 94, "name" : "阎君", "age" : 17, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42079"), "id" : 81, "name" : "王晨旭", "age" : 18, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4208e"), "id" : 102, "name" : "靳璠", "age" : 18, "dept" : "客服部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4202b"), "id" : 3, "name" : "刘嘉", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207b"), "id" : 83, "name" : "张静", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc4207f"), "id" : 87, "name" : "孙洁", "age" : 63, "dept" : "市场部" }
{ "_id" : ObjectId("5e1420e0b3d4feafbfc42082"), "id" : 90, "name" : "杨志超", "age" : 67, "dept" : "市场部" }
> db.list.distinct("dept") #distingct去重
[ "会计部", "市场部", "教师部", "工程部", "客服部" ]
4、数据备份与恢复
1、数据备份方法
数据导入命令:mongoimport
数据导出导出:mongoexport
备份:
逻辑备份:mongodump
物理备份:冷备份
恢复:mongorestore
2、复制数据库
复制本地数据库:db.copyDatabase(“from_db”,”to_db”,”locolhost”)
复制远程数据库:db.copyDatabase(“from_db”,”to_db”,”192.168.200.101”)
克隆集合:db.runCommand({cloneCollection:”cloud2.t1”,from:”192.168.200.101”})
3、查看帮助的方法:
[root@mongodb ~]# /usr/local/mongodb/bin/mongoimport --help
[root@mongodb ~]# /usr/local/mongodb/bin/mongoexport --help
[root@mongodb ~]# /usr/local/mongodb/bin/mongodump --help
4、备份案例
(1)将MySQL数据库内容导入mongodb环境
[root@mongodb ~]# yum -y install mariadb-server mariadb-devel
[root@mongodb ~]# systemctl enable mariadb
[root@mongodb ~]# systemctl start mariadb
[root@mongodb ~]# netstat -lnpt | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 65647/mysqld
[root@mongodb ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database cloud;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use cloud
Database changed
MariaDB [cloud]> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
MariaDB [cloud]> insert into t1 values(1,"Jack");
Query OK, 1 row affected (0.00 sec)
MariaDB [cloud]> insert into t1 values(2,"Rose");
Query OK, 1 row affected (0.00 sec)
MariaDB [cloud]> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | Jack |
| 2 | Rose |
+------+------+
2 rows in set (0.00 sec)
导出t1表里的内容到/var/lib/mysql/t1_mysql.csv文件,以“,”逗号分割
MariaDB [cloud]> select * from t1 into outfile '/var/lib/mysql/t1_mysql.csv' fields terminated by ",";
Query OK, 2 rows affected (0.00 sec)
MariaDB [cloud]> exit
Bye
[root@mongodb ~]# cat /var/lib/mysql/t1_mysql.csv
1,Jack
2,Rose
错误解决:
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
[root@mongodb ~]# vim /etc/my.cnf
[mysqld]
secure-file-priv=/var/lib/mysql
[root@mongodb ~]# systemctl restart mariadb
[root@mongodb ~]# mysql
mysql> select * from t1 into outfile '/var/lib/mysql/t1_mysql.csv' fields terminated by ",";
Query OK, 2 rows affected (0.01 sec)
将/var/lib/mysql/t1_mysql.csv文件导入到mongodb的cloud数据库下的tt1表,字段名称为id和name,文件类型为csv
[root@mongodb ~]# /usr/local/mongodb/bin/mongoimport -d cloud -c tt1 -f id,name --file /var/lib/mysql/t1_mysql.csv --type csv
2020-01-07T14:50:11.962+0800 connected to: localhost
2020-01-07T14:50:11.986+0800 imported 2 documents
> use cloud
switched to db cloud
> db
cloud
> show tables
list
tt1
user
> db.tt1.find();
{ "_id" : ObjectId("5e142a23b3d4feafbfc420e5"), "id" : 1, "name" : "Jack" }
{ "_id" : ObjectId("5e142a23b3d4feafbfc420e6"), "id" : 2, "name" : "Rose" }
(2) 将mongodb中的cloud数据库的user集合,导出到/tmp/user.json文件中
[root@mongodb ~]# /usr/local/mongodb/bin/mongoexport -d cloud -c user -o /tmp/user.json
2020-01-07T14:53:11.374+0800 connected to: localhost
2020-01-07T14:53:11.376+0800 exported 1 record
[root@mongodb ~]# cat /tmp/user.json
{"_id":{"$oid":"5e14181b354871170c398ef4"},"id":1.0,"name":"Crushlinux"}
(3)mongodump备份
mongodump命令脚本语法如下:
mongodump -h dbhost -d dbname -o dbdirectory
-h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017
-d:需要备份的数据库实例,例如:test
-o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。
[root@mongodb ~]# mkdir /backup
[root@mongodb ~]# /usr/local/mongodb/bin/mongodump -d cloud -o /backup/
2020-01-07T14:55:46.774+0800 writing cloud.list to 2020-01-07T14:55:46.775+0800 writing cloud.tt1 to
2020-01-07T14:55:46.775+0800 writing cloud.user to
2020-01-07T14:55:46.778+0800 done dumping cloud.tt1 (2 documents)
2020-01-07T14:55:46.795+0800 done dumping cloud.list (102 documents)
2020-01-07T14:55:46.796+0800 done dumping cloud.user (1 document)
[root@mongodb ~]# ll -h /backup/
总用量 0
drwxr-xr-x 2 root root 133 1月 7 14:55 cloud
[root@mongodb ~]# ll -h /backup/cloud/
总用量 28K
-rw-r--r-- 1 root root 7.8K 1月 7 14:55 list.bson
-rw-r--r-- 1 root root 124 1月 7 14:55 list.metadata.json
-rw-r--r-- 1 root root 90 1月 7 14:55 tt1.bson
-rw-r--r-- 1 root root 123 1月 7 14:55 tt1.metadata.json
-rw-r--r-- 1 root root 55 1月 7 14:55 user.bson
-rw-r--r-- 1 root root 124 1月 7 14:55 user.metadata.json
(4)mongorestore还原
mongorestore命令脚本语法如下:
mongorestore -h <hostname><:port> -d dbname <path> --host <:port>, -h <:port>:
MongoDB所在服务器地址,默认为: localhost:27017
--db , -d :需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2
--drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!
<path>:mongorestore 最后的一个参数,设置备份数据所在位置,例如:c:\data\dump\test。
你不能同时指定 <path> 和 --dir 选项,--dir也可以设置备份目录。
--dir:指定备份的目录
注意:不能同时指定 <path> 和 --dir 选项。
[root@mongodb ~]# /usr/local/mongodb/bin/mongorestore -d crushlinux --dir=/backup/cloud/
2020-01-07T14:57:04.143+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated an
d will not exist in the future; use --nsInclude instead2020-01-07T14:57:04.143+0800 building a list of collections to restore from /backup/cloud dir
2020-01-07T14:57:04.144+0800 reading metadata for crushlinux.list from /backup/cloud/list.metadata.json
2020-01-07T14:57:04.146+0800 reading metadata for crushlinux.tt1 from /backup/cloud/tt1.metadata.json
2020-01-07T14:57:04.245+0800 restoring crushlinux.list from /backup/cloud/list.bson
2020-01-07T14:57:04.248+0800 restoring crushlinux.tt1 from /backup/cloud/tt1.bson
2020-01-07T14:57:04.251+0800 reading metadata for crushlinux.user from /backup/cloud/user.metadata.json
2020-01-07T14:57:04.257+0800 restoring crushlinux.user from /backup/cloud/user.bson
2020-01-07T14:57:04.261+0800 no indexes to restore
2020-01-07T14:57:04.261+0800 finished restoring crushlinux.list (102 documents)
2020-01-07T14:57:04.262+0800 no indexes to restore
2020-01-07T14:57:04.262+0800 finished restoring crushlinux.user (1 document)
2020-01-07T14:57:04.262+0800 no indexes to restore
2020-01-07T14:57:04.262+0800 finished restoring crushlinux.tt1 (2 documents)
2020-01-07T14:57:04.262+0800 done
[root@mongodb ~]# mongo
> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
crushlinux 0.078GB
local 0.078GB
study 0.078GB
> use crushlinux
switched to db crushlinux
> show collections
list
tt1
user
(5)复制数据库
> db.copyDatabase("crushlinux","crushlinux2","localhost");
WARNING: db.copyDatabase is deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation{
"note" : "Support for the copydb command has been deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation", "ok" : 1
}
> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
crushlinux 0.078GB
crushlinux2 0.078GB
local 0.078GB
study 0.078GB
(6)两个mongodb实例之间的数据导入
[root@mongodb ~]# netstat -lntp |grep mongod
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 1521/mongod
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 1498/mongod
[root@mongodb ~]# mongo --port 27018
> show dbs
admin 0.078GB
config 0.078GB
local 0.078GB
> use db1
switched to db db1
> db.student.insert({"id":1,"name":"zhangsan"});
WriteResult({ "nInserted" : 1 })
> show dbs
admin 0.078GB
config 0.078GB
db1 0.078GB
local 0.078GB
> show collections
student
[root@mongodb ~]# mongo
> db.runCommand({"cloneCollection":"db1.student","from":"localhost:27018"});
{ "ok" : 1 }
> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
crushlinux 0.078GB
crushlinux2 0.078GB
db1 0.078GB
local 0.078GB
study 0.078GB
> use db1
switched to db db1
> db.student.find()
{ "_id" : ObjectId("5e142cc3c7724b99c1a744a2"), "id" : 1, "name" : "zhangsan" }
案例:
(1)批量插入1万条数据
[root@mongodb ~]# mongo
> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
crushlinux 0.078GB
crushlinux2 0.078GB
db1 0.078GB
local 0.078GB
study 0.078GB
> use cloud
switched to db cloud
> show collections
list
tt1
user
> for(var i=1;i<=10000;i++) db.test.insert({"id":i,"name":"name"+i});
WriteResult({ "nInserted" : 1 })
(2)仅显示5行信息
> db.test.find().limit(5)
{ "_id" : ObjectId("5e142df76e516a20cb463335"), "id" : 1, "name" : "name1" }
{ "_id" : ObjectId("5e142df76e516a20cb463336"), "id" : 2, "name" : "name2" }
{ "_id" : ObjectId("5e142df76e516a20cb463337"), "id" : 3, "name" : "name3" }
{ "_id" : ObjectId("5e142df76e516a20cb463338"), "id" : 4, "name" : "name4" }
{ "_id" : ObjectId("5e142df76e516a20cb463339"), "id" : 5, "name" : "name5" }
(3)条件导出,导出test表中id大于5000的文档
> db.test.find({"id":{"$gt":5000}}).limit(3); #查询id大于5000的文档,打印出3行
{ "_id" : ObjectId("5e142df96e516a20cb4646bd"), "id" : 5001, "name" : "name5001" }
{ "_id" : ObjectId("5e142df96e516a20cb4646be"), "id" : 5002, "name" : "name5002" }
{ "_id" : ObjectId("5e142df96e516a20cb4646bf"), "id" : 5003, "name" : "name5003" }
(4)统计大于5000的行数量
> db.test.find({"id":{"$gt":5000}}).count();
5000
(5)导出test集合到/tmp/test.json文件
[root@mongodb ~]# /usr/local/mongodb/bin/mongoexport -d cloud -c test -q '{"id":{"$gt":5000}}' -o /tmp/test.json
2020-01-07T15:08:17.652+0800 connected to: localhost
2020-01-07T15:08:17.735+0800 exported 5000 records
[root@mongodb ~]# ll /tmp/test.json
-rw-r--r-- 1 root root 370002 1月 7 15:08 /tmp/test.json
[root@mongodb ~]# wc -l /tmp/test.json
5000 /tmp/test.json
5、MongoDB安全管理
(1)配置mongodb实例1监听特定IP地址
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb1.conf
port=27017
dbpath=/data/mongodb1
logpath=/data/logs/mongodb/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1 #慢查询日志,超过1秒的查询请求将被记录
profile=1 #0=off,1=slow,2=all
bind_ip=192.168.200.111 #限制监听特定IP
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 restart
killing process with pid: 64008
about to fork child process, waiting until server is ready for connections.
forked process: 66271
child process started successfully, parent exiting
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop
killing process with pid: 64030
[root@mongodb ~]# netstat -lnpt |grep mongod
tcp 0 0 192.168.200.111:27017 0.0.0.0:* LISTEN 66271/mongod
[root@mongodb ~]# mongo #连接失败
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2020-01-07T15:20:36.885+0800 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:343:13
@(connect):1:6
exception: connect failed
[root@mongodb ~]# mongo --host 192.168.200.111
> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
crushlinux 0.078GB
crushlinux2 0.078GB
db1 0.078GB
local 0.078GB
study 0.078GB
(2)配置实例授权启动,连接时必须验证用户密码。
[root@mongodb ~]# mongo --host 192.168.200.111
> use admin
switched to db admin
> show collections
> db.createUser({"user":"root","pwd":"123456","roles":["root"]}) #创建root用户,密码123456,角色为超级管理员
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
> db.system.users.findOne()
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "wUdZoiSEn+4QONV/H9WMQA==",
"storedKey" : "Xe3CkqWtvXqKA8xqKj9LBmsMVOs=",
"serverKey" : "cvPNmKqrifs56OtrEdlrHXJlajU="
},
"SCRAM-SHA-256" : {
"iterationCount" : 15000,
"salt" : "Ci8s7svV/OOyFzqPqims+NsccM3QUKF0gC+llA==",
"storedKey" : "sKiqEge7kTrMfn7GqwkDOUKNCg/LO9JbjxMyae6JUYw=",
"serverKey" : "WfFcuLdoKj+MFlfDu58YJ1ZfHL6B29ysell2oD7XLI0="
}
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
> exit
bye
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb1.conf
port=27017
dbpath=/data/mongodb1
logpath=/data/logs/mongodb/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
bind_ip=192.168.200.111
auth=true
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 restart
[root@mongodb ~]# mongo --host 192.168.200.111
> show dbs
> use admin
switched to db admin
> db.auth("root","123456")
1
> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
crushlinux 0.078GB
crushlinux2 0.078GB
db1 0.078GB
local 0.078GB
study 0.078GB
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb1.conf
port=27017
dbpath=/data/mongodb1
logpath=/data/logs/mongodb/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
#bind_ip=192.168.200.111
#auth=true
还原默认配置
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 restart
killing process with pid: 2112
about to fork child process, waiting until server is ready for connections.
forked process: 2138
child process started successfully, parent exiting
[root@mongodb ~]# mongo
> use admin
switched to db admin
> db.dropUser("root") #删除用户
true
6、MongoDB角色管理
Built-In Roles(内置角色):
数据库用户角色:read、readWrite
数据库管理角色:dbAdmin、dbOwner、userAdmin
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager
备份恢复角色:backup、restore
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase dbAdminAnyDatabase
超级用户角色:root
内部角色:_system
7、MongoDB进程管理
(1)查看当前正在运行的进程:db.currentOp()
[root@mongodb ~]# mongo
> db.currentOp()
{
"inprog" : [
{
"host" : "mongodb:27017",
"desc" : "conn2",
"connectionId" : 2,
"client" : "127.0.0.1:34798",
"appName" : "MongoDB Shell",
"clientMetadata" : {
"application" : {
"name" : "MongoDB Shell"
},
"driver" : {
"name" : "MongoDB Internal Client",
"version" : "4.0.6"
},
"os" : {
"type" : "Linux",
"name" : "CentOS Linux release 7.5.1804 (Core) ",
"architecture" : "x86_64",
"version" : "Kernel 3.10.0-862.el7.x86_64"
}
},
"active" : true,
"currentOpTime" : "2020-01-07T15:30:18.383+0800",
"opid" : 1504,
"lsid" : {
"id" : UUID("16363cc6-9b3f-4719-8340-7b105c173e89"),
"uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
},
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(1765),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : {
"currentOp" : 1,
"lsid" : {
"id" : UUID("16363cc6-9b3f-4719-8340-7b105c173e89")
},
"$db" : "admin"
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
}
],
"ok" : 1
}
(2)杀掉当前正在运行的高消耗资源的进程:db.killOp(opid号)
> db.killOp(1504)
{ "info" : "attempting to kill op", "ok" : 1 }
> db.currentOp()
{
"inprog" : [
{
"host" : "mongodb:27017",
"desc" : "conn2",
"connectionId" : 2,
"client" : "127.0.0.1:34798",
"appName" : "MongoDB Shell",
"clientMetadata" : {
"application" : {
"name" : "MongoDB Shell"
},
"driver" : {
"name" : "MongoDB Internal Client",
"version" : "4.0.6"
},
"os" : {
"type" : "Linux",
"name" : "CentOS Linux release 7.5.1804 (Core) ",
"architecture" : "x86_64",
"version" : "Kernel 3.10.0-862.el7.x86_64"
}
},
"active" : true,
"currentOpTime" : "2020-01-07T15:31:20.027+0800",
"opid" : 2171,
"lsid" : {
"id" : UUID("16363cc6-9b3f-4719-8340-7b105c173e89"),
"uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
},
"secs_running" : NumberLong(0),
"microsecs_running" : NumberLong(105),
"op" : "command",
"ns" : "admin.$cmd.aggregate",
"command" : {
"currentOp" : 1,
"lsid" : {
"id" : UUID("16363cc6-9b3f-4719-8340-7b105c173e89")
},
"$db" : "admin"
},
"numYields" : 0,
"locks" : {
},
"waitingForLock" : false,
"lockStats" : {
}
}
],
"ok" : 1
}
8、MongDB监控管理
查看数据库实例状态信息:db.serverStatus()
> db.serverStatus()
查看当前数据库统计信息:db.stats()
> db.stats()
{
"db" : "test",
"collections" : 0,
"views" : 0,
"objects" : 0,
"avgObjSize" : 0,
"dataSize" : 0,
"storageSize" : 0,
"numExtents" : 0,
"indexes" : 0,
"indexSize" : 0,
"fileSize" : 0,
"fsUsedSize" : 0,
"fsTotalSize" : 0,
"ok" : 1
}
查看集合统计信息:db.集合.stats()
> use cloud
switched to db cloud
> db.user.stats()
{
"ns" : "cloud.user",
"size" : 112,
"count" : 1,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintainedin 3.0. It remains hard coded to 1.0 for compatibility only.", "userFlags" : 1,
"capped" : false,
"nindexes" : 1,
"totalIndexSize" : 8176,
"indexSizes" : {
"_id_" : 8176
},
"ok" : 1
}
查看集合大小:db.集合.dataSize()
> db.user.dataSize()
112
mongostat是mongodb自带的状态检测工具,在命令行下使用。它会间隔固定时间获取mongodb的当前运行状态,并输出。如果你发现数据库突然变慢或者有其他问题的话,你第一手的操作就考虑采用mongostat来查看mongo的状态。
[root@mongodb ~]# /usr/local/mongodb/bin/mongostat
insert query update delete getmore command flushes mapped vsize res faults qrw arw net_in net_out conn time
*0 *0 *0 *0 0 1|0 0 2.22G 169M 0 0|0 0|0 157b 31.7k 1 Jan 7 15:39:06.541
*0 *0 *0 *0 0 1|0 0 2.22G 169M 0 0|0 0|0 157b 31.8k 1 Jan 7 15:39:07.543
*0 *0 *0 *0 0 2|0 0 2.22G 169M 0 0|0 0|0 158b 31.8k 1 Jan 7 15:39:08.543
*0 *0 *0 *0 0 1|0 0 2.22G 169M 0 0|0 0|0 157b 31.8k 1 Jan 7 15:39:09.544
mongotop也是mongodb下的一个内置工具,mongotop提供了一个方法,用来跟踪一个MongoDB的实例,查看哪些大量的时间花费在读取和写入数据。 mongotop提供每个集合的水平的统计数据。默认情况下,mongotop返回值的每一秒。
[root@mongodb ~]# /usr/local/mongodb/bin/mongotop
2020-01-07T15:41:07.382+0800 connected to: 127.0.0.1
ns total read write 2020-01-07T15:41:08+08:00
admin.$cmd.aggregate 0ms 0ms 0ms
admin.system.indexes 0ms 0ms 0ms
admin.system.namespaces 0ms 0ms 0ms
admin.system.profile 0ms 0ms 0ms
admin.system.roles 0ms 0ms 0ms
admin.system.users 0ms 0ms 0ms
admin.system.version 0ms 0ms 0ms
cloud.list 0ms 0ms 0ms
cloud.system.indexes 0ms 0ms 0ms
cloud.system.namespaces 0ms 0ms 0ms
输出结果字段说明:
ns:
包含数据库命名空间,后者结合了数据库名称和集合。
db:
包含数据库的名称。名为 . 的数据库针对全局锁定,而非特定数据库。
total:
mongod花费的时间工作在这个命名空间提供总额。
read:
提供了大量的时间,这mongod花费在执行读操作,在此命名空间。
write:
提供这个命名空间进行写操作,这mongod花了大量的时间。
六、MongoDB复制集集群部署及管理
1、MongoDB复制
将一个数据库实例中的所有数据改变复制到另一个独立的数据库实例的过程,默认是主从复制集群(未来不再使用)。缺点是一旦主库出现故障,需要手动把主库角色切换到最可靠的从库上,而其他从库还需配置从新的主库去同步。
复制是将数据同步在多个服务器的过程。
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
复制还允许您从硬件故障和服务中断中恢复数据。
复制的特征
保障数据的安全性
数据高可用性 (7*24)
数据灾难恢复
无需停机维护(如备份,重建索引,压缩)
分布式读取数据
2、MongoDB复制集ReplSet(副本集)
原理上也是MongoDB主从复制技术,但当主库出现故障时,能自动实现主从切换,从而故障得以恢复,其他从库自动从新的主库上同步数据,整个过程不需要手动干预。类似于MySQL中的MHA技术。
MongoDB的复制至少需要两个节点。其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据,达到数据一致性。
MongoDB各个节点常见的搭配方式为:一主一从、一主多从。
主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。
MongoDB复制结构图如下所示:
以上结构图中,客户端从主节点读取数据,在客户端写入数据到主节点时, 主节点与从节点进行数据交互保障数据的一致性。
复制集的特征
N 个节点的集群
任何节点可作为主节点
所有写入操作都在主节点上
自动故障转移
自动故障恢复
3、构建MongoDB复制集集群
删除之前的实例
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop
[root@mongodb ~]# rm -rf /data/
配置4个MongoDB实例
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb1.conf
port=27017
dbpath=/data/mongodb1
logpath=/data/logs/mongodb/mongodb1.log
logappend=on #on和true根据系统配置进行选择
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux #名字随意取
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb2.conf
port=27018
dbpath=/data/mongodb2
logpath=/data/logs/mongodb/mongodb2.log
logappend=on
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb3.conf
port=27019
dbpath=/data/mongodb3
logpath=/data/logs/mongodb/mongodb3.log
logappend=on
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux
[root@mongodb ~]# vim /usr/local/mongodb/conf/mongodb4.conf
port=27020
dbpath=/data/mongodb4
logpath=/data/logs/mongodb/mongodb4.log
logappend=on
fork=true
maxConns=5000
storageEngine=mmapv1
slowms=1
profile=1
replSet=crushlinux
[root@mongodb ~]# mkdir /data/mongodb{1..4} -p #创建四个启动目录
[root@mongodb ~]# mkdir /data/logs/mongodb -p #创建日志目录
[root@mongodb ~]# touch /data/logs/mongodb/mongodb{1..4}.log #创建四个日志文件
[root@mongodb ~]# chmod 777 /data/logs/mongodb/mongodb* #授权
[root@mongodb ~]# ll /data/logs/mongodb/mongodb* #查看是否授权成功
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb1.log
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb2.log
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb3.log
-rwxrwxrwx 1 root root 0 1月 7 16:38 /data/logs/mongodb/mongodb4.log
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start #启动服务
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb3 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb4 start
[root@mongodb ~]# netstat -lnpt | grep mongod #查看服务状态
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 67461/mongod
tcp 0 0 127.0.0.1:27018 0.0.0.0:* LISTEN 67487/mongod
tcp 0 0 127.0.0.1:27019 0.0.0.0:* LISTEN 67513/mongod
tcp 0 0 127.0.0.1:27020 0.0.0.0:* LISTEN 67539/mongod
[root@mongodb ~]# mongo
crushlinux:PRIMARY> rs.help() #查看复制集帮助指令
> rs.status()
{
"operationTime" : Timestamp(0, 0),
"ok" : 0,
"errmsg" : "no replset config has been received",
"code" : 94,
"codeName" : "NotYetInitialized",
"$clusterTime" : {
"clusterTime" : Timestamp(0, 0),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA
="), "keyId" : NumberLong(0)
}
}
}
> cfg={"_id":"crushlinux","members":[{"_id":0,"host":"127.0.0.1:27017"},{"_id":1,"host":"127.0.0.1:27018"},{"_id":2,"host":"127.0.0.1:27019"}]} #添加端口
{
"_id" : "crushlinux",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017"
},
{
"_id" : 1,
"host" : "127.0.0.1:27018"
},
{
"_id" : 2,
"host" : "127.0.0.1:27019"
}
]
}
> rs.initiate(cfg) #初始化副本集
{
"ok" : 1,
"operationTime" : Timestamp(1578386438, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386438, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.status() #获取副本集状态
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:41:04.997Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1, #1为健康,0为宕机
"state" : 1, #1为主,2为从
"stateStr" : "PRIMARY",
"uptime" : 125,
"optime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:40:52Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578386449, 1),
"electionDate" : ISODate("2020-01-07T08:40:49Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 26,
"optime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:40:52Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:40:52Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:41:03.888Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:41:04.492Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 26,
"optime" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386452, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:40:52Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:40:52Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:41:03.888Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:41:04.441Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578386452, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386452, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.add("127.0.0.1:27020") #增加一个节点
{
"ok" : 1,
"operationTime" : Timestamp(1578386643, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386643, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:44:25.733Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 326,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1578386449, 1),
"electionDate" : ISODate("2020-01-07T08:40:49Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 227,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:44:21Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:44:25.210Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:44:24.254Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27020",
"syncSourceHost" : "127.0.0.1:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 227,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:44:21Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:44:25.210Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:44:24.255Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27020",
"syncSourceHost" : "127.0.0.1:27020",
"syncSourceId" : 3,
"infoMessage" : "",
"configVersion" : 2
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 22,
"optime" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578386661, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-07T08:44:21Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:44:21Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:44:25.229Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:44:25.732Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"operationTime" : Timestamp(1578386661, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386661, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.remove("127.0.0.1:27020") #删除节点
{
"ok" : 1,
"operationTime" : Timestamp(1578386690, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386690, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb ~]# ps aux | grep mongod #查看服务pid
root 67461 1.0 4.5 10304208 45700 ? Sl 16:38 0:04 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb1.conf
root 67487 1.0 4.6 6120932 46896 ? Sl 16:39 0:03 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb2.conf
root 67513 1.0 4.7 6048004 47108 ? Sl 16:39 0:03 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb3.conf
root 67539 0.9 4.5 6088480 45272 ? Sl 16:39 0:03 /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb4.conf
[root@mongodb ~]# kill -9 67461 #模拟故障(杀死主进程)
[root@mongodb ~]# mongo --port 27018
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:47:01.674Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:47:01.427Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:46:45.095Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1, #27018进程变为主
"stateStr" : "PRIMARY",
"uptime" : 478,
"optime" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-01-07T08:46:56Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578386815, 1),
"electionDate" : ISODate("2020-01-07T08:46:55Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 381,
"optime" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1578386816, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-01-07T08:46:56Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:46:56Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:47:01.421Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:47:01.588Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1578386816, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386816, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start #重启主进程
about to fork child process, waiting until server is ready for connections.
forked process: 67927
child process started successfully, parent exiting
[root@mongodb ~]# mongo --port 27018
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:48:37.546Z"),
"myState" : 1,
"term" : NumberLong(2),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
},
"appliedOpTime" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
},
"durableOpTime" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1, #27017主进程变为从进程
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 29,
"optime" : {
"ts" : Timestamp(1578386906, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1578386906, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-01-07T08:48:26Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:48:26Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:48:35.591Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:48:36.200Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 574,
"optime" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-01-07T08:48:36Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578386815, 1),
"electionDate" : ISODate("2020-01-07T08:46:55Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 477,
"optime" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
},
"optimeDurable" : {
"ts" : Timestamp(1578386916, 1),
"t" : NumberLong(2)
},
"optimeDate" : ISODate("2020-01-07T08:48:36Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:48:36Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:48:37.544Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:48:35.755Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1578386916, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578386916, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
手动切换主实例
[root@mongodb ~]# mongo --port 27018
crushlinux:PRIMARY> rs.help() #查看帮助
crushlinux:PRIMARY> rs.freeze(30) #暂停30秒不参加选举
{
"operationTime" : Timestamp(1578387047, 1),
"ok" : 0,
"errmsg" : "cannot freeze node when primary or running for elec
tion. state: Primary", "code" : 95,
"codeName" : "NotSecondary",
"$clusterTime" : {
"clusterTime" : Timestamp(1578387047, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.stepDown(60,30) #修改成从节点
2020-01-07T16:51:20.451+0800 E QUERY [js] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' o
n host '127.0.0.1:27018' :DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
rs.stepDown@src/mongo/shell/utils.js:1444:12
@(shell):1:1
2020-01-07T16:51:20.458+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27018 failed
2020-01-07T16:51:20.459+0800 I NETWORK [js] reconnect 127.0.0.1:27018 ok
crushlinux:SECONDARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:52:23.684Z"),
"myState" : 2,
"term" : NumberLong(3),
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"appliedOpTime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 256,
"optime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2020-01-07T08:52:22Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:52:22Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:52:23.611Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:52:22.601Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1578387080, 1),
"electionDate" : ISODate("2020-01-07T08:51:20Z"),
"configVersion" : 3
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 800,
"optime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2020-01-07T08:52:22Z"),
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 703,
"optime" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1578387142, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2020-01-07T08:52:22Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:52:22Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:52:23.428Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:52:23.252Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1578387142, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578387142, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop
killing process with pid: 67927
当模拟mongodb1实例故障时,由于mongodb2实例不参与选举,因此mongodb3也就是27019将会成为新的主节点
crushlinux:SECONDARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T08:53:50.785Z"),
"myState" : 2,
"term" : NumberLong(4),
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:53:50.107Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:53:10.770Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 887,
"optime" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-07T08:53:41Z"),
"syncingTo" : "127.0.0.1:27019",
"syncSourceHost" : "127.0.0.1:27019",
"syncSourceId" : 2,
"infoMessage" : "",
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 1, #27019从节点变为主节点
"stateStr" : "PRIMARY",
"uptime" : 790,
"optime" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1578387221, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-07T08:53:41Z"),
"optimeDurableDate" : ISODate("2020-01-07T08:53:41Z"),
"lastHeartbeat" : ISODate("2020-01-07T08:53:50.068Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T08:53:49.674Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1578387199, 1),
"electionDate" : ISODate("2020-01-07T08:53:19Z"),
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1578387221, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578387221, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
4、复制集选举原理
1、复制的原理
MongoDB复制是基于操作日志oplog实现,oplog相当于mysql中的二进制日志,只记录数据发生的改变操作。
2、选举的原理
(1)节点类型:标准节点,被动节点,仲裁节点
只有标准节点可能被选举为活跃(主)节点,有选举权
被动节点有完整副本,不可能成为活跃节点,有选举权
仲裁节点不复制数据,不可能成为活跃节点,只有选举权
(2)标准节点与被动节点的区别
priority值高者是标准节点,低者则为被动节点
(3)选举规则
票数高者获胜,priority是优先权0-1000值,相当于额外增加0-1000的票数。
选举结果:票数高者获胜;若票数相同,数据新者获胜。
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start
about to fork child process, waiting until server is ready for connections.
forked process: 68123
child process started successfully, parent exiting
[root@mongodb ~]# /etc/init.d/mongodb mongodb3 restart
killing process with pid: 68225
about to fork child process, waiting until server is ready for connections.
forked process: 68294
child process started successfully, parent exiting
[root@mongodb ~]# mongo --port 27017
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T09:01:05.755Z"),
"myState" : 1,
"term" : NumberLong(5),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"appliedOpTime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"durableOpTime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 117,
"optime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2020-01-07T09:00:56Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578387634, 1),
"electionDate" : ISODate("2020-01-07T09:00:34Z"),
"configVersion" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 116,
"optime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2020-01-07T09:00:56Z"),
"optimeDurableDate" : ISODate("2020-01-07T09:00:56Z"),
"lastHeartbeat" : ISODate("2020-01-07T09:01:04.700Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T09:01:05.342Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 29,
"optime" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"optimeDurable" : {
"ts" : Timestamp(1578387656, 1),
"t" : NumberLong(5)
},
"optimeDate" : ISODate("2020-01-07T09:00:56Z"),
"optimeDurableDate" : ISODate("2020-01-07T09:00:56Z"),
"lastHeartbeat" : ISODate("2020-01-07T09:01:04.700Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T09:01:04.950Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 3
}
],
"ok" : 1,
"operationTime" : Timestamp(1578387656, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578387656, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> show dbs
local 4.076GB
crushlinux:PRIMARY> use cloud
switched to db cloud
crushlinux:PRIMARY> db.t1.insert({"id":1,"name":"Tom"})
WriteResult({ "nInserted" : 1 })
crushlinux:PRIMARY> db.t1.insert({"id":2,"name":"Bob"})
WriteResult({ "nInserted" : 1 })
crushlinux:PRIMARY> db.t1.find()
{ "_id" : ObjectId("5e144996d734a182fa29e746"), "id" : 1, "name" : "Tom" }
{ "_id" : ObjectId("5e144996d734a182fa29e747"), "id" : 2, "name" : "Bob" }
crushlinux:PRIMARY> show dbs
admin 0.078GB
cloud 0.078GB
config 0.078GB
local 4.076GB
crushlinux:PRIMARY> db.t1.remove({"id":2})
WriteResult({ "nRemoved" : 1 })
crushlinux:PRIMARY> db.t1.find()
{ "_id" : ObjectId("5e144996d734a182fa29e746"), "id" : 1, "name" : "Tom" }
crushlinux:PRIMARY> use local
switched to db local
crushlinux:PRIMARY> show collections
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
crushlinux:PRIMARY> db.oplog.rs.find() #记录着增删改的操作
{ "ts" : Timestamp(1578386531, 1), "t" : NumberLong(1), "h" : NumberLong("-4279155608099685404"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODa
te("2020-01-07T08:42:11.763Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386541, 1), "t" : NumberLong(1), "h" : NumberLong("5621407293650141782"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODat
e("2020-01-07T08:42:21.764Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386551, 1), "t" : NumberLong(1), "h" : NumberLong("4929556758383417714"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODat
e("2020-01-07T08:42:31.765Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386561, 1), "t" : NumberLong(1), "h" : NumberLong("-5780453312293482550"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODa
te("2020-01-07T08:42:41.778Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386571, 1), "t" : NumberLong(1), "h" : NumberLong("-4752120832734390339"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODa
te("2020-01-07T08:42:51.778Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386581, 1), "t" : NumberLong(1), "h" : NumberLong("6473202685615866212"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODat
e("2020-01-07T08:43:01.780Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386591, 1), "t" : NumberLong(1), "h" : NumberLong("2124753924185966466"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODat
e("2020-01-07T08:43:11.782Z"), "o" : { "msg" : "periodic noop" } }{ "ts" : Timestamp(1578386601, 1), "t" : NumberLong(1), "h" : NumberLong("3774933450762899504"), "v" : 2, "op" : "n", "ns" : "", "wall" : ISODat
e("2020-01-07T08:43:21.797Z"), "o" : { "msg" : "periodic noop" } }Type "it" for more
重新定义集群,将27020节点设置为仲裁节点
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop
[root@mongodb ~]# /etc/init.d/mongodb mongodb3 stop
[root@mongodb ~]# /etc/init.d/mongodb mongodb4 stop
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb3 start
[root@mongodb ~]# /etc/init.d/mongodb mongodb4 start
[root@mongodb ~]# mongo
crushlinux:PRIMARY> cfg={"_id":"crushlinux","protocolVersion":1,"members":[{"_id":0,"host":"127.0.0.1:27017","priority":100}, {"_id":1,"host":"127.0.0.1:27018","priority":100}, {"_id":2,"host":"127.0.0.1:27019","priority":0}, {"_id":3,"host":"127.0.0.1:27020","arbiterOnly":true}]} #代表其为仲裁节点
{
"_id" : "crushlinux",
"protocolVersion" : 1,
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017",
"priority" : 100
},
{
"_id" : 1,
"host" : "127.0.0.1:27018",
"priority" : 100
},
{
"_id" : 2,
"host" : "127.0.0.1:27019",
"priority" : 0
},
{
"_id" : 3,
"host" : "127.0.0.1:27020",
"arbiterOnly" : true
}
]
}
crushlinux:PRIMARY> rs.reconfig(cfg)
{
"ok" : 1,
"operationTime" : Timestamp(1578408147, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578408147, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T14:45:21.366Z"),
"myState" : 1,
"term" : NumberLong(9),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"durableOpTime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 551,
"optime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2020-01-07T14:45:13Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1578407782, 1),
"electionDate" : ISODate("2020-01-07T14:36:22Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 548,
"optime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"optimeDurable" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2020-01-07T14:45:13Z"),
"optimeDurableDate" : ISODate("2020-01-07T14:45:13Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:45:20.179Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:45:21.168Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 545,
"optime" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"optimeDurable" : {
"ts" : Timestamp(1578408313, 1),
"t" : NumberLong(9)
},
"optimeDate" : ISODate("2020-01-07T14:45:13Z"),
"optimeDurableDate" : ISODate("2020-01-07T14:45:13Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:45:20.179Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:45:20.177Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 5,
"lastHeartbeat" : ISODate("2020-01-07T14:45:20.044Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:45:20.036Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"operationTime" : Timestamp(1578408313, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578408313, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.isMaster()
{
"hosts" : [ #标准节点
"127.0.0.1:27017",
"127.0.0.1:27018"
],
"passives" : [ #被动节点
"127.0.0.1:27019"
],
"arbiters" : [ #仲裁节点
"127.0.0.1:27020"
],
"setName" : "crushlinux",
"setVersion" : 6,
"ismaster" : true,
"secondary" : false,
"primary" : "127.0.0.1:27017",
"me" : "127.0.0.1:27017",
"electionId" : ObjectId("7fffffff0000000000000009"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1578408453, 1),
"t" : NumberLong(9)
},
"lastWriteDate" : ISODate("2020-01-07T14:47:33Z"),
"majorityOpTime" : {
"ts" : Timestamp(1578408453, 1),
"t" : NumberLong(9)
},
"majorityWriteDate" : ISODate("2020-01-07T14:47:33Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2020-01-07T14:47:38.355Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 7,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1578408453, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578408453, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 stop #模拟mongodb1故障
killing process with pid: 71029
[root@mongodb ~]# mongo --port 27018
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T14:49:35.115Z"),
"myState" : 1,
"term" : NumberLong(10),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578408543, 1),
"t" : NumberLong(9)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578408543, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1578408571, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1578408571, 1),
"t" : NumberLong(10)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:49:33.749Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:49:08.548Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 802,
"optime" : {
"ts" : Timestamp(1578408571, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2020-01-07T14:49:31Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578408559, 1),
"electionDate" : ISODate("2020-01-07T14:49:19Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 798,
"optime" : {
"ts" : Timestamp(1578408571, 1),
"t" : NumberLong(10)
},
"optimeDurable" : {
"ts" : Timestamp(1578408571, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2020-01-07T14:49:31Z"),
"optimeDurableDate" : ISODate("2020-01-07T14:49:31Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:49:33.734Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:49:34.123Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 257,
"lastHeartbeat" : ISODate("2020-01-07T14:49:33.734Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:49:34.443Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"operationTime" : Timestamp(1578408571, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578408571, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 stop #模拟mongodb2故障
killing process with pid: 71085
[root@mongodb ~]# mongo --port 27019
crushlinux:SECONDARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T14:50:44.624Z"),
"myState" : 2,
"term" : NumberLong(10),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578408543, 1),
"t" : NumberLong(9)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578408543, 1),
"t" : NumberLong(9)
},
"appliedOpTime" : {
"ts" : Timestamp(1578408611, 1),
"t" : NumberLong(10)
},
"durableOpTime" : {
"ts" : Timestamp(1578408611, 1),
"t" : NumberLong(10)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:50:44.459Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:49:08.548Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:50:44.459Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:50:09.782Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Error connecting to 127.0.0.1:27018 :: caused by :: Connection refused",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY", #被动节点不能成为活跃节点
"uptime" : 869,
"optime" : {
"ts" : Timestamp(1578408611, 1),
"t" : NumberLong(10)
},
"optimeDate" : ISODate("2020-01-07T14:50:11Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 328,
"lastHeartbeat" : ISODate("2020-01-07T14:50:44.301Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:50:44.559Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"operationTime" : Timestamp(1578408611, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578408611, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 start
about to fork child process, waiting until server is ready for connections.
forked process: 71593
child process started successfully, parent exiting
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 start
about to fork child process, waiting until server is ready for connections.
forked process: 71657
child process started successfully, parent exiting
[root@mongodb ~]# mongo
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T14:52:08.708Z"),
"myState" : 1,
"term" : NumberLong(11),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578408728, 1),
"t" : NumberLong(11)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578408728, 1),
"t" : NumberLong(11)
},
"appliedOpTime" : {
"ts" : Timestamp(1578408728, 1),
"t" : NumberLong(11)
},
"durableOpTime" : {
"ts" : Timestamp(1578408728, 1),
"t" : NumberLong(11)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 32,
"optime" : {
"ts" : Timestamp(1578408728, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2020-01-07T14:52:08Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578408707, 1),
"electionDate" : ISODate("2020-01-07T14:51:47Z"),
"configVersion" : 6,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 28,
"optime" : {
"ts" : Timestamp(1578408718, 1),
"t" : NumberLong(11)
},
"optimeDurable" : {
"ts" : Timestamp(1578408718, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2020-01-07T14:51:58Z"),
"optimeDurableDate" : ISODate("2020-01-07T14:51:58Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:52:07.263Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:52:07.298Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 32,
"optime" : {
"ts" : Timestamp(1578408718, 1),
"t" : NumberLong(11)
},
"optimeDurable" : {
"ts" : Timestamp(1578408718, 1),
"t" : NumberLong(11)
},
"optimeDate" : ISODate("2020-01-07T14:51:58Z"),
"optimeDurableDate" : ISODate("2020-01-07T14:51:58Z"),
"lastHeartbeat" : ISODate("2020-01-07T14:52:07.263Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:52:07.156Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27017",
"syncSourceHost" : "127.0.0.1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 6
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 32,
"lastHeartbeat" : ISODate("2020-01-07T14:52:07.263Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T14:52:08.690Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 6
}
],
"ok" : 1,
"operationTime" : Timestamp(1578408728, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578408728, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.conf()
{
"_id" : "crushlinux",
"version" : 6,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 100,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "127.0.0.1:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 100,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "127.0.0.1:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "127.0.0.1:27020",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5e144406b7cfc8347d386f8a")
}
}
crushlinux:PRIMARY> use local
switched to db local
crushlinux:PRIMARY> show collections
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
crushlinux:PRIMARY> db.system.replset.findOne()
{
"_id" : "crushlinux",
"version" : 6,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 100,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "127.0.0.1:27018",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 100,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "127.0.0.1:27019",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "127.0.0.1:27020",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5e144406b7cfc8347d386f8a")
}
}
配置从节点可以读取数据
[root@mongodb ~]# mongo --port 27018
crushlinux:SECONDARY> show dbs
2020-01-07T22:57:46.974+0800 E QUERY [js] Error: listDatabases failed:{
"operationTime" : Timestamp(1578409058, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1578409058, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:139:1
shellHelper.show@src/mongo/shell/utils.js:882:13
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
crushlinux:SECONDARY> rs.slaveOk() #使从节点可以读取数据
crushlinux:SECONDARY> show dbs
admin 0.078GB
config 0.078GB
local 2.077GB
crushlinux:SECONDARY> rs.printSlaveReplicationInfo()
source: 127.0.0.1:27018
syncedTo: Tue Jan 07 2020 22:59:18 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
source: 127.0.0.1:27019
syncedTo: Tue Jan 07 2020 22:59:18 GMT+0800 (CST)
0 secs (0 hrs) behind the primary
crushlinux:SECONDARY> rs.printReplicationInfo()
configured oplog size: 1989.61328125MB
log length start to end: 22730secs (6.31hrs)
oplog first event time: Tue Jan 07 2020 16:40:38 GMT+0800 (CST)
oplog last event time: Tue Jan 07 2020 22:59:28 GMT+0800 (CST)
now: Tue Jan 07 2020 22:59:33 GMT+0800 (CST)
5、复制集管理
配置oplog文件大小
生产常见中oplog文件默认大小是不能满足频繁的更新业务需求的,在配置复制集启动时,就应该针对oplog有大小预计,旧版本修改oplog大小必须重启主数据库。
[root@mongodb ~]# mongo
crushlinux:PRIMARY> use local
switched to db local
crushlinux:PRIMARY> show collections
oplog.rs
replset.election
replset.minvalid
replset.oplogTruncateAfterPoint
startup_log
crushlinux:PRIMARY> db.oplog.rs.stats()
{
"ns" : "local.oplog.rs",
"size" : 152968,
"count" : 1341,
"avgObjSize" : 114,
"numExtents" : 2,
"storageSize" : 2412138480,
"lastExtentSize" : 266002432,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibil
ity only.", "userFlags" : 1,
"capped" : true,
"max" : NumberLong("9223372036854775807"),
"maxSize" : 2412429296,
"nindexes" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"ok" : 1,
"operationTime" : Timestamp(1578409448, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578409448, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
修改oplog的大小:100MB
crushlinux:PRIMARY> db.runCommand({"convertToCapped":"oplog.rs","size":102400000})
{
"ok" : 1,
"operationTime" : Timestamp(1578410396, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578410396, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
crushlinux:PRIMARY> rs.printReplicationInfo()
configured oplog size: 97.65625MB
log length start to end: 124secs (0.03hrs)
oplog first event time: Tue Jan 07 2020 23:17:52 GMT+0800 (CST)
oplog last event time: Tue Jan 07 2020 23:19:56 GMT+0800 (CST)
now: Tue Jan 07 2020 23:20:00 GMT+0800 (CST)
crushlinux:PRIMARY> db.oplog.rs.stats()
{
"ns" : "local.oplog.rs",
"size" : 2212,
"count" : 16,
"avgObjSize" : 138,
"numExtents" : 1,
"storageSize" : 102400000,
"lastExtentSize" : 102400000,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : true,
"max" : NumberLong("9223372036854775807"),
"maxSize" : 102400000,
"nindexes" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"ok" : 1,
"operationTime" : Timestamp(1578410396, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578410396, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
部署认证的复制
复制集群以密钥认证,其他登录需要密码认证
[root@mongodb ~]# mongo
crushlinux:PRIMARY> use admin
switched to db admin
crushlinux:PRIMARY> db.createUser({"user":"root","pwd":"123456","roles":["root"]})
Successfully added user: { "user" : "root", "roles" : [ "root" ] }
[root@mongodb ~]# cat << END >> /usr/local/mongodb/conf/mongodb1.conf
clusterAuthMode=keyFile
keyFile=/usr/local/mongodb/conf/crushlinuxkey
END
[root@mongodb ~]# cat << END >> /usr/local/mongodb/conf/mongodb2.conf
clusterAuthMode=keyFile
keyFile=/usr/local/mongodb/conf/crushlinuxkey
END
[root@mongodb ~]# cat << END >> /usr/local/mongodb/conf/mongodb3.conf
clusterAuthMode=keyFile
keyFile=/usr/local/mongodb/conf/crushlinuxkey
END
[root@mongodb ~]# cat << END >> /usr/local/mongodb/conf/mongodb4.conf
clusterAuthMode=keyFile
keyFile=/usr/local/mongodb/conf/crushlinuxkey
END
[root@mongodb ~]# echo "123456 key" > /usr/local/mongodb/conf/crushlinuxkey
[root@mongodb ~]# chmod 600 /usr/local/mongodb/conf/crushlinuxkey
[root@mongodb ~]# /etc/init.d/mongodb mongodb1 restart
killing process with pid: 72622
about to fork child process, waiting until server is ready for connections.
forked process: 72860
child process started successfully, parent exiting
[root@mongodb ~]# /etc/init.d/mongodb mongodb2 restart
killing process with pid: 72717
about to fork child process, waiting until server is ready for connections.
forked process: 72951
child process started successfully, parent exiting
[root@mongodb ~]# /etc/init.d/mongodb mongodb3 restart
killing process with pid: 72325
about to fork child process, waiting until server is ready for connections.
forked process: 73039
child process started successfully, parent exiting
[root@mongodb ~]# /etc/init.d/mongodb mongodb4 restart
killing process with pid: 72351
about to fork child process, waiting until server is ready for connections.
forked process: 73121
child process started successfully, parent exiting
[root@mongodb ~]# mongo --port 27018
crushlinux:PRIMARY> rs.status()
{
"operationTime" : Timestamp(1578411059, 1),
"ok" : 0,
"errmsg" : "command replSetGetStatus requires authentication",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1578411059, 1),
"signature" : {
"hash" : BinData(0,"l1lJ3gzWNN8WxBxzTeoa24PlLYU="),
"keyId" : NumberLong("6779220553745039361")
}
}
}
crushlinux:PRIMARY> use admin
switched to db admin
crushlinux:PRIMARY> db.auth("root","123456")
1
crushlinux:PRIMARY> rs.status()
{
"set" : "crushlinux",
"date" : ISODate("2020-01-07T15:31:45.135Z"),
"myState" : 1,
"term" : NumberLong(4),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"appliedOpTime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"durableOpTime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
}
},
"members" : [
{
"_id" : 0,
"name" : "127.0.0.1:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 117,
"optime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-07T15:31:39Z"),
"optimeDurableDate" : ISODate("2020-01-07T15:31:39Z"),
"lastHeartbeat" : ISODate("2020-01-07T15:31:44.371Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T15:31:44.395Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 1,
"name" : "127.0.0.1:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 118,
"optime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-07T15:31:39Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578410998, 1),
"electionDate" : ISODate("2020-01-07T15:29:58Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "127.0.0.1:27019",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 113,
"optime" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"optimeDurable" : {
"ts" : Timestamp(1578411099, 1),
"t" : NumberLong(4)
},
"optimeDate" : ISODate("2020-01-07T15:31:39Z"),
"optimeDurableDate" : ISODate("2020-01-07T15:31:39Z"),
"lastHeartbeat" : ISODate("2020-01-07T15:31:44.371Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T15:31:44.771Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "127.0.0.1:27018",
"syncSourceHost" : "127.0.0.1:27018",
"syncSourceId" : 1,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 3,
"name" : "127.0.0.1:27020",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 109,
"lastHeartbeat" : ISODate("2020-01-07T15:31:44.371Z"),
"lastHeartbeatRecv" : ISODate("2020-01-07T15:31:43.364Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578411099, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578411099, 1),
"signature" : {
"hash" : BinData(0,"QC+j7Z45z1wdrXX8RLuYO4y5ccI="),
"keyId" : NumberLong("6779220553745039361")
}
}
}
七、部署分片服务器
1、分片简介
分片也属于MongoDB集群技术,分片目的为了突破单点数据库服务器的I/O能力限制,对数据库存储进行水平扩展,满足MongoDB数据量大量增长的需求。严格地说,每一个服务器或者实例或者复制集就是一个分片。
MongoDB处理存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量。这时,我们就可以通过在多台机器上分割数据,使得数据库系统能存储和处理更多的数据。
2、分片优势
提供类似现行增长架构
提高数据可用性
提高大型数据库查询服务器性能
单个副本集限制在12个节点
垂直扩展价格昂贵
3、分片应用环境
单点数据库服务器存储成为瓶颈
单点数据库服务器的性能成为瓶颈
大型应用分散数据库已充分利用内存
4、分片架构及组件
分片三个主要组件:
Shard:
用于存储实际的数据块,实际生产环境中一个shard server角色可由几台机器组一个replicaSet承担,防止主机单点故障。
Config Server:
mongod实例,存储了整个 Cluster Metadata(元数据),其中包括 chunk(块)信息。
Query Routers:
前端路由,客户端由此接入,且让整个集群看上去像单一数据库,前端应用可以透明使用。
生成环境实现分片,至少三台路由实例,至少三台配置实例,每一分片都是一个副本集。
5、MongoDB分片集群部署
1、所有主机关闭防火墙及Selinux
2、操作系统采用Centos7.x、MongoDB-4.0.6
3、主机及端口规划如下:
IP地址 路由服务端口 配置服务端口 分片1端口 分片2端口 分片3端口
192.168.200.111 27017 27018 27001 27002 27003
192.168.200.112 27017 27018 27001 27002 27003
192.168.200.113 27017 27018 27001 27002 27003
4、拓扑架构说明
三台机器的配置服务(27018)形成配置服务的复制集,分片1、2、3也在各机器都部署一个实例,它们之间形成复制集,客户端直接连接3个路由服务与之交互,配置服务和分片服务对客户端是透明的。
5、下载部署MongoDB(3台服务器执行相同操作)
设置主机名关闭防火墙和selinux
[root@localhost ~]# hostname mongodb1
[root@localhost ~]# bash
[root@mongodb1 ~]# iptables -F
[root@mongodb1 ~]# setenforce 0
[root@mongodb1 ~]# systemctl stop firewalld
解压到/usr/local/mongodb,设置环境变量:
[root@mongodb1 ~]# tar xf mongodb-linux-x86_64-rhel70-4.0.6.tgz
[root@mongodb1 ~]# mv mongodb-linux-x86_64-rhel70-4.0.6 /usr/local/mongodb
[root@mongodb1 ~]# cat << END >> /etc/profile
export PATH=$PATH:/usr/local/mongodb/bin/
END
[root@mongodb1 ~]# source /etc/profile
6、创建路由、配置、日志、分片等的相关目录与文件(3台服务器执行相同操作)
创建配置文件存放目录: /usr/local/mongodb/conf
各类日志存放目录: /usr/local/mongodb/logs/
配置服务数据存放目录: /usr/local/mongodb/data/config
分片1服务数据存放目录: /usr/local/mongodb/data/shard1
分片2服务数据存放目录: /usr/local/mongodb/data/shard2
分片3服务数据存放目录: /usr/local/mongodb/data/shard3
配置服务日志存放文件: /usr/local/mongodb/logs/config.log
路由服务日志存放文件: /usr/local/mongodb/logs/mongos.log
分片1服务日志存放文件: /usr/local/mongodb/logs/shard1.log
分片2服务日志存放文件: /usr/local/mongodb/logs/shard2.log
分片3服务日志存放文件: /usr/local/mongodb/logs/shard3.log
mkdir -p /usr/local/mongodb/conf
mkdir -p /usr/local/mongodb/logs/
mkdir -p /usr/local/mongodb/data/config
mkdir -p /usr/local/mongodb/data/shard1
mkdir -p /usr/local/mongodb/data/shard2
mkdir -p /usr/local/mongodb/data/shard3
touch /usr/local/mongodb/logs/config.log
touch /usr/local/mongodb/logs/mongos.log
touch /usr/local/mongodb/logs/shard1.log
touch /usr/local/mongodb/logs/shard2.log
touch /usr/local/mongodb/logs/shard3.log
7、准备配置实例配置文件
在/usr/local/mongodb/conf/目录创建config.conf配置文件(3台服务器执行相同操作)
[root@mongodb1 ~]# cat << END > /usr/local/mongodb/conf/config.conf
dbpath=/usr/local/mongodb/data/config
logpath=/usr/local/mongodb/logs/config.log
port=27018
logappend=true
fork=true
maxConns=5000
replSet=configs
configsvr=true
bind_ip=0.0.0.0
END
[root@mongodb1 ~]# scp /usr/local/mongodb/conf/config.conf 192.168.200.112:/usr/local/mongodb/conf/config.conf
[root@mongodb1 ~]# scp /usr/local/mongodb/conf/config.conf 192.168.200.113:/usr/local/mongodb/conf/config.conf
8、三台服务器分别启动配置服务实例(3台服务器执行相同操作)
[root@mongodb1 ~]# mongod -f /usr/local/mongodb/conf/config.conf
[root@mongodb1 ~]# netstat -lnpt | grep 27018
tcp 0 0 0.0.0.0:27018 0.0.0.0:* LISTEN 1683/mongod
9、连接mongo构建配置服务复制集
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27018
切换数据库
> use admin
switched to db admin
初始化复制集
> rs.initiate({_id:"configs",members:[{_id:0,host:"192.168.200.111:27018"},{_id:1,host:"192.168.200.112:27018"}, {_id:2,host:"192.168.200.113:27018"}]})
{
"ok" : 1,
"operationTime" : Timestamp(1578475216, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1578475216, 1),
"electionId" : ObjectId("000000000000000000000000")
},
"lastCommittedOpTime" : Timestamp(0, 0),
"$clusterTime" : {
"clusterTime" : Timestamp(1578475216, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
其中_id:"configs"是上面config.conf配置文件里的复制集名称,此命令目的是把三台服务器的配置服务组成复制集。
查看状态
configs:PRIMARY> rs.status()
{
"set" : "configs",
"date" : ISODate("2020-01-08T09:21:58.278Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
}
},
"lastStableCheckpointTimestamp" : Timestamp(1578475289, 1),
"members" : [
{
"_id" : 0,
"name" : "192.168.200.111:27018",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 232,
"optime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:21:49Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578475228, 1),
"electionDate" : ISODate("2020-01-08T09:20:28Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.200.112:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 101,
"optime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:21:49Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:21:49Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:21:56.344Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:21:57.073Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27018",
"syncSourceHost" : "192.168.200.111:27018",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.200.113:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 101,
"optime" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578475309, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:21:49Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:21:49Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:21:56.346Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:21:57.045Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27018",
"syncSourceHost" : "192.168.200.111:27018",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578475309, 1),
"$gleStats" : {
"lastOpTime" : Timestamp(1578475216, 1),
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1578475309, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578475309, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
三台机器的配置服务就已形成复制集,其中1台为PRIMARY,其他2台为SECONDARY。 10、准备分片服务的相关文件
在/usr/local/mongodb/conf/目录下创建shard1.conf、shard2.conf、shard3.conf分片实例配置文件(3台服务器执行相同操作)
[root@mongodb1 ~]# vim /usr/local/mongodb/conf/shard1.conf
dbpath=/usr/local/mongodb/data/shard1 //其他实例修改数据目录位置
logpath=/usr/local/mongodb/logs/shard1.log //其他实例修改日志文件位置
port=27001 //其他实例修改监听端口
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
replSet=shard1 //其他实例修改分片名称
bind_ip=0.0.0.0
[root@mongodb1 ~]# vim /usr/local/mongodb/conf/shard2.conf
dbpath=/usr/local/mongodb/data/shard2
logpath=/usr/local/mongodb/logs/shard2.log
port=27002
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
replSet=shard2
bind_ip=0.0.0.0
[root@mongodb1 ~]# vim /usr/local/mongodb/conf/shard3.conf
dbpath=/usr/local/mongodb/data/shard3
logpath=/usr/local/mongodb/logs/shard3.log
port=27003
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true
replSet=shard3
bind_ip=0.0.0.0
[root@mongodb1 ~]# scp /usr/local/mongodb/conf/shard{1,2,3}.conf 192.168.200.112:/usr/local/mongodb/conf/
[root@mongodb1 ~]# scp /usr/local/mongodb/conf/shard{1,2,3}.conf 192.168.200.113:/usr/local/mongodb/conf/
以上分片实例端口分别是27001、27002、27003,分别对应了shard1.conf、shard2.conf、shard3.conf等文件。稍后会在3台机器的相同端口上形成一个分片服务的复制集,由于3台机器都需要这3个文件,所以根据这9个配置文件分别启动分片服务
[root@mongodb1 ~]# mongod -f /usr/local/mongodb/conf/shard1.conf
[root@mongodb1 ~]# mongod -f /usr/local/mongodb/conf/shard2.conf
[root@mongodb1 ~]# mongod -f /usr/local/mongodb/conf/shard3.conf
[root@mongodb1 ~]# netstat -lnpt | grep -E "2700[1-3]"
tcp 0 0 0.0.0.0:27001 0.0.0.0:* LISTEN 1907/mongod
tcp 0 0 0.0.0.0:27002 0.0.0.0:* LISTEN 1946/mongod
tcp 0 0 0.0.0.0:27003 0.0.0.0:* LISTEN 1971/mongod
11、将分片配置为复制集
连接mongo,只需在任意一台机器执行即可,分别连接到27001、27002、27003的端口进行复制集配置
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27001
切换数据库
> use admin
switched to db admin
初始化复制集
> rs.initiate({_id:"shard1",members:[{_id:0,host:"192.168.200.111:27001"},{_id:1,host:"192.168.200.112:27001"},{_id:2,host:"192.168.200.113:27001"}]})
{
"ok" : 1,
"operationTime" : Timestamp(1578475912, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578475912, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shard1:PRIMARY> rs.status()
{
"set" : "shard1",
"date" : ISODate("2020-01-08T09:32:54.239Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.200.111:27001",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 399,
"optime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:32:44Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578475922, 1),
"electionDate" : ISODate("2020-01-08T09:32:02Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.200.112:27001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 61,
"optime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:32:44Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:32:44Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:32:52.657Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:32:53.658Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27001",
"syncSourceHost" : "192.168.200.111:27001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.200.113:27001",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 61,
"optime" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578475964, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:32:44Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:32:44Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:32:52.650Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:32:52.870Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27001",
"syncSourceHost" : "192.168.200.111:27001",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578475964, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578475964, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27002
切换数据库
> use admin
switched to db admin
初始化复制集
> rs.initiate({_id:"shard2",members:[{_id:0,host:"192.168.200.111:27002"},{_id:1,host:"192.168.200.112:27002"},{_id:2,host:"192.168.200.113:27002"}]})
{
"ok" : 1,
"operationTime" : Timestamp(1578476138, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476138, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shard2:PRIMARY> rs.status()
{
"set" : "shard2",
"date" : ISODate("2020-01-08T09:36:06.441Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.200.111:27002",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 570,
"optime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:36:00Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578476148, 1),
"electionDate" : ISODate("2020-01-08T09:35:48Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.200.112:27002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 27,
"optime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:36:00Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:36:00Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:36:04.766Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:36:05.711Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27002",
"syncSourceHost" : "192.168.200.111:27002",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.200.113:27002",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 27,
"optime" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578476160, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:36:00Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:36:00Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:36:04.768Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:36:05.718Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27002",
"syncSourceHost" : "192.168.200.111:27002",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578476160, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476160, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27003
切换数据库
> use admin
switched to db admin
初始化复制集
> rs.initiate({_id:"shard3",members:[{_id:0,host:"192.168.200.111:27003"},{_id:1,host:"192.168.200.112:27003"},{_id:2,host:"192.168.200.113:27003"}]})
{
"ok" : 1,
"operationTime" : Timestamp(1578476243, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476243, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
shard3:PRIMARY> rs.status()
{
"set" : "shard3",
"date" : ISODate("2020-01-08T09:37:43.421Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.200.111:27003",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 654,
"optime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:37:36Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1578476254, 1),
"electionDate" : ISODate("2020-01-08T09:37:34Z"),
"configVersion" : 1,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "192.168.200.112:27003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 20,
"optime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:37:36Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:37:36Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:37:42.565Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:37:43.370Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27003",
"syncSourceHost" : "192.168.200.111:27003",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.200.113:27003",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 20,
"optime" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1578476256, 2),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2020-01-08T09:37:36Z"),
"optimeDurableDate" : ISODate("2020-01-08T09:37:36Z"),
"lastHeartbeat" : ISODate("2020-01-08T09:37:42.570Z"),
"lastHeartbeatRecv" : ISODate("2020-01-08T09:37:43.405Z"),
"pingMs" : NumberLong(1),
"lastHeartbeatMessage" : "",
"syncingTo" : "192.168.200.111:27003",
"syncSourceHost" : "192.168.200.111:27003",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1578476256, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476256, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
让3个分片各自形成1主2从的复制集,注意端口及仲裁节点的问题即可,操作完成后3个分片都启动完成,并完成复制集模式。
12、路由服务部署(3台服务器执行相同操作)
在/usr/local/mongodb/conf/目录下创建mongos.conf路由实例文件
[root@mongodb1 ~]# cat << END > /usr/local/mongodb/conf/mongos.conf
logpath=/usr/local/mongodb/logs/mongos.log
logappend = true
port = 27017
fork = true
configdb = configs/192.168.200.111:27018,192.168.200.112:27018,192.168.200.113:27018
maxConns=20000
bind_ip=0.0.0.0
END
[root@mongodb1 ~]# scp /usr/local/mongodb/conf/mongos.conf 192.168.200.112:/usr/local/mongodb/conf/
[root@mongodb1 ~]# scp /usr/local/mongodb/conf/mongos.conf 192.168.200.113:/usr/local/mongodb/conf/
13、启动mongos
分别在三台服务器启动:
[root@mongodb1 ~]# mongos -f /usr/local/mongodb/conf/mongos.conf
[root@mongodb1 ~]# netstat -lnpt | grep 27017
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 2378/mongos
14、启动分片功能
连接mongo:
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27017
切换数据库:
mongos> use admin
switched to db admin
添加分片,只需在一台机器执行即可:
mongos> sh.addShard("shard1/192.168.200.111:27001,192.168.200.112:27001,192.168.200.113:27001")
{
"shardAdded" : "shard1",
"ok" : 1,
"operationTime" : Timestamp(1578476761, 8),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476761, 8),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shard2/192.168.200.111:27002,192.168.200.112:27002,192.168.200.113:27002")
{
"shardAdded" : "shard2",
"ok" : 1,
"operationTime" : Timestamp(1578476769, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476769, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.addShard("shard3/192.168.200.111:27003,192.168.200.112:27003,192.168.200.113:27003")
{
"shardAdded" : "shard3",
"ok" : 1,
"operationTime" : Timestamp(1578476776, 6),
"$clusterTime" : {
"clusterTime" : Timestamp(1578476776, 6),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
查看集群状态:
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e167713d3eb45c6d793e5a9")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.200.111:27001,192.168.200.112:27001,192.168.200.113:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.200.111:27002,192.168.200.112:27002,192.168.200.113:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.200.111:27003,192.168.200.112:27003,192.168.200.113:27003", "state" : 1 }
most recently active mongoses:
"4.0.6" : 3
autosplit:
Currently enabled: yes
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:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
15、测试分片功能
设置分片chunk大小
mongos> use config
switched to db config
设置块大小为1M测试不然需要插入海量数据
mongos> db.setting.save({"_id":"chunksize","value":1})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
模拟写入数据
mongos> use cloud
switched to db cloud
模拟往cloud数据库的user表写入10000数据
mongos> for(i=1;i<=10000;i++){db.user.insert({"id":i,"name":"liangge"+i})}
WriteResult({ "nInserted" : 1 })
启用数据库分片
mongos> sh.enableSharding("cloud")
{
"ok" : 1,
"operationTime" : Timestamp(1578532217, 3),
"$clusterTime" : {
"clusterTime" : Timestamp(1578532217, 3),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
16、创建索引,对表进行分片
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e167713d3eb45c6d793e5a9")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.200.111:27001,192.168.200.112:27001,192.168.200.113:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.200.111:27002,192.168.200.112:27002,192.168.200.113:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.200.111:27003,192.168.200.112:27003,192.168.200.113:27003", "state" : 1 }
most recently active mongoses:
"4.0.6" : 3
autosplit:
Currently enabled: yes
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:
{ "_id" : "cloud", "primary" : "shard2", "partitioned" : true, "version" : { "uuid" : UUID("8c7ddd50-c8df-4421-ac29-39fda903b3e3")
, "lastMod" : 1 } } { "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
mongos> db.user.createIndex({"id":1}) #以"id"作为索引
{
"raw" : {
"shard2/192.168.200.111:27002,192.168.200.112:27002,192.168.200.113:27002" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1578477241, 2),
"$clusterTime" : {
"clusterTime" : Timestamp(1578477241, 2),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.shardCollection("cloud.user",{"id":1}) #根据"id"对user表进行分片
{
"collectionsharded" : "cloud.user",
"collectionUUID" : UUID("00911eae-5adc-47a8-83af-8d091036f17c"),
"ok" : 1,
"operationTime" : Timestamp(1578477472, 13),
"$clusterTime" : {
"clusterTime" : Timestamp(1578477472, 13),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.status() #查看分片情况
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e167713d3eb45c6d793e5a9")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.200.111:27001,192.168.200.112:27001,192.168.200.113:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.200.111:27002,192.168.200.112:27002,192.168.200.113:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.200.111:27003,192.168.200.112:27003,192.168.200.113:27003", "state" : 1 }
most recently active mongoses:
"4.0.6" : 3
autosplit:
Currently enabled: yes
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:
{ "_id" : "cloud", "primary" : "shard2", "partitioned" : true, "version" : { "uuid" : UUID("8c7ddd50-c8df-4421-ac29-39fda903b3e3")
, "lastMod" : 1 } } cloud.user
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard2 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
到此,MongoDB分布式分片集群已经部署完毕。
导入一组数据 sales.txt
[root@mongodb1 ~]# mongoimport -d study -c sales -f id,num,pid,price --file sales.txt --type=csv
2020-01-09T17:31:31.864+0800 connected to: localhost
2020-01-09T17:31:34.861+0800 [#############...........] study.sales 1.28MB/2.32MB (55.3%)
2020-01-09T17:31:36.986+0800 [#######################.] study.sales 2.32MB/2.32MB (100.0%)
2020-01-09T17:31:36.986+0800 imported 121317 documents
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27017
mongos> use study
switched to db study
mongos> db.sales.find().limit(3)
{ "_id" : ObjectId("5e16f2f35c6e96bdd5fccd06"), "id" : 1, "num" : 1, "pid" : 776, "price" : 2024.994 }
{ "_id" : ObjectId("5e16f2f35c6e96bdd5fccd07"), "id" : 2, "num" : 3, "pid" : 777, "price" : 2024.994 }
{ "_id" : ObjectId("5e16f2f35c6e96bdd5fccd08"), "id" : 3, "num" : 1, "pid" : 778, "price" : 2024.994 }
创建id列为索引
mongos> db.sales.createIndex({"id":1})
{
"raw" : {
"shard3/192.168.200.111:27003,192.168.200.112:27003,192.168.200.113:27003" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1578562384, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578562384, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
对sales表基于id列分片
mongos> sh.shardCollection("study.sales",{"id":1})
{
"ok" : 0,
"errmsg" : "sharding not enabled for db study",
"code" : 20,
"codeName" : "IllegalOperation",
"operationTime" : Timestamp(1578562423, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1578562423, 4),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5e167713d3eb45c6d793e5a9")
}
shards:
{ "_id" : "shard1", "host" : "shard1/192.168.200.111:27001,192.168.200.112:27001,192.168.200.113:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.200.111:27002,192.168.200.112:27002,192.168.200.113:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.200.111:27003,192.168.200.112:27003,192.168.200.113:27003", "state" : 1 }
most recently active mongoses:
"4.0.6" : 3
autosplit:
Currently enabled: yes
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:
{ "_id" : "cloud", "primary" : "shard2", "partitioned" : true, "version" : { "uuid" : UUID("8c7ddd50-c8df-4421-ac29-39fda903b3e3")
, "lastMod" : 1 } } cloud.sales
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard2 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)
cloud.user
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard2 1
{ "id" : { "$minKey" : 1 } } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 0)
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard1 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 0)
{ "_id" : "study", "primary" : "shard3", "partitioned" : false, "version" : { "uuid" : UUID("211f2fb8-f42e-4fe3-90e6-ffe199616eb5"
), "lastMod" : 1 } }
mongos> db.sales.stats()
{
"sharded" : false,
"primary" : "shard3",
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
"capped" : false,
"ns" : "study.sales",
"count" : 121317,
"numExtents" : 7,
"size" : 13587504,
"storageSize" : 22507520,
"totalIndexSize" : 7023184,
"indexSizes" : {
"_id_" : 3949008,
"id_1" : 3074176
},
"avgObjSize" : 112,
"maxSize" : NumberLong(0),
"nindexes" : 2,
"nchunks" : 1,
"shards" : {
"shard3" : {
"ns" : "study.sales",
"size" : 13587504,
"count" : 121317,
"avgObjSize" : 112,
"numExtents" : 7,
"storageSize" : 22507520,
"lastExtentSize" : 11325440,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility
only.", "userFlags" : 1,
"capped" : false,
"nindexes" : 2,
"indexDetails" : {
},
"totalIndexSize" : 7023184,
"indexSizes" : {
"_id_" : 3949008,
"id_1" : 3074176
},
"ok" : 1,
"operationTime" : Timestamp(1578562513, 1),
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1578562384, 1),
"t" : NumberLong(1)
},
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1578562513, 1),
"$configServerState" : {
"opTime" : {
"ts" : Timestamp(1578562512, 3),
"t" : NumberLong(1)
}
},
"$clusterTime" : {
"clusterTime" : Timestamp(1578562513, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
},
"ok" : 1,
"operationTime" : Timestamp(1578562513, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1578562513, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
分片几乎是平均分配,选择分片依据的列时,尽量选择不重复的列,不要选择例如性别这种只有两个的。
连接配置实例
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 27018
configs:PRIMARY> show dbs
admin 0.000GB
config 0.001GB
local 0.000GB
configs:PRIMARY> use config
switched to db config
configs:PRIMARY> show collections
changelog
chunks
collections
databases
lockpings
locks
migrations
mongos
setting
shards
tags
transactions
version
configs:PRIMARY> db.chunks.find() #查看块的信息
{ "_id" : "config.system.sessions-_id_MinKey", "ns" : "config.system.sessions", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : { "$ma
xKey" : 1 } }, "shard" : "shard1", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("5e167cc007699f06427ee744"), "history" : [ { "validAfter" : Timestamp(1578532032, 4), "shard" : "shard1" } ] }{ "_id" : "cloud.user-id_MinKey", "ns" : "cloud.user", "min" : { "id" : { "$minKey" : 1 } }, "max" : { "id" : { "$maxKey" : 1 } }, "shard" : "s
hard2", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("5e167dd99439d227a0354d08"), "history" : [ { "validAfter" : Timestamp(1578532313, 3), "shard" : "shard2" } ] }{ "_id" : "cloud.sales-id_MinKey", "ns" : "cloud.sales", "min" : { "id" : { "$minKey" : 1 } }, "max" : { "id" : { "$maxKey" : 1 } }, "shard" :
"shard2", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("5e16f3659439d227a036bbfd"), "history" : [ { "validAfter" : Timestamp(1578562405, 6), "shard" : "shard2" } ] }
分片管理
configs:PRIMARY> sh.addShard("127.0.0.1:27004")
configs:PRIMARY> db.runCommand({"removeshard":"127.0.0.1:27004"}) #删除分片节点
八、利用Nginx Stream代理路由实例
nginx从1.9.0版本开始,新增了ngx_stream_core_module模块,使nginx支持四层负载均衡。默认编译的时候该模块并未编译进去,需要编译的时候添加--with-stream,使其支持stream代理。
[root@mongodb1 ~]# yum -y install pcre-devel zlib-devel openssl-devel
[root@mongodb1 ~]# useradd -M -s /sbin/nologin nginx
[root@mongodb1 ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src/
[root@mongodb1 ~]# cd /usr/src/nginx-1.16.0/
[root@mongodb1 nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-stream && make && make install
stream段的配置要与http段在同级目录
[root@mongodb1 ~]# vim /usr/local/nginx/conf/nginx.conf
stream {
upstream mongodb {
server 192.168.200.111:27017 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.200.112:27017 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.200.113:27017 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 27017;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass mongodb;
}
}
[root@mongodb1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@mongodb1 ~]# /usr/local/nginx/sbin/nginx
[root@mongodb1 ~]# netstat -anpt | grep "17017"
tcp 0 0 0.0.0.0:17017 0.0.0.0:* LISTEN 67120/nginx: master
连接测试
[root@mongodb1 ~]# mongo --host 192.168.200.111 --port 17017