为什么用mongodbmongodb都是0.078gb

MongoDB的安全配置策略
上一次我写了一篇介绍了Linux下如何安装与连接MongoDB教程,默认MongoDB启动后任何机器都能通过默认端口连接上,非常危险,如果来个恶意分子突然把你数据给remove那就哭死了。所以今天来说说MongoDB的安全配置。
MongoDB可以从以下几方面入手解决安全问题
一、添加用户验证
在MongoDB中,用户权限是与库绑定的,需要注意。
添加一个用户
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42:27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test
& use test1
switched to db test1
& db.addUser('lee','pass')
&user& : &lee&,
&readOnly& : false,
&pwd& : &e6d78598d0bfe068a43e57a4bc82183f&,
&_id& : ObjectId(&f69&)
我们添加了一个lee的用户,密码为pass,其中有一个字段readOnly表示只读权限,如果字段值为false表示可以增删改读,如果字段值为true只能读取数据。
添加用户后,我们可以重新连接看会不会提示输入用户名或其他的
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42:27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test
& use test1
switched to db test1
& db.test1.find()
{ &_id& : ObjectId(&51593aff3ddfadb&), &name& : &lizhong& }
{ &_id& : ObjectId(&ddfadc&), &name& : &lizhong8532& }
我们发现依然可以不需要用户名密码就能连接MongoDB,其实这里需要重启MongoDB,并且在启动参数加入-auth参数表示验证用户名与密码
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod -dbpath=/root/db -auth
1 16:24:01.738 [initandlisten] MongoDB starting : pid=14561 port=27017 dbpath=/root/db 64-bit host=lee
1 16:24:01.739 [initandlisten] db version v2.4.1
1 16:24:01.739 [initandlisten] git version: ce11a693be8b4d0d160d633eee75110
1 16:24:01.739 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST
BOOST_LIB_VERSION=1_49
1 16:24:01.739 [initandlisten] allocator: tcmalloc
1 16:24:01.739 [initandlisten] options: { auth: true, dbpath: &/root/db& }
1 16:24:01.771 [initandlisten] journal dir=/root/db/journal
1 16:24:01.771 [initandlisten] recover : no journal files present, no recovery needed
1 16:24:01.953 [initandlisten] waiting for connections on port 27017
客户端再重新空用户连接
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42:27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test
& use test1
switched to db test1
& db.test1.find()
error: { &$err& : &not authorized for query on test1.test1&, &code& : 16550 }
发现能连接上,但查询test1库时提示错误,因为我们之前添加的lee用户是在test1库添加的,所以空用户名连接上服务器后对于需要验证用户名的库没有执行权限
我们使用lee用户登录服务器,如果密码错误会有以下错误提示
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo test1 --host 192.168.6.42 -u lee -p passs --port 27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test1
1 16:50:20.153 JavaScript execution failed: Error: 18 { ok: 0.0, errmsg: &auth fails& } at src/mongo/shell/db.js:L228
exception: login failed
如果密码正确,则可以正常登录并可以操作一切
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo test1 --host 192.168.6.42 -u lee -p pass --port 27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test1
& show tables
system.indexes
system.users
& db.test1.find()
{ &_id& : ObjectId(&51593aff3ddfadb&), &name& : &lizhong& }
{ &_id& : ObjectId(&ddfadc&), &name& : &lizhong8532& }
& db.test1.save({'name':'xiaoli'})
& db.test1.find()
{ &_id& : ObjectId(&51593aff3ddfadb&), &name& : &lizhong& }
{ &_id& : ObjectId(&ddfadc&), &name& : &lizhong8532& }
{ &_id& : ObjectId(&51594adfe74e32e5b6e3431e&), &name& : &xiaoli& }
如果用户的readOnly为true那么这个用户只能读取数据,添加一个readOnly用户zhansan
& db.addUser('zhansan','pass',true)
&user& : &zhansan&,
&readOnly& : true,
&pwd& : &a75dfcd2d2913319bfcc042a&,
&_id& : ObjectId(&e32e5b6e3431f&)
& db.system.users.find()
{ &_id& : ObjectId(&f69&), &user& : &lee&, &readOnly& : false, &pwd& : &e6d78598d0bfe068a43e57a4bc82183f& }
{ &_id& : ObjectId(&e32e5b6e3431f&), &user& : &zhansan&, &readOnly& : true, &pwd& : &a75dfcd2d2913319bfcc042a& }
使用zhansan登录并且添加数据,提示:not authorized for insert on test1.test1
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo test1 --host 192.168.6.42 -u zhansan -p pass --port 27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test1
& db.test1.save({'name':'laoliu'})
not authorized for insert on test1.test1
& db.test1.find()
{ &_id& : ObjectId(&51593aff3ddfadb&), &name& : &lizhong& }
{ &_id& : ObjectId(&ddfadc&), &name& : &lizhong8532& }
{ &_id& : ObjectId(&51594adfe74e32e5b6e3431e&), &name& : &xiaoli& }
当然,用户可以添加,也可以删除
删除zhansan用户
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo test1 --host 192.168.6.42 -u lee -p pass --port 27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test1
& db.system.users.remove({user:&zhansan&})
& db.system.users.find()
{ &_id& : ObjectId(&f69&), &user& : &lee&, &readOnly& : false, &pwd& : &e6d78598d0bfe068a43e57a4bc82183f& }
再使用zhansan用户登录提示验证失败
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo test1 --host 192.168.6.42 -u zhansan -p pass --port 27017
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:27017/test1
1 17:00:38.194 JavaScript execution failed: Error: 18 { ok: 0.0, errmsg: &auth fails& } at src/mongo/shell/db.js:L228
exception: login failed
[root@DELL113 mongodb-linux-i686-2.4.1]#
二、更改端口
MongoDB默认启动的端口是27017,其实很多程序都有默认端口比如ftp、ssh、memcache等等,更改默认端口是安全策略中不可缺少的一部。
MongoDB其实在启动的时候,加入监听端口号即可,当然最好这个端口好不要被占用。
监听8532端口
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod -dbpath=/root/db --auth --port 8532
1 17:04:21.353 [initandlisten] MongoDB starting : pid=14633 port=8532 dbpath=/root/db 64-bit host=lee
1 17:04:21.354 [initandlisten] db version v2.4.1
1 17:04:21.354 [initandlisten] git version: ce11a693be8b4d0d160d633eee75110
1 17:04:21.354 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST
BOOST_LIB_VERSION=1_49
1 17:04:21.354 [initandlisten] allocator: tcmalloc
1 17:04:21.354 [initandlisten] options: { auth: true, dbpath: &/root/db&, port: 8532 }
1 17:04:21.369 [initandlisten] journal dir=/root/db/journal
1 17:04:21.369 [initandlisten] recover : no journal files present, no recovery needed
1 17:04:21.503 [websvr] admin web console waiting for connections on port 9532
1 17:04:21.503 [initandlisten] waiting for connections on port 8532
用户端按8532端口连接
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo test1 --host 192.168.6.42 -u lee -p pass --port 8532
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:8532/test1
同一个服务器,可以运行多个MongoDB进程监听不同的端口,但要注意制定--dbpath数据库路径与--logpath路径不能与其他正在运行的进程冲突
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod --dbpath=/root/db1 --port 8533
1 17:14:12.322 [initandlisten] MongoDB starting : pid=14696 port=8533 dbpath=/root/db1 64-bit host=lee
1 17:14:12.322 [initandlisten] db version v2.4.1
1 17:14:12.322 [initandlisten] git version: ce11a693be8b4d0d160d633eee75110
1 17:14:12.322 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST
BOOST_LIB_VERSION=1_49
1 17:14:12.322 [initandlisten] allocator: tcmalloc
1 17:14:12.322 [initandlisten] options: { dbpath: &/root/db1&, port: 8533 }
1 17:14:12.338 [initandlisten] journal dir=/root/db1/journal
1 17:14:12.338 [initandlisten] recover : no journal files present, no recovery needed
1 17:14:12.599 [websvr] admin web console waiting for connections on port 9533
1 17:14:12.600 [initandlisten] waiting for connections on port 8533
1 17:14:18.854 [initandlisten] connection accepted from 192.168.4.86:4 connection now open)
客户端连接8533端口
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42:8533
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42:8533/test
& show dbs
0.078125GB
三、IP限制
IP限制几乎是所有互联网安全策略的最后一道墙了。
我们绑定一个IP 127.0.0.1运行
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod -dbpath=/root/db --auth --port 8532 --bind_ip 127.0.0.1
1 17:22:16.534 [initandlisten] MongoDB starting : pid=14713 port=8532 dbpath=/root/db 64-bit host=lee
1 17:22:16.535 [initandlisten] db version v2.4.1
1 17:22:16.535 [initandlisten] git version: ce11a693be8b4d0d160d633eee75110
1 17:22:16.535 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST
BOOST_LIB_VERSION=1_49
1 17:22:16.535 [initandlisten] allocator: tcmalloc
1 17:22:16.535 [initandlisten] options: { auth: true, bind_ip: &127.0.0.1&, dbpath: &/root/db&, port: 8532 }
1 17:22:16.541 [initandlisten] journal dir=/root/db/journal
1 17:22:16.541 [initandlisten] recover : no journal files present, no recovery needed
1 17:22:16.664 [websvr] admin web console waiting for connections on port 9532
1 17:22:16.664 [initandlisten] waiting for connections on port 8532
使用本机连接
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod --dbpath=/root/db --bind_ip 127.0.0.1
1 17:35:38.128 [initandlisten] MongoDB starting : pid=14829 port=27017 dbpath=/root/db 64-bit host=lee
1 17:35:38.128 [initandlisten] db version v2.4.1
1 17:35:38.128 [initandlisten] git version: ce11a693be8b4d0d160d633eee75110
1 17:35:38.128 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST
BOOST_LIB_VERSION=1_49
1 17:35:38.128 [initandlisten] allocator: tcmalloc
1 17:35:38.128 [initandlisten] options: { bind_ip: &127.0.0.1&, dbpath: &/root/db& }
1 17:35:38.133 [initandlisten] journal dir=/root/db/journal
1 17:35:38.133 [initandlisten] recover : no journal files present, no recovery needed
1 17:35:38.265 [initandlisten] waiting for connections on port 27017
1 17:35:38.266 [websvr] admin web console waiting for connections on port 28017
1 17:36:54.385 [initandlisten] connection accepted from 127.0.0.1:3 connection now open)
使用别的机器连接提示连接失败
[root@DELL113 mongodb-linux-i686-2.4.1]# ./bin/mongo 192.168.6.42
MongoDB shell version: 2.4.1
connecting to: 192.168.6.42/test
1 17:38:34.171 JavaScript execution failed: Error: couldn't connect to server 192.168.6.42:27017 at src/mongo/shell/mongo.js:L114
exception: connect failed
绑定其它IP
[root@lee mongodb-linux-x86_64-2.4.1]# ./bin/mongod --dbpath=/root/db --bind_ip 192.168.4.86
1 17:52:57.127 [initandlisten] MongoDB starting : pid=15031 port=27017 dbpath=/root/db 64-bit host=lee
1 17:52:57.128 [initandlisten] db version v2.4.1
1 17:52:57.128 [initandlisten] git version: ce11a693be8b4d0d160d633eee75110
1 17:52:57.128 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST
BOOST_LIB_VERSION=1_49
1 17:52:57.128 [initandlisten] allocator: tcmalloc
1 17:52:57.128 [initandlisten] options: { bind_ip: &192.168.4.86&, dbpath: &/root/db& }
1 17:52:57.145 [initandlisten] journal dir=/root/db/journal
1 17:52:57.145 [initandlisten] recover : no journal files present, no recovery needed
1 17:52:57.388 [initandlisten] preallocateIsFaster=true 2.16
1 17:52:57.583 [initandlisten] ERROR: listen(): bind() failed errno:99 Cannot assign requested address for socket: 192.168.4.86:27017
1 17:52:57.583 [initandlisten] now exiting
1 17:52:57.583 dbexit:
1 17:52:57.583 [initandlisten] shutdown: going to close listening sockets...
1 17:52:57.583 [initandlisten] shutdown: going to flush diaglog...
1 17:52:57.583 [initandlisten] shutdown: going to close sockets...
1 17:52:57.584 [initandlisten] shutdown: waiting for fs preallocator...
1 17:52:57.584 [initandlisten] shutdown: lock for final commit...
1 17:52:57.584 [initandlisten] shutdown: final commit...
1 17:52:57.588 [websvr] ERROR: listen(): bind() failed errno:99 Cannot assign requested address for socket: 192.168.4.86:28017
1 17:52:57.592 [initandlisten] shutdown: closing all files...
1 17:52:57.592 [initandlisten] closeAllFiles() finished
1 17:52:57.592 [initandlisten] journalCleanup...
1 17:52:57.592 [initandlisten] removeJournalFiles
1 17:52:57.595 [initandlisten] shutdown: removing fs lock...
1 17:52:57.595 dbexit: really exiting now
[root@lee mongodb-linux-x86_64-2.4.1]#
出错,不知道为什么?目前正在找问题中。。。
虽然在bind_ip 192.168.4.86后面加上其它端口号能成功运行,但发现bind_ip参数不能阻止其它非指定ip的连接。
其实在绑定IP这一块可以借助iptables完成,iptables还可以指定范围!
文字链接:《》
文章地址:
除非标注,所有博文均为原创,转载请加文字链接注明来源
Recommendation后使用快捷导航没有帐号?
查看: 475|回复: 1
MongoDB学习札记第八篇之Replica Set 实战
金牌会员, 积分 2663, 距离下一级还需 337 积分
论坛徽章:49
MongoDB学习札记第八篇之Replica Set 实战[size=0.8em] ·
· 阅读 2675[size=1.5em][url=]mongodb[/url][size=0.8em]MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似json的bjson格式,因此可以存储比较复杂的数据类型。Mongo较大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。[size=0.8em]
环境Ubuntu12.0.4mongodb3.0.3三台机器,分别为: 192.168.236.131 ; 192.168.236.133 ; 192.168.236.134
如果对于怎么安装Mongodb还不清楚的同学可以查看我之前的学习札记第一步:在三台机器上分别运行(都要运行)root@ubuntu:/usr/local/mongodb#& & mongod --dbpath /usr/local/mongodb/data --replSet rs0注意这里的 —replSet 参数指定了副本集的名称,每一个副本集都有一个的名称。运行之后可以看到下面这样的信息:2015-06-09T17:54:20.845-0700 I JOURNAL&&[initandlisten] journal dir=/usr/local/mongodb/data/journal2015-06-09T17:54:20.846-0700 I JOURNAL&&[initandlisten] recover : no journal files present, no recovery needed2015-06-09T17:54:20.925-0700 I JOURNAL&&[durability] Durability thread started2015-06-09T17:54:20.926-0700 I JOURNAL&&[journal writer] Journal writer thread started2015-06-09T17:54:20.931-0700 I CONTROL&&[initandlisten] MongoDB starting : pid=2539 port=27017 dbpath=/usr/local/mongodb/data/ 64-bit host=ubuntu2015-06-09T17:54:20.931-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:54:20.931-0700 I CONTROL&&[initandlisten]2015-06-09T17:54:20.932-0700 I CONTROL&&[initandlisten]2015-06-09T17:54:20.932-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:54:20.932-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:54:20.932-0700 I CONTROL&&[initandlisten]2015-06-09T17:54:20.932-0700 I CONTROL&&[initandlisten] db version v3.0.32015-06-09T17:54:20.933-0700 I CONTROL&&[initandlisten] git version: b40106b36eecd1baf6bc2015-06-09T17:54:20.933-0700 I CONTROL&&[initandlisten] OpenSSL version: OpenSSL 1.0.1 14 Mar 20122015-06-09T17:54:20.933-0700 I CONTROL&&[initandlisten] build info:
ip-10-216-207-166 3.2.0-36-virtual #57-Ubuntu SMP Tue Jan 8 22:04:49 UTC 2013 x86_64 BOOST_LIB_VERSION=1_492015-06-09T17:54:20.933-0700 I CONTROL&&[initandlisten] allocator: tcmalloc2015-06-09T17:54:20.933-0700 I CONTROL&&[initandlisten] options: { replication: { replSet: &rs0& }, storage: { dbPath: &/usr/local/mongodb/data/& } }2015-06-09T17:54:20.954-0700 I NETWORK&&[initandlisten] waiting for connections on port 270172015-06-09T17:54:20.973-0700 W NETWORK&&[ReplicationExecutor] Failed to connect to 192.168.236.134:27017, reason: errno:111 Connection refused2015-06-09T17:54:20.974-0700 W NETWORK&&[ReplicationExecutor] Failed to connect to 192.168.236.131:27017, reason: errno:111 Connection refused2015-06-09T17:54:20.975-0700 I REPL& &&&[ReplicationExecutor] New replica set config in use: { _id: &rs0&, version: 3, members: [ { _id: 1, host: &192.168.236.133:27017&, arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 2, host: &192.168.236.134:27017&, arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 3, host: &192.168.236.131:27017&, arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatTimeoutSecs: 10, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 } } }2015-06-09T17:54:20.975-0700 I REPL& &&&[ReplicationExecutor] This node is 192.168.236.133:27017 in the config2015-06-09T17:54:20.975-0700 I REPL& &&&[ReplicationExecutor] transition to STARTUP22015-06-09T17:54:20.975-0700 I REPL& &&&[ReplicationExecutor] Starting replication applier threads2015-06-09T17:54:20.977-0700 I REPL& &&&[ReplicationExecutor] transition to RECOVERING2信息大概就是这样的,等到三台机器都启动完了之后。使用mongo客户端登录其中一台mongod服务器。这里我登录到 192.168.236.131 这台机器root@ubuntu:~# mongo登录之后要切换到admin数据库,这样我们可以进行副本集的配置,具体怎么配置,代码如下:& use adminswitched to db admin& config = {_id:&rs0&,members:[... {_id:0,host:&192.168.236.131:27017&},... {_id:1,host:&192.168.236.133:27017&},... {_id:2,host:&192.168.236.134:27017&}]}{& && &&&&_id& : &rs0&,& && &&&&members& : [& && && && && & {& && && && && && && && &&_id& : 0,& && && && && && && && &&host& : &192.168.236.131:27017&& && && && && & },& && && && && & {& && && && && && && && &&_id& : 1,& && && && && && && && &&host& : &192.168.236.133:27017&& && && && && & },& && && && && & {& && && && && && && && &&_id& : 2,& && && && && && && && &&host& : &192.168.236.134:27017&& && && && && & }& && &&&]}& rs.initiate(config);{ &ok& : 1 }先定义 config 的配置信息, 然后通过 rs.initiate(config) 方法,将配置信息初始化。这两个步骤完成之后就表示我们的副本集配置信息初始化完成了,在这个rs0的副本集中我们定义了三台主机(注意在定义配置信息的时候指定的 _id 必须和我们启动mongod的时候指定的参数 —replSet 这个参数的值是一样的。)过一会,mongodb就会帮我们选举出Primary节点和Secondary节点了。那在mongo客户端,我们可以通过 rs.status() 来查看副本集的状态信息rs0:OTHER&rs0:PRIMARY& rs.status(){& && &&&&set& : &rs0&,& && &&&&date& : ISODate(&T00:10:06.941Z&),& && &&&&myState& : 1,& && &&&&members& : [& && && && && & {& && && && && && && && &&_id& : 0,& && && && && && && && &&name& : &192.168.236.131:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 1,& && && && && && && && &&stateStr& : &PRIMARY&,& && && && && && && && &&uptime& : 468,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:06:13Z&),& && && && && && && && &&electionTime& : Timestamp(, 1),& && && && && && && && &&electionDate& : ISODate(&T00:06:17Z&),& && && && && && && && &&configVersion& : 1,& && && && && && && && &&self& : true& && && && && & },& && && && && & {& && && && && && && && &&_id& : 1,& && && && && && && && &&name& : &192.168.236.133:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 233,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:06:13Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:10:06.278Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:10:06.245Z&),& && && && && && && && &&pingMs& : 1,& && && && && && && && &&configVersion& : 1& && && && && & },& && && && && & {& && && && && && && && &&_id& : 2,& && && && && && && && &&name& : &192.168.236.134:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 233,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:06:13Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:10:05.943Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:10:05.890Z&),& && && && && && && && &&pingMs& : 1,& && && && && && && && &&configVersion& : 1& && && && && & }& && &&&],& && &&&&ok& : 1}其中name表示我么你的主机, health表示主机是否健康(0/1) , state(主节点还是从节点,或者是不可达节点)如果上面信息正常显示出来说明整个副本集群已经建立起来了。这时候我们来验证一下是否是真的能够自动备份数据,是否能够自动从失败中恢复,自动选举新的Primary节点。这个实验我们这样来做:先往Primary节点插入数据(131那台机器)在133和134两台Secondary节点中查询数据,验证是否能够正常的同步机器。
rs0:PRIMARY& use testswitched to db testrs0:PRIMARY& show collectionsrs0:PRIMARY& db.guids.insert({&name&:&replica set&,&author&:&webinglin&})WriteResult({ &nInserted& : 1 })rs0:PRIMARY& exitbyeroot@ubuntu:~# mongo --host 192.168.236.134MongoDB shell version: 3.0.3connecting to: 192.168.236.134:27017/testServer has startup warnings:2015-06-09T17:03:27.744-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not re& && && && && && && && && && && && &&&commended.2015-06-09T17:03:27.744-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& show dbs2015-06-09T17:13:49.138-0700 E QUERY& & Error: listDatabases failed:{ &note& : &from execCommand&, &ok& : 0, &errmsg& : &not master& }& & at Error (&anonymous&)& & 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:47rs0:SECONDARY& use testswitched to db testrs0:SECONDARY& db.guids.find()Error: error: { &$err& : &not master and slaveOk=false&, &code& : 13435 }rs0:SECONDARY& rs.slaveOk()rs0:SECONDARY& rs.slaveOk()rs0:SECONDARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }rs0:SECONDARY& show collections()T17:14:24.219-0700 E QUERY& & Error: don't know how to show [collections()]& & at Error (&anonymous&)& & at shellHelper.show (src/mongo/shell/utils.js:733:11)& & at shellHelper (src/mongo/shell/utils.js:524:36)& & at (shellhelp2):1:1 at src/mongo/shell/utils.js:733rs0:SECONDARY& show collectionsguidssystem.indexesrs0:SECONDARY& exitbyeroot@ubuntu:~# mongo --host 192.168.236.133MongoDB shell version: 3.0.3connecting to: 192.168.236.133:27017/testServer has startup warnings:T17:03:11.647-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not re& && && && && && && && && && && && &&&commended.T17:03:11.647-0700 I CONTROL&&[initandlisten]T17:03:11.647-0700 I CONTROL&&[initandlisten]T17:03:11.648-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.T17:03:11.648-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'T17:03:11.648-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& rs.slaveOk()rs0:SECONDARY& show dbslocal&&1.078GBtest& &0.078GBrs0:SECONDARY& use testswitched to db testrs0:SECONDARY& show collectionsguidssystem.indexesrs0:SECONDARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }rs0:SECONDARY& exitbye至此,整个验证过程说明了我们集群部署是成功的。数据能够正常同步了。那么接下来我们还要验证另一种情况,Primary异常终止之后(131),另外两个Secondary节点会不会自动选举出新的Primary节点呢? 这个实验我们这样处理: 将131机器的mongod服务停止掉。然后再来连接133或者134任意一台机器,通过rs.status()查看集群状态。通过 ps -e | grep mongod 查看mongod服务是否开启,然后通过 killall mongod 或者 kill -15 &进程号& 来杀死mongod进程root@ubuntu:~# ps -e | grep mongod 3279 pts/0& & 00:00:19 mongodroot@ubuntu:~# killall mongodroot@ubuntu:~# mongo --host 192.168.236.133MongoDB shell version: 3.0.3connecting to: 192.168.236.133:27017/testServer has startup warnings:2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& rs.status(){& && &&&&set& : &rs0&,& && &&&&date& : ISODate(&T00:22:40.283Z&),& && &&&&myState& : 2,& && &&&&members& : [& && && && && & {& && && && && && && && &&_id& : 0,& && && && && && && && &&name& : &192.168.236.131:27017&,& && && && && && && && &&health& : 0,& && && && && && && && &&state& : 8,& && && && && && && && &&stateStr& : &(not reachable/healthy)&,& && && && && && && && &&uptime& : 0,& && && && && && && && &&optime& : Timestamp(0, 0),& && && && && && && && &&optimeDate& : ISODate(&T00:00:00Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:22:39.642Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:18:22.292Z&),& && && && && && && && &&pingMs& : 3,& && && && && && && && &&lastHeartbeatMessage& : &Failed attempt to connect to 192.168.236.131:27017; couldn't connect to server 192.168.236.131:28.236.131), connection attempt failed&,& && && && && && && && &&configVersion& : -1& && && && && & },& && && && && & {& && && && && && && && &&_id& : 1,& && && && && && && && &&name& : &192.168.236.133:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 1169,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:15:42Z&),& && && && && && && && &&configVersion& : 1,& && && && && && && && &&self& : true& && && && && & },& && && && && & {& && && && && && && && &&_id& : 2,& && && && && && && && &&name& : &192.168.236.134:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 1,& && && && && && && && &&stateStr& : &PRIMARY&,& && && && && && && && &&uptime& : 986,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:15:42Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:22:38.952Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:22:38.951Z&),& && && && && && && && &&pingMs& : 6,& && && && && && && && &&electionTime& : Timestamp(, 1),& && && && && && && && &&electionDate& : ISODate(&T00:18:23Z&),& && && && && && && && &&configVersion& : 1& && && && && & }& && &&&],& && &&&&ok& : 1}rs0:SECONDARY& exitbye通过上面这段代码的观察,我们发现,当把原来的Primary节点停止掉后(131停止), 那么整个mongodb的副本集群会重新选举出新的Primary节点( 134 机器)为了验证一下新选举的Primary是否正常,我们再次验证一把数据的同步情况,先 连接到134 主节点,将原来的数据删掉,在到133进行验证,数据是否也被删除root@ubuntu:~# mongo --192.168.236.134Error parsing command line: unknown option 192.168.236.134try 'mongo --help' for more informationroot@ubuntu:~# mongo --host 192.168.236.134MongoDB shell version: 3.0.3connecting to: 192.168.236.134:27017/testServer has startup warnings:2015-06-09T17:03:27.744-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:03:27.744-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten]rs0:PRIMARY& use testswitched to db testrs0:PRIMARY& show collectionsguidssystem.indexesrs0:PRIMARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }{ &_id& : ObjectId(&557781aed5ed7ed61c16abfd&), &name& : &mongodb& }rs0:PRIMARY& db.guids.remove({name:&mongodb&})WriteResult({ &nRemoved& : 1 })rs0:PRIMARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }rs0:PRIMARY& exitbyeroot@ubuntu:~# mongo --host 192.168.236.133MongoDB shell version: 3.0.3connecting to: 192.168.236.133:27017/testServer has startup warnings:2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& rs.slaveOk()rs0:SECONDARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }rs0:SECONDARY& exitbye实践后发现,先选举的Primary节点也正常工作。我们的整个Mongodb副本集群测试完成。
动态添加节点,删除节点。在开始这个实验之前,先把131的机器重新启动,然后用mongo客户端连到131进行验证数据是否也同步了。登录131之后,我们发现数据也同步了,然后131节点变成了 Secondary节点了。root@ubuntu:~# mongoMongoDB shell version: 3.0.3connecting to: testServer has startup warnings:2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten]2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten]2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& rs.status(){& && &&&&set& : &rs0&,& && &&&&date& : ISODate(&T00:25:02.631Z&),& && &&&&myState& : 2,& && &&&&syncingTo& : &192.168.236.133:27017&,& && &&&&members& : [& && && && && & {& && && && && && && && &&_id& : 0,& && && && && && && && &&name& : &192.168.236.131:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 14,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:23:54Z&),& && && && && && && && &&syncingTo& : &192.168.236.133:27017&,& && && && && && && && &&configVersion& : 1,& && && && && && && && &&self& : true& && && && && & },& && && && && & {& && && && && && && && &&_id& : 1,& && && && && && && && &&name& : &192.168.236.133:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 13,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:23:54Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:25:01.196Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:25:02.228Z&),& && && && && && && && &&pingMs& : 1,& && && && && && && && &&syncingTo& : &192.168.236.134:27017&,& && && && && && && && &&configVersion& : 1& && && && && & },& && && && && & {& && && && && && && && &&_id& : 2,& && && && && && && && &&name& : &192.168.236.134:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 1,& && && && && && && && &&stateStr& : &PRIMARY&,& && && && && && && && &&uptime& : 13,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:23:54Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:25:01.235Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:25:02.446Z&),& && && && && && && && &&pingMs& : 10,& && && && && && && && &&electionTime& : Timestamp(, 1),& && && && && && && && &&electionDate& : ISODate(&T00:18:23Z&),& && && && && && && && &&configVersion& : 1& && && && && & }& && &&&],& && &&&&ok& : 1}rs0:SECONDARY& exitbye登录到134 Primary节点,通过 rs.remove() 方法来删除副本集中的某一个节点,这里我们还是将 131删除。删除之后我们还往134主节点中加入数据.rs0:PRIMARY& rs.remove(&192.168.236.131:27017&){ &ok& : 1 }rs0:PRIMARY& rs.statusfunction () { return db._adminCommand(&replSetGetStatus&); }rs0:PRIMARY& rs.status(){& && &&&&set& : &rs0&,& && &&&&date& : ISODate(&T00:32:15.795Z&),& && &&&&myState& : 1,& && &&&&members& : [& && && && && & {& && && && && && && && &&_id& : 1,& && && && && && && && &&name& : &192.168.236.133:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 1562,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:32:09Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:32:13.909Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:32:15.633Z&),& && && && && && && && &&pingMs& : 1,& && && && && && && && &&syncingTo& : &192.168.236.134:27017&,& && && && && && && && &&configVersion& : 2& && && && && & },& && && && && & {& && && && && && && && &&_id& : 2,& && && && && && && && &&name& : &192.168.236.134:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 1,& && && && && && && && &&stateStr& : &PRIMARY&,& && && && && && && && &&uptime& : 1729,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:32:09Z&),& && && && && && && && &&electionTime& : Timestamp(, 1),& && && && && && && && &&electionDate& : ISODate(&T00:18:23Z&),& && && && && && && && &&configVersion& : 2,& && && && && && && && &&self& : true& && && && && & }& && &&&],& && &&&&ok& : 1}rs0:PRIMARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }rs0:PRIMARY& db.guids.insert({&name&:&remove one node dync&})WriteResult({ &nInserted& : 1 })rs0:PRIMARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }{ &_id& : ObjectId(&557785bcbb341&), &name& : &remove one node dync& }rs0:PRIMARY& exitbye删除131节点后,我们往primary节点中加入了新的数据,然后先不要将131的mongod服务停掉,我们通过mongo连接到131的mongod服务来查看数据root@ubuntu:~# mongo --host 192.168.236.131MongoDB shell version: 3.0.3connecting to: 192.168.236.131:27017/testServer has startup warnings:2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten]2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten]2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten]& db.guids.find()Error: error: { &$err& : &not master and slaveOk=false&, &code& : 13435 }& db.slaveOk()2015-06-09T17:33:40.243-0700 E QUERY& & TypeError: Property 'slaveOk' of object test is not a function& & at (shell):1:4& rs.slaveOk()& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }& exitbye实验结果可以知道,我们在134新加入的数据 {name:”remove one node dync”} 并没有同步到131(已从副本集中删除).为了让实验结果更加确切,我们查看133是否有同步了数据:root@ubuntu:~# mongo --host 192.168.236.133MongoDB shell version: 3.0.3connecting to: 192.168.236.133:27017/testServer has startup warnings:2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:11.647-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:03:11.648-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& rs.slaveOk()rs0:SECONDARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }{ &_id& : ObjectId(&557785bcbb341&), &name& : &remove one node dync& }rs0:SECONDARY& exitbye实验数据可以看到,133同步了在134主节点中新增的文档 {“name”:”remove one node dync”},这样就证明了动态删除副本集中的某一个节点的实验成功了。那怎么动态添加节点到副本集中呢?原理是一样的,但是调用的方法变成了 rs.add(&192.168.236.131:27017&)root@ubuntu:~# mongo --host 192.168.236.134MongoDB shell version: 3.0.3connecting to: 192.168.236.134:27017/testServer has startup warnings:2015-06-09T17:03:27.744-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:03:27.744-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten]2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:03:27.745-0700 I CONTROL&&[initandlisten]rs0:PRIMARY& rs.add(&192.168.236.131:27017&);{ &ok& : 1 }rs0:PRIMARY& rs.status(){& && &&&&set& : &rs0&,& && &&&&date& : ISODate(&T00:34:45.974Z&),& && &&&&myState& : 1,& && &&&&members& : [& && && && && & {& && && && && && && && &&_id& : 1,& && && && && && && && &&name& : &192.168.236.133:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 1712,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:34:42Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:34:44.207Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:34:45.901Z&),& && && && && && && && &&pingMs& : 2,& && && && && && && && &&syncingTo& : &192.168.236.134:27017&,& && && && && && && && &&configVersion& : 3& && && && && & },& && && && && & {& && && && && && && && &&_id& : 2,& && && && && && && && &&name& : &192.168.236.134:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 1,& && && && && && && && &&stateStr& : &PRIMARY&,& && && && && && && && &&uptime& : 1879,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:34:42Z&),& && && && && && && && &&electionTime& : Timestamp(, 1),& && && && && && && && &&electionDate& : ISODate(&T00:18:23Z&),& && && && && && && && &&configVersion& : 3,& && && && && && && && &&self& : true& && && && && & },& && && && && & {& && && && && && && && &&_id& : 3,& && && && && && && && &&name& : &192.168.236.131:27017&,& && && && && && && && &&health& : 1,& && && && && && && && &&state& : 2,& && && && && && && && &&stateStr& : &SECONDARY&,& && && && && && && && &&uptime& : 1,& && && && && && && && &&optime& : Timestamp(, 1),& && && && && && && && &&optimeDate& : ISODate(&T00:32:09Z&),& && && && && && && && &&lastHeartbeat& : ISODate(&T00:34:44.217Z&),& && && && && && && && &&lastHeartbeatRecv& : ISODate(&T00:34:44.234Z&),& && && && && && && && &&pingMs& : 1,& && && && && && && && &&syncingTo& : &192.168.236.134:27017&,& && && && && && && && &&configVersion& : 3& && && && && & }& && &&&],& && &&&&ok& : 1}rs0:PRIMARY& exitbye在rs.status()返回的结果中可以看到,131节点已经成功加入副本集中了。加入之后,理论上应该会把在134主节点加入的数据同步过来,刚才删除之后是不会同步的。那这时候重新加入副本集,应该是要同步的。下面是实验结果:root@ubuntu:~# mongoMongoDB shell version: 3.0.3connecting to: testServer has startup warnings:2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten]2015-06-09T17:24:49.146-0700 I CONTROL&&[initandlisten]2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten] **& && &&&We suggest setting it to 'never'2015-06-09T17:24:49.147-0700 I CONTROL&&[initandlisten]rs0:SECONDARY& rs.slaveOk()rs0:SECONDARY& db.guids.find(){ &_id& : ObjectId(&557780ebd147ed&), &name& : &replica set&, &author& : &webinglin& }{ &_id& : ObjectId(&557785bcbb341&), &name& : &remove one node dync& }rs0:SECONDARY& exitbye实验结果显示,动态添加操作也正常。动态的将131节点加入到副本集中能够保证数据同步成功。注意在调用 rs.add(“host:ip”) 或者 rs.remove(“host:ip”) 的时候,必须要在 Primary 节点中进行。add方法可以加入一个document对象,这样就可以在指定具体的Secondary节点的更多的设置项了,比如指定为priority: 0 或 priority: 0,hidden: true 或 priority:0,hidden:true,arbiterOnly:true{&&_id: &int&,&&host: &string&,&&arbiterOnly: &boolean&,&&buildIndexes: &boolean&,&&hidden: &boolean&,&&priority: &number&,&&tags: &document&,&&slaveDelay: &int&,&&votes: &number&}怎么对副本集进行权限验证,参考主从复制的安全部分,也是通过openssl来生成keyfile,然后再启动mongod的时候指定keyFile来设置安全的
Sincerely!
《MongoDB权威指南》
原作者:原文地址:
中级会员, 积分 345, 距离下一级还需 155 积分
论坛徽章:10
主题不错,就是格式太乱了。

我要回帖

更多关于 mongodb 为什么快 的文章

 

随机推荐