<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ElmerZhang&#039;s Blog</title>
	<atom:link href="http://www.elmerzhang.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.elmerzhang.com</link>
	<description>又一个 WordPress 站点</description>
	<lastBuildDate>Wed, 28 Sep 2011 08:11:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>NodeJS 模块开发及发布详解</title>
		<link>http://www.elmerzhang.com/2011/09/nodejs-module-develop-publish/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=nodejs-module-develop-publish</link>
		<comments>http://www.elmerzhang.com/2011/09/nodejs-module-develop-publish/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 07:24:19 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[NPM]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=484</guid>
		<description><![CDATA[NodeJS 是一门年轻的语言，扩展模块并不太全，经常我们想用某个模块但是却找不到合适的。比如前两天我需要使用hmac和sha1来做签名，就没有找到一个比较好用的模块，这时候就需要我们自己来实现相应的功能了。自己写完之后，再把它打包成一个模块分享给大家来用，即方便了其他人，又能让自己有点小小的成就感，实在是一件一举多得的好事情。接下来，我就为大家介绍一下如何封装一个NodeJS模块并把它分享给其他人。 NPM (Node Package Manager, http://npmjs.org ) 是 NodeJS 的模块管理软件，除 NodeJS 内置的核心模块外，其他模块的安装、卸载等管理操作都要通过 NPM 来进行，我们自己写的模块，就要发布到NPM上来供其他人使用。 接下来，我们做一个非常简单的模块 “hello”，这个模块的功能只有一个：提供一个参数 “name” ，它在控制台输出 “Hello name”。在开始之前，我们首先要把node和npm装好，安装方法在其官方网站都有介绍，这里就不再多说了。 首先，我们创建一个名为”hello”的目录，作为模块的主目录。进入该目录，开始我们的工作。 然后，写模块的核心代码，很简单，只有以下三行： exports.Hello = function ( name ) { console.log( "Hello " + name ); } 把它保存为 hello.js。 NodeJS每个扩展模块中都有一个package.json文件，用来描述模块的一些基本属性，比如模块名称、作者、版本号等等。关于package.json写法的详细说明，可以使用 &#8230;<p class="read-more"><a href="http://www.elmerzhang.com/2011/09/nodejs-module-develop-publish/">继续阅读 &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>NodeJS 是一门年轻的语言，扩展模块并不太全，经常我们想用某个模块但是却找不到合适的。比如前两天我需要使用hmac和sha1来做签名，就没有找到一个比较好用的模块，这时候就需要我们自己来实现相应的功能了。自己写完之后，再把它打包成一个模块分享给大家来用，即方便了其他人，又能让自己有点小小的成就感，实在是一件一举多得的好事情。接下来，我就为大家介绍一下如何封装一个NodeJS模块并把它分享给其他人。</p>
<p><span id="more-484"></span>NPM (Node Package Manager, http://npmjs.org ) 是 NodeJS 的模块管理软件，除 NodeJS 内置的核心模块外，其他模块的安装、卸载等管理操作都要通过 NPM 来进行，我们自己写的模块，就要发布到NPM上来供其他人使用。</p>
<p>接下来，我们做一个非常简单的模块 “hello”，这个模块的功能只有一个：提供一个参数 “name” ，它在控制台输出 “Hello name”。在开始之前，我们首先要把node和npm装好，安装方法在其官方网站都有介绍，这里就不再多说了。</p>
<p>首先，我们创建一个名为”hello”的目录，作为模块的主目录。进入该目录，开始我们的工作。</p>
<p>然后，写模块的核心代码，很简单，只有以下三行：</p>
<pre>exports.Hello = function ( name ) {
    console.log( "Hello " + name );
}</pre>
<p>把它保存为 hello.js。</p>
<p>NodeJS每个扩展模块中都有一个package.json文件，用来描述模块的一些基本属性，比如模块名称、作者、版本号等等。关于package.json写法的详细说明，可以使用 “npm help json” 命令来查看。</p>
<p>我们可以在模块主目录下执行 npm init 来生成一个最基本的package.json。按照命令的提示依次输入信息即可。以下是在 hello 目录下执行 npm init 并填入相关信息后的结果：</p>
<pre>$ npm init
Package name: (hello)     //模块名字，npm init会自动取当前目录名作为默认名字，这里不需要改，直接确认即可
Description: A example for write a module    //模块说明
Package version: (0.0.0) 0.0.1    //模块版本号，这个大家按自己习惯来定就可以
Project homepage: (none)     //模块的主页，如果有的话可以填在这里，也可以不填
Project git repository: (none)    //模块的git仓库，选填。npm的用户一般都使用github做为自己的git仓库
Author name: Elmer Zhang    //模块作者名字
Author email: (none) freeboy6716@gmail.com     //模块作者邮箱
Author url: (none) http://www.elmerzhang.com    //模块作者URL
Main module/entry point: (none) hello.js     //模块的入口文件，我们这里是hello.js
Test command: (none)    //测试脚本，选填
What versions of node does it run on? (~v0.5.7) *   //依赖的node版本号，我们这个脚本可以运行在任何版本的node上，因此填 *
About to write to /home/elmer/hello/package.json
// 以下是生成的package.json文件内容预览
{
  "author": "Elmer Zhang &lt;freeboy6716@gmail.com&gt; (http://www.elmerzhang.com)",
  "name": "hello",
  "description": "A example for write a module",
  "version": "0.0.1",
  "repository": {
    "url": ""
  },
  "main": "hello.js",
  "engines": {
    "node": "*"
  },
  "dependencies": {},
  "devDependencies": {}
}

Is this ok? (yes)   //对以上内容确认无误后，就可以直接回车确认了</pre>
<p>到此为止，我们这个模块就写完了。这时hello目录下应该有两个文件：hello.js和package.json。</p>
<p>我们可以返回到hello的上级目录，来测试安装一下这个模块：</p>
<pre>$ npm install hello/
hello@0.0.1 ./node_modules/hello</pre>
<p>显示安装成功。简单的测试一下：</p>
<pre>$ node
&gt; var Hello = require('hello').Hello;
&gt; Hello('world');
Hello world</pre>
<p>正确输出了”Hello world”。</p>
<p>接下来我们把它发布到NPM上。</p>
<p>首先，我们需要有一个NPM帐号，可以使用npm adduser来注册一个：</p>
<pre>$ npm adduser
Username: elmerzhang
Password:
Email: freeboy6716@gmail.com</pre>
<p>简单三步，一个NPM用户注册成功。</p>
<p>最后回到 hello 根目录，执行一下npm publish，如果没有任何错误提示，那么就发布成功了。去 <a href="http://search.npmjs.org/">http://search.npmjs.org/</a>上看一下吧，你的模块应该已经显示在”Latest Updates”一栏里了。</p>
<p>至此，一个NodeJS模块成功发布到NPM，以后就可以在任何能访问npm库的地方通过npm install来安装你的模块了。</p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F09%2Fnodejs-module-develop-publish%2F&amp;linkname=NodeJS%20%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E5%8F%8A%E5%8F%91%E5%B8%83%E8%AF%A6%E8%A7%A3" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F09%2Fnodejs-module-develop-publish%2F&amp;linkname=NodeJS%20%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E5%8F%8A%E5%8F%91%E5%B8%83%E8%AF%A6%E8%A7%A3" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F09%2Fnodejs-module-develop-publish%2F&amp;linkname=NodeJS%20%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E5%8F%8A%E5%8F%91%E5%B8%83%E8%AF%A6%E8%A7%A3" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F09%2Fnodejs-module-develop-publish%2F&amp;linkname=NodeJS%20%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E5%8F%8A%E5%8F%91%E5%B8%83%E8%AF%A6%E8%A7%A3" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F09%2Fnodejs-module-develop-publish%2F&amp;linkname=NodeJS%20%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E5%8F%8A%E5%8F%91%E5%B8%83%E8%AF%A6%E8%A7%A3" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F09%2Fnodejs-module-develop-publish%2F&amp;title=NodeJS%20%E6%A8%A1%E5%9D%97%E5%BC%80%E5%8F%91%E5%8F%8A%E5%8F%91%E5%B8%83%E8%AF%A6%E8%A7%A3">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2011/09/nodejs-module-develop-publish/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MongoDB 使用经验小结</title>
		<link>http://www.elmerzhang.com/2011/05/mongodb-experience/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mongodb-experience</link>
		<comments>http://www.elmerzhang.com/2011/05/mongodb-experience/#comments</comments>
		<pubDate>Tue, 31 May 2011 09:37:34 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[replica]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=462</guid>
		<description><![CDATA[1. Database数量多了之后，mongod的启动会需要很长的时间，期间进程绝大多数时间处于R(Running or runnable)和D(Uninterruptible sleep) 状态。这段时间mongod是在做init，会读取所有Database的namespace文件的header，检查pdfile version。 2. listDatabases指令(show dbs;)会首先取得所有Database名称，然后依次打开所有Database的namespace，检查Database是否为空。是一个非常消耗IO的操作，Database数量多了之后，也会非常慢，应该尽量避免执行listDatabases指令。注：mongodump导出全部Databases和Replica Set(或者Master/Slave)中从库初始同步时会发送listDatabases指令，这两个操作应该尽量避免。 3. dbStats 也是一个非常耗资源的操作，要尽量少做。具体它是怎么实现的，为何如此耗资源，由于本人C++水平有限暂时没有研究出来。 4. 上面说过了，在Replica Set中直接添加一个新的空节点，新节点的initialSync会使它的source产生非常大的IO消耗，严重影响性能。因此，添加新节点时要使用initialSync指定一个不提供线上服务的节点为source。如果没有提前配置一个hidden的节点，可以直接停掉某个secondary（当然，前提是剩下的节点能够撑得住线上服务的压力），然后将此secondary的文件直接复制一份来创建一个新的节点。这个方法也适用于做冷备。具体步骤我会另写一篇BLOG来介绍。 5. 前几天和同事交流时得知他们仍然在用Master/Slave方式来做主从，其实现在MongoDB的Replica Set已经非常稳定了，它完全自动的failover是非常爽的，大家可以直接使用Replica Set，抛弃M/S了，正在使用M/S方式的也建议升级到Replica Set.]]></description>
			<content:encoded><![CDATA[<p>1. Database数量多了之后，mongod的启动会需要很长的时间，期间进程绝大多数时间处于R(Running or runnable)和D(Uninterruptible sleep) 状态。这段时间mongod是在做init，会读取所有Database的namespace文件的header，检查pdfile version。</p>
<p>2. listDatabases指令(show dbs;)会首先取得所有Database名称，然后依次打开所有Database的namespace，检查Database是否为空。是一个非常消耗IO的操作，Database数量多了之后，也会非常慢，<span id="more-462"></span>应该尽量避免执行listDatabases指令。注：mongodump导出全部Databases和Replica Set(或者Master/Slave)中从库初始同步时会发送listDatabases指令，这两个操作应该尽量避免。</p>
<p>3. dbStats 也是一个非常耗资源的操作，要尽量少做。具体它是怎么实现的，为何如此耗资源，由于本人C++水平有限暂时没有研究出来。</p>
<p>4. 上面说过了，在Replica Set中直接添加一个新的空节点，新节点的initialSync会使它的source产生非常大的IO消耗，严重影响性能。因此，添加新节点时要使用initialSync指定一个不提供线上服务的节点为source。如果没有提前配置一个hidden的节点，可以直接停掉某个secondary（当然，前提是剩下的节点能够撑得住线上服务的压力），然后将此secondary的文件直接复制一份来创建一个新的节点。这个方法也适用于做冷备。具体步骤我会另写一篇BLOG来介绍。</p>
<p>5. 前几天和同事交流时得知他们仍然在用Master/Slave方式来做主从，其实现在MongoDB的Replica Set已经非常稳定了，它完全自动的failover是非常爽的，大家可以直接使用Replica Set，抛弃M/S了，正在使用M/S方式的也建议升级到Replica Set.</p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F05%2Fmongodb-experience%2F&amp;linkname=MongoDB%20%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C%E5%B0%8F%E7%BB%93" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F05%2Fmongodb-experience%2F&amp;linkname=MongoDB%20%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C%E5%B0%8F%E7%BB%93" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F05%2Fmongodb-experience%2F&amp;linkname=MongoDB%20%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C%E5%B0%8F%E7%BB%93" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F05%2Fmongodb-experience%2F&amp;linkname=MongoDB%20%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C%E5%B0%8F%E7%BB%93" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F05%2Fmongodb-experience%2F&amp;linkname=MongoDB%20%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C%E5%B0%8F%E7%BB%93" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F05%2Fmongodb-experience%2F&amp;title=MongoDB%20%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C%E5%B0%8F%E7%BB%93">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2011/05/mongodb-experience/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>我喜欢用的Chrome插件</title>
		<link>http://www.elmerzhang.com/2011/04/my-favorite-chrome-plugins/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-favorite-chrome-plugins</link>
		<comments>http://www.elmerzhang.com/2011/04/my-favorite-chrome-plugins/#comments</comments>
		<pubDate>Thu, 14 Apr 2011 02:20:48 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[互联网]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=427</guid>
		<description><![CDATA[原本是发在微博上的，在这里也发一遍跟大家分享一下： 1. QR-Code Tag Extension 可以很方便的把网页上的网址、文字啥的转成二维码，拍一下就到手机上了。 2. 网页截图（由Google提供） 可以截定制区域，截可见区域，截整页，截的图还能直接分享到新浪微博。 3. Proxy Switchy! 翻墙必用，可以配置只对某些站点使用代理。 4. RSS Subscription Extension（由 Google 提供） 在地址栏中添加一个RSS订阅按钮，可以很方便的订阅博客到Google Reader。 5. XML Tree 和 Pretty JSON 测试REST接口时看XML和JSON格式的返回数据用的。 6. TinEye Reverse Image Search 图片搜索，有时遇到一些带配图的骗子微博，一搜就知道图片原始来源了]]></description>
			<content:encoded><![CDATA[<p>原本是发在微博上的，在这里也发一遍跟大家分享一下：</p>
<p>1. <a href="https://chrome.google.com/webstore/detail/bcfddoencoiedfjgepnlhcpfikgaogdg">QR-Code Tag Extension</a><br />
可以很方便的把网页上的网址、文字啥的转成二维码，拍一下就到手机上了。</p>
<p>2. <a href="https://chrome.google.com/webstore/detail/cpngackimfmofbokmjmljamhdncknpmg">网页截图（由Google提供）</a><br />
可以截定制区域，截可见区域，截整页，截的图还能直接分享到新浪微博。</p>
<p>3. <a href="https://chrome.google.com/webstore/detail/caehdcpeofiiigpdhbabniblemipncjj">Proxy Switchy!</a><br />
翻墙必用，可以配置只对某些站点使用代理。</p>
<p>4. <a href="https://chrome.google.com/webstore/detail/nlbjncdgjeocebhnmkbbbdekmmmcbfjd">RSS Subscription Extension（由 Google 提供）<br />
</a>在地址栏中添加一个RSS订阅按钮，可以很方便的订阅博客到Google Reader。</p>
<p>5. <a href="https://chrome.google.com/webstore/detail/gbammbheopgpmaagmckhpjbfgdfkpadb">XML Tree</a> 和 <a href="https://chrome.google.com/webstore/detail/ddngkjbldiejbheifcmnfmmfiniimbbg">Pretty JSON</a><br />
测试REST接口时看XML和JSON格式的返回数据用的。</p>
<p>6. <a href="https://chrome.google.com/webstore/detail/haebnnbpedcbhciplfhjjkbafijpncjl">TinEye Reverse Image Search</a><br />
图片搜索，有时遇到一些带配图的骗子微博，一搜就知道图片原始来源了</p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F04%2Fmy-favorite-chrome-plugins%2F&amp;linkname=%E6%88%91%E5%96%9C%E6%AC%A2%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F04%2Fmy-favorite-chrome-plugins%2F&amp;linkname=%E6%88%91%E5%96%9C%E6%AC%A2%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F04%2Fmy-favorite-chrome-plugins%2F&amp;linkname=%E6%88%91%E5%96%9C%E6%AC%A2%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F04%2Fmy-favorite-chrome-plugins%2F&amp;linkname=%E6%88%91%E5%96%9C%E6%AC%A2%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F04%2Fmy-favorite-chrome-plugins%2F&amp;linkname=%E6%88%91%E5%96%9C%E6%AC%A2%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F04%2Fmy-favorite-chrome-plugins%2F&amp;title=%E6%88%91%E5%96%9C%E6%AC%A2%E7%94%A8%E7%9A%84Chrome%E6%8F%92%E4%BB%B6">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2011/04/my-favorite-chrome-plugins/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>CentOS 下将ext3分区转为ext4格式</title>
		<link>http://www.elmerzhang.com/2011/03/change-filesystem-from-ext3-to-ext4-centos/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=change-filesystem-from-ext3-to-ext4-centos</link>
		<comments>http://www.elmerzhang.com/2011/03/change-filesystem-from-ext3-to-ext4-centos/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 07:15:20 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[Linux/Unix]]></category>
		<category><![CDATA[ext3]]></category>
		<category><![CDATA[ext4]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=412</guid>
		<description><![CDATA[系统环境： 发行版本： CentOS release 5.4 (Final) Kernel:  Linux localhost.localdomain 2.6.18-164.el5 #1 SMP Thu Sep 3 03:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux 假设要转换的分区为/dev/sda7，已挂载在/data0目录上 首先安装 ext4 支持： yum install -y e4fsprogs 然后取消挂载 /dev/sda7 umount /dev/sda7 如果分区正在使用中，无法取消挂载，可以先用 lsof &#124; grep data0 查出哪些进程正在使用 /data0 &#8230;<p class="read-more"><a href="http://www.elmerzhang.com/2011/03/change-filesystem-from-ext3-to-ext4-centos/">继续阅读 &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>系统环境：</p>
<div id="_mcePaste">发行版本： CentOS release 5.4 (Final)</div>
<div>Kernel:  Linux localhost.localdomain 2.6.18-164.el5 #1 SMP Thu Sep 3 03:28:30 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux</div>
<div>假设要转换的分区为/dev/sda7，已挂载在/data0目录上</div>
<div>首先安装 ext4 支持：</div>
<pre>yum install -y e4fsprogs</pre>
<div><span id="more-412"></span>然后取消挂载 /dev/sda7</div>
<pre>umount /dev/sda7</pre>
<div>如果分区正在使用中，无法取消挂载，可以先用</div>
<pre>lsof | grep data0</pre>
<div>查出哪些进程正在使用 /data0 目录，将其kill掉。然后再重新umount。</div>
<div>接下来在ext3分区上启用ext4的特性：</div>
<pre>tune4fs -O extents,uninit_bg,dir_index /dev/sda7</pre>
<div>然后修复一下转换好的ext4分区：</div>
<pre>e4fsck -yfDC0 /dev/sda7</pre>
<div>最后把 /dev/sda7 重新 mount 到 /data0</div>
<pre>mount /dev/sda7 /data0</pre>
<div>我们再使用mount查看一下/dev/sda7的格式，发现已经变成ext4了</div>
<pre>[root@localhost data0]# mount
/dev/sda1 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
.............................
/dev/sda7 on /data0 type ext4 (rw)</pre>
<div><span style="color: #ff0000;">到这儿还没完，最后一步非常重要，一定要记得把/etc/fstab中相应条目的挂载格式改成ext4，不然下次启动就启动不起来了</span></div>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fchange-filesystem-from-ext3-to-ext4-centos%2F&amp;linkname=CentOS%20%E4%B8%8B%E5%B0%86ext3%E5%88%86%E5%8C%BA%E8%BD%AC%E4%B8%BAext4%E6%A0%BC%E5%BC%8F" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fchange-filesystem-from-ext3-to-ext4-centos%2F&amp;linkname=CentOS%20%E4%B8%8B%E5%B0%86ext3%E5%88%86%E5%8C%BA%E8%BD%AC%E4%B8%BAext4%E6%A0%BC%E5%BC%8F" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fchange-filesystem-from-ext3-to-ext4-centos%2F&amp;linkname=CentOS%20%E4%B8%8B%E5%B0%86ext3%E5%88%86%E5%8C%BA%E8%BD%AC%E4%B8%BAext4%E6%A0%BC%E5%BC%8F" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fchange-filesystem-from-ext3-to-ext4-centos%2F&amp;linkname=CentOS%20%E4%B8%8B%E5%B0%86ext3%E5%88%86%E5%8C%BA%E8%BD%AC%E4%B8%BAext4%E6%A0%BC%E5%BC%8F" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fchange-filesystem-from-ext3-to-ext4-centos%2F&amp;linkname=CentOS%20%E4%B8%8B%E5%B0%86ext3%E5%88%86%E5%8C%BA%E8%BD%AC%E4%B8%BAext4%E6%A0%BC%E5%BC%8F" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fchange-filesystem-from-ext3-to-ext4-centos%2F&amp;title=CentOS%20%E4%B8%8B%E5%B0%86ext3%E5%88%86%E5%8C%BA%E8%BD%AC%E4%B8%BAext4%E6%A0%BC%E5%BC%8F">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2011/03/change-filesystem-from-ext3-to-ext4-centos/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MongoDB Auto-Sharding 入门介绍</title>
		<link>http://www.elmerzhang.com/2011/03/mongodb-auto-sharding-introduction/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mongodb-auto-sharding-introduction</link>
		<comments>http://www.elmerzhang.com/2011/03/mongodb-auto-sharding-introduction/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 02:24:26 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[MongoDB Sharding Auto-Sharding]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=405</guid>
		<description><![CDATA[MongoDB是10gen团队开发的一款面向文档的NoSQL数据库。最近一年多以来，MongoDB被越来越多的大型网站应用到生产环境中，比较著名的有Foursquare, bit.ly, SourceForge, Boxed等。MongoDB提供了Auto-Sharding功能，使用者通过简单的配置就可以很方便地构建一个分布式MongoDB集群。 MongoDB的Auto-Sharding能够做到： 当各Sharding间负载和数据分布不平衡时，自动rebalancing 简单方便的添加和删除节点 自动故障转移(auto failover) 可扩展至上千台节点 一个MongoDB Sharding由三部分组成： 1. Shards Shard即存储实际数据的分片，每个Shard可以是一个mongod实例，也可以是一组mongod实例构成的Replica Set。为了实现每个Shard内部的auto-failover，MongoDB官方建议每个Shard为一组Replica Set。 2. Config Servers 为了将一个collection拆分为多个chunk，存储在多个shard中，需要为该collection指定一个shard key. 例如{name: 1}, {_id: 1}, {lastname:1, firstname:1}等。shard key决定了该条记录属于哪个chunk，例如当1 &#60; shard key &#60; 100时为一个chunk，该chunk保存在shard1上。而Config Servers就是用来存储：所有shard节点的配置信息；每个chunk的shard key范围；chunk在各shard的分布；该集群中所有DB和collection的sharding配置。 3. Routing Process MongoDB的二进制包中有一个mongos程序，它是用来做为MongoDB集群的Routing &#8230;<p class="read-more"><a href="http://www.elmerzhang.com/2011/03/mongodb-auto-sharding-introduction/">继续阅读 &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 0.56cm; widows: 2; orphans: 2;"><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">是</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">10gen</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">团队开发的一款面向文档的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">NoSQL</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">数据库。最近一年多以来，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">被越来越多的大型网站应用到生产环境中，比较著名的有</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Foursquare, bit.ly, SourceForge, Boxed</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">等。</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">提供了</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Auto-Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">功能，使用者通过简单的配置就可以很方便地构建一个分布式</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">集群。</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0.5cm; line-height: 150%; widows: 2; orphans: 2;"><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Auto-Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">能够做到：</span></span></span></span></span></p>
<ul>
<li>
<p style="margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">当各</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">间负载和数据分布不平衡时，自动</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">rebalancing</span></span></span></span></span></span></p>
</li>
<li>
<p style="margin-bottom: 0cm; font-style: normal; font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;">简单方便的添加和删除节点</span></span></span></p>
</li>
<li>
<p style="margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">自动故障转移</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">(auto 	failover)</span></span></span></span></span></span></p>
</li>
<li>
<p style="margin-bottom: 0cm; font-style: normal; font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;">可扩展至上千台节点</span></span></span></p>
</li>
</ul>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span id="more-405"></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">由三部分组成：</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; font-style: normal; font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;">1. Shards</span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">即存储实际数据的分片，每个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">可以是一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongod</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">实例，也可以是一组</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongod</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">实例构成的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Replica Set</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">。为了实现每个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">内部的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">auto-failover</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">官方建议每个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">为一组</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Replica Set</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">。</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; font-style: normal; font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;">2. Config Servers</span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">为了将一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">collection</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">拆分为多个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，存储在多个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">中，需要为该</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">collection</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">指定一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard key. </span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">例如</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">{name: 1}, {_id: 1}, {lastname:1, firstname:1}</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">等。</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard key</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">决定了该条记录属于哪个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，例如当</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">1 &lt; shard key &lt; 100</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">时为一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，该</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">保存在</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard1</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上。而</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Config Servers</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">就是用来存储：所有</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">节点的配置信息；每个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard key</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">范围；</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">在各</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的分布；该集群中所有</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">DB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">和</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">collection</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">配置。</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; font-style: normal; font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;">3. Routing Process</span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的二进制包中有一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongos</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">程序，它是用来做为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">集群的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Routing Process</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的。它相当于一个透明代理，接收来自客户端的查询或更新请求，然后询问</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Config Servers</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">需要到哪个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上查询或保存记录，再连接相应的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">进行操作，最后将结果返回给客户端。客户端只需要将原本发给</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongod</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的查询或更新请求原封不动地发给</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Routing Process</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，而不必关心所操作的记录存储在哪个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上。</span></span></span></span></span></p>
<p style="margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;">
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">接下来我就为大家介绍一下如何搭建一个简单的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">集群用来测试</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Auto-Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">功能。</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">这个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">集群将包含两个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shards</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Config Server</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">和一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Routing Process</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">。我们将使用</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB 1.6.5</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">来做这个测试，下载地址为： </span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000080;"><span lang="zxx"><span style="text-decoration: underline;"><a href="http://www.mongodb.org/downloads"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">http://www.mongodb.org/downloads</span></span></span></span></a></span></span></span></span></p>
<p style="text-indent: 0.74cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">首先，我们为两个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shards</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">和一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Config Server</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">创建数据目录：</span></span></span></span></span></p>
<pre class="cjk" style="margin-bottom: 0.5cm; font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>sudo mkdir -p /data0/mongo/shard1 /data0/mongo/shard2 /data0/mongo/config</em></span></span></span></pre>
<p style="text-indent: 0.74cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">然后，我们依次启动两个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongod</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">进程作为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongod</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">进程作为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Config Server</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，一个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongos</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">进程作为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Routing Process</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">：</span></span></span></span></span></p>
<pre class="cjk" style="font-weight: normal; line-height: 0.56cm; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>sudo mongod --port 27017 --fork --logpath /var/log/mongo_shard1.log --dbpath /data0/mongo/shard1 --shardsvr</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>sudo mongod --port 27018 --fork --logpath /var/log/mongo_shard2.log --dbpath /data0/mongo/shard2 --shardsvr</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>sudo mongod --port 27217 --fork --logpath /var/log/mongo_config.log --dbpath /data0/mongo/config --configsvr</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>sudo mongos --port 27417 --fork --logpath /var/log/mongos.log --configdb 127.0.0.1:27217 --chunkSize 1</em></span></span></span></pre>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongos</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">启动参数中，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunkSize</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">这一项是用来指定</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的大小的，单位是</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，默认大小为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">200MB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，为了方便测试</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">效果，我们把</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">chunkSize</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">指定为 </span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">1MB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">。</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">接下来，我们使用</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongo shell</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">登录到</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongos</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，添加</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">节点：</span></span></span></span></span></p>
<pre class="cjk" style="font-weight: normal; line-height: 0.56cm; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>mongo --port 27417</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>MongoDB shell version: 1.6.5</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>connecting to: 127.0.0.1:27417/test</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>&gt; use admin;</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>switched to db admin</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>&gt; db.runCommand({addshard:"127.0.0.1:27017"})</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>{ "shardAdded" : "shard0000", "ok" : 1 }</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>&gt; db.runCommand({addshard:"127.0.0.1:27018"})</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>{ "shardAdded" : "shard0001", "ok" : 1 }</em></span></span></span></pre>
<p style="text-indent: 0.74cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">下面我们为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">DataBase “foo”</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">启用</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，并将其中的 </span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Collection “col” </span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的 </span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard key</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">设置为</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">“{_id: 1}”</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，用来测试</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">功能：</span></span></span></span></span></p>
<pre class="cjk" style="font-weight: normal; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>&gt; db.runCommand({enablesharding:'foo'});</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>{ "ok" : 1 }</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>&gt; db.runCommand({shardcollection:"foo.col", key:{_id:1}});</em></span></span></span>
<span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><em>{ "collectionsharded" : "foo.col", "ok" : 1 }</em></span></span></span></pre>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">为了测试</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">balance</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">效果，我陆续插入了大约</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">200M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的数据，插入过程中使用</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">db.stats() </span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">查询数据分布情况。发现在数据量较小，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">30M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">以下时，所有</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">trunk</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">都存储在了</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard0000</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上，但继续插入后，数据开始平均分布，并且</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">mongos</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">会对多个</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">之间的数据进行</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">rebalance </span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">。在插入数据达到</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">200M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，刚插入结束时，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard0000</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上大约有</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">135M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">数据，而</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard0001</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上大约有</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">65M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">数据，但过一段时间之后，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard0000</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上的数据量减少到了</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">115M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">shard0001</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">上的数据量达到了</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">85M</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">。</span></span></span></span></span></p>
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;">
<p style="text-indent: 0.74cm; margin-bottom: 0cm; line-height: 150%; widows: 2; orphans: 2;"><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">的</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">Auto-Sharding</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">功能自</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">1.6</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">版本开始才</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">production-ready</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">，至今不过半年多的时间，大多数公司仍在观望中，不敢将其用到生产环境，因此目前网上并没有太多相关资料可以参考。今后我会陆续为大家分享更多</span></span></span></span></span><span style="font-family: 'DejaVu Serif', serif;"><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">MongoDB</span></span></span></span></span></span><span style="color: #000000;"><span style="font-family: Verdana, Arial;"><span style="font-size: x-small;"><span style="font-style: normal;"><span style="font-weight: normal;">使用过程中的经验心得。</span></span></span></span></span></p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fmongodb-auto-sharding-introduction%2F&amp;linkname=MongoDB%20Auto-Sharding%20%E5%85%A5%E9%97%A8%E4%BB%8B%E7%BB%8D" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fmongodb-auto-sharding-introduction%2F&amp;linkname=MongoDB%20Auto-Sharding%20%E5%85%A5%E9%97%A8%E4%BB%8B%E7%BB%8D" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fmongodb-auto-sharding-introduction%2F&amp;linkname=MongoDB%20Auto-Sharding%20%E5%85%A5%E9%97%A8%E4%BB%8B%E7%BB%8D" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fmongodb-auto-sharding-introduction%2F&amp;linkname=MongoDB%20Auto-Sharding%20%E5%85%A5%E9%97%A8%E4%BB%8B%E7%BB%8D" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fmongodb-auto-sharding-introduction%2F&amp;linkname=MongoDB%20Auto-Sharding%20%E5%85%A5%E9%97%A8%E4%BB%8B%E7%BB%8D" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F03%2Fmongodb-auto-sharding-introduction%2F&amp;title=MongoDB%20Auto-Sharding%20%E5%85%A5%E9%97%A8%E4%BB%8B%E7%BB%8D">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2011/03/mongodb-auto-sharding-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>本博客自今日起迁移至Sina App Engine</title>
		<link>http://www.elmerzhang.com/2011/01/moved-this-blog-to-sina-app-engine/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=moved-this-blog-to-sina-app-engine</link>
		<comments>http://www.elmerzhang.com/2011/01/moved-this-blog-to-sina-app-engine/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 02:29:36 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[SAE]]></category>
		<category><![CDATA[Sina App Engine]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=397</guid>
		<description><![CDATA[买的Godaddy虚拟机的IP被联通给封掉了，公司里访问不了BLOG，也懒得找代理，于是博客停止更新了N个月。 这几天花时间自己改了一个WordPress 3.0.4 for SAE，把博客迁了过来，以后恢复更新，争取每月至少两篇博客。 由于这个WordPress是基于SAE正在测试的新PHP环境修改的，还不适用于目前的SAE线上环境，而且某些插件还有些小问题没有修改，因此暂时还是自己用。等明年SAE新PHP环境上线后，我会把这个WordPress for SAE放在Google Code上和大家分享。 快过年了，预祝大家新年快乐。 附上SAE(Sina App Engine)地址，欢迎大家体验SAE: http://sae.sina.com.cn]]></description>
			<content:encoded><![CDATA[<p>买的Godaddy虚拟机的IP被联通给封掉了，公司里访问不了BLOG，也懒得找代理，于是博客停止更新了N个月。</p>
<p>这几天花时间自己改了一个WordPress 3.0.4 for SAE，把博客迁了过来，以后恢复更新，争取每月至少两篇博客。</p>
<p>由于这个WordPress是基于SAE正在测试的新PHP环境修改的，还不适用于目前的SAE线上环境，而且某些插件还有些小问题没有修改，因此暂时还是自己用。等明年SAE新PHP环境上线后，我会把这个WordPress for SAE放在Google Code上和大家分享。</p>
<p>快过年了，预祝大家新年快乐。</p>
<p>附上SAE(Sina App Engine)地址，欢迎大家体验SAE: <a href="http://sae.sina.com.cn" target="_blank">http://sae.sina.com.cn</a></p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F01%2Fmoved-this-blog-to-sina-app-engine%2F&amp;linkname=%E6%9C%AC%E5%8D%9A%E5%AE%A2%E8%87%AA%E4%BB%8A%E6%97%A5%E8%B5%B7%E8%BF%81%E7%A7%BB%E8%87%B3Sina%20App%20Engine" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F01%2Fmoved-this-blog-to-sina-app-engine%2F&amp;linkname=%E6%9C%AC%E5%8D%9A%E5%AE%A2%E8%87%AA%E4%BB%8A%E6%97%A5%E8%B5%B7%E8%BF%81%E7%A7%BB%E8%87%B3Sina%20App%20Engine" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F01%2Fmoved-this-blog-to-sina-app-engine%2F&amp;linkname=%E6%9C%AC%E5%8D%9A%E5%AE%A2%E8%87%AA%E4%BB%8A%E6%97%A5%E8%B5%B7%E8%BF%81%E7%A7%BB%E8%87%B3Sina%20App%20Engine" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F01%2Fmoved-this-blog-to-sina-app-engine%2F&amp;linkname=%E6%9C%AC%E5%8D%9A%E5%AE%A2%E8%87%AA%E4%BB%8A%E6%97%A5%E8%B5%B7%E8%BF%81%E7%A7%BB%E8%87%B3Sina%20App%20Engine" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F01%2Fmoved-this-blog-to-sina-app-engine%2F&amp;linkname=%E6%9C%AC%E5%8D%9A%E5%AE%A2%E8%87%AA%E4%BB%8A%E6%97%A5%E8%B5%B7%E8%BF%81%E7%A7%BB%E8%87%B3Sina%20App%20Engine" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2011%2F01%2Fmoved-this-blog-to-sina-app-engine%2F&amp;title=%E6%9C%AC%E5%8D%9A%E5%AE%A2%E8%87%AA%E4%BB%8A%E6%97%A5%E8%B5%B7%E8%BF%81%E7%A7%BB%E8%87%B3Sina%20App%20Engine">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2011/01/moved-this-blog-to-sina-app-engine/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>关于PHP对文件元信息的缓存</title>
		<link>http://www.elmerzhang.com/2010/06/php-stat-cache/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php-stat-cache</link>
		<comments>http://www.elmerzhang.com/2010/06/php-stat-cache/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 10:36:43 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Cache]]></category>
		<category><![CDATA[clearstatcache]]></category>
		<category><![CDATA[stat]]></category>
		<category><![CDATA[Wrapper]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=391</guid>
		<description><![CDATA[今天在测试 SAE （Sina App Engine） 的Memcache Wrapper 时发现，PHP会在同一个页面的执行过程中对文件的元信息进行缓存。 根据PHP文档对 clearstatcache() 这个方法的说明得知： 在使用 stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), 或 fileperms() 方法查询文件信息时，PHP会将文件的stat的缓存以提高性能。 clearstatcache()方法可以用来清除这个缓存。另外unlink()会自动清除stat缓存。 例如以下一段程序： ?View Code PHP1 2 3 4 &#8230;<p class="read-more"><a href="http://www.elmerzhang.com/2010/06/php-stat-cache/">继续阅读 &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>今天在测试 <a href="http://sae.sina.com.cn" target="_blank">SAE （Sina App Engine）</a> 的Memcache Wrapper 时发现，PHP会在同一个页面的执行过程中对文件的元信息进行缓存。</p>
<p>根据PHP文档对 <a href="http://www.php.net/manual/en/function.clearstatcache.php" target="_blank">clearstatcache()</a> 这个方法的说明得知：<br />
在使用 stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), 或 fileperms() 方法查询文件信息时，PHP会将文件的stat的缓存以提高性能。 clearstatcache()方法可以用来清除这个缓存。另外unlink()会自动清除stat缓存。</p>
<p>例如以下一段程序：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p391code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3912"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p391code2"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/file_put_contents"><span style="color: #990000;">file_put_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;test_content<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/file_put_contents"><span style="color: #990000;">file_put_contents</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;test_content<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> FILE_APPEND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/clearstatcache"><span style="color: #990000;">clearstatcache</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/filesize"><span style="color: #990000;">filesize</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;test.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>最后会输出</p>
<pre>int(13)
int(13)
int(26)</pre>
<p>但是通过阅读PHP源码，我发现只有在对本地文件进行unlink, rename和rmdir操作时会清除stat缓存，而在通过其他的wrapper进行unlink, rename和rmdir操作时，并不会清除stat缓存。因此在写wrapper时我们要自己在unlink等方法中通过clearstatcache()来清除stat。</p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F06%2Fphp-stat-cache%2F&amp;linkname=%E5%85%B3%E4%BA%8EPHP%E5%AF%B9%E6%96%87%E4%BB%B6%E5%85%83%E4%BF%A1%E6%81%AF%E7%9A%84%E7%BC%93%E5%AD%98" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F06%2Fphp-stat-cache%2F&amp;linkname=%E5%85%B3%E4%BA%8EPHP%E5%AF%B9%E6%96%87%E4%BB%B6%E5%85%83%E4%BF%A1%E6%81%AF%E7%9A%84%E7%BC%93%E5%AD%98" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F06%2Fphp-stat-cache%2F&amp;linkname=%E5%85%B3%E4%BA%8EPHP%E5%AF%B9%E6%96%87%E4%BB%B6%E5%85%83%E4%BF%A1%E6%81%AF%E7%9A%84%E7%BC%93%E5%AD%98" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F06%2Fphp-stat-cache%2F&amp;linkname=%E5%85%B3%E4%BA%8EPHP%E5%AF%B9%E6%96%87%E4%BB%B6%E5%85%83%E4%BF%A1%E6%81%AF%E7%9A%84%E7%BC%93%E5%AD%98" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F06%2Fphp-stat-cache%2F&amp;linkname=%E5%85%B3%E4%BA%8EPHP%E5%AF%B9%E6%96%87%E4%BB%B6%E5%85%83%E4%BF%A1%E6%81%AF%E7%9A%84%E7%BC%93%E5%AD%98" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F06%2Fphp-stat-cache%2F&amp;title=%E5%85%B3%E4%BA%8EPHP%E5%AF%B9%E6%96%87%E4%BB%B6%E5%85%83%E4%BF%A1%E6%81%AF%E7%9A%84%E7%BC%93%E5%AD%98">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2010/06/php-stat-cache/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>[转]10 个最酷的 Linux 单行命令</title>
		<link>http://www.elmerzhang.com/2010/03/top-10-useful-linux-one-line-shell/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=top-10-useful-linux-one-line-shell</link>
		<comments>http://www.elmerzhang.com/2010/03/top-10-useful-linux-one-line-shell/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 01:57:25 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[Linux/Unix]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=385</guid>
		<description><![CDATA[下面是来自 Commandlinefu 网站由用户投票决出的 10 个最酷的 Linux 单行命令，希望对你有用。 sudo !! 以 root 帐户执行上一条命令。 python -m SimpleHTTPServer 利用 Python 搭建一个简单的 Web 服务器，可通过 http://$HOSTNAME:8000 访问。 :w !sudo tee % 在 Vim 中无需权限保存编辑的文件。 cd - 更改到上一次访问的目录。 ^foo^bar 将上一条命令中的 foo 替换为 bar，并执行。 cp filename{,.bak} 快速备份或复制文件。 mtr &#8230;<p class="read-more"><a href="http://www.elmerzhang.com/2010/03/top-10-useful-linux-one-line-shell/">继续阅读 &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>下面是来自 <a href="http://commandlinefu.com/">Commandlinefu</a> 网站由用户投票决出的 10 个最酷的 Linux 单行命令，希望对你有用。</p>
<ol>
<li><code><span style="color: #ff0000;">sudo !!</span></code><br />
以 root 帐户执行上一条命令。</li>
<li><code><span style="color: #ff0000;">python -m SimpleHTTPServer</span></code><br />
利用 Python 搭建一个简单的 Web 服务器，可通过 http://$HOSTNAME:8000 访问。</li>
<li><code><span style="color: #ff0000;">:w !sudo tee %</span></code><br />
在 Vim 中无需权限保存编辑的文件。</li>
<li><code><span style="color: #ff0000;">cd -</span></code><br />
更改到上一次访问的目录。</li>
<li><code><span style="color: #ff0000;">^foo^bar</span></code><br />
将上一条命令中的 foo 替换为 bar，并执行。</li>
<li><code><span style="color: #ff0000;">cp filename{,.bak}</span></code><br />
快速备份或复制文件。</li>
<li><code><span style="color: #ff0000;">mtr google.com</span></code><br />
traceroute + ping。</li>
<li><code><span style="color: #ff0000;">!whatever:p</span></code><br />
搜索命令历史，但不执行。</li>
<li><code><span style="color: #ff0000;">$ssh-copy-id user@host</span></code><br />
将 ssh keys 复制到 user@host 以启用无密码 SSH 登录。</li>
<li><code><span style="color: #ff0000;">ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg</span></code><br />
把 Linux 桌面录制为视频。</li>
</ol>
<p>原文地址：<a href="http://linuxtoy.org/archives/top-10-one-liners.html">http://linuxtoy.org/archives/top-10-one-liners.html</a></p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F03%2Ftop-10-useful-linux-one-line-shell%2F&amp;linkname=%5B%E8%BD%AC%5D10%20%E4%B8%AA%E6%9C%80%E9%85%B7%E7%9A%84%20Linux%20%E5%8D%95%E8%A1%8C%E5%91%BD%E4%BB%A4" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F03%2Ftop-10-useful-linux-one-line-shell%2F&amp;linkname=%5B%E8%BD%AC%5D10%20%E4%B8%AA%E6%9C%80%E9%85%B7%E7%9A%84%20Linux%20%E5%8D%95%E8%A1%8C%E5%91%BD%E4%BB%A4" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F03%2Ftop-10-useful-linux-one-line-shell%2F&amp;linkname=%5B%E8%BD%AC%5D10%20%E4%B8%AA%E6%9C%80%E9%85%B7%E7%9A%84%20Linux%20%E5%8D%95%E8%A1%8C%E5%91%BD%E4%BB%A4" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F03%2Ftop-10-useful-linux-one-line-shell%2F&amp;linkname=%5B%E8%BD%AC%5D10%20%E4%B8%AA%E6%9C%80%E9%85%B7%E7%9A%84%20Linux%20%E5%8D%95%E8%A1%8C%E5%91%BD%E4%BB%A4" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F03%2Ftop-10-useful-linux-one-line-shell%2F&amp;linkname=%5B%E8%BD%AC%5D10%20%E4%B8%AA%E6%9C%80%E9%85%B7%E7%9A%84%20Linux%20%E5%8D%95%E8%A1%8C%E5%91%BD%E4%BB%A4" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F03%2Ftop-10-useful-linux-one-line-shell%2F&amp;title=%5B%E8%BD%AC%5D10%20%E4%B8%AA%E6%9C%80%E9%85%B7%E7%9A%84%20Linux%20%E5%8D%95%E8%A1%8C%E5%91%BD%E4%BB%A4">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2010/03/top-10-useful-linux-one-line-shell/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP中流程控制的替代语法</title>
		<link>http://www.elmerzhang.com/2010/01/php-alternative-syntax-for-control-structures/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php-alternative-syntax-for-control-structures</link>
		<comments>http://www.elmerzhang.com/2010/01/php-alternative-syntax-for-control-structures/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 08:12:22 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[endfor]]></category>
		<category><![CDATA[endforeach]]></category>
		<category><![CDATA[endif]]></category>
		<category><![CDATA[endswitch]]></category>
		<category><![CDATA[endwhile]]></category>
		<category><![CDATA[流程控制]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=374</guid>
		<description><![CDATA[PHP 提供了一些流程控制的替代语法，包括 if，while，for，foreach 和 switch。替代语法的基本形式是把左花括号（{）换成冒号（:），把右花括号（}）分别换成 endif;，endwhile;，endfor;，endforeach; 以及 endswitch;。 使用这种语法时需要注意：elseif不可以分开写为else if，否则会编译不通过 例如： ?View Code PHP1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 &#8230;<p class="read-more"><a href="http://www.elmerzhang.com/2010/01/php-alternative-syntax-for-control-structures/">继续阅读 &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>PHP 提供了一些流程控制的替代语法，包括 if，while，for，foreach 和 switch。替代语法的基本形式是把左花括号（{）换成冒号（:），把右花括号（}）分别换成 endif;，endwhile;，endfor;，endforeach; 以及 endswitch;。</p>
<p><span style="color: #ff0000;">使用这种语法时需要注意：elseif不可以分开写为else if，否则会编译不通过</span><br />
<span id="more-374"></span><br />
例如：</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p374code4'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3744"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p374code4"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//以下三种写法是正确的</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test1'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test2'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test1'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test2'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span><span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test3'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test4'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test1'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test2'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test3'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test4'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//下面这种写法是错误的</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test1'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test2'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test3'</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'test4'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fphp-alternative-syntax-for-control-structures%2F&amp;linkname=PHP%E4%B8%AD%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%AF%AD%E6%B3%95" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fphp-alternative-syntax-for-control-structures%2F&amp;linkname=PHP%E4%B8%AD%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%AF%AD%E6%B3%95" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fphp-alternative-syntax-for-control-structures%2F&amp;linkname=PHP%E4%B8%AD%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%AF%AD%E6%B3%95" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fphp-alternative-syntax-for-control-structures%2F&amp;linkname=PHP%E4%B8%AD%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%AF%AD%E6%B3%95" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fphp-alternative-syntax-for-control-structures%2F&amp;linkname=PHP%E4%B8%AD%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%AF%AD%E6%B3%95" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fphp-alternative-syntax-for-control-structures%2F&amp;title=PHP%E4%B8%AD%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%9A%84%E6%9B%BF%E4%BB%A3%E8%AF%AD%E6%B3%95">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2010/01/php-alternative-syntax-for-control-structures/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>mt_rand与rand</title>
		<link>http://www.elmerzhang.com/2010/01/mt_rand-rand/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mt_rand-rand</link>
		<comments>http://www.elmerzhang.com/2010/01/mt_rand-rand/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 05:27:50 +0000</pubDate>
		<dc:creator>ElmerZhang</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mt_rand]]></category>
		<category><![CDATA[rand]]></category>

		<guid isPermaLink="false">http://www.elmerzhang.com/?p=322</guid>
		<description><![CDATA[貌似好久没写日志了 今天在手册中查rand()用法时，无意发现了mt_rand()这个函数。 很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。mt_rand() 函数是非正式用来替换它的。该函数用了 » Mersenne Twister 中已知的特性作为随机数发生器，它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。 手册真的是个好东西呀，时常查查手册，也能发现不少小技巧，呵呵。]]></description>
			<content:encoded><![CDATA[<p>貌似好久没写日志了</p>
<p>今天在手册中查rand()用法时，无意发现了mt_rand()这个函数。</p>
<blockquote><p>很多老的 libc 的随机数发生器具有一些不确定和未知的特性而且很慢。PHP 的 rand() 函数默认使用 libc 随机数发生器。<strong>mt_rand()</strong> 函数是非正式用来替换它的。该函数用了 <a title="Link : http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" href="http://www.math.sci.hiroshima-u.ac.jp/%7Em-mat/MT/emt.html">» Mersenne  Twister</a> 中已知的特性作为随机数发生器，它可以产生随机数值的平均速度比 libc 提供的 rand() 快四倍。</p></blockquote>
<p>手册真的是个好东西呀，时常查查手册，也能发现不少小技巧，呵呵。</p>
<p><a class="a2a_button_sina_weibo" href="http://www.addtoany.com/add_to/sina_weibo?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fmt_rand-rand%2F&amp;linkname=mt_rand%E4%B8%8Erand" title="Sina Weibo" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/sina_weibo.png" width="16" height="16" alt="Sina Weibo"/></a> <a class="a2a_button_twitter" href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fmt_rand-rand%2F&amp;linkname=mt_rand%E4%B8%8Erand" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a class="a2a_button_facebook" href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fmt_rand-rand%2F&amp;linkname=mt_rand%E4%B8%8Erand" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a class="a2a_button_stumbleupon" href="http://www.addtoany.com/add_to/stumbleupon?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fmt_rand-rand%2F&amp;linkname=mt_rand%E4%B8%8Erand" title="StumbleUpon" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/stumbleupon.png" width="16" height="16" alt="StumbleUpon"/></a> <a class="a2a_button_google_reader" href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fmt_rand-rand%2F&amp;linkname=mt_rand%E4%B8%8Erand" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.elmerzhang.com/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.elmerzhang.com%2F2010%2F01%2Fmt_rand-rand%2F&amp;title=mt_rand%E4%B8%8Erand">书签</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.elmerzhang.com/2010/01/mt_rand-rand/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

