MongoDB副本集搭建和测试
前段时间花了时间看了看mongodb,室友买了mongodb权威指南也借过来瞅瞅,书对于刚刚接触的人还是不错的。废话不多说上代码。
1 .搭建环境
MacBook pro 15款840 <p>
VMWare Fusion8 <p>
Centos 6.7 <p>
mongodb-linux-x86_64-3.0.6.tgz
- 安装mongodb
这一步其实就是一个解压的过程。很简单。
在安装前把dbpath和logpath路径新建好,然后会报错。
[root@rabbitmq-node2 ~]# mkdir -p /mongodb/data/arbiter
[root@rabbitmq-node2 ~]# mkdir -p /mongodb/logs
[root@rabbitmq-node2 data]# cd /usr/programmer/
[root@rabbitmq-node2 programmer]# tar zxf mongodb-linux-x86_64-3.0.6.tgz
[root@rabbitmq-node2 programmer]# mv mongodb-linux-x86_64-3.0.6 mongodb
[root@rabbitmq-node2 programmer]# mv mongodb /usr/local/
[root@rabbitmq-node2 programmer]# ls
apache-tomcat-8.0.26.tar.gz nginx-1.9.4.tar.gz
erlang-17.4-1.el6.x86_64.rpm openssl-1.0.2c.tar.gz
go1.4.2.linux-amd64.tar.gz rabbitmq-server-3.5.6-1.noarch.rpm
jdk-7u80-linux-x64.tar.gz redis-3.0.3.tar
mongodb-linux-x86_64-3.0.6.tgz solr-5.2.1.tgz
mongodb-osx-x86_64-3.0.6.tgz zookeeper-3.4.6.tar.gz
nginx-1.8.0.tar.gz
[root@rabbitmq-node2 programmer]# cd /usr/local/
[root@rabbitmq-node2 local]# cd mongodb/
[root@rabbitmq-node2 mongodb]# ls
bin GNU-AGPL-3.0 README THIRD-PARTY-NOTICES
[root@rabbitmq-node2 mongodb]# mkdir conf
[root@rabbitmq-node2 mongodb]# cd conf/
这里有三台虚拟机都是一样这样安装。我就省略其他的了。
- 副本集群搭建
进入集群搭建,需要加一些配置:如下
#master.conf
#数据存放目录
dbpath=/mongodb/data/master
#日志存放目录
logpath=/mongodb/logs/master.log
#进程文件
pidfilepath=/mongodb/master.pid
#按照数据库名字建立目录存放数据
directoryperdb=true
#指定日志文件,该文件将保存所有的日志记录、诊断信息。除非另有指定,
#mongod将所有的日志信息输出到标准输出。如果没有指定logappend,重启则日
#志会进行覆盖操作
logappend=true
#副本集名字
replSet=szss
#绑定IP地址
bind_ip=192.168.31.101
#端口号
port=27017
#操作日志大小
oplogSize=10000
#是否后台运行 进程在后台运行的守护进程模式
fork=true
#预分配文件缩短启动时间
noprealloc=true
注意如果之后关闭mongodb后重启报错,可能是你强行关闭造成锁表,或者因为你的磁盘不够大了。需要加上 nojournal = true 关闭操作日志。 但是 非常不建议这样做。因为它可以帮助你备份和故障修复,提高写入的速率。
其他的两个配置也是大差不差。就不一一贴代码了。
启动mongodb服务:
[root@rabbitmq-node1 bin]# ./mongod --fork --rest ../conf/master.config
2015-11-04T15:58:02.803-0500 I CONTROL ** WARNING: --rest is specified without --httpinterface,
2015-11-04T15:58:02.803-0500 I CONTROL ** enabling http interface
BadValue --fork has to be used with --logpath or --syslog
try './mongod --help' for more information
[root@rabbitmq-node1 bin]# ./mongod --rest -f ../conf/master.config
2015-11-04T15:58:41.235-0500 I CONTROL ** WARNING: --rest is specified without --httpinterface,
2015-11-04T15:58:41.235-0500 I CONTROL ** enabling http interface
note: noprealloc may hurt performance in many applications
about to fork child process, waiting until server is ready for connections.
forked process: 2494
child process started successfully, parent exiting
[root@rabbitmq-node1 bin]# ps -ef | grep mongod
root 2494 1 0 15:58 ? 00:00:00 ./mongod --rest -f ../conf/master.config
root 2537 2382 0 16:01 pts/0 00:00:00 grep mongod
[root@rabbitmq-node1 bin]# ./mongo 192.168.31.101:27017
MongoDB shell version: 3.0.6
connecting to: 192.168.31.101:27017/test
Server has startup warnings:
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten]
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten]
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten]
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-04T15:49:55.987-0500 I CONTROL [initandlisten]
> use admin
switched to db admin
> db.shutdownServer();
assert failed : unexpected error: Error: shutdownServer failed: shutdown must run from localhost when running db without auth
Error: assert failed : unexpected error: Error: shutdownServer failed: shutdown must run from localhost when running db without auth
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at assert (src/mongo/shell/assert.js:20:5)
at DB.shutdownServer (src/mongo/shell/db.js:212:9)
at (shell):1:4
2015-11-04T15:54:48.911-0500 E QUERY Error: assert failed : unexpected error: Error: shutdownServer failed: shutdown must run from localhost when running db without auth
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at assert (src/mongo/shell/assert.js:20:5)
at DB.shutdownServer (src/mongo/shell/db.js:212:9)
at (shell):1:4 at src/mongo/shell/assert.js:13
> ^C
bye
--rest是开启webUI监考页面
这里写图片描述这里说 使用使用这种方式关闭服务 必须在有登录验证情况下远程ssh才拥有这个权限的,所以
[root@rabbitmq-node1 bin]# ./mongod --shutdown --dbpath /mongodb/data/master/
killing process with pid: 2434
使用客户端命令来关闭。
启动mongodb服务:
[root@rabbitmq-node1 bin]# ./mongo 127.0.0.1
MongoDB shell version: 3.0.6
connecting to: 127.0.0.1/test
2015-11-04T16:01:53.200-0500 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
2015-11-04T16:01:53.201-0500 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
可以看出是不识别localhost 127.0.0.1的
[root@rabbitmq-node1 bin]# ./mongo 192.168.31.101
MongoDB shell version: 3.0.6
connecting to: 192.168.31.101/test
Server has startup warnings:
2015-11-04T15:58:41.235-0500 I CONTROL ** WARNING: --rest is specified without --httpinterface,
2015-11-04T15:58:41.235-0500 I CONTROL ** enabling http interface
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten]
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten]
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten]
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-04T15:58:41.304-0500 I CONTROL [initandlisten]
> use admin
switched to db admin
> cfg={ "_id" : "szss", "members" : [ { "_id" : 0, "host" : "192.168.31.101:27017" } ] }
{
"_id": "szss",
"members": [
{
"_id": 0,
"host": "192.168.31.101:27017"
}
]
}
> rs.initiate(cfg)
{ "ok" : 1 }
szss:OTHER> rs.add("192.168.31.154:27017")
{ "ok" : 1 }
szss:PRIMARY> rs.add("192.168.31.159:27017")
{ "ok" : 1 }
加入集群配置 并初始化 并且加上其他虚机的服务 这里也可以一步到位。 我也比较推荐这种方式。
{
_id: "szss",
members: [
{
_id: 0,
host: '192.168.31.101: 27017',
priority: 2
},
{
_id: 1,
host: '192.168.31.154: 27017',
priority: 1
},
{
_id: 2,
host: '192.168.31.159: 27017',
arbiterOnly: true
}
]
};
这种方式是 主节点,备节点,仲裁节点的副本集集群,但是在实际中,仲裁节点比较鸡肋,他要占用一个服务,但是只是为了投票而已,只要将备节点的个数控制在奇数就足够了。
回到现在的这种副本集集群搭建的方式,现在要赋予节点权重。权重是选举中优先级最高的。
szss:PRIMARY> rs.conf()
{
"_id": "szss",
"version": 3,
"members": [
{
"_id": 0,
"host": "192.168.31.101:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {
},
"slaveDelay": 0,
"votes": 1
},
{
"_id": 1,
"host": "192.168.31.154:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {
},
"slaveDelay": 0,
"votes": 1
},
{
"_id": 2,
"host": "192.168.31.159: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
}
}
}
szss:PRIMARY> cfgs=rs.conf()
{
"_id": "szss",
"version": 3,
"members": [
{
"_id": 0,
"host": "192.168.31.101:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {},
"slaveDelay": 0,
"votes": 1
},
{
"_id": 1,
"host": "192.168.31.154:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {},
"slaveDelay": 0,
"votes": 1
},
{
"_id": 2,
"host": "192.168.31.159: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
}
}
}
#赋予权重
szss:PRIMARY> cfgs.members[0].priority=2
2
szss:PRIMARY> rs.conf()
{
"_id": "szss",
"version": 3,
"members": [
{
"_id": 0,
"host": "192.168.31.101:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {},
"slaveDelay": 0,
"votes": 1
},
{
"_id": 1,
"host": "192.168.31.154:27017",
"arbiterOnly": false,
"buildIndexes": true,
"hidden": false,
"priority": 1,
"tags": {},
"slaveDelay": 0,
"votes": 1
},
{
"_id": 2,
"host": "192.168.31.159: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
}
}
}
#重新读取 初始化
szss:PRIMARY> rs.reconfig(cfgs)
{ "ok" : 1 }
进入到浏览器 输入 192.168.31.101:28017 如果是27017将访问不了,需要将启动端口号加上1000
这里写图片描述
- 测试
测试一下集群是否可用:
关掉主节点,模拟主节点宕机的情景
[root@rabbitmq-node1 bin]# ps -ef | grep mongod
root 2494 1 0 15:58 ? 00:00:17 ./mongod --rest -f ../conf/master.config
root 3344 2382 0 16:30 pts/0 00:00:00 grep mongod
[root@rabbitmq-node1 bin]# kill -9 2494
看一下备节点的状态:
这里写图片描述这里可以看到备节点已经变为主节点。
szss:PRIMARY>
for(var i=0;i<10000;i++)db.test.insert(
{
"name": "lxc"+i,
"age": 22,
"sex": "man"
}
)
WriteResult({ "nInserted" : 1 })
在主节点插入1w条数据。再看看备节点。
szss:SECONDARY> use admin
switched to db admin
szss:SECONDARY> use test
switched to db test
szss:SECONDARY> db.getMongo().setSlaveOk()
szss:SECONDARY> db.test.count()
10000
szss:SECONDARY>
szss:SECONDARY> use test
switched to db test
szss:SECONDARY> db.getMongo().setSlaveOk()
szss:SECONDARY> db.test.count()
10000
均已经同步过来了。
到这里就基本完成搭建了。mongodb的中文社区也很不错。而且它的官方文档写的也很不错。一起学习吧~
借鉴大神的文章:
http://www.cnblogs.com/zhanjindong/category/507349.html
http://blog.csdn.net/luonanqin/article/details/8497860
欢迎光临我的个人博客