一、Mongdb3.0安装
官网下载最新稳定版本的mongodb软体
https://www.mongodb.org/downloads
解压软体
[root@test-weidangkou service]# tar zxvf mongodb-linux-x86_64-rhel62-3.0.3.tgz mongodb-linux-x86_64-rhel62-3.0.3/README mongodb-linux-x86_64-rhel62-3.0.3/THIRD-PARTY-NOTICES mongodb-linux-x86_64-rhel62-3.0.3/GNU-AGPL-3.0 mongodb-linux-x86_64-rhel62-3.0.3/bin/mongodump mongodb-linux-x86_64-rhel62-3.0.3/bin/mongorestore mongodb-linux-x86_64-rhel62-3.0.3/bin/mongoexport mongodb-linux-x86_64-rhel62-3.0.3/bin/mongoimport mongodb-linux-x86_64-rhel62-3.0.3/bin/mongostat mongodb-linux-x86_64-rhel62-3.0.3/bin/mongotop mongodb-linux-x86_64-rhel62-3.0.3/bin/bsondump mongodb-linux-x86_64-rhel62-3.0.3/bin/mongofiles mongodb-linux-x86_64-rhel62-3.0.3/bin/mongooplog mongodb-linux-x86_64-rhel62-3.0.3/bin/mongoperf mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod mongodb-linux-x86_64-rhel62-3.0.3/bin/mongos mongodb-linux-x86_64-rhel62-3.0.3/bin/mongo
创建mongdb数据存放路径及日志存放路径
[root@test-weidangkou service]# mkdir -p /date/{mongodbdata,mongodb_logs}
建立mongod账号,授权数据目录
[root@test-weidangkou date]# useradd mongod [root@test-weidangkou date]# chown -R mongod.mongod /date/mongodbdata/
创建mongod.conf配置文件,只需要以下几个选项即可
[root@test-weidangkou date]#cat /etc/mongod.conf dbpath = /data/mongodata #mongodb数据存放路径 logpath = /data/mongodb_logs/mongodb.log #mongodb日志存放路径 httpinterface = true #mongodb管理端口默认+1000:280 fork = true #后台启动
二、启动Mongodb初始化数据
(1)第一种直接在命令行中指定路径和日志
/usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod --dbpath=/date/mongodbdate/ --logpath=/date/mongodb_logs/mongodb.log --fork about to fork child process, waiting until server is ready for connections. forked process: 5238 child process started successfully, parent exiting 端口已经起来了: [root@test-weidangkou mongodb_logs]# lsof -i :27017 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mongod 5238 root 6u IPv4 41939 0t0 TCP *:27017 (LISTEN) 初始化由于要同步数据,建立本地的一些数据文件。会启动比较慢。
(2)第二种方式启动:通过加载配置文件启动(推荐)
[root@test-weidangkou mongodbdate]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 5382 child process started successfully, parent exiting
进入终端查看数据库
[root@test-weidangkou mongodb_logs]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongo MongoDB shell version: 3.0.3 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: 2015-06-30T17:37:53.882+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-06-30T17:37:53.882+0800 I CONTROL [initandlisten] 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] 2015-06-30T17:37:53.905+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-06-30T17:37:53.906+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-06-30T17:37:53.906+0800 I CONTROL [initandlisten] > show dbs; local 0.078GB
启动报错会出现以下:
[root@test-weidangkou mongodbdate]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongod -f /etc/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 5298
检查原因:
第一:由于之前没有正常的退出Mongodb服务,需要删除mongod.lock文件,再次启动恢复正常。
第二:配置文件有问题,需要检查配置文件是否有特殊字符,导致无法解析
三、mongodb副本集配置
配置mongodb的副本集务必要做好以下几点:
1、各个节点能够正常访问mongodb的socket端口,iptables要放行
2、SELINUX必须要关闭掉,以免禁止端口复制
3、各个服务器节点要做好解析/etc/hosts
4、由于副本集是通过Heartbeat心跳线连接到,务必同步好各个节点的时间
小注:本人由于之前没有注意到这些,被坑了好久。哈哈!!!
mongodb复制过程及概念
(1)主节点将数据修改操作保存至oplog,oplog:大小固定的文件,存储在local数据库
(2)初始同步(inital sync)
节点没有任何数据
节点丢失副本复制历史
:克隆所有的数据库
:应用数据集的所有改变:复制oplog并应用于本地
:为所有collection的构建索引
(3)回滚后追赶(post-rollback catch-up)
(4)切分块迁移(shareding chunk migrations)
local:存放了副本集的所有元素和oplog;用于存储oplog的是一个名为oplog.rs的collection;
oplog.rs的大小依赖于OS及文件系统;但可以自定义其大小oplogsize
1、环境配置
192.168.254.7 samba
192.168.254.229 git
192.168.254.9 weidangkou
3t台机器的配置文件修改vim /etc/mongodb.conf,增加两个选项
replIndexPrefetch = _id_only #索引的ID号复制 replSet = 9tong1 #副本集设置成9tong1
为了测试证明复制的数据,可以先在主节点上导入点数据,测试环境的数据如下:
[root@samba mongodata]# ls 9tong.0 9tong.1 9tong.2 9tongAPI.0 9tongAPI.1 9tongAPI.ns 9tong.ns base.0 base.1 base.ns journal local.0 local.ns mongod.lock storage.bson
2、初始化
> show dbs; 2015-07-01T11:38:18.324+0800 E QUERY Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at Error () at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47 配置好副本集选项启动后,不能直接查看dbs的情况,因为目前还没有初始化不是primary > rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "samba:27017", "ok" : 1 } 9tong1:OTHER> 9tong1:PRIMARY> 初始化后会直接变成9tong1:PRIMARY的标识,可以查看数据库的容量信息 9tong1:PRIMARY> show dbs; 9tong 0.453GB 9tongAPI 0.203GB base 0.203GB local 2.077GB
3、添加副本节点
9tong1:PRIMARY> rs.add rs.add( rs.addArb( 9tong1:PRIMARY> rs.add("192.168.254.9") #默认端口不需要加端口号码 9tong1:PRIMARY> rs.add("192.168.254.9") { "ok" : 1 } 9tong1:PRIMARY> rs.status() { "set" : "9tong1", "date" : ISODate("2015-07-01T04:04:51.535Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "samba:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1645, "optime" : Timestamp(1435723485, 1), "optimeDate" : ISODate("2015-07-01T04:04:45Z"), "electionTime" : Timestamp(1435722018, 2), "electionDate" : ISODate("2015-07-01T03:40:18Z"), "configVersion" : 2, "self" : true }, { "_id" : 1, "name" : "192.168.254.9:27017", "health" : 1, "state" : 5, "stateStr" : "STARTUP2", "uptime" : 6, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-07-01T04:04:51.261Z"), "lastHeartbeatRecv" : ISODate("2015-07-01T04:04:51.300Z"), "pingMs" : 2, "syncingTo" : "samba:27017", "configVersion" : 2 } ], "ok" : 1 StateStr:STARTUPU2是复制数据追赶主节点的状态模式。 数据同步好后会变成SECONDARY { "_id" : 1, "name" : "192.168.254.9:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 63, "optime" : Timestamp(1435723485, 1), "optimeDate" : ISODate("2015-07-01T04:04:45Z"), "lastHeartbeat" : ISODate("2015-07-01T04:05:47.313Z"), "lastHeartbeatRecv" : ISODate("2015-07-01T04:05:47.344Z"), "pingMs" : 1, "configVersion" : 2 } ], "ok" : 1 也可以通过rs.conf()查看各个节点状态 9tong1:PRIMARY> rs.conf() { "_id" : "9tong1", "version" : 2, "members" : [ { "_id" : 0, "host" : "samba:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "192.168.254.9:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } } 9tong1:PRIMARY> use 9tong; switched to db 9tong 9tong1:PRIMARY> db.user_contacts.find().count(); 171639 从节点上查看 9tong1:SECONDARY> show dbs; 9tong 0.203GB 9tongAPI 0.078GB base 0.078GB local 6.075GB test 0.078GB 9tong1:SECONDARY> use 9tong; switched to db 9tong 9tong1:SECONDARY> db.user_contacts.find().count(); 171639 以上证明数据已经同步过来,状态变成SECONDARY 查看OPOLOG状态 9tong1:PRIMARY> db.printReplicationInfo() configured oplog size: 1962.66015625MB log length start to end: 3053secs (0.85hrs) oplog first event time: Wed Jul 01 2015 11:40:18 GMT+0800 (CST) #第一次更新时间 oplog last event time: Wed Jul 01 2015 12:31:11 GMT+0800 (CST) #最后一次更新时间 now: Wed Jul 01 2015 13:30:25 GMT+0800 (CST) #现在时间 9tong1:SECONDARY> db.printReplicationInfo() configured oplog size: 4469.238250732422MB log length start to end: 1586secs (0.44hrs) oplog first event time: Wed Jul 01 2015 12:04:45 GMT+0800 (CST) oplog last event time: Wed Jul 01 2015 12:31:11 GMT+0800 (CST) #与主节点最近一次时间一 样说明数据同步完成了。 now: Wed Jul 01 2015 13:31:01 GMT+0800 (CST)
4、连接到从节点192.168.254.9查看状态
[root@weidangkou data]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongo MongoDB shell version: 3.0.3 connecting to: test Server has startup warnings: 2015-07-01T12:00:01.448+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-07-01T12:00:01.448+0800 I CONTROL [initandlisten] 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] 9tong1:SECONDARY> 已经变成副本集的SECONDARY状态。 查看数据库容量信息,会出现以下错误。 9tong1:SECONDARY> show dbs; 2015-07-01T12:14:14.768+0800 E QUERY Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } at Error () at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47 因为此时该节点不属于主节点,也没有默认为从节点。需要运行如下这个命令: 9tong1:SECONDARY> rs.slaveOk() 9tong1:SECONDARY> show dbs; 9tong 0.203GB 9tongAPI 0.078GB base 0.078GB local 6.075GB 也可以通过rs.isMaster()查看是否自己属于主节点 9tong1:SECONDARY> rs.isMaster() { "setName" : "9tong1", "setVersion" : 2, "ismaster" : false, "secondary" : true, "hosts" : [ "samba:27017", "192.168.254.9:27017" ], "primary" : "samba:27017", "me" : "192.168.254.9:27017", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-07-01T04:23:34.493Z"), "maxWireVersion" : 3, "minWireVersion" : 0, "ok" : 1 }
5、添加192.168.254.229成为仲裁节点。仲裁节点只有投票选举权,没有数据复制功能。
9tong1:PRIMARY> rs.addArb("192.168.254.229") { "ok" : 1 } 9tong1:PRIMARY> rs.status() { "set" : "9tong1", "date" : ISODate("2015-07-01T04:27:36.132Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "samba:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 3010, "optime" : Timestamp(1435724813, 1), "optimeDate" : ISODate("2015-07-01T04:26:53Z"), "electionTime" : Timestamp(1435722018, 2), "electionDate" : ISODate("2015-07-01T03:40:18Z"), "configVersion" : 3, "self" : true }, { "_id" : 1, "name" : "192.168.254.9:27017", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1370, "optime" : Timestamp(1435724813, 1), "optimeDate" : ISODate("2015-07-01T04:26:53Z"), "lastHeartbeat" : ISODate("2015-07-01T04:27:35.993Z"), "lastHeartbeatRecv" : ISODate("2015-07-01T04:27:34.470Z"), "pingMs" : 0, "syncingTo" : "samba:27017", "configVersion" : 3 }, { "_id" : 2, "name" : "192.168.254.229:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 42, "lastHeartbeat" : ISODate("2015-07-01T04:27:35.987Z"), "lastHeartbeatRecv" : ISODate("2015-07-01T04:27:35.986Z"), "pingMs" : 0, "configVersion" : 3 } ], "ok" : 1 }
6、测试主节点写数据,查看从节点是否有
主节点: 9tong1:PRIMARY> db.classes.insert({class: "ONE",nostu :40}) WriteResult({ "nInserted" : 1 }) 9tong1:PRIMARY> show collections; classes system.indexes 从节点查看: 9tong1:SECONDARY> db.classes.findOne() { "_id" : ObjectId("55936d0f24c674b1e83360d6"), "class" : "ONE", "nostu" : 40 } 注意,从节点没有写权限,插入数据后会报错 9tong1:SECONDARY> db.classes.insert({class: "TOW",nostu :50}) WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })
7、主从切换测试
要点:副本集的重新选举的影响条件:
心跳信息,heartbeat会每隔几秒检查连接状态信息
优先级,默认都是1,值越高优先级越高。值为0的节点是不能成为主节点的,也不能触发选举。
optime:主节点必须optime更新的时间要快于从节点是最新的,如果这个值小于线上的其他机器, 是不肯能成为主节点的。
网络连接票数超过法定票数一半
网络分区:票数选举
选举机制:
触发选举的事件
新副本集初始化时
从节点联系不到主节点时
主节点“下台”时
收到setpDown()命令时
某从节点有更高的优先级且已经满足其他所有条件
主节点无法联系到副本集的“多数方”
(1)关闭192.168.254.7-SAMBA,主节点的MongoDB服务
rs.status() { "set" : "9tong1", "date" : ISODate("2015-07-01T05:36:43.453Z"), "myState" : 1, "members" : [ { "_id" : 0, "name" : "samba:27017", "health" : 0, "state" : 8, "stateStr" : "(not reachable/healthy)", "uptime" : 0, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-07-01T05:36:42.029Z"), "lastHeartbeatRecv" : ISODate("2015-07-01T05:36:14.991Z"), "pingMs" : 0, "lastHeartbeatMessage" : "Failed attempt to connect to samba:27017; couldn't connect to server samba:27017 (192.168.254.7), connection attempt failed", "configVersion" : -1 }, { "_id" : 1, "name" : "192.168.254.9:27017", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 5916, "optime" : Timestamp(1435725071, 2), "optimeDate" : ISODate("2015-07-01T04:31:11Z"), "electionTime" : Timestamp(1435728977, 1), "electionDate" : ISODate("2015-07-01T05:36:17Z"), "configVersion" : 3, "self" : true }, { "_id" : 2, "name" : "192.168.254.229:27017", "health" : 1, "state" : 7, "stateStr" : "ARBITER", "uptime" : 4188, "lastHeartbeat" : ISODate("2015-07-01T05:36:43.328Z"), "lastHeartbeatRecv" : ISODate("2015-07-01T05:36:42.799Z"), "pingMs" : 0, "configVersion" : 3 } ], "ok" : 1 }
停止后,10秒左右的时间,明显的看到原来是主节点的SAMBA,监控状态变为0,1表示正常,原先192.168.254.9从SECONDARY变成了PRIMARY。
(2)更改192.168.254.9的优先级。使它变为高优先级成为主节点
9tong1:PRIMARY> cfg=rs.conf() #保存当前状态 { "_id" : "9tong1", "version" : 3, "members" : [ { "_id" : 0, "host" : "samba:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 1, "host" : "192.168.254.9:27017", "arbiterOnly" : false, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 }, { "_id" : 2, "host" : "192.168.254.229:27017", "arbiterOnly" : true, "buildIndexes" : true, "hidden" : false, "priority" : 1, "tags" : { }, "slaveDelay" : 0, "votes" : 1 } ], "settings" : { "chainingAllowed" : true, "heartbeatTimeoutSecs" : 10, "getLastErrorModes" : { }, "getLastErrorDefaults" : { "w" : 1, "wtimeout" : 0 } } } 9tong1:PRIMARY> cfg.members[1].priority=2 #更改ID号为1的优先级。 2 9tong1:PRIMARY> rs.reconfig(cfg) #使配置生效 { "ok" : 1 } 9tong1:PRIMARY> 9tong1:PRIMARY> 2015-07-01T14:01:15.000+0800 I NETWORK DBClientCursor::init call() failed 2015-07-01T14:01:15.002+0800 I NETWORK trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed 2015-07-01T14:01:15.002+0800 I NETWORK reconnect 127.0.0.1:27017 (127.0.0.1) ok 9tong1:SECONDARY> 9tong1:SECONDARY> 可以看到原来主节点SAMBA,自动降级为SECONDARY,从节点自动升级到PRIMARY [root@weidangkou data]# /usr/local/service/mongodb-linux-x86_64-rhel62-3.0.3/bin/mongo MongoDB shell version: 3.0.3 connecting to: test Server has startup warnings: 2015-07-01T12:00:01.448+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2015-07-01T12:00:01.448+0800 I CONTROL [initandlisten] 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2015-07-01T12:00:01.449+0800 I CONTROL [initandlisten] 9tong1:PRIMARY>