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
<wbr style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px"><div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
--autoresync<span style="white-space:pre"> </span>automatically resync if slave data is stale</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<span style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">主从配置</span>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
master 启动: ./mongod --dbpath /data/db/master --logpath /data/db/master.log --logappend --fork --port 2717 --master --oplogSize 64</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
slave 启动: ./mongod --dbpath /data/db/slave --logpath /data/db/slaver.log --logappend --fork -port 27018 --slave --slavedelay 5 --autoresync --source localhost:27017 </div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<strong>问题解决:</strong>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
1.Client::~Client _context should be null but is not; client:replslave</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
从库启动,同步时报错自动关闭;需要删除从库db目录下的记录即可。</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<strong>参考:</strong>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-size:12px; line-height:23px">注意:这里,最好是用scp命令把主机器的mongodb的所有目录和文件远程拷贝<br><span style="white-space:pre"></span>到从机器上去, 之前不要去启动,主机器,否则启动从的时候会报错(在<br><span style="white-space:pre"></span>日志文件中可以看到错误:mongodb ERROR: Client::~Client _context should be null but is not; client:replslave)<br><span style="white-space:pre"></span>这样的话, 只需要把数据文件data目录下的所有文件删除,重新启动即可</span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
2. mongodb 启动失败,因为非法关闭造成</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
摘自:<a rel="nofollow" href="http://codingstandards.iteye.com/blog/1214093" style="color:rgb(121,75,21); text-decoration:initial">http://codingstandards.iteye.com/blog/1214093</a>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="color:rgb(255,0,0); font-family:Helvetica,Tahoma,Arial,sans-serif">mongod没有后台执行,在终端连接非正常断开后,再次执行mongod报错,需要repair修复;</span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif">[initandlisten] exception in initAndListen: 12596 old lock file, terminating</span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif">dbexit</span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<div style="font-weight:bold; padding:5px; margin:5px 0px 0px 15px; font-family:Helvetica,Tahoma,Arial,sans-serif">
http://www.mongodb.org/display/DOCS/Durability+and+Repair#DurabilityandRepair-RepairCommand 写道</div>
<div style="border:1px solid rgb(204,204,204); margin:0px 5px 5px 15px; padding:3px; background-color:rgb(250,250,250); font-family:Helvetica,Tahoma,Arial,sans-serif">
Repair Command<br><br>
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.<br><br>
When journaling is enabled, it should not be necessary to run repair. However one could still use the repair command to compact a database.<br><br>
From the command line:<br><br>
mongod --repair<br><br>
From the shell (you have to do for all dbs including local if you go this route):<br><br>
> db.repairDatabase();<br><br>
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.<br><br>
Note that repair is a slow operation which inspects the entire database.<br><br>
After running with --repair, mongod will start up normally.</div>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><br></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<div><img alt="mongo 实战二主从集群配置 - zhengjunwei2007 - 蛇族团长的博客" src="http://img5.ph.126.net/Ft7Jg6SuvuTXZzw3lquEbQ==/587156801435885385.jpg" style="border:0px; max-width:100%; margin:0px 10px 0px 0px"></div>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><br></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><strong>互为主从配置</strong></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif">摘自:</span><span style="font-family:Helvetica,Tahoma,Arial,sans-serif">http://blog.youkuaiyun.com/liuyuanshijie/article/details/6735625</span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><br></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><br></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><strong>Replic Sets配置:</strong></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><strong>摘自: http://virusswb.blog.51cto.com/115214/792897</strong></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><br></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
MongoDB支持两种复制的模式:</p>
<ol style="margin:5px 0px 5px 40px; padding:0px">
<li>Master/Slave,主从复制,角色包括master和slave。</li>
<li>Replica Set,复制集复制,角色包括primary和secondary。</li>
</ol>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
介绍Master/Slave的官方地址:</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<a target="_blank" rel="nofollow" href="http://www.mongodb.org/display/DOCS/Master+Slave" style="color:rgb(121,75,21); text-decoration:initial">http://www.mongodb.org/display/DOCS/Master+Slave</a></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
介绍Replica Set的官方地址:</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<a target="_blank" rel="nofollow" href="http://www.mongodb.org/display/DOCS/Replica+Sets" style="color:rgb(121,75,21); text-decoration:initial">http://www.mongodb.org/display/DOCS/Replica+Sets</a></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
今天实战的是replica set,也就是复制集复制。</p>
<ul style="margin:5px 0px 5px 40px; padding:0px">
<li>replica set可以实现自动的failover和自动的recovery。</li>
<li>replica set由两个或者更多的节点组成,实现彼此的复制。</li>
<li>replica set自动选择primary节点,没有一个节点是固定的primary。</li>
<li>mongos会自动发现一个replica set的primary节点发生变化,并将写操作发送给这个新的primary节点。</li>
</ul>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
通常用于下面几个场景</p>
<ul style="margin:5px 0px 5px 40px; padding:0px">
<li>数据冗余。</li>
<li>自动failover,提供高可用性的服务。</li>
<li>分散读的负载。</li>
<li>简化维护(相对于master-slave来说)。</li>
<li>灾难恢复。</li>
</ul>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
首先还是启动mongod,用于replica set的参数有两个:</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<tt>--replSet <setname>,复制集的名称。</tt></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<tt>--oplogSize <MB>,操作日志的大小,单位为MB。</tt></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
这回我们在ubuntu下面配置replica set,先启动两个mongod节点。<br></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
mongod--dbpath/home/andyshi/mongo1/--logpath/home/andyshi/mongo1/log.log--replSetshard1--port10001--bind_ip192.168.0.21</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
mongod--dbpath/home/andyshi/mongo2/--logpath/home/andyshi/mongo2/log.log--replSetshard1--port10002--bind_ip192.168.0.21</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
你会注意到,上面两个mongod的启动参数replSet指定了相同的值shard1,也就是两个个mongod节点处于同一个replica set中。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
Replica Set的初始化</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
光启动了两个个mongod节点,还不能提供任何的服务,这时候你使用mongo连接之后,进行db.book.insert会提示你no master,也就是说没有primary节点,所以他不知道往哪一个节点写入数据。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
在启动了两个个mongod节点之后,需要进行初始化。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
首先用mongo连接到任意一个mongod节点,然后执行下面的命令。</p>
<pre><ol style="margin:5px 0px 5px 40px; padding:0px">
<li>cfg={_id:'shard1',members:[</li>
<li>{_id:0,host:'192.168.0.21:10001'},</li>
<li>{_id:1,host:'192.168.0.21:10002'}]</li>
<li>}</li>
<li>rs.initiate(cfg)</li>
</ol></pre>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
出现下面的提示信息就代表成功了,如果没有成功,可以google一下错误提示,会找到很多的答案的。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<pre><ol style="margin:5px 0px 5px 40px; padding:0px">
<li>{</li>
<li>"info":"Confignowsavedlocally.Shouldcomeonlineinaboutaminute.",</li>
<li>"ok":1</li>
<li>}</li>
</ol></pre>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
继续执行</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
rs.status()</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
可以查看replica set的状态,包括名称,时间,当前登录的mongod是primary还是secondary,以及成员的信息等。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
在replica set的信息中,其中重要的是:</p>
<ul style="margin:5px 0px 5px 40px; padding:0px"><li>myState的值,如果是1代表当前登录的是primary;如果是2代表当前登录的是secondary。</li></ul>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
成员信息中包括地址,健康状态,是primary还是secondary等。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
成员信息中比较重要的是</p>
<ul style="margin:5px 0px 5px 40px; padding:0px">
<li><span style="line-height:21px; font-family:宋体,Arial; background-color:rgb(245,247,248)">state:1表示该host是当前可以进行读写,2:不能读写</span></li>
<li><span style="line-height:21px; font-family:宋体,Arial; background-color:rgb(245,247,248)">health:1表示该host目前是正常的,0:异常</span></li>
</ul>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
这时候登录primary的mongod,插入一条数据。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<pre><ol style="margin:5px 0px 5px 40px; padding:0px">
<li>//假设10001是primary,可以通过查询rs.status来获取</li>
<li>mongo192.168.0.21:10001</li>
<li>
<li>usetest</li>
<li>db.book.insert({'title':'computer'})</li>
</ol></pre>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
然后查看secondary的log文件,会发现发生了复制行为。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
这时候登录secondary,use test,db.book.find(),可以报错了。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<pre><ol style="margin:5px 0px 5px 40px; padding:0px"><li>error:{"$err":"notmasterandslaveok=false","code":13435}</li></ol></pre>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
没有关系,在secondary读取数据还需要我们做最后的一步,在需要读取数据的secondary上执行。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
rs.slaveOK()</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
这时候再次db.book.find(),正常显示结果了,没有问题了。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
添加节点</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
启动新mongod节点</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
mongod--dbpath/home/andyshi/mongo3/--logpath/home/andyshi/mongo3/log.log--replSetshard1--port10003--bind_ip192.168.0.21</p>
<pre></pre>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
连接primary节点,执行下面的命令。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<pre><ol style="margin:5px 0px 5px 40px; padding:0px">
<li>rs.add('192.168.0.21:10003')</li>
<li>rs.addArb('192.168.0.21:10003')</li>
<li>//重新配置</li>
<li>rs.reconfig(rs.conf())</li>
</ol></pre>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
强制一个节点成为primary</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
在mongodb2.0之后可以使用下面</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
相比master-slave,replica set的优点就是没有单点故障,primary故障之后,整个replica set会自动选择一个健康的节点成为primary,承担写的任务,可用性比master-slave的高,提供更高的可用性。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<br></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<strong>遇到的问题:</strong></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
在replset运行正常的情况下,停止服务,更换端口,重新分配set,造成的问题。</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
可通过use admin db.runcommand({replSetReconfig:config})从新分配,其中config={} 是set的新配置;当此法还无法解决问题时,可以更改local.system.replset的记录,或者重做local表了</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
具体可借鉴:<a rel="nofollow" href="http://groups.google.com/group/mongoid/browse_thread/thread/6291b8b80158031e" style="color:rgb(121,75,21); text-decoration:initial">http://groups.google.com/group/mongoid/browse_thread/thread/6291b8b80158031e</a></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<a rel="nofollow" href="http://blog.nroed.com/2011/12/15/mongo-replset-badconfig/?replytocom=489" style="color:rgb(121,75,21); text-decoration:initial">http://blog.nroed.com/2011/12/15/mongo-replset-badconfig/?replytocom=489</a></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
<a rel="nofollow" href="http://groups.google.com/group/mongoid/browse_thread/thread/6291b8b80158031e?pli=1" style="color:rgb(121,75,21); text-decoration:initial">http://groups.google.com/group/mongoid/browse_thread/thread/6291b8b80158031e?pli=1</a></p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
run rs.initiate(...) if not yet done for the set</p>
<p style="margin-top:0px; margin-bottom:10px; padding-top:0px; padding-bottom:0px">
local.oplog.rs is not empty on the initiating member. cannot initiate.</p>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif">loading local.system.replset config (LOADINGCONFIG)</span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<span style="font-family:Helvetica,Tahoma,Arial,sans-serif"><br></span>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<br>
</div>
<div style="line-height:25px; color:rgb(78,78,78); font-family:Arial,Helvetica,simsun,u5b8bu4f53; font-size:14px">
<strong>集群配置</strong>
</div>
<br></wbr>