﻿﻿<?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>NoDBA [Not only DBA]</title>
	<atom:link href="http://www.ningoo.net/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ningoo.net</link>
	<description>NinGoo&#039;s blog</description>
	<lastBuildDate>Mon, 08 Mar 2010 13:35:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cassandra Commitlog</title>
		<link>http://www.ningoo.net/html/2010/cassandra_commitlog.html</link>
		<comments>http://www.ningoo.net/html/2010/cassandra_commitlog.html#comments</comments>
		<pubDate>Mon, 01 Mar 2010 12:42:31 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Cassandra]]></category>
		<category><![CDATA[Commitlog]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1195</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 上一篇blog中，大致介绍了一下Cassandra的存储机制，通过将最新的写操作放在内存中的Memtable，然后定期刷新到磁盘持久化为SSTable，Cassandra将随机写操作转换成了顺序... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/cassandra_commitlog.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p><a href="http://www.ningoo.net/html/2010/cassandra_storage.html">上一篇blog</a>中，大致介绍了一下Cassandra的存储机制，通过将最新的写操作放在内存中的Memtable，然后定期刷新到磁盘持久化为SSTable，Cassandra将随机写操作转换成了顺序写操作，这可以提升IO性能。</p>
<p>最新写入的脏数据是在内存Memtable表中，因此必须有机制来确保异常情况下，能够将内存中的数据恢复出来。和关系型数据库系统一样，Cassandra也是采用的先写日志再写数据的方式，其日志称之为Commitlog。</p>
<p>和Memtable/SSTable不一样的是，<strong>Commitlog是server级别的，不是Column Family级别的</strong>。每个Commitlog文件的大小是固定的，称之为一个Commitlog Segment，目前版本(0.5.1)中，这个大小是128MB，这是硬编码在代码(src\java\org\apache\cassandra\db\Commitlog.java)中的。当一个Commitlog文件写满以后，会新建一个的文件。当旧的Commitlog文件不再需要时，会自动清除。</p>
<p>每个Commitlog文件(Segment)都有一个固定大小（大小根据Column Family的数目而定）的<strong>CommitlogHeader</strong>结构，其中有两个重要的数组，每一个Column Family在这两个数组中都存在一个对应的元素。其中一个是位图数组(<strong>BitSet dirty</strong>)，如果Column Family对应的Memtable中有脏数据，则置为1，否则为0，这在恢复的时候可以指出哪些Column Family是需要利用Commitlog进行恢复的。另外一个是整数数组(<strong>int[] lastFlushedAt</strong>)，保存的是Column Family在上一次Flush时日志的偏移位置，恢复时则可以从这个位置读取Commitlog记录。通过这两个数组结构，Cassandra可以在异常重启服务的时候根据持久化的SSTable和Commitlog重构内存中Memtable的内容，也就是类似Oracle等关系型数据库的实例恢复。</p>
<p>当Memtable flush到磁盘的SStable时，会将所有Commitlog文件的dirty数组对应的位清零，而在Commitlog达到大小限制创建新的文件时，dirty数组会从上一个文件中继承过来。如果一个Commitlog文件的dirty数组全部被清零，则表示这个Commitlog在恢复的时候不再需要，可以被清除。因此，在恢复的时候，所有的磁盘上存在的Commitlog文件都是需要的。</p>
<p>参考文章：<br />
[1].<a href="http://wiki.apache.org/cassandra/ArchitectureCommitLog">http://wiki.apache.org/cassandra/ArchitectureCommitLog</a></p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/cassandra_storage.html" title="Cassandra存储机制">Cassandra存储机制</a></li><li><a href="http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html" title="NoSQL，关系数据库终结者？">NoSQL，关系数据库终结者？</a></li><li><a href="http://www.ningoo.net/html/2008/how_to_open_wireless_on_thinkpad_t61.html" title="ThinkPad T61如何开启无线开关">ThinkPad T61如何开启无线开关</a></li><li><a href="http://www.ningoo.net/html/2008/how_mysql_proxy_works.html" title="MySQL Proxy工作机制浅析">MySQL Proxy工作机制浅析</a></li><li><a href="http://www.ningoo.net/html/2008/asm_bad_opration_on_disk.html" title="ASM环境中有关磁盘的操作要慎重">ASM环境中有关磁盘的操作要慎重</a></li><li><a href="http://www.ningoo.net/html/2008/rman_restore_db_via_nfs_bug.html" title="通过NFS执行RMAN恢复遭遇ORA-27054">通过NFS执行RMAN恢复遭遇ORA-27054</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/cassandra_commitlog.html'>http://www.ningoo.net/html/2010/cassandra_commitlog.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/cassandra_commitlog.html#comment'>Add Comments(0)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/cassandra_commitlog.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cassandra存储机制</title>
		<link>http://www.ningoo.net/html/2010/cassandra_storage.html</link>
		<comments>http://www.ningoo.net/html/2010/cassandra_storage.html#comments</comments>
		<pubDate>Thu, 25 Feb 2010 09:17:58 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Bloom Filter]]></category>
		<category><![CDATA[Cassandra]]></category>
		<category><![CDATA[Dynamo]]></category>
		<category><![CDATA[Memtable]]></category>
		<category><![CDATA[SSTable]]></category>
		<category><![CDATA[分布式]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=737</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 在2009年兴起的NoSQL运动中，Cassandra是其中重要的一个分布式key-value数据库产品，由Facebook在2008年开源，目前是Apache的顶级项目。最近twitter的一篇声明，表示将从MySQL迁... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/cassandra_storage.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>在2009年兴起的<a href="http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html">NoSQL</a>运动中，Cassandra是其中重要的一个分布式key-value数据库产品，由Facebook在2008年开源，目前是Apache的顶级项目。最近twitter的<a href="http://www.computerworld.com/s/article/9161078/Twitter_growth_prompts_switch_from_MySQL_to_NoSQL_database?taxonomyId=9">一篇声明</a>，表示将从MySQL迁移到Cassandra，更让其声名大振。Cassandra是结合了Google Bigtable的数据模型和Amazon Dynamo高可用框架的一个产品。其数据模型可以参考张瑞的<a href="http://www.hellodba.net/">blog</a>。</p>
<p>值得说一下的是Cassandra的存储机制，也是借鉴了Bigtable的设计，采用Memtable和SSTable的方式。和关系数据库一样，Cassandra在写数据之前，也需要先记录日志，称之为<strong>commitlog</strong>，然后数据才会写入到Column Family对应的Memtable中，并且Memtable中的内容是按照key排序好的。Memtable是一种内存结构，满足一定条件后批量刷新到磁盘上，存储为SSTable。这种机制，相当于缓存写回机制(Write-back Cache)，优势在于将随机IO写变成顺序IO写，降低大量的写操作对于存储系统的压力。SSTable一旦完成写入，就不可变更，只能读取。下一次Memtable需要刷新到一个新的SSTable文件中。<strong>所以对于Cassandra来说，可以认为只有顺序写，没有随机写操作。</strong></p>
<p>因为SSTable数据不可更新，可能导致同一个Column Family的数据存储在多个SSTable中，这时查询数据时，需要去合并读取Column Family所有的SSTable和Memtable，这样到一个Column Family的数量很大的时候，可能导致查询效率严重下降。因此需要有一种机制能快速定位查询的Key落在哪些SSTable中，而不需要去读取合并所有的SSTable。Cassandra采用的是<strong>Bloom Filter</strong>算法，通过多个hash函数将key映射到一个位图中，来快速判断这个key属于哪个SSTable。关于Bloom Filter，有兴趣的可以去看看参考文章4,5和6。</p>
<p>为了避免大量SSTable带来的性能影响，Cassandra也提供一种定期将多个SSTable合并成一个新的SSTable的机制，因为每个SSTable中的key都是已经排序好的，因此只需要做一次合并排序就可以完成该任务，代价还是可以接受的。所以在Cassandra的数据存储目录中，可以看到三种类型的文件，格式类似于：</p>
<ul>
<li>Column Family Name-序号-Data.db</li>
<li>Column Family Name-序号-Filter.db</li>
<li>Column Family Name-序号-index.db</li>
</ul>
<p>其中Data.db文件是SSTable数据文件，SSTable是Sorted Strings Table的缩写，按照key排序后存储key/value键值字符串。index.db是索引文件，保存的是每个key在数据文件中的偏移位置，而Filter.db则是Bloom Filter算法生产的映射文件。</p>
<p><strong>参考文章：</strong><br />
[1].<a href="http://wiki.apache.org/cassandra/ArchitectureOverview">http://wiki.apache.org/cassandra/ArchitectureOverview</a><br />
[2].<a href="http://wiki.apache.org/cassandra/MemtableSSTable">http://wiki.apache.org/cassandra/MemtableSSTable</a><br />
[3].<a href="http://wiki.apache.org/cassandra/ArchitectureSSTable">http://wiki.apache.org/cassandra/ArchitectureSSTable</a><br />
[4].<a href="http://blog.csdn.net/jiaomeng/archive/2007/01/27/1495500.aspx">http://blog.csdn.net/jiaomeng/archive/2007/01/27/1495500.aspx</a><br />
[5].<a href="http://www.hellodba.net/2009/04/bloom_filter.html">http://www.hellodba.net/2009/04/bloom_filter.html</a><br />
[6].<a href="http://www.googlechinablog.com/2007/07/bloom-filter.html">http://www.googlechinablog.com/2007/07/bloom-filter.html</a><br />
[7].<a href="http://labs.google.com/papers/bigtable.html">http://labs.google.com/papers/bigtable.html</a></p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/cassandra_commitlog.html" title="Cassandra Commitlog">Cassandra Commitlog</a></li><li><a href="http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html" title="CAP原理与最终一致性">CAP原理与最终一致性</a></li><li><a href="http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html" title="NoSQL，关系数据库终结者？">NoSQL，关系数据库终结者？</a></li><li><a href="http://www.ningoo.net/html/2009/amoeba_for_mysql_distribute_environment.html" title="用Amoeba构架MySQL分布式数据库环境">用Amoeba构架MySQL分布式数据库环境</a></li><li><a href="http://www.ningoo.net/html/2009/dbms_stats_defaut_parameters.html" title="dbms_stats收集统计信息时的默认参数">dbms_stats收集统计信息时的默认参数</a></li><li><a href="http://www.ningoo.net/html/2007/start_to_use_fireforx3.html" title="开始使用FireFox 3 Beta 2">开始使用FireFox 3 Beta 2</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/cassandra_storage.html'>http://www.ningoo.net/html/2010/cassandra_storage.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/cassandra_storage.html#comment'>Add Comments(1)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/cassandra_storage.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>CAP原理与最终一致性</title>
		<link>http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html</link>
		<comments>http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html#comments</comments>
		<pubDate>Thu, 25 Feb 2010 04:32:00 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[CAP]]></category>
		<category><![CDATA[分布式]]></category>
		<category><![CDATA[最终一致性]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1239</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net CAP原理(CAP Theorem)
在足球比赛里，一个球员在一场比赛中进三个球，称之为帽子戏法(Hat-trick)。在分布式数据系统中，也有一个帽子原理(CAP Theorem)，不过此帽子非彼帽... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p><strong>CAP原理(CAP Theorem)</strong></p>
<p>在足球比赛里，一个球员在一场比赛中进三个球，称之为帽子戏法(Hat-trick)。在分布式数据系统中，也有一个帽子原理(CAP Theorem)，不过此帽子非彼帽子。CAP原理中，有三个要素：</p>
<ul>
<li>一致性(<strong>C</strong>onsistency)</li>
<li>可用性(<strong>A</strong>vailability)</li>
<li>分区容忍性(<strong>P</strong>artition tolerance)</li>
</ul>
<p>CAP原理指的是，这三个要素最多只能同时实现两点，不可能三者兼顾。因此在进行分布式架构设计时，必须做出取舍。而<strong>对于分布式数据系统，分区容忍性是基本要求</strong>，否则就失去了价值。因此设计分布式数据系统，就是在一致性和可用性之间取一个平衡。对于大多数web应用，其实并不需要强一致性，因此牺牲一致性而换取高可用性，是目前多数分布式数据库产品的方向。</p>
<p>当然，牺牲一致性，并不是完全不管数据的一致性，否则数据是混乱的，那么系统可用性再高分布式再好也没有了价值。牺牲一致性，只是不再要求关系型数据库中的强一致性，而是只要系统能达到<strong>最终一致性</strong>即可，考虑到客户体验，这个最终一致的时间窗口，要尽可能的对用户透明，也就是需要保障“用户感知到的一致性”。通常是通过数据的多份异步复制来实现系统的高可用和数据的最终一致性的，“用户感知到的一致性”的时间窗口则取决于数据复制到一致状态的时间。</p>
<p><strong>最终一致性(eventually consistent)</strong></p>
<p>对于一致性，可以分为从客户端和服务端两个不同的视角。从客户端来看，一致性主要指的是多并发访问时更新过的数据如何获取的问题。从服务端来看，则是更新如何复制分布到整个系统，以保证数据最终一致。一致性是因为有并发读写才有的问题，因此在理解一致性的问题时，一定要注意结合考虑并发读写的场景。</p>
<p>从客户端角度，多进程并发访问时，更新过的数据在不同进程如何获取的不同策略，决定了不同的一致性。对于关系型数据库，要求更新过的数据能被后续的访问都能看到，这是<strong>强一致性</strong>。如果能容忍后续的部分或者全部访问不到，则是<strong>弱一致性</strong>。如果经过一段时间后要求能访问到更新后的数据，则是最终一致性。</p>
<p>最终一致性根据更新数据后各进程访问到数据的时间和方式的不同，又可以区分为：</p>
<blockquote>
<ul>
<li><strong>因果一致性</strong>。如果进程A通知进程B它已更新了一个数据项，那么进程B的后续访问将返回更新后的值，且一次写入将保证取代前一次写入。与进程A无因果关系的进程C的访问遵守一般的最终一致性规则。</li>
<li><strong>“读己之所写（read-your-writes）”一致性</strong>。当进程A自己更新一个数据项之后，它总是访问到更新过的值，绝不会看到旧值。这是因果一致性模型的一个特例。</li>
<li><strong>会话（Session）一致性</strong>。这是上一个模型的实用版本，它把访问存储系统的进程放到会话的上下文中。只要会话还存在，系统就保证“读己之所写”一致性。如果由于某些失败情形令会话终止，就要建立新的会话，而且系统的保证不会延续到新的会话。</li>
<li><strong>单调（Monotonic）读一致性</strong>。如果进程已经看到过数据对象的某个值，那么任何后续访问都不会返回在那个值之前的值。</li>
<li><strong>单调写一致性</strong>。系统保证来自同一个进程的写操作顺序执行。要是系统不能保证这种程度的一致性，就非常难以编程了。</li>
</ul>
</blockquote>
<p>上述最终一致性的不同方式可以进行组合，例如单调读一致性和读己之所写一致性就可以组合实现。并且从实践的角度来看，这两者的组合，读取自己更新的数据，和一旦读取到最新的版本不会再读取旧版本，对于此架构上的程序开发来说，会少很多额外的烦恼。</p>
<p>从服务端角度，如何尽快将更新后的数据分布到整个系统，降低达到最终一致性的时间窗口，是提高系统的可用度和用户体验非常重要的方面。对于分布式数据系统：</p>
<ul>
<li>N &#8212; 数据复制的份数</li>
<li>W &#8212; 更新数据是需要保证写完成的节点数</li>
<li>R &#8212; 读取数据的时候需要读取的节点数</li>
</ul>
<p>如果W+R>N，写的节点和读的节点重叠，则是强一致性。例如对于典型的一主一备同步复制的关系型数据库，N=2,W=2,R=1，则不管读的是主库还是备库的数据，都是一致的。</p>
<p>如果W+R<=N，则是弱一致性。例如对于一主一备异步复制的关系型数据库，N=2,W=1,R=1，则如果读的是备库，就可能无法读取主库已经更新过的数据，所以是弱一致性。</p>
<p>对于分布式系统，为了保证高可用性，一般设置N>=3。不同的N,W,R组合，是在可用性和一致性之间取一个平衡，以适应不同的应用场景。</p>
<ul>
<li>如果N=W,R=1，任何一个写节点失效，都会导致写失败，因此可用性会降低，但是由于数据分布的N个节点是同步写入的，因此可以保证强一致性。</li>
<li>
如果N=R,W=1，只需要一个节点写入成功即可，写性能和可用性都比较高。但是读取其他节点的进程可能不能获取更新后的数据，因此是弱一致性。这种情况下，如果W<(N+1)/2，并且写入的节点不重叠的话，则会存在写冲突</li>
</ul>
<p><strong><br />
参考文章：</strong><br />
[1].<a href="http://devblog.streamy.com/2009/08/24/cap-theorem/">http://devblog.streamy.com/2009/08/24/cap-theorem/</a><br />
[2].<a href="http://www.julianbrowne.com/article/viewer/brewers-cap-theorem">http://www.julianbrowne.com/article/viewer/brewers-cap-theorem</a><br />
[3].<a href="http://www.allthingsdistributed.com/2008/12/eventually_consistent.html">http://www.allthingsdistributed.com/2008/12/eventually_consistent.html</a><br />
[4].<a href="http://www.infoq.com/cn/news/2008/01/consistency-vs-availability">http://www.infoq.com/cn/news/2008/01/consistency-vs-availability</a></p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/cassandra_storage.html" title="Cassandra存储机制">Cassandra存储机制</a></li><li><a href="http://www.ningoo.net/html/2009/amoeba_for_mysql_distribute_environment.html" title="用Amoeba构架MySQL分布式数据库环境">用Amoeba构架MySQL分布式数据库环境</a></li><li><a href="http://www.ningoo.net/html/2010/my_2010_plan.html" title="2010，风生水起">2010，风生水起</a></li><li><a href="http://www.ningoo.net/html/2008/get_top_n_cpu_sql_in_realtime.html" title="实时获得最耗CPU资源的SQL语句">实时获得最耗CPU资源的SQL语句</a></li><li><a href="http://www.ningoo.net/html/2007/oracle_optimizer_development_team_starts_a_blog.html" title="Oracle优化器开发团队开博">Oracle优化器开发团队开博</a></li><li><a href="http://www.ningoo.net/html/2007/an_introduction_to_apache_derby_database.html" title="Apache Derby Database介绍">Apache Derby Database介绍</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html'>http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html#comment'>Add Comments(0)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/cap_theorem_and_eventually_consistent.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>dstat:一款简单直观的os实时监控工具</title>
		<link>http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html</link>
		<comments>http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html#comments</comments>
		<pubDate>Fri, 05 Feb 2010 03:43:21 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[监控]]></category>
		<category><![CDATA[dstat]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1255</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 在oschina上闲逛，发现一款不错的os实时监控工具dstat，整合了vmstat, iostat, ifstat, netstat等常见os监控工具的优点，输出的结果简单直观，并且结果可以保存到csv文件，这... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>在<a href="http://www.oschina.net/">oschina</a>上闲逛，发现一款不错的os实时监控工具<a href="http://dag.wieers.com/home-made/dstat/">dstat</a>，整合了vmstat, iostat, ifstat, netstat等常见os监控工具的优点，输出的结果简单直观，并且结果可以保存到csv文件，这样再写一个简单的perl脚本，就能将os的主要监控信息一次性全部抓取出来，保存到监控数据库中用于分析展示。试用了一下觉得非常不错，因此在这里分享一下这个用python写的工具。</p>
<pre>
$dstat
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw
  2   0  98   0   0   0|  80k   54k|   0     0 | 335B  381B|1297  1301
 22   2  74   0   0   2|   0   416k| 621k  219k|   0     0 |1158    26k
 23   3  72   0   0   2|  64k  484k|  11k   11k|   0     0 |1109    30k
 21   3  75   0   0   2|4096B  416k|  77k   77k|   0     0 |2104    25k
 29   4  66   0   0   2|   0  1240k| 996k  425k|   0     0 |1350    28k
</pre>
<pre>
$dstat -ta --output osstat.csv
-----time----- ----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
  date/time   |usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw
05-02 11:37:08|  2   0  98   0   0   0|  80k   54k|   0     0 | 335B  381B|1297  1301
05-02 11:37:09| 16   4  78   0   0   3|   0  1404k|1478k  939k|   0     0 |4316    33k
05-02 11:37:10| 20   2  76   0   0   2|   0  1144k|1109k  828k|   0     0 |5653    28k
05-02 11:37:11| 13   2  83   0   0   2|   0   588k|2590k 1684k|   0     0 |4256    23k
</pre>
<pre>
$dstat -h
Usage: dstat [-afv] [options..] [delay [count]]
Versatile tool for generating system resource statistics

Dstat options:
  -c, --cpu              enable cpu stats
     -C 0,3,total           include cpu0, cpu3 and total
  -d, --disk             enable disk stats
     -D total,hda           include hda and total
  -g, --page             enable page stats
  -i, --int              enable interrupt stats
     -I 5,eth2              include int5 and interrupt used by eth2
  -l, --load             enable load stats
  -m, --mem              enable memory stats
  -n, --net              enable network stats
     -N eth1,total          include eth1 and total
  -p, --proc             enable process stats
  -s, --swap             enable swap stats
     -S swap1,total         include swap1 and total
  -t, --time             enable time/date output
  -T, --epoch            enable time counter (seconds since epoch)
  -y, --sys              enable system stats
  --ipc                  enable ipc stats
  --lock                 enable lock stats
  --raw                  enable raw stats
  --tcp                  enable tcp stats
  --udp                  enable udp stats
  --unix                 enable unix stats

  -M stat1,stat2         enable external stats
     --mods stat1,stat2

  -a, --all              equals -cdngy (default)
  -f, --full             expand -C, -D, -I, -N and -S discovery lists
  -v, --vmstat           equals -pmgdsc -D total

  --integer              show integer values
  --nocolor              disable colors (implies --noupdate)
  --noheaders            disable repetitive headers
  --noupdate             disable intermediate updates
  --output file          write CSV output to file

  delay is the delay in seconds between each update
  count is the number of updates to display before exiting
  The default delay is 1 and count is unspecified (unlimited)
</pre>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html" title="tbstat:实时监控数据库统计状态的小工具">tbstat:实时监控数据库统计状态的小工具</a></li><li><a href="http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html" title="使用jpgraph绘制数据库监控图形">使用jpgraph绘制数据库监控图形</a></li><li><a href="http://www.ningoo.net/html/2009/use_oracle_regexp_monitor_database_session.html" title="使用Oracle正则表达式监控应用到数据库的连接情况">使用Oracle正则表达式监控应用到数据库的连接情况</a></li><li><a href="http://www.ningoo.net/html/2009/how_to_monitor_stats_of_memcached.html" title="如何监控MemCached的状态">如何监控MemCached的状态</a></li><li><a href="http://www.ningoo.net/html/2009/use_jquery_and_tablesorter_for_data_sort_and_pager.html" title="用jQuery+Tablesorter实现客户端分页与排序">用jQuery+Tablesorter实现客户端分页与排序</a></li><li><a href="http://www.ningoo.net/html/2008/using_perl_monitor_database.html" title="用Perl的hash数组实现个性化监控">用Perl的hash数组实现个性化监控</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html'>http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html#comment'>Add Comments(3)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PostgreSQL备份</title>
		<link>http://www.ningoo.net/html/2010/postgresql_backup.html</link>
		<comments>http://www.ningoo.net/html/2010/postgresql_backup.html#comments</comments>
		<pubDate>Wed, 03 Feb 2010 04:31:55 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[pg_dump]]></category>
		<category><![CDATA[pg_rman]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[备份]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1240</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net PostgreSQL也支持逻辑备库和物理备份两种方式。物理备份可以和Oracle一样实现联机热备份，并且同样也需要将数据库设置为归档模式。
逻辑备份
PostgreSQL提供了pg_dump/pg_... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/postgresql_backup.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>PostgreSQL也支持逻辑备库和物理备份两种方式。物理备份可以和Oracle一样实现联机热备份，并且同样也需要将数据库设置为归档模式。</p>
<p><strong>逻辑备份</strong></p>
<p>PostgreSQL提供了pg_dump/pg_dumpall两个程序可以用来将数据dump成文本文件，实现数据的逻辑备份。使用不同的参数，可以将数据dump成PostgreSQL专用的数据格式(生成copy语句)或者标准SQL语句(生成insert语句)格式。恢复只需要简单的使用psql将文件执行一遍即可。另外也可以使用pg_restore工具来恢复数据。</p>
<p>利用pg_dump备份test数据库(只有一张test表)，包括重建表的DDL语句，授权语句等所有信息，生成copy格式的文件：</p>
<pre>
$ ./pg_dump test
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET search_path = public, pg_catalog;
SET default_tablespace = '';
SET default_with_oids = false;
--
-- Name: test; Type: TABLE; Schema: public; Owner: postgres; Tablespace:
--
CREATE TABLE test (
    i integer
);
ALTER TABLE public.test OWNER TO postgres;
--
-- Data for Name: test; Type: TABLE DATA; Schema: public; Owner: postgres
--
COPY test (i) FROM stdin;
1
2
\.
--
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--
REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;
--
-- PostgreSQL database dump complete
--
</pre>
<p>利用pg_dump备份test库，只保存数据，insert语句格式：</p>
<pre>
[postgres@dbconsole bin]$ ./pg_dump --inserts -a test
--
-- PostgreSQL database dump
--
SET statement_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = off;
SET check_function_bodies = false;
SET client_min_messages = warning;
SET escape_string_warning = off;
SET search_path = public, pg_catalog;
--
-- Data for Name: test; Type: TABLE DATA; Schema: public; Owner: postgres
--
INSERT INTO test VALUES (1);
INSERT INTO test VALUES (2);
--
-- PostgreSQL database dump complete
--
</pre>
<p><strong>物理备份</strong></p>
<p>和Oracle一样，物理备份可以分为冷备份和热备份。冷备份就是将数据库实例关闭，然后直接复制data目录的所有文件，恢复时只需要将文件copy到data目录，无须利用日志(WAL&#8211;Write-Ahead Logging）进行恢复。而热备份，则需要利用日志将数据库恢复到一致状态，因此需要先将数据库置于归档模式。</p>
<p>PostgreSQL归档模式使用参数archive_mode控制，这是一个静态参数，修改postgresql.conf文件，加入如下参数，然后重启生效：</p>
<pre>
archive_mode = on
archive_command = 'cp "%p" /u01/postgresql/arch/"%f"'
archive_timeout = 600
</pre>
<pre>
postgres=# show archive_mode;
 archive_mode
--------------
 on
(1 row)
</pre>
<p>由于postgresql没有归档进程，因此归档是通过外部命令(OS)完成的，archive_command用于指定该外部命令，具体格式请参考文档。如果是linux归档到本地，使用cp即可，如果是到远程，则可以使用scp或者rsync。archive_timeout强制N秒以后进行一次归档，这个和Oracle的archive_lag_target参数的作用一样。当然也可以手工进行日志切换：</p>
<pre>
postgres=# select pg_switch_xlog();
 pg_switch_xlog
----------------
 0/7000000
(1 row)
</pre>
<p>热备份前，需要先将数据库置于备份状态，这一点和Oracle的手工也备份也是一样的。该命令会导致一次checkpoint，可能在业务高峰期会带来一些压力和风险，因此备份还是需要安排在业务低谷期间执行比较稳妥。</p>
<pre>
postgres=# select pg_start_backup('test_backup');
 pg_start_backup
-----------------
 0/8000020
(1 row)
</pre>
<p>然后在os层面将data目录进行复制备份。完成后，再取消备份状态，该命令同时会执行一次日志切换，并进行归档，以保证热备份期间的日志都已经归档，保证恢复数据库到一致状态的所有日志都能从归档中找到：</p>
<pre>
postgres=# select pg_stop_backup();
 pg_stop_backup
----------------
 0/8000088
(1 row)
</pre>
<p>备份完成后，可以在归档目录找到一个记录了本次备份信息的文件：</p>
<pre>
$ more 000000010000000000000008.00000020.backup
START WAL LOCATION: 0/8000020 (file 000000010000000000000008)
STOP WAL LOCATION: 0/8000088 (file 000000010000000000000008)
CHECKPOINT LOCATION: 0/8000020
START TIME: 2010-02-03 12:24:57 CST
LABEL: test_backup
STOP TIME: 2010-02-03 12:24:59 CST
</pre>
<p>PostgreSQL官方并没有提供和Oracle的rman一样的备份工具，不过因为PostgreSQL是开源的数据库，因此也有一个开源的工具<a href="http://code.google.com/p/pg-rman/wiki/readme">pg_rman</a>可以使用，从命令行来看功能已经非常强大，暂时还没有测试，有兴趣的可以研究一下。</p>
<pre>
pg_rman [ OPTIONS ] { init | backup | restore | show [ DATE | timeline ] |
 validate [ DATE ] | delete DATE }
</pre>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/installpostgresql.html" title="PostgreSQL安装">PostgreSQL安装</a></li><li><a href="http://www.ningoo.net/html/2010/introduce_to_postgresql.html" title="PostgreSQL简介">PostgreSQL简介</a></li><li><a href="http://www.ningoo.net/html/2008/lock_mechanism_of_rebuild_index_online_2.html" title="rebuild index online的锁机制浅析(续)">rebuild index online的锁机制浅析(续)</a></li><li><a href="http://www.ningoo.net/html/2008/chm_file_can_not_open.html" title="WindowsXP上chm文件无法打开一例">WindowsXP上chm文件无法打开一例</a></li><li><a href="http://www.ningoo.net/html/2007/an_introduction_to_apache_derby_database.html" title="Apache Derby Database介绍">Apache Derby Database介绍</a></li><li><a href="http://www.ningoo.net/html/2008/cpi_and_life.html" title="肉包变成了小笼包">肉包变成了小笼包</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/postgresql_backup.html'>http://www.ningoo.net/html/2010/postgresql_backup.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/postgresql_backup.html#comment'>Add Comments(0)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/postgresql_backup.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQL安装</title>
		<link>http://www.ningoo.net/html/2010/installpostgresql.html</link>
		<comments>http://www.ningoo.net/html/2010/installpostgresql.html#comments</comments>
		<pubDate>Sat, 23 Jan 2010 14:35:10 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1230</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net PostgreSQL8.x版本的安装已经非常的简单了。EnterpriseDB制作了一键安装的版本，包括FreeBSD/Linux/Mac OS X/Solaris/Windows平台都有。不过即使使用源码编译，也非常的简单。各个... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/installpostgresql.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>PostgreSQL8.x版本的安装已经非常的简单了。EnterpriseDB制作了一键安装的版本，包括FreeBSD/Linux/Mac OS X/Solaris/Windows平台都有。不过即使使用源码编译，也非常的简单。各个版本的源码可以点<a href="http://www.postgresql.org/ftp/source/">这里下载</a>。</p>
<p>创建os用户</p>
<pre>
#useradd -g dba postgres
#su - postgres
</pre>
<p>编译</p>
<pre>
$tar -zxvf postgresql-8.4.2.tar.gz
$cd postgresql-8.4.2
$./configure --prefix=/OPT/postgresql --enable-profiling
--with-blocksize=8 --with-wal-blocksize=8
$make &#038;&#038; make install
</pre>
<p>其中with-blocksize指定数据块大小，默认8k，with-wal-blocksize指定日志块大小，默认也是8k。更多编译配置选项，可以通过./configure &#8211;help查看。</p>
<p>初始化database，注意PostgreSQL在服务端不支持GBK编码。</p>
<pre>
$cd /opt/postgresql/bin
$ ./initdb --encoding=utf8 -D /opt/postgresql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale en_US.UTF-8.
The default text search configuration will be set to "english".

creating directory /opt/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /opt/postgresql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

    ./postgres -D /opt/postgresql/data
or
    ./pg_ctl -D /opt/postgresql/data -l logfile start
</pre>
<p>初始化完成后，会在 /opt/postgresql/data目录生成数据库的文件，至此，软件安装完毕，数据库创建完毕。</p>
<p>启动数据库</p>
<pre>
./pg_ctl -D /opt/postgresql/data/ -l /opt/postgresql/log/alert.log start
</pre>
<p>启动后，可以发现PostgreSQL实例一共运行了5个进程</p>
<pre>
$ ps -ef | grep postgres
postgres 17572     1  0 16:41 pts/2    00:00:00 /opt/postgresql/bin/postgres
-D /opt/postgresql/data
postgres 17574 17572  0 16:41 ?        00:00:00 postgres: writer process
postgres 17575 17572  0 16:41 ?        00:00:00 postgres: wal writer process
postgres 17576 17572  0 16:41 ?        00:00:00 postgres: autovacuum launcher process
postgres 17577 17572  0 16:41 ?        00:00:00 postgres: stats collector process
postgres 18679 20791  0 16:47 pts/2    00:00:00 grep postgres
</pre>
<p>其中wal writer process是日志写进程。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/postgresql_backup.html" title="PostgreSQL备份">PostgreSQL备份</a></li><li><a href="http://www.ningoo.net/html/2010/introduce_to_postgresql.html" title="PostgreSQL简介">PostgreSQL简介</a></li><li><a href="http://www.ningoo.net/html/2008/oracle_crashed_as_aix_bug_iz03260.html" title="流年不利，万事小心">流年不利，万事小心</a></li><li><a href="http://www.ningoo.net/html/2009/oracle11gr2_first_installation.html" title="Oracle11gR2初体验">Oracle11gR2初体验</a></li><li><a href="http://www.ningoo.net/html/2007/he_xie_t_shirt.html" title="T-shirt也河蟹">T-shirt也河蟹</a></li><li><a href="http://www.ningoo.net/html/2009/generate_gzip_file_with_zlib.html" title="使用zlib输出gzip格式的文件">使用zlib输出gzip格式的文件</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/installpostgresql.html'>http://www.ningoo.net/html/2010/installpostgresql.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/installpostgresql.html#comment'>Add Comments(4)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/installpostgresql.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PostgreSQL简介</title>
		<link>http://www.ningoo.net/html/2010/introduce_to_postgresql.html</link>
		<comments>http://www.ningoo.net/html/2010/introduce_to_postgresql.html#comments</comments>
		<pubDate>Sat, 23 Jan 2010 03:18:46 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[EnterpriseDB]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1221</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 上个周末，无聊的时候关注了一下PostgreSQL。第一次尝试去安装PostgreSQL，还是好几年前的事了，那是8.0版本刚出来，终于开始原生的支持windows了，所以在自己电脑上折... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/introduce_to_postgresql.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>上个周末，无聊的时候关注了一下PostgreSQL。第一次尝试去安装PostgreSQL，还是好几年前的事了，那是8.0版本刚出来，终于开始原生的支持windows了，所以在自己电脑上折腾了一个。不过那时候也仅限于安装了一次而已，甚至psql的命令行都不知道怎么用。</p>
<p>同样作为开源关系型数据库，MySQL在这几年获得了更多的关注。大量的互联网公司都基于MySQL来构架系统，也导致MySQL DBA开始火热，一大堆年轻有为的同学投入到其中，渐成燎原之势。MySQL数据库火热了，MySQL AB公司却被<a href="http://www.ningoo.net/html/2008/sun_acquires_mysql.html">sun收购</a>，现在又随着sun要投入Oracle的怀抱，而且欧盟已经无条件批准这个收购，只剩下中国和俄罗斯，大局已定。作为商业数据库的绝对老大，Oracle的这次收购，让MySQL的支持者感到了威胁，其创始人甚至发起了一场<a href="http://monty-says.blogspot.com/2009/12/help-saving-mysql.html">保护MySQL</a>（有墙），阻击Oracle收购的运动。</p>
<p>这也是PostgreSQL的机会，最近PostgreSQL的开发节奏很快，8.5已经连续出到了alpha3版，在这个版本中，最吸引我的是<strong>hot standby</strong>，类似于Oracle11g的active data guard，hot standby也可以在恢复的同时提供读服务，而以往版本，PostgreSQL的物理备库<strong>warm standby</strong>，则只能处于恢复状态，一旦open，则需要重做，比较痛苦。PostgreSQL的很多特性，都和Oracle相当的类似，甚至有一家商业化的公司<strong>EnterpriseDB</strong>，在致力于将PostgreSQL打包，使得应用程序从Oracle迁移到PostgreSQL更方便，据说80%的Oracle应用代码甚至不需要做修改就能在PostgreSQL运行。因此，我在<a href="http://twitter.com/NinGoo">twitter</a>上说，如果PostgreSQL在人机交互的工具和配置部分，能够更加友好一点，完全是一个影子版本的Oracle。</p>
<p>PostgreSQL也支持mvcc多版本一致性控制。不过其实现的机制，和innodb的方式比较像，而和Oracle的不一样。Oracle是将变化的前映像记录到单独的undo段中，而PostgreSQL则只是将前映像(Tuples)上做个标记，如果是delete，则相当于是逻辑删除，实际的数据还是在原来的段中，如果是insert，相当于先delete，再insert，而且会在原来的记录上加一条指向新记录的指针，形成一个链表，查询的时候需要沿着这个链表找到一致的数据。这样会造成一个问题，一段时间以后，dml操作使得数据段和索引段中都有大量的前映像信息存在，会严重影响数据查询的效率。PostgreSQL的mvcc的这种实现方式，带来的一个好处是回滚非常快，只需要修改前映像上的几个标志位即可，而不像oracle需要从undo段将前映像再复制回来。但是，这种方便回滚，却会损失查询性能的设计思路，真的比较诡异。PostgreSQL中有一个专门用来清理这些旧版本数据的程序，叫做vacuum。在以前的版本中，需要定期执行vacuum来优化数据存储结构。这对于DBA来说，无疑是一件痛苦的事情。直到8.1版本，引入了<a href="http://www.pgsqldb.org/pgsqldoc-cvs/maintenance.html#AUTOVACUUM">autovacuum</a>，系统可以自动来进行这些清理工作，终于人性化了一点点。</p>
<p>在8.3版本，引入了一个新的特性HOT(Heap Only Tuples)，主要的目的是努力避免update造成的性能低下的问题。其实这个HOT，说白了很简单，对于update，要实现mvcc，其机制还是一样的，区别在于select，在沿着链表找一致性数据的过程中，如果发现这个检查过的版本已经没有任何事物在引用了，就会顺便把清理工作做掉，而不是像以前要等vacuum来做。因此这会加大一点select的压力，但前人栽树，后人乘凉，接下来需要访问这些数据的其他select就会快很多了，这和Oracle的延迟块清除其实有些类似的，当然两者的设计目的并不一样。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/postgresql_backup.html" title="PostgreSQL备份">PostgreSQL备份</a></li><li><a href="http://www.ningoo.net/html/2010/installpostgresql.html" title="PostgreSQL安装">PostgreSQL安装</a></li><li><a href="http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html" title="遭遇MySQL Replication Fatal Error 1236">遭遇MySQL Replication Fatal Error 1236</a></li><li><a href="http://www.ningoo.net/html/2009/gearman-for-mysql.html" title="Gearman for MySQL">Gearman for MySQL</a></li><li><a href="http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html" title="InnoDB线程并发检查机制">InnoDB线程并发检查机制</a></li><li><a href="http://www.ningoo.net/html/2009/xtradb_enhancements_in_show-innodb-status.html" title="从show innodb status看XtraDB的增强特性">从show innodb status看XtraDB的增强特性</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/introduce_to_postgresql.html'>http://www.ningoo.net/html/2010/introduce_to_postgresql.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/introduce_to_postgresql.html#comment'>Add Comments(4)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/introduce_to_postgresql.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>tbstat:实时监控数据库统计状态的小工具</title>
		<link>http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html</link>
		<comments>http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html#comments</comments>
		<pubDate>Mon, 11 Jan 2010 06:11:22 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[监控]]></category>
		<category><![CDATA[统计信息]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[tbstat]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1202</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 用perl写了一个简单的工具，用于实时查看数据库的统计状态信息，展现信息主要来源于Oracle数据字典中v$systat和v$system_event。写这个工具的初衷，是因为目前我们对于... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>用perl写了一个简单的工具，用于实时查看数据库的统计状态信息，展现信息主要来源于Oracle数据字典中v$systat和v$system_event。写这个工具的初衷，是因为目前我们对于数据库的监控，更多的是分钟级别抽样的数据来绘制的图形，粒度相对还比较粗，有一些比较深的问题，需要更加细粒度的数据。而如果把监控的粒度做到秒级，则收集的数据量就会非常大，因此需要一个平衡，平时采用分钟级别的抽样数据已经足够用于预警，而秒级的则用于某个具体问题的分析。</p>
<p>当前tbstat功能还比较简单，类似于iostat/vmstat等os工具，tbstat可以通过指定抽样间隔和抽样次数，来循环抓取Oracle的统计状态信息。tbstat支持三个参数 -i 表示间隔时间 -c 表示循环次数 -n 表示需要查看的统计信息的名字(使用前后%的like来查询) </p>
<ul>
<li>tbstat -i 2 -c 10 表示间隔时间2s，循环次数10次，展示经过人工筛选的36项统计信息</li>
<li>tbstat -i 2 -c 10 -n parse 表示间隔时间2s，循环次数10次，展示所有名字包含parse的统计信息</li>
<li>tbstat -i 2 -c 10 -n all 表示间隔时间2s，循环次数10次，展示所有不为零的统计信息</li>
</ul>
<p>也可以使用简化的参数输入方法，第一位表示间隔时间，第二位表示循环次数，第三位表示统计信息名。直接敲入tbstat，则默认的参数为间隔时间10s，次数无限，经过挑选过滤的一些常用的v$sysstat中的统计信息。如果name参数传入的值是event，则展示v$system_event中的等待事件的信息。</p>
<pre>
$tbstat 1 0
--------------------------------------------------------------------------
-- tbstat v0.3.3 --- a tool for oracle system statistics and event.
-- Powered by NinGoo.net
--------------------------------------------------------------------------                      

               CPU used by this session:     40                       CR blocks created:       5
        DBWR checkpoint buffers written:    569                  DBWR undo block writes:      64
 bytes received via SQL*Net from client: 314297        bytes sent via SQL*Net to client: 2761660
  cleanouts only - consistent read gets:      4                         consistent gets:   48855
                       db block changes:   2122                           db block gets:    3714
                       enqueue requests:    900                           enqueue waits:       7
                          execute count:   3145                   free buffer requested:    1402
         index crx upgrade (positioned):      3            index fast full scans (full):       0
                 leaf node 90-10 splits:      0                        leaf node splits:       0
                      logons cumulative:      1                  parse count (failures):       0
                     parse count (hard):      0                          physical reads:    1546
          physical reads cache prefetch:      0                         physical writes:     603
                              redo size: 618436                         redo synch time:      16
                      redo synch writes:    181                         redo write time:      15
                            redo writes:    174   rollbacks only - consistent read gets:       0
                           sorts (disk):      0                          sorts (memory):     259
              table scans (long tables):      0              table scans (short tables):       9
                  transaction rollbacks:      0                            user commits:     182
</pre>
<pre>
$tbstat 1 0 event
-------------------------------------------------------------------------------
-- tbstat v0.3.3 --- a tool for oracle system statistics and event.
-- Powered by NinGoo.net
-------------------------------------------------------------------------------              

                   Event Name:   waits   time                       Event Name: waits   time
--------------------------------------------------------------------------------------------
      LGWR wait for redo copy:       1   0.01    SQL*Net more data from client:   151  19.95
  SQL*Net more data to client:    1218   0.01                buffer busy waits:     2   0.01
  control file parallel write:       1   0.51     control file sequential read:     1   0.26
                cursor: pin S:       0   0.00          cursor: pin S wait on X:     0   0.00
        db file parallel read:       0   0.00           db file parallel write:     0   0.00
       db file scattered read:       0   0.00          db file sequential read:  2040   3.43
             direct path read:     269   0.71            direct path read temp:     0   0.00
            direct path write:      23   0.26           direct path write temp:     0   0.00
         enq: CF - contention:       0   0.00             enq: HW - contention:     7   9.00
         enq: SQ - contention:       0   0.00     enq: TX - allocate ITL entry:     0   0.00
   enq: TX - index contention:       0   0.00    enq: TX - row lock contention:     0   0.00
                   latch free:       0   0.00      latch: cache buffers chains:     0   0.00
         latch: library cache:       0   0.00              latch: redo writing:     0   0.00
    latch: session allocation:       0   0.00               library cache lock:     0   0.00
             log buffer space:       0   0.00          log file parallel write:   145   0.60
     log file sequential read:     145   0.53       log file switch completion:     0   0.00
                log file sync:     147   0.78                os thread startup:     0   0.00
        read by other session:       0   0.00                   row cache lock:     0   0.00
       undo segment extension:       0   0.00
</pre>
<p>如果输入的name是精确匹配到只有一条统计信息的，会在后面打印出间隔时间内排名前10的sid的值。利用此功能，可以很方便的抓到造成某些统计信息异常的会话和SQL语句，会话和SQL信息是通过关联v$session来获取的。因此需要注意，如果统计信息对应的事件持续时间很短，从v$session里抓取到的sql可能并不是造成统计信息升高的罪魁祸首，但是sid一般来说还是准确的，因为应用采用的大多是连接池来连接数据库的，因此还是可以更具sid和machine信息来看看造成异常的是哪个具体的应用。</p>
<p>例如，全表扫描一般会导致physical reads cache prefetch等待事件，因此可以通过查看该事件对应的top sid来获得具体的语句，当然，不是所有的physical reads cache prefetch都是全表扫描导致的，因此对于获得的结果，还需要DBA根据具体情况做进一步分析：</p>
<pre>
$tbstat 1 0 'physical reads cache prefetch'
-------------------------------------------------------------------------------
-- tbstat v0.3.3 --- a tool for oracle system statistics and event.
-- Powered by NinGoo.net
-------------------------------------------------------------------------------
 physical reads cache prefetch:         526                             

              sid        value     %              machine         sql_id
       ----------  ----------- -----  ------------------- --------------
             2928          302  69.7               test11  79db58a3dg921
             4902           67  15.5               test71  79db58a3dg921
             4821           64  14.8               test33  3afdq50xt03ch
             4544            0   0.0               test54  3afdq50xt03ch
             1801            0   0.0               test06  79db58a3dg921
             2830            0   0.0               test12  79db58a3dg921
              898            0   0.0               test09  4n7675hwwcndc
             1031            0   0.0               test16  79db58a3dg921
              463            0   0.0               test04  3afdq50xt03ch
             1364            0   0.0               test08 cq749u66x06uj
             1408            0   0.0               test27  39rbqj3ck76w3
              722            0   0.0               test37  26hdkf07336uf
</pre>
<p>当然，tbstat只是一个用于抽取统计状态的小工具而已，如果要用于故障诊断，则还是要求DBA对于v$systat和v$system_event中各种统计和事件非常的熟悉。tbstat使用了DBD::Oracle以sysdba身份来连接数据库，因此需要为Perl安装DBI和DBD::Oracle模块，并且在数据库服务器本机上执行。如果你对于这个工具有兴趣，可以<a href="http://www.ningoo.net/software/tbstat">在这里下载源代码</a>，使用过程中，如果有什么建议和需求，欢迎告诉我。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/use_oracle_regexp_monitor_database_session.html" title="使用Oracle正则表达式监控应用到数据库的连接情况">使用Oracle正则表达式监控应用到数据库的连接情况</a></li><li><a href="http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html" title="dstat:一款简单直观的os实时监控工具">dstat:一款简单直观的os实时监控工具</a></li><li><a href="http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html" title="使用jpgraph绘制数据库监控图形">使用jpgraph绘制数据库监控图形</a></li><li><a href="http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html" title="使用BBED帮助理解Oracle数据块结构">使用BBED帮助理解Oracle数据块结构</a></li><li><a href="http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html" title="安装使用BBED">安装使用BBED</a></li><li><a href="http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html" title="记一次redo损坏导致ora-600[4000]的恢复">记一次redo损坏导致ora-600[4000]的恢复</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html'>http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html#comment'>Add Comments(9)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>安昌古镇与乔波冰雪世界</title>
		<link>http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html</link>
		<comments>http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html#comments</comments>
		<pubDate>Mon, 04 Jan 2010 14:32:58 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[杂记]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[安昌]]></category>
		<category><![CDATA[乔波冰雪世界]]></category>
		<category><![CDATA[滑雪]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1196</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 元旦小假，第一天窝在家里，睡到自然醒，醒到自然睡，好好的放纵了一整天。2号一大早就赶到东站，出发去绍兴柯桥，计划去乔波冰雪世界滑雪，晚上则夜宿安昌... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>元旦小假，第一天窝在家里，睡到自然醒，醒到自然睡，好好的放纵了一整天。2号一大早就赶到东站，出发去绍兴柯桥，计划去乔波冰雪世界滑雪，晚上则夜宿安昌古镇。</p>
<blockquote><p>“乔波冰雪世界”是由前速滑世界冠军叶乔波女士倡导，清华科技园（启迪控股）投资建设，为我国唯一一家以室内滑雪为主、酒店（住宿、会议、餐饮、娱乐）为辅的综合性高档运动休闲场所。公司目标是成为我国运动与休闲产业的行业巨人。“绍兴乔波冰雪世界”是“乔波冰雪世界”在全国范围内的连锁企业之一，于 2009年10月初隆重开业。</p>
<p>公司坐落于国家AAAA级风景名胜“浙江省绍兴县鉴湖-柯岩风景区”内，南傍山北依水。项目总投资超过4亿元，建筑面积逾6万㎡。以大型室内滑雪馆、真冰溜冰场为主，并配套四星级会议休闲酒店，是一家集四季滑雪、溜冰、旅游、会议、餐饮、度假、娱乐为一体的综合性运动休闲场所。</p></blockquote>
<p>结果11点多到的时候，人满为患，滑雪服等用具已经被先到的人一扫而空，无奈之下，只能临时改变计划，先去安昌，第二天再赶个大早过来，反正买的是全天票，正好可以多玩点时间。3号早上9点赶到，刚开门没几个人，玩的更high。</p>
<p><img src="http://pic.yupoo.com/ningoo/345178a5704a/medium.jpg" alt="乔波冰雪世界" /></p>
<blockquote><p>位于浙江省绍兴县安昌镇，始建于北宋时期，后因战乱，多次焚毁，又于明清时期重建，其建筑风格传承了典型的江南水乡特色，一依带水，古朴典雅，为浙江省重点历史保护地，其特产安昌腊肠、扯白糖远近闻名，具有水乡风情的水上婚礼也是别具特色。</p>
<p>安昌镇南靠柯桥，北邻杭甬高速公路，是一个具有千年历史的典型江南水乡古镇。境内现存白洋新石器时代越族先民遗址。相传大禹曾在镇东涂山娶妻成家。公元896年，钱镠奉唐王朝之命屯兵该地平董昌之乱，因命其乡为安昌。现存老街始建于明成化、弘治年间，数百年来，棉、布、米集散旺盛，蔚为越北大市重镇。抗战前夕尚有商号933家，是城区外市集之最。</p>
<p>安昌明清老街依河而建，全长1747米，至今保存完好。粼粼河水，石板街路，错落有致的翻轩骑楼，传统特色的店铺作坊，姿态各异的拱桥石梁，古老凝重的台门，幽深僻静的弄堂，风貌古朴典雅，无不体现出浓浓的水乡特色。</p></blockquote>
<p>安昌也可以算是典型的江南水乡古镇，可惜河水已经是惨不忍睹，不知何年能重见清清河水沿街流，小小乌篷河上游的写意画卷了。到达的时候沿街人声鼎沸，熙熙攘攘，可以说无趣。唯一可让人怀念的，则是满街满门口挂的腊肠，确实名不虚传。<br />
<img src="http://pic.yupoo.com/ningoo/117768a56827/xdhbebbi.jpg" alt="安昌古镇" /></p>
<p><img src="http://pic.yupoo.com/ningoo/281778a56c05/qipo22zn.jpg" alt="安昌古镇" /></p>
<p>第二天从乔波冰雪世界出来已经是下午2点，精疲力尽的赶到轻纺城汽车站，不幸的是回杭州的班车最早的也要到5点多了。最后四个人一咬牙，决定倒公交车回去，先从柯桥坐到萧山，再在萧山转301到武林小广场，一路辗转，到家已是晚上6点。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/my_2010_plan.html" title="2010，风生水起">2010，风生水起</a></li><li><a href="http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html" title="往事不会随风-记我的2009">往事不会随风-记我的2009</a></li><li><a href="http://www.ningoo.net/html/2009/get_usa_vis.html" title="签证是个体力活">签证是个体力活</a></li><li><a href="http://www.ningoo.net/html/2009/welcome_to_change.html" title="拥抱变化">拥抱变化</a></li><li><a href="http://www.ningoo.net/html/2009/2009_07_22_total_solar_eclipse.html" title="日全食">日全食</a></li><li><a href="http://www.ningoo.net/html/2009/zong_guan_xian_hangzhou.html" title="纵贯线，老男人的春天">纵贯线，老男人的春天</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html'>http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html#comment'>Add Comments(3)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>2010，风生水起</title>
		<link>http://www.ningoo.net/html/2010/my_2010_plan.html</link>
		<comments>http://www.ningoo.net/html/2010/my_2010_plan.html#comments</comments>
		<pubDate>Fri, 01 Jan 2010 03:58:59 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[杂记]]></category>
		<category><![CDATA[2010]]></category>
		<category><![CDATA[life]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1188</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 无可奈何的，到了2010，无可奈何的，三十而立。无可奈何花落去，似曾相识燕归来，当年背得烂熟的诗，年少轻狂未解其意，而今嚼来一声叹息。
酸气倒完，生活继... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2010/my_2010_plan.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>无可奈何的，到了2010，无可奈何的，三十而立。无可奈何花落去，似曾相识燕归来，当年背得烂熟的诗，年少轻狂未解其意，而今嚼来一声叹息。</p>
<p>酸气倒完，生活继续。2009年，总体来说，虽有诸多不如意处，也做了不少事，有了不少改变。与己，逐步完成转变，从技术一线开始尝试学习团队管理；与事，数据库整体还算稳定，无奈Q4因为各种原因有点晚节不保，由此也可以看出任重道远，还有很多事情需要去做。一个人做好，一个团队做好，一个部门做好，一个公司做好，挑战各有不同，诚如古人言，修身，治家，齐国，平天下，境界不同，或许可以类比。2009年，挣扎彷徨在个人技术能力与团队之间，结果技术能力没有多大增长，团队管理也不尽如人意，这是硬伤，2010年，这两个方面需要平衡好，最大的挑战。</p>
<p>2010，我的wishlist，实际上主要三点昨天也在<a href="http://twitter.com/NinGoo">twitter</a>上唠叨过了：</p>
<ul>
<li>技术上远离一线操作，更需要精研深入，Oracle和MySQL方面至少各看一到两本好书</li>
<li>英语，年年念叨，年年没进步，2010，希望能达到初步口语沟通</li>
<li>技术之外，看十本书，小说，历史，经管，皆可</li>
<li>拿到驾照，2009年4月份就报考，却一直没有去练车，拖到2010，必须完成</li>
<li>拿到房子，准备装修。房子交付在2010年底，估计也只能是先做准备</li>
<li>写一本书，总结这两年Oracle的经验。如果2010年再不写，估计就再也写不出来了</li>
<li>买一辆车，不需要太好，代步足矣</li>
<li>至少出国旅游一次，已经定了4月去东南亚的行程，应该靠谱。</li>
</ul>
<p>希望我的2010，搅他个风生水起。待明年今日，再细数往事前程。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html" title="安昌古镇与乔波冰雪世界">安昌古镇与乔波冰雪世界</a></li><li><a href="http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html" title="往事不会随风-记我的2009">往事不会随风-记我的2009</a></li><li><a href="http://www.ningoo.net/html/2009/get_usa_vis.html" title="签证是个体力活">签证是个体力活</a></li><li><a href="http://www.ningoo.net/html/2009/welcome_to_change.html" title="拥抱变化">拥抱变化</a></li><li><a href="http://www.ningoo.net/html/2009/2009_07_22_total_solar_eclipse.html" title="日全食">日全食</a></li><li><a href="http://www.ningoo.net/html/2009/zong_guan_xian_hangzhou.html" title="纵贯线，老男人的春天">纵贯线，老男人的春天</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2010/my_2010_plan.html'>http://www.ningoo.net/html/2010/my_2010_plan.html</a></p><p><b><a href='http://www.ningoo.net/html/2010/my_2010_plan.html#comment'>Add Comments(7)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2010/my_2010_plan.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>往事不会随风-记我的2009</title>
		<link>http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html</link>
		<comments>http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html#comments</comments>
		<pubDate>Wed, 30 Dec 2009 14:54:49 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[杂记]]></category>
		<category><![CDATA[2009]]></category>
		<category><![CDATA[life]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1184</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 这一年，二零零九，二十九年的记忆朦胧
这一年，不再孤独，惦记回家是一种幸福
这一年，成为房奴，空中楼阁没有交付
这一年，远渡重洋，第一次亲游异国大陆
... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>这一年，二零零九，二十九年的记忆朦胧<br />
这一年，不再孤独，惦记回家是一种幸福<br />
这一年，成为房奴，空中楼阁没有交付<br />
这一年，远渡重洋，第一次亲游异国大陆<br />
这一年，调整变化，来如朝露去无踪<br />
这一年，风起云涌，墙内墙外变幻不同<br />
这一年，爬山喝茶，遍走杭城青峰<br />
这一年，二零零九，往事不会飘散随风</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/first_of_2009.html" title="2009，先声夺人">2009，先声夺人</a></li><li><a href="http://www.ningoo.net/html/2010/anchang_and_qiaobo_ice_snow_world.html" title="安昌古镇与乔波冰雪世界">安昌古镇与乔波冰雪世界</a></li><li><a href="http://www.ningoo.net/html/2010/my_2010_plan.html" title="2010，风生水起">2010，风生水起</a></li><li><a href="http://www.ningoo.net/html/2009/get_usa_vis.html" title="签证是个体力活">签证是个体力活</a></li><li><a href="http://www.ningoo.net/html/2009/welcome_to_change.html" title="拥抱变化">拥抱变化</a></li><li><a href="http://www.ningoo.net/html/2009/2009_07_22_total_solar_eclipse.html" title="日全食">日全食</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html'>http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html#comment'>Add Comments(5)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/my_2009_never_gone_with_wind.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>使用jpgraph绘制数据库监控图形</title>
		<link>http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html</link>
		<comments>http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html#comments</comments>
		<pubDate>Thu, 17 Dec 2009 06:40:45 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[监控]]></category>
		<category><![CDATA[jpgraph]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1170</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 对于数据库的监控，可以分成两种类型，一种是实时的错误告警，需要尽快将一些错误信息发送给相关责任人，这更多的属于救火的性质。另外一种就是关键指标历史... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>对于数据库的监控，可以分成两种类型，一种是实时的错误告警，需要尽快将一些错误信息发送给相关责任人，这更多的属于救火的性质。另外一种就是关键指标历史趋势的展示和分析，可以帮助DBA更加直观的发现数据库的指标的异常波动，提前发现问题。</p>
<p>市面上有不少商品的数据库监控产品，数据库厂商们也在不遗余力的推广自己的解决方案，其中不乏优秀的东西，不过能做到多产品兼容、可扩展伸缩和高度可定制的产品就凤毛麟角了。所以很多公司会自己开发一些数据库监控产品，不求大而全，只要能满足自己的业务需求就足矣。</p>
<p>对于自己开发的轻量级监控产品，使用LAMP等开源产品是比较合适的。数据的抽取和告警的发送，可以使用shell或者perl，几个简单的脚本就可以实现，放在crontab里定期跑跑就行，监控数据库使用MySQL存放，如果需要实现高可用的监控，考虑一下冗余，MySQL可以使用Master-Master Replication。而展示可以使用php等搭建一个简单的web网站，成本不高，效率很高。</p>
<p>对于历史趋势的展示，图形绘制是必须的，一图胜千言。如果使用的是PHP，<a href="http://www.aditus.nu/jpgraph/">jpgraph</a>或者是<a href="http://pchart.sourceforge.net/">pChart</a>都是不错的选择。相对而言，jpgraph的文档和开发都还在不断的丰富，最新版本3.0.6发布于2009-10-10，而pChart的最新版本则是2008-10-06发布的1.27d，因此建议使用jpgraph。如果使用java来架构监控网站，则<a href="http://www.anysql.net">anysql</a>的<a href="http://www.dbatools.net/mytools/webchart-feature-overview.html">DataReport</a>是值得推荐的一款产品，简单易上手，功能很强大很邪恶。</p>
<p>扯的有点远了，回到这篇文章的标题。jpgraph只是一个图形绘制的库，如果需要比较方便的用于数据库监控中，最好在上面再封装一层，以实现根据SQL语句查询的结果集自动绘制图形，这样可以更方便的做到批量化的增加关键指标的图形展示。另外，jpgraph对于中文的支持，因为使用了truetype字体的关系，有一点点复杂，需要从windows复制c:\windows\fonts\simsun.ttc和c:\windows\fonts\simhei.ttc到linux的/usr/share/fonts/truetype目录。jpgraph默认会将所有的中文转化成UTF8字符，如果数据库和web使用的是gb2312，则这个转化会导致乱码，解决办法是修改<strong>jpgraph_ttf.inc.php</strong>，注释掉转化部分的代码：</p>
<pre>
     /*   elseif( $aFF === FF_SIMSUN ) {
            // Do Chinese conversion
            if( $this->g2312 == null ) {
                include_once 'jpgraph_gb2312.php' ;
                $this->g2312 = new GB2312toUTF8();
            }
            return $this->g2312->gb2utf8($aTxt);
        }*/
</pre>
<p>绘制图形时，在需要使用中文的地方，显式的调用字体设置函数，将字体设置为simsun，即可正确的显示中文：</p>
<pre>
$graph->title->SetFont(FF_SIMSUN,FS_BOLD);
$graph->title->Set("中文测试");
</pre>
<p>最后，贴两张用jpgraph绘制的Oracle数据库监控效果图：<br />
<img src="http://pic.yupoo.com/ningoo/3703988d4951/f5qv3ypm.jpg" alt="sql_executions" width="480" height="200" border="0" /></p>
<p><img src="http://pic.yupoo.com/ningoo/4557588d4951/ndnpb506.jpg" alt="sequential_reads" width="480" height="200" border="0" /></p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/use_jquery_and_tablesorter_for_data_sort_and_pager.html" title="用jQuery+Tablesorter实现客户端分页与排序">用jQuery+Tablesorter实现客户端分页与排序</a></li><li><a href="http://www.ningoo.net/html/2010/dstat_os_monitor_tool.html" title="dstat:一款简单直观的os实时监控工具">dstat:一款简单直观的os实时监控工具</a></li><li><a href="http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html" title="tbstat:实时监控数据库统计状态的小工具">tbstat:实时监控数据库统计状态的小工具</a></li><li><a href="http://www.ningoo.net/html/2009/use_oracle_regexp_monitor_database_session.html" title="使用Oracle正则表达式监控应用到数据库的连接情况">使用Oracle正则表达式监控应用到数据库的连接情况</a></li><li><a href="http://www.ningoo.net/html/2009/how_to_monitor_stats_of_memcached.html" title="如何监控MemCached的状态">如何监控MemCached的状态</a></li><li><a href="http://www.ningoo.net/html/2009/use_memcached_in_php.html" title="在PHP使用MemCached">在PHP使用MemCached</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html'>http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html#comment'>Add Comments(5)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/use_jpgraph_for_database_monitor.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>NoSQL，关系数据库终结者？</title>
		<link>http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html</link>
		<comments>http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html#comments</comments>
		<pubDate>Wed, 16 Dec 2009 12:56:27 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[rdbms]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1160</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 作为一个DBA，几年走过来，工作中历经MS SQL Server 2000/2005，Oracle9i/10g/11g，MySQL5.0/5.1等不同的数据库产品和版本，对于关系型数据库系统(RDBMS)应该说有了一定的了解。... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>作为一个DBA，几年走过来，工作中历经MS SQL Server 2000/2005，Oracle9i/10g/11g，MySQL5.0/5.1等不同的数据库产品和版本，对于关系型数据库系统(RDBMS)应该说有了一定的了解。从一个数据库切换到另外一个数据库，虽然有些许艰难，却谈不上抗拒，甚至乐意主动去深入了解其他的数据库产品。关系理论的成熟，造就了这三十年来关系数据库在数据领域的绝对地位。</p>
<p>这两年在互联网行业，时间虽然不长，却深切的感受到了一个大型网站真实的压力。随着业务滚雪球式的增长，关系数据库由于对数据一致性的强硬要求(ACID)，在扩展性方面天生不足，可以预见的天花板犹如达摩克利斯之剑，随时可能落下。SQL优化，应用重构，硬件升级，垂直拆分，能用的武器轮番上场，也不过救急于一时。要能做到系统随着网站压力几乎无限制的扩展需求，水平拆分横向扩展是不可避免的终极魔法。过去一年，我们也在一些应用上逐步尝试水平拆分，遇到过各种各样的问题，当然也有很大的收获，积累了很多的经验。MySQL作为开源的关系数据库，也开始得到大规模的应用，毕竟水平拆分后数据库的规模将是指数级的增长，商业数据库如Oracle的成本，是不得不考虑的一个问题。</p>
<p>我们所遇到的问题，相信所有大型的互联网站都会碰到。关系数据库有其不可替代的特性，但是，我们真的所有的数据都需要使用关系数据库么？或许一个简单但是更加容易扩展的key-value存储更能够满足很多互联网应用的数据存储需求，Google和Amazon相继推出<a href="http://en.wikipedia.org/wiki/BigTable">BigTable</a>和<a href="http://en.wikipedia.org/wiki/Dynamo_%28storage_system%29">Dynamo</a>，可以说是在分布式数据库方面一个开荒式的尝试。2009年初，一个叫做<a href="http://en.wikipedia.org/wiki/NoSQL">NoSQL</a>的组织开始出现并运作，纠集了一支优秀的开源部队想要革了关系型数据库的命，至少是想革了关系型数据库在互联网行业的命。不管NoSQL是不是关系型数据库的终结者，却确确实实在推广一些优秀的开源分布式数据库产品，作为DBA，或许我们是时候花点时间去了解一下这个对手了，不然饭碗丢了可能还不明白是谁抢去了，呵呵。</p>
<p>下面是wikipedia上列出了NoSQL的一些开源项目，有时间应该去了解一下。实际上，<a href="http://www.taobao.com">淘宝</a>也有在开发并使用自己的一套分布式cache和持久化系统，不知道什么时候也能看到开源版本的出现，呵呵。<br />
    * <a href="http://en.wikipedia.org/wiki/Cassandra_(database)">Cassandra</a><br />
    * <a href="http://en.wikipedia.org/wiki/Chordless">Chordless</a><br />
    * <a href="http://en.wikipedia.org/wiki/CouchDB">CouchDB</a><br />
    * <a href="http://en.wikipedia.org/wiki/Db4o">Db4o</a><br />
    * <a href="http://en.wikipedia.org/wiki/GT.M">GT.M</a><br />
    * <a href="http://en.wikipedia.org/wiki/Hbase">Hbase</a><br />
    * <a href="http://en.wikipedia.org/wiki/Hypertable">Hypertable</a><br />
    * <a href="http://en.wikipedia.org/wiki/Memcachedb">Memcachedb</a><br />
    * <a href="http://en.wikipedia.org/wiki/Mnesia">Mnesia</a><br />
    * <a href="http://en.wikipedia.org/wiki/MongoDB">MongoDB</a><br />
    * <a href="http://en.wikipedia.org/wiki/Neo4j">Neo4j</a><br />
    * <a href="http://en.wikipedia.org/wiki/Project_Voldemort">Project Voldemort</a><br />
    * <a href="http://en.wikipedia.org/wiki/Redis_(dbms)">Redis</a></p>
<p>发现有半个月没写blog了，这篇比较水，充一下数。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/cassandra_commitlog.html" title="Cassandra Commitlog">Cassandra Commitlog</a></li><li><a href="http://www.ningoo.net/html/2010/cassandra_storage.html" title="Cassandra存储机制">Cassandra存储机制</a></li><li><a href="http://www.ningoo.net/html/2008/open_source_and_sun_mysql.html" title="开源，是一种精神">开源，是一种精神</a></li><li><a href="http://www.ningoo.net/html/2007/adjust_blog_theme.html" title="模板颜色调整完毕">模板颜色调整完毕</a></li><li><a href="http://www.ningoo.net/html/2008/chm_file_can_not_open.html" title="WindowsXP上chm文件无法打开一例">WindowsXP上chm文件无法打开一例</a></li><li><a href="http://www.ningoo.net/html/2007/oracle_olap_active_catalog_views.html" title="Oracle OLAP之活动目录视图">Oracle OLAP之活动目录视图</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html'>http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html#comment'>Add Comments(2)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/nosql_teminator_of_rdbms.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>如何在AIX中编译Perl</title>
		<link>http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html</link>
		<comments>http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html#comments</comments>
		<pubDate>Tue, 01 Dec 2009 11:25:52 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[AIX]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1146</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net AIX默认是不带编译器的，所以如果要自己编译Perl，需要先安装编译器。开源的gcc自然可以算是最佳选择。
在64位平台的AIX中，如果选择使用gcc来编译perl源代码，默认... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>AIX默认是不带编译器的，所以如果要自己编译Perl，需要先安装编译器。开源的<a href="http://www.ningoo.net/html/2009/how_to_install_gcc_on_aix.html">gcc</a>自然可以算是最佳选择。</p>
<p>在64位平台的AIX中，如果选择使用gcc来编译perl源代码，默认情况下是编译成32位的版本。这样在编译DBD::Oracle的时候也需要选择正确的32位库，否则无法编译成功。也可以选择将Perl编译成64位的，这样在64位平台上应该更方便些，很多依赖库的路径使用默认即可。</p>
<p>编译成32位Perl</p>
<pre>
$./Configure -des -Dprefix=/opt/perl  -Dcc=gcc
$make &#038;&#038; make install
</pre>
<p>编译成64位Perl</p>
<pre>
$./Configure -des -Dprefix=/opt/perl  -Dcc='gcc -maix64'
$make &#038;&#038; make install
</pre>
<p>注：gcc在linux下64位编译选项为-m64，在HP-UX下64位编译选项为-mlp64。不同平台下需要编译64位程序，选用不同的选项即可。为了确认编译后的版本，可以使用-V选项运行perl：</p>
<pre>
$./perl -V
Summary of my perl5 (revision 5 version 10 subversion 1) configuration:

  Platform:
    osname=aix, osvers=5.3.0.0, archname=aix-64all
    uname='aix dbtest 3 5 00cad8cf4c00 '
    config_args='-des -Dprefix=/opt/perl -Dcc=gcc -maix64'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='gcc -maix64 -maix64', ccflags ='-D_ALL_SOURCE -D_ANSI_C_SOURCE
-D_POSIX_SOURCE -DUSE_NATIVE_DLOPEN -fno-strict-aliasing -pipe
-maix64 -DUSE_64_BIT_ALL',
    optimize='-O',
    cppflags='-D_ALL_SOURCE -D_ANSI_C_SOURCE -D_POSIX_SOURCE
-DUSE_NATIVE_DLOPEN -fno-strict-aliasing -pipe'
    ccversion='', gccversion='4.2.0', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=87654321
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=8
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='gcc -maix64 -maix64', ldflags =' -Wl,-brtl -Wl,-bdynamic -Wl,-b64'
    libpth=/lib /usr/lib /usr/ccs/lib
    libs=-lbind -lnsl -ldbm -ldl -lld -lm -lcrypt -lc
    perllibs=-lbind -lnsl -ldl -lld -lm -lcrypt -lc
    libc=/lib/libc.a, so=a, useshrplib=false, libperl=libperl.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_aix.xs, dlext=so, d_dlsymun=undef,
ccdlflags='-Xlinker -bE:/opt/perl/lib/5.10.1/aix-64all/CORE/perl.exp'
    cccdlflags=' ', lddlflags='  -Wl,-b64 -Wl,-bhalt:4 -Wl,
-G -Wl,-bI:$(PERL_INC)/perl.exp -Wl,
-bE:$(BASEEXT).exp -Wl,-bnoentry -lc -lm'

Characteristics of this binary (from libperl):
  Compile-time options: PERL_DONT_CREATE_GVSV PERL_MALLOC_WRAP USE_64_BIT_ALL
                        USE_64_BIT_INT USE_LARGE_FILES USE_PERLIO
  Built under aix
  Compiled at Dec  1 2009 19:48:22
  @INC:
    /opt/perl/lib/5.10.1/aix-64all
    /opt/perl/lib/5.10.1
    /opt/perl/lib/site_perl/5.10.1/aix-64all
    /opt/perl/lib/site_perl/5.10.1
</pre>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/how_to_install_gcc_on_aix.html" title="如何在AIX上安装gcc">如何在AIX上安装gcc</a></li><li><a href="http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html" title="如何让Perl脚本同时只运行一个实例">如何让Perl脚本同时只运行一个实例</a></li><li><a href="http://www.ningoo.net/html/2009/perl_module_use_lib_path.html" title="Perl自定义模块的路径包含问题">Perl自定义模块的路径包含问题</a></li><li><a href="http://www.ningoo.net/html/2009/sendmail_by_perl.html" title="使用Perl发送邮件">使用Perl发送邮件</a></li><li><a href="http://www.ningoo.net/html/2009/set_env_for_perl_script_crontab_schedule.html" title="在Crontab中调度Perl程序的环境变量问题">在Crontab中调度Perl程序的环境变量问题</a></li><li><a href="http://www.ningoo.net/html/2008/how_to_run_processes_on_background_in_linux.html" title="Linux中如何让进程在后台运行">Linux中如何让进程在后台运行</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html'>http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html#comment'>Add Comments(0)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何让Perl脚本同时只运行一个实例</title>
		<link>http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html</link>
		<comments>http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html#comments</comments>
		<pubDate>Sun, 29 Nov 2009 04:18:59 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[lockfile]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1136</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 用Perl写了一些监控脚本，放在crontab中调度执行。有时候会发现一个脚本运行时间过长，会同时跑起多个实例，因此有必要为脚本加上控制，只运行一个实例运行。
最... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>用Perl写了一些监控脚本，放在crontab中调度执行。有时候会发现一个脚本运行时间过长，会同时跑起多个实例，因此有必要为脚本加上控制，只运行一个实例运行。</p>
<p>最简单自然的想法，在脚本中检查并创建一个空的lock文件，脚本结束时再删除。通过判断文件是否存在的方式来判断脚本是否已经运行。不过这样做有个bug，如果脚本运行过程中异常终止，lock文件没有正常删除，就会导致脚本无法再运行。</p>
<p>空的lock文件不行，那么考虑在lock文件中加入一点内容，比如进程的PID号，然后通过检查该PID号的进程是否还在运行，就能避免上述bug了。在<a href="http://search.cpan.org">CPAN</a>上有很多现成的模块能够完成上述功能，如<strong>File::Lockfile</strong>， <strong>File::Pid</strong>， <strong>Proc::PID::File</strong> 等。</p>
<p>下面是File::Lockfile的一个示例，非常简单：</p>
<pre>
#!/usr/bin/perl -w
use File::Lockfile;
# lock文件位于/tmp目录，名为test_file_lock.lck
my $lockfile = File::Lockfile->new('test_file_lock','/tmp');
# 检查脚本是否已经运行，如已运行则退出
if ( my $pid = $lockfile->check ) {
  print "program is already running with PID: $pid";
  exit;
}
#更新lock文件
$lockfile->write;
# 脚本逻辑
sleep 30
#删除lock文件
$lockfile->remove;
</pre>
<p>通过查看File/Lockfile.pm的源代码可以看到，判断lock文件中记录的进程是否已经运行，简单的通过<strong>kill 0,$pid</strong>即可实现。所以即使不用上述模块，自己实现也是非常容易的。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html" title="如何在AIX中编译Perl">如何在AIX中编译Perl</a></li><li><a href="http://www.ningoo.net/html/2009/perl_module_use_lib_path.html" title="Perl自定义模块的路径包含问题">Perl自定义模块的路径包含问题</a></li><li><a href="http://www.ningoo.net/html/2009/sendmail_by_perl.html" title="使用Perl发送邮件">使用Perl发送邮件</a></li><li><a href="http://www.ningoo.net/html/2009/set_env_for_perl_script_crontab_schedule.html" title="在Crontab中调度Perl程序的环境变量问题">在Crontab中调度Perl程序的环境变量问题</a></li><li><a href="http://www.ningoo.net/html/2008/how_mysql_get_current_running_sql_statements.html" title="MySQL如何获取当前执行的SQL">MySQL如何获取当前执行的SQL</a></li><li><a href="http://www.ningoo.net/html/2008/using_perl_monitor_database.html" title="用Perl的hash数组实现个性化监控">用Perl的hash数组实现个性化监控</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html'>http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html#comment'>Add Comments(1)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Perl自定义模块的路径包含问题</title>
		<link>http://www.ningoo.net/html/2009/perl_module_use_lib_path.html</link>
		<comments>http://www.ningoo.net/html/2009/perl_module_use_lib_path.html#comments</comments>
		<pubDate>Sat, 28 Nov 2009 14:44:22 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[findbin]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1131</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net Perl模块是重用代码的好方法，但是在调用自定义模块时的路径问题困扰了我许久。之前一直都是通过在代码中直接将自定义模块所在的绝对路径写入到@INC数组来解决... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/perl_module_use_lib_path.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p><a href="http://www.ningoo.net/html/2008/how_to_programming_perl_module.html">Perl模块</a>是重用代码的好方法，但是在调用自定义模块时的路径问题困扰了我许久。之前一直都是通过在代码中直接将自定义模块所在的绝对路径写入到@INC数组来解决的，以下示例，加入perl脚本放置在/opt/perl/bin，而自定义模块放在/opt/perl/lib目录：</p>
<pre>
BEGIN {
    push (@INC,'/opt/perl/lib');
}
</pre>
<p>或者</p>
<pre>
BEGIN {
    unshift @INC,'/opt/perl/lib';
}
</pre>
<p>或者</p>
<pre>
 use lib '/opt/perl/lib';
</pre>
<p>使用绝对路径比较麻烦，如果将程序迁移到另外的安装目录，就需要去更改所有的脚本。而直接在use lib中使用相对路径，如use lib &#8216;../lib&#8217;;，测试发现手动执行bin目录下的perl脚本是可以的，但是放到crontab里去跑就找不到模块了。于是想先找到bin目录的路径，然后通过相对路径跳转到lib目录，在《Perl Cookbook》终于找到了我想要的，使用FindBin模块就能实现梦想了：</p>
<pre>
use FindBin qw($Bin);
use lib "$Bin/../lib";
</pre>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2008/how_to_programming_perl_module.html" title="编写Perl模块">编写Perl模块</a></li><li><a href="http://www.ningoo.net/html/2009/how_to_compile_perl_on_aix.html" title="如何在AIX中编译Perl">如何在AIX中编译Perl</a></li><li><a href="http://www.ningoo.net/html/2009/how_to_run_perl_script_only_one_instance.html" title="如何让Perl脚本同时只运行一个实例">如何让Perl脚本同时只运行一个实例</a></li><li><a href="http://www.ningoo.net/html/2009/sendmail_by_perl.html" title="使用Perl发送邮件">使用Perl发送邮件</a></li><li><a href="http://www.ningoo.net/html/2009/set_env_for_perl_script_crontab_schedule.html" title="在Crontab中调度Perl程序的环境变量问题">在Crontab中调度Perl程序的环境变量问题</a></li><li><a href="http://www.ningoo.net/html/2008/how_mysql_get_current_running_sql_statements.html" title="MySQL如何获取当前执行的SQL">MySQL如何获取当前执行的SQL</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/perl_module_use_lib_path.html'>http://www.ningoo.net/html/2009/perl_module_use_lib_path.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/perl_module_use_lib_path.html#comment'>Add Comments(1)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/perl_module_use_lib_path.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>遭遇MySQL Replication Fatal Error 1236</title>
		<link>http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html</link>
		<comments>http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html#comments</comments>
		<pubDate>Fri, 27 Nov 2009 14:48:49 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1123</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 一套Master-Master Replication的MySQL集群，版本5.1.37。其中一个节点A出现OS异常重启，数据库启动后表现正常。但是没过多久另外一个节点B报错：

091127 21:50:21 [ERROR] Error rea... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>一套Master-Master Replication的MySQL集群，版本5.1.37。其中一个节点A出现OS异常重启，数据库启动后表现正常。但是没过多久另外一个节点B报错：</p>
<pre>
091127 21:50:21 [ERROR] Error reading packet from server: Client requested master to start replication
from impossible position ( server_errno=1236)
091127 21:50:21 [ERROR] Got fatal error 1236: 'Client requested master to start replication
from impossible position' from master when reading data from binary log
091127 21:50:21 [Note] Slave I/O thread exiting, read up to log 'mysql-bin.000535', position 193022771
</pre>
<p>Slave_IO_Running线程终止。仔细看上面的报错信息，说slave进程试图从mysql-bin.000535日志的position 193022771开始启动恢复，但是该日志中是没有这个position的。</p>
<p>跑到A上通过mysqlbinlog查看该日志，发现最后一个有效position是193009460。而要求的193022771已经大于最后有效的position了。这个原因就搞不明白了，难道是因为A库异常关闭后导致A节点的binlog没有来得及刷到磁盘，而B节点slave已经恢复到前面去了？</p>
<pre>
$mysqlbinlog mysql-bin.000535 > 1.txt

$tail -n 7 1.txt
# at 193009460
#091127 20:50:21 server id 1  end_log_pos 193009487     Xid = 194299849
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
</pre>
<p>尝试将B节点change master到最后一个有效的position处，问题暂时得到解决：</p>
<pre>
change master to master_log_file='mysql-bin.000535', master_log_pos=193009460
</pre>
<p>网上搜索了一把，发现logzgh之前也碰到过<a href="http://logzgh.itpub.net/post/3185/467447">同样的问题</a>，版本是5.0.51。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/introduce_to_postgresql.html" title="PostgreSQL简介">PostgreSQL简介</a></li><li><a href="http://www.ningoo.net/html/2009/gearman-for-mysql.html" title="Gearman for MySQL">Gearman for MySQL</a></li><li><a href="http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html" title="InnoDB线程并发检查机制">InnoDB线程并发检查机制</a></li><li><a href="http://www.ningoo.net/html/2009/xtradb_enhancements_in_show-innodb-status.html" title="从show innodb status看XtraDB的增强特性">从show innodb status看XtraDB的增强特性</a></li><li><a href="http://www.ningoo.net/html/2009/xtradb_storage_engine.html" title="XtraDB存储引擎">XtraDB存储引擎</a></li><li><a href="http://www.ningoo.net/html/2009/xtrabackup-0-9_released.html" title="Xtrabackup 0.9发布">Xtrabackup 0.9发布</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html'>http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html#comment'>Add Comments(1)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>使用BBED帮助理解Oracle数据块结构</title>
		<link>http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html</link>
		<comments>http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html#comments</comments>
		<pubDate>Sat, 21 Nov 2009 14:55:26 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[bbed]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1116</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net BBED是Oracle提供的块编辑器，借助BBED，可以帮助我们更好的理解Oracle的Block的结构。当然，反过来说，也只有更加理解块的结构，才能更好的利用BBED完成某些特殊情况... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>BBED是Oracle提供的块编辑器，借助BBED，可以帮助我们更好的理解Oracle的Block的结构。当然，反过来说，也只有更加理解块的结构，才能更好的利用BBED完成某些特殊情况下的灾难恢复。</p>
<p>Oracle Data Block的结构简图如下，其中从Data header到Row Data部分合称Data Layer:</p>
<pre>
---------------------
- Cache Layer       -
---------------------
- Transaction Layer -
---------------------
- Data Header       -
---------------------
- Table Directory   -
---------------------
- Row Directory     -
---------------------
- Free Space        -
---------------------
- Row Data          -
---------------------
- Tailchk           -
---------------------
</pre>
<p>通过bbed的map命令，可以看到数据块内部的一些数据结构名：</p>
<pre>
BBED> map
 File: /u01/oracle/oradata/dbmon/system.dbf (1)
 Block: 31729                                 Dba:0x00407bf1
------------------------------------------------------------
 KTB Data Block (Table/Cluster)
 struct kcbh, 20 bytes                      @0
 struct ktbbh, 72 bytes                     @20
 struct kdbh, 14 bytes                      @92
 struct kdbt[1], 4 bytes                    @106
 sb2 kdbr[336]                              @110
 ub1 freespace[821]                         @782
 ub1 rowdata[6585]                          @1603
 ub4 tailchk                                @8188
</pre>
<p><strong>Cache Layer：</strong>Block的第一部分，长度为20字节，内部数据结构名为kcbh，包括<br />
 type_kcbh:块类型(table/index,rollback segment,temporary segment等)<br />
 frmt_kcbh:块格式(v6,v7,v8)<br />
 rdba_kcbh:块地址DBA<br />
 bas_kcbh/wrp_kcbh:SCN<br />
 seq_kcbh:块的序列号<br />
 flg_kcbh:块的标志</p>
<pre>
BBED> p kcbh
struct kcbh, 20 bytes                       @0
   ub1 type_kcbh                            @0        0x06
   ub1 frmt_kcbh                            @1        0xa2
   ub1 spare1_kcbh                          @2        0x00
   ub1 spare2_kcbh                          @3        0x00
   ub4 rdba_kcbh                            @4        0x00407bf1
   ub4 bas_kcbh                             @8        0xd6449de8
   ub2 wrp_kcbh                             @12       0x0595
   ub1 seq_kcbh                             @14       0x03
   ub1 flg_kcbh                             @15       0x04 (KCBHFCKV)
   ub2 chkval_kcbh                          @16       0x9130
   ub2 spare3_kcbh                          @18       0x0000
  </pre>
<p><strong>Transaction Layer：</strong>内部结构名kcbbh。分成两部分，第一部分为固定长度，长度为24字节，包含事务相关的一些基本信息。第二部分为可变长度，包含itl，长度根据itl条目的个数变化，每个itl长度为24字节，内部结构名ktbbhitl。</p>
<pre>
BBED> p ktbbh
struct ktbbh, 72 bytes                      @20
   ub1 ktbbhtyp                             @20       0x01 (KDDBTDATA)
   union ktbbhsid, 4 bytes                  @24
      ub4 ktbbhsg1                          @24       0x000050fc
      ub4 ktbbhod1                          @24       0x000050fc
   struct ktbbhcsc, 8 bytes                 @28
      ub4 kscnbas                           @28       0xd6449de7
      ub2 kscnwrp                           @32       0x0595
   b2 ktbbhict                              @36       2
   ub1 ktbbhflg                             @38       0x02 (NONE)
   ub1 ktbbhfsl                             @39       0x00
   ub4 ktbbhfnx                             @40       0x00000000
   struct ktbbhitl[0], 24 bytes             @44
      struct ktbitxid, 8 bytes              @44
         ub2 kxidusn                        @44       0x0008
         ub2 kxidslt                        @46       0x000c
         ub4 kxidsqn                        @48       0x0000e991
      struct ktbituba, 8 bytes              @52
         ub4 kubadba                        @52       0x0080222e
         ub2 kubaseq                        @56       0x03a4
         ub1 kubarec                        @58       0x22
      ub2 ktbitflg                          @60       0x8000 (KTBFCOM)
      union _ktbitun, 2 bytes               @62
         b2 _ktbitfsc                       @62       1429
         ub2 _ktbitwrp                      @62       0x0595
      ub4 ktbitbas                          @64       0xd6449de6
   struct ktbbhitl[1], 24 bytes             @68
      struct ktbitxid, 8 bytes              @68
         ub2 kxidusn                        @68       0x0008
         ub2 kxidslt                        @70       0x0015
         ub4 kxidsqn                        @72       0x0000e992
      struct ktbituba, 8 bytes              @76
         ub4 kubadba                        @76       0x0080222e
         ub2 kubaseq                        @80       0x03a4
         ub1 kubarec                        @82       0x23
      ub2 ktbitflg                          @84       0x0001 (NONE)
      union _ktbitun, 2 bytes               @86
         b2 _ktbitfsc                       @86       0
         ub2 _ktbitwrp                      @86       0x0000
      ub4 ktbitbas                          @88       0x00000000
</pre>
<p><strong>Data Layer：</strong>包括Data Header，Table Directory，Row Directory，Free Space和Row Data。其中<br />
Data Header：长度14字节，内部数据结构名kdbh</p>
<pre>
BBED> p kdbh
struct kdbh, 14 bytes                       @92
   ub1 kdbhflag                             @92       0x00 (NONE)
   b1 kdbhntab                              @93       1
   b2 kdbhnrow                              @94       336
   sb2 kdbhfrre                             @96      -1
   sb2 kdbhfsbo                             @98       690
   sb2 kdbhfseo                             @100      1511
   b2 kdbhavsp                              @102      821
   b2 kdbhtosp                              @104      821
</pre>
<p>Table Directory: 一般table只有一个条目，cluster则有一个或多个条目。每个条目长4字节，内部数据结构名kdbt。</p>
<pre>
BBED> p kdbt
struct kdbt[0], 4 bytes                     @106
   b2 kdbtoffs                              @106      0
   b2 kdbtnrow                              @108      336
</pre>
<p>Row Directory：数目由块中数据的行数决定，每个条目长2字节，内部数据结构名kdbr</p>
<pre>
BBED> p kdbr
sb2 kdbr[0]                                 @110      7998
sb2 kdbr[1]                                 @112      8017
...
sb2 kdbr[335]                               @780      1511
</pre>
<p>Free Space：表示数据块中可用空间，内部数据结构名freespace</p>
<p>Row Data：表示实际的数据，内部数据结构名rowdata</p>
<p>Tailchk:保存在块结尾用于校验的数据，长度4个字节，内部结构名tailchk。</p>
<pre>
BBED>p tailchk
ub4 tailchk                                 @8188     0x9de80603
</pre>
<p>注意到tailchk=bas_kcbh低2字节(9de8)+type_kcbh(06)+seq_kcbh(03)</p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html" title="安装使用BBED">安装使用BBED</a></li><li><a href="http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html" title="记一次redo损坏导致ora-600[4000]的恢复">记一次redo损坏导致ora-600[4000]的恢复</a></li><li><a href="http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html" title="tbstat:实时监控数据库统计状态的小工具">tbstat:实时监控数据库统计状态的小工具</a></li><li><a href="http://www.ningoo.net/html/2009/oracle_open_world_2009_fouth_day.html" title="OOW2009美国行:大会第四天">OOW2009美国行:大会第四天</a></li><li><a href="http://www.ningoo.net/html/2009/oracle_open_world_2009_third_day.html" title="OOW2009美国行:大会第三天">OOW2009美国行:大会第三天</a></li><li><a href="http://www.ningoo.net/html/2009/use_oracle_regexp_monitor_database_session.html" title="使用Oracle正则表达式监控应用到数据库的连接情况">使用Oracle正则表达式监控应用到数据库的连接情况</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html'>http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html#comment'>Add Comments(0)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>安装使用BBED</title>
		<link>http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html</link>
		<comments>http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html#comments</comments>
		<pubDate>Sat, 14 Nov 2009 16:41:20 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[bbed]]></category>
		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1107</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 在一次故障处理中，使用到了BBED(Oracle Block Brower and EDitor Tool)，这是Oracle一款内部工具，可以直接修改Oracle数据文件块的内容，在一些极端恢复场景下比较有用。该工... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p>在<a href="http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html">一次故障处理</a>中，使用到了<strong>BBED</strong>(Oracle <strong>B</strong>lock <strong>B</strong>rower and <strong>ED</strong>itor Tool)，这是Oracle一款内部工具，可以直接修改Oracle数据文件块的内容，在一些极端恢复场景下比较有用。该工具不受Oracle支持，所以默认是没有生成可执行文件的，在使用前需要重新连接。</p>
<p>在9i/10g中连接生成bbed:</p>
<pre>
cd $ORACLE_HOME/rdbms/lib
make -f ins_rdbms.mk $ORACLE_HOME/rdbms/lib/bbed
</pre>
<p>以上生成的bbed可执行文件在$ORACLE_HOME/rdbms/lib目录，可以复制到其他位置或者其他同Oracle版本的机器上运行。也可通过以下命令将bbed生成到$ORACLE_HOME/bin目录</p>
<pre>
make -f ins_rdbms.mk BBED=$ORACLE_HOME/bin/bbed $ORACLE_HOME/bin/bbed
</pre>
<p>在11g中生成bbed，需要先从10g中复制如下文件到相应目录，然后再执行上述连接命令:</p>
<pre>
$ORACLE_HOME/rdbms/lib/ssbbded.o
$ORACLE_HOME/rdbms/lib/sbbdpt.o
$ORACLE_HOME/rdbms/mesg/bbedus.msb
</pre>
<p>BBED设置了口令保护，密码为<strong>blockedit</strong>。BBED的命令行参数：</p>
<pre>
$bbed help=yes
PASSWORD - Required parameter
FILENAME - Database file name
BLOCKSIZE - Database block size
LISTFILE - List file name
MODE - [browse/edit]
SPOOL - Spool to logfile [no/yes]
CMDFILE - BBED command file name
LOGFILE - BBED log file name
PARFILE - Parameter file name
BIFILE - BBED before-image file name
REVERT - Rollback changes from BIFILE [no/yes]
SILENT - Hide banner [no/yes]
HELP - Show all valid parameters [no/yes]
</pre>
<p>一般将一些常用选项写入到一个parfile中：</p>
<pre>
blocksize=8192
listfile=filelist.txt
mode=edit
</pre>
<p>其中filelist.txt列出了需要使用BBED编辑的数据文件列表，格式为</p>
<pre>
文件编号 文件名字 文件大小

1 /u01/oracle/oradata/NinGoo/system.dbf 2097160192
2 /u01/oracle/oradata/NinGoo/test.dbf 2097160192
</pre>
<p>这里的文件编号不一定要和数据库里的file_id相同，当然最好能设置相同，以免在处理过程中搞混淆了。</p>
<pre>
$bbed parfile=bbed.par
Password: 

BBED: Release 2.0.0.0.0 - Limited Production on Sun Nov 15 00:26:14 2009

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

************* !!! For Oracle Internal Use only !!! ***************

BBED> help
HELP [ <bbed command> | ALL ]

BBED> help all
SET DBA [ dba | file#, block# ]
SET FILENAME 'filename'
SET FILE file#
SET BLOCK [+/-]block#
SET OFFSET [ [+/-]byte offset | symbol | *symbol ]
SET BLOCKSIZE bytes
SET LIST[FILE] 'filename'
SET WIDTH character_count
SET COUNT bytes_to_display
SET IBASE [ HEX | OCT | DEC ]
SET OBASE [ HEX | OCT | DEC ]
SET MODE  [ BROWSE | EDIT ]
SET SPOOL [ Y | N ]
SHOW [ <SET parameter> | ALL ]
INFO
MAP[/v] [ DBA | FILENAME | FILE | BLOCK ]
DUMP[/v] [ DBA | FILENAME | FILE | BLOCK | OFFSET | COUNT ]
PRINT[/x|d|u|o|c] [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
EXAMINE[/Nuf] [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
</Nuf>:
N - a number which specifies a repeat count.
u - a letter which specifies a unit size:
  b - b1, ub1 (byte)
  h - b2, ub2 (half-word)
  w - b4, ub4(word)
  r - Oracle table/index row
f - a letter which specifies a display format:
  x - hexadecimal
  d - decimal
  u - unsigned decimal
  o - octal
  c - character (native)
  n - Oracle number
  t - Oracle date
  i - Oracle rowid
FIND[/x|d|u|o|c] numeric/character string [ TOP | CURR ]
COPY [ DBA | FILE | FILENAME | BLOCK ] TO [ DBA | FILE | FILENAME | BLOCK ]
MODIFY[/x|d|u|o|c] numeric/character string
      [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
ASSIGN[/x|d|u|o] <target spec>=<source spec>
<target spec> : [ DBA | FILE | FILENAME | BLOCK | OFFSET | symbol | *symbol ]
<source spec> : [ value | <target spec options> ]
SUM [ DBA | FILE | FILENAME | BLOCK ] [ APPLY ]
PUSH [ DBA | FILE | FILENAME | BLOCK | OFFSET ]
POP [ALL]
REVERT [ DBA | FILE | FILENAME | BLOCK ]
UNDO
HELP [ <bbed command> | ALL ]
VERIFY [ DBA | FILE | FILENAME | BLOCK ]
CORRUPT [ DBA | FILE | FILENAME | BLOCK ]
</pre>
<p>关于bbed的具体用法，这里就不罗嗦了，有兴趣的可以自行学习，下面这篇参考文档非常的详尽：<br />
<a href="http://orafaq.com/papers/dissassembling_the_data_block.pdf">http://orafaq.com/papers/dissassembling_the_data_block.pdf</a></p>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2009/bbed_and_oracle_data_block_structure.html" title="使用BBED帮助理解Oracle数据块结构">使用BBED帮助理解Oracle数据块结构</a></li><li><a href="http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html" title="记一次redo损坏导致ora-600[4000]的恢复">记一次redo损坏导致ora-600[4000]的恢复</a></li><li><a href="http://www.ningoo.net/html/2010/tbstat_a_tool_for_oracle_sysstat_realtime_monitor.html" title="tbstat:实时监控数据库统计状态的小工具">tbstat:实时监控数据库统计状态的小工具</a></li><li><a href="http://www.ningoo.net/html/2009/oracle_open_world_2009_fouth_day.html" title="OOW2009美国行:大会第四天">OOW2009美国行:大会第四天</a></li><li><a href="http://www.ningoo.net/html/2009/oracle_open_world_2009_third_day.html" title="OOW2009美国行:大会第三天">OOW2009美国行:大会第三天</a></li><li><a href="http://www.ningoo.net/html/2009/use_oracle_regexp_monitor_database_session.html" title="使用Oracle正则表达式监控应用到数据库的连接情况">使用Oracle正则表达式监控应用到数据库的连接情况</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html'>http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html#comment'>Add Comments(2)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/how_to_link_oracle_bbed_tools.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Gearman for MySQL</title>
		<link>http://www.ningoo.net/html/2009/gearman-for-mysql.html</link>
		<comments>http://www.ningoo.net/html/2009/gearman-for-mysql.html#comments</comments>
		<pubDate>Thu, 12 Nov 2009 11:13:43 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[gearman]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1099</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net Gearman是一个开源的分布式调度框架，支持多种语言。在分布式环境中，如何管理大量的服务器，将某些任务分发到大量的机器上调度执行，是一个比较大的挑战，Gearm... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/gearman-for-mysql.html'>NinGoo.net</a> <a href='http://rss.ningoo.net'><img style='border: 0pt none;' align='middle' src='http://www.feedsky.com/feed/NinGoo/sc/gif'></img></a></p><p><a href="http://gearman.org/">Gearman</a>是一个开源的分布式调度框架，支持多种语言。在分布式环境中，如何管理大量的服务器，将某些任务分发到大量的机器上调度执行，是一个比较大的挑战，Gearman为该类任务提供了一个不错的思路。在未来的MySQL集群环境中，Gearman这类工具应当大有用武之地，所以它也提供了MySQL UDF的支持。<br />
<img src="http://pic.yupoo.com/ningoo/3860385f6347/5b5mt8kp.jpg" alt="gearman_stack" width="479" height="302" border="0" /></p>
<p>一个Gearman请求的处理过程涉及三个角色：Client -> Job -> Worker。<br />
　　Client：请求的发起者，可以是 C，PHP，Perl，MySQL UDF 等等。<br />
　　Job：请求的调度者，用来负责协调把 Client 发出的请求转发给合适的 Worker。<br />
　　Worker：请求的处理者，可以是 C，PHP，Perl 等等。<br />
因为 Client，Worker 并不限制用一样的语言，所以有利于多语言多系统之间的集成。</p>
<div style="width:425px;text-align:left" id="__ss_2482321"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/datacharmer/gearman-for-mysql" title="Gearman for MySQL">Gearman for MySQL</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=gearmanformysql-091112043234-phpapp02&#038;rel=0&#038;stripped_title=gearman-for-mysql" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=gearmanformysql-091112043234-phpapp02&#038;rel=0&#038;stripped_title=gearman-for-mysql" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="font-size:11px;font-family:tahoma,arial;height:26px;padding-top:2px;">View more <a style="text-decoration:underline;" href="http://www.slideshare.net/">documents</a> from <a style="text-decoration:underline;" href="http://www.slideshare.net/datacharmer">Giuseppe Maxia</a>.</div>
</div>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2010/introduce_to_postgresql.html" title="PostgreSQL简介">PostgreSQL简介</a></li><li><a href="http://www.ningoo.net/html/2009/mysql_replication_fatal_error_1236.html" title="遭遇MySQL Replication Fatal Error 1236">遭遇MySQL Replication Fatal Error 1236</a></li><li><a href="http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html" title="InnoDB线程并发检查机制">InnoDB线程并发检查机制</a></li><li><a href="http://www.ningoo.net/html/2009/xtradb_enhancements_in_show-innodb-status.html" title="从show innodb status看XtraDB的增强特性">从show innodb status看XtraDB的增强特性</a></li><li><a href="http://www.ningoo.net/html/2009/xtradb_storage_engine.html" title="XtraDB存储引擎">XtraDB存储引擎</a></li><li><a href="http://www.ningoo.net/html/2009/xtrabackup-0-9_released.html" title="Xtrabackup 0.9发布">Xtrabackup 0.9发布</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/gearman-for-mysql.html'>http://www.ningoo.net/html/2009/gearman-for-mysql.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/gearman-for-mysql.html#comment'>Add Comments(3)</a></b> | <b><a href='http://twitter.com/NinGoo'>Follow NinGoo@Twitter</a></b> | <b><a href='http://www.google.com/ig/add?feedurl=http://rss.ningoo.net'>Google Reader</a></b></p>]]></content:encoded>
			<wfw:commentRss>http://www.ningoo.net/html/2009/gearman-for-mysql.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
