mongo 学习

mongod 启动参数详解:
--fastsync
--oplogSize arg size limit(in MB) for op log

--master   master mode 
--slave     slave  mode 
--source arg  <serverIp:port> if slave mode ,specify master. if master mode , specify slave ;
--only  arg      if slave mode , specify a single db to rep
--slavedelay arg  delay time in seconds 
--autoresync  automatically resync if slave data is stale 



主从配置 
master 启动: ./mongod --dbpath /data/db/master --logpath /data/db/master.log --logappend  --fork --port 2717 --master --oplogSize 64 

slave 启动: ./mongod  --dbpath /data/db/slave  --logpath  /data/db/slaver.log --logappend  --fork  -port 27018  --slave --slavedelay 5 --autoresync --source localhost:27017  

问题解决: 
1. Client::~Client _context should be null but is not; client:replslave 
从库启动,同步时报错自动关闭;需要删除从库db目录下的记录即可。
参考: 
注意:这里,最好是用scp命令把主机器的mongodb的所有目录和文件远程拷贝
到从机器上去, 之前不要去启动,主机器,否则启动从的时候会报错(在
日志文件中可以看到错误:mongodb ERROR: Client::~Client _context should be null but is not; client:replslave)
这样的话, 只需要把数据文件data目录下的所有文件删除,重新启动即可

2. mongodb 启动失败,因为非法关闭造成
mongod没有后台执行,在终端连接非正常断开后,再次执行mongod报错,需要repair修复;
 [initandlisten] exception in initAndListen: 12596 old lock file, terminating 
dbexit 
http://www.mongodb.org/display/DOCS/Durability+and+Repair#DurabilityandRepair-RepairCommand 写道
Repair Command

When not using journaling (--nojournal), after a machine crash or kill -9 termination, run the repairDatabase command. This command will check all data for corruption, remove any corruption found, and compact data files a bit. Repair is analogous to running fsck for a file system.

When journaling is enabled, it should not be necessary to run repair. However one could still use the repair command to compact a database.

From the command line:

mongod --repair

From the shell (you have to do for all dbs including local if you go this route):

> db.repairDatabase();

During a repair operation, mongod must store temporary files to disk. By default, mongod creates temporary directories under the dbpath for this purpose. Alternatively, the --repairpath command line option can be used to specify a base directory for temporary repair files.

Note that repair is a slow operation which inspects the entire database.

After running with --repair, mongod will start up normally.

mongo 实战二主从集群配置 - zhengjunwei2007 - 蛇族团长的博客
 

互为主从配置
摘自:  http://blog.csdn.net/liuyuanshijie/article/details/6735625


Replic Sets配置: 
摘自: http://virusswb.blog.51cto.com/115214/792897

MongoDB支持两种复制的模式:

  1. Master/Slave,主从复制,角色包括master和slave。
  2. Replica Set,复制集复制,角色包括primary和secondary。

 

介绍Master/Slave的官方地址:

http://www.mongodb.org/display/DOCS/Master+Slave

介绍Replica Set的官方地址:

http://www.mongodb.org/display/DOCS/Replica+Sets

今天实战的是replica set,也就是复制集复制。

  • replica set可以实现自动的failover和自动的recovery。
  • replica set由两个或者更多的节点组成,实现彼此的复制。
  • replica set自动选择primary节点,没有一个节点是固定的primary。
  • mongos会自动发现一个replica set的primary节点发生变化,并将写操作发送给这个新的primary节点。

通常用于下面几个场景

  • 数据冗余。
  • 自动failover,提供高可用性的服务。
  • 分散读的负载。
  • 简化维护(相对于master-slave来说)。
  • 灾难恢复。

首先还是启动mongod,用于replica set的参数有两个:

--replSet <setname>,复制集的名称。

--oplogSize <MB>,操作日志的大小,单位为MB。

这回我们在ubuntu下面配置replica set,先启动两个mongod节点。

 mongod --dbpath /home/andyshi/mongo1/ --logpath /home/andyshi/mongo1/log.log --replSet shard1 --port 10001 --bind_ip 192.168.0.21 

 mongod --dbpath /home/andyshi/mongo2/ --logpath /home/andyshi/mongo2/log.log --replSet shard1 --port 10002 --bind_ip 192.168.0.21 

你会注意到,上面两个mongod的启动参数replSet指定了相同的值shard1,也就是两个个mongod节点处于同一个replica set中。

Replica Set的初始化

光启动了两个个mongod节点,还不能提供任何的服务,这时候你使用mongo连接之后,进行db.book.insert会提示你no master,也就是说没有primary节点,所以他不知道往哪一个节点写入数据。

在启动了两个个mongod节点之后,需要进行初始化。

首先用mongo连接到任意一个mongod节点,然后执行下面的命令。

   
   
  1. cfg={_id:'shard1',members:[ 
  2. {_id:0,host:'192.168.0.21:10001'}, 
  3. {_id:1,host:'192.168.0.21:10002'}] 
  4. rs.initiate(cfg) 

出现下面的提示信息就代表成功了,如果没有成功,可以google一下错误提示,会找到很多的答案的。

 

   
   
  1.    "info" : "Config now saved locally.  Should come online in about a minute.", 
  2.    "ok" : 1 

继续执行

 rs.status() 

可以查看replica set的状态,包括名称,时间,当前登录的mongod是primary还是secondary,以及成员的信息等。

在replica set的信息中,其中重要的是:

  • myState的值,如果是1代表当前登录的是primary;如果是2代表当前登录的是secondary。

成员信息中包括地址,健康状态,是primary还是secondary等。

成员信息中比较重要的是

  • state:1表示该host是当前可以进行读写,2:不能读写
  • health:1表示该host目前是正常的,0:异常

这时候登录primary的mongod,插入一条数据。

 

   
   
  1. //假设10001是primary,可以通过查询rs.status来获取 
  2. mongo 192.168.0.21:10001 
  3.  
  4. use test 
  5. db.book.insert({'title':'computer'}) 

然后查看secondary的log文件,会发现发生了复制行为。

这时候登录secondary,use test,db.book.find(),可以报错了。

 

   
   
  1. error: { "$err" : "not master and slaveok=false", "code" : 13435 } 

没有关系,在secondary读取数据还需要我们做最后的一步,在需要读取数据的secondary上执行。

 rs.slaveOK()

这时候再次db.book.find(),正常显示结果了,没有问题了。

 添加节点

 启动新mongod节点

 mongod --dbpath /home/andyshi/mongo3/ --logpath /home/andyshi/mongo3/log.log --replSet shard1 --port 10003 --bind_ip 192.168.0.21   

 
  

连接primary节点,执行下面的命令。

 

   
   
  1. rs.add('192.168.0.21:10003') 
  2. rs.addArb('192.168.0.21:10003') 
  3. //重新配置 
  4. rs.reconfig(rs.conf()) 

 

强制一个节点成为primary

在mongodb2.0之后可以使用下面

相比master-slave,replica set的优点就是没有单点故障,primary故障之后,整个replica set会自动选择一个健康的节点成为primary,承担写的任务,可用性比master-slave的高,提供更高的可用性。


遇到的问题: 

在replset运行正常的情况下,停止服务,更换端口,重新分配set,造成的问题。

可通过use admin    db.runcommand({replSetReconfig:config})从新分配,其中config={} 是set的新配置;当此法还无法解决问题时,可以更改local.system.replset的记录,或者重做local表了

具体可借鉴:http://groups.google.com/group/mongoid/browse_thread/thread/6291b8b80158031e

http://blog.nroed.com/2011/12/15/mongo-replset-badconfig/?replytocom=489

http://groups.google.com/group/mongoid/browse_thread/thread/6291b8b80158031e?pli=1

run rs.initiate(...) if not yet done for the set

local.oplog.rs is not empty on the initiating member.  cannot initiate.

loading local.system.replset config (LOADINGCONFIG)


集群配置 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大家这么喜欢,我就不要资源分了,改成0分了,我为我一己私利感到抱歉…… MongoDB学习手册 ......................................................................................................................... 1 说明.......................................................................................................................................... 2 一、 Mongodb简介 .......................................................................................................... 4 二、 MongoDB特性 ......................................................................................................... 5 适用场景: ....................................................................................................................... 5 不适用场景: ................................................................................................................... 6 三、 MongoDB的工作方式.............................................................................................. 6 四、 MongoDB的下载 ..................................................................................................... 8 五、 MongoDB的安装 ..................................................................................................... 9 六、 MongoDB数据类型................................................................................................ 12 1. Timestamp类型 ...................................................................................................... 12 2. ObjectId类型 .......................................................................................................... 12 3. 数据库关联 ............................................................................................................. 14 七、 GridFS文件系统 ..................................................................................................... 15 八、 索引 ......................................................................................................................... 16 九、 主(Master)/从(slave)数据库同步 ................................................................ 20 1. 建立主/从服务器 ................................................................................................... 20 2. 相关参数说明 ......................................................................................................... 21 3. Slave顶替Master ................................................................................................... 22 4. 切换Master/Slave角色 ......................................................................................... 22 5. 更新主服务器位置 ................................................................................................. 22 十、 MongoDB分片和集群............................................................................................ 24 1. 简单分片实例 ......................................................................................................... 24 2. 高级分片实例 ......................................................................................................... 29 十一、 数据库基本操作:增查删改 ............................................................................. 37 1. Insert ....................................................................................................................... 37 2. Query ....................................................................................................................... 38 3. Remove.................................................................................................................... 52 4. Update ..................................................................................................................... 53 十二、 Shell控制台 ........................................................................................................ 62 1. 执行.js文件 ............................................................................................................ 62 2. –eval ........................................................................................................................ 62 3. 脚本和互动的区别 ................................................................................................. 62 十三、 安全与认证 ......................................................................................................... 63 1) 开启安全认证 ......................................................................................................... 63 2) 添加用户 ................................................................................................................. 64 3) 认证 ......................................................................................................................... 64

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值