﻿﻿<?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>NinGoo.net</title>
	<atom:link href="http://www.ningoo.net/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ningoo.net</link>
	<description>High Availability DBA &#38; Simple Life</description>
	<lastBuildDate>Fri, 05 Feb 2010 03:46:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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(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/2010/dstat_os_monitor_tool.html/feed</wfw:commentRss>
		<slash:comments>2</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/oracle10gr2_logical_standby_managing_and_tuning.html" title="Oracle10gR2 Logical Standby(七)日常管理与优化">Oracle10gR2 Logical Standby(七)日常管理与优化</a></li><li><a href="http://www.ningoo.net/html/2008/ready_go_beijing.html" title="出发，北京">出发，北京</a></li><li><a href="http://www.ningoo.net/html/2007/oracle11g_new_feature_virtual_column.html" title="Oracle11g新特性：虚拟列virtual column">Oracle11g新特性：虚拟列virtual column</a></li><li><a href="http://www.ningoo.net/html/2008/one_year_in_hangzhou.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/2007/oracle11g_new_feather_advanced_compression_option.html" title="Oracle11g新特性：Advanced Compression Option">Oracle11g新特性：Advanced Compression Option</a></li><li><a href="http://www.ningoo.net/html/2008/shanghai_passer_by.html" title="上海过客">上海过客</a></li><li><a href="http://www.ningoo.net/html/2009/oracle_open_world_2009_arrived_at_new_york.html" title="OOW2009美国行:抵达纽约">OOW2009美国行:抵达纽约</a></li><li><a href="http://www.ningoo.net/html/2007/my_2007.html" title="懵懵懂懂又一年">懵懵懂懂又一年</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[互联网]]></category>
		<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/2008/open_source_and_sun_mysql.html" title="开源，是一种精神">开源，是一种精神</a></li><li><a href="http://www.ningoo.net/html/2008/how_to_set_alternate_archive_destination.html" title="Oracle如何设置备用归档路径">Oracle如何设置备用归档路径</a></li><li><a href="http://www.ningoo.net/html/2008/mysql_innodb_transaction_isolation_level.html" title="MySQL InnoDB存储引擎的事务隔离级别">MySQL InnoDB存储引擎的事务隔离级别</a></li><li><a href="http://www.ningoo.net/html/2008/2008_is_over.html" title="2008逗号完了">2008逗号完了</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/2007/using_cognos_script_editor_interactive_with_cognos_products.html" title="使用CognosScript Editor编程和Cognos产品交互">使用CognosScript Editor编程和Cognos产品交互</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(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/mysql_replication_fatal_error_1236.html/feed</wfw:commentRss>
		<slash:comments>0</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>
		<item>
		<title>记一次redo损坏导致ora-600[4000]的恢复</title>
		<link>http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html</link>
		<comments>http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html#comments</comments>
		<pubDate>Wed, 11 Nov 2009 14:23:06 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[bbed]]></category>
		<category><![CDATA[ora-600]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[恢复]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1082</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net 据说今天是光棍节，逢年过节，必有大事。快下班的时候，一位朋友碰到了一个大问题，数据库服务器异常断电重启以后，数据库无法启动，报ora-600[4000]错误，尝试... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.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>ora-600[4000]</strong>错误，尝试了使用隐藏参数，还是无法打开。</p>
<pre>
ORA-00704: bootstrap process failure
ORA-00704: bootstrap process failure
ORA-00600: internal error code, arguments: [4000], [46], [], [], [], [], [], []
</pre>
<p>从<a href="http://www.ningoo.net/html/2009/ora-600_internal_error_categories.html">ora-600内部错误的类型</a>可以看到，4000是trasaction undo相关的内部错误。搜索一下可以发现，logzgh同学已经两次碰到过该问题了(<a href="http://logzgh.itpub.net/post/3185/191423">第一次</a>，<a href="http://logzgh.itpub.net/post/3185/489281">第二次</a>)。看来在异常宕机的情况下，这个问题出现的机率还是比较高的。既然是有先例的，恢复起来应该是没有问题的，这个案例和logzgh的第一个案例基本一样，trace文件里显示也是obj$上有异常事务:</p>
<pre>
ORA-00600: internal error code, arguments: [4000], [46], [], [], [], [], [], []
Current SQL statement for this session:
select ctime, mtime, stime from obj$ where obj# = :1
</pre>
<p>将trace文件往下翻，可以看到出现问题的block的dump信息，在block的itl中果然有一条活动事务存在。用<strong>bbed</strong>找到对应块，设置offset到61，将itl的flag从20改为80，重新计算block的checksum并apply。然后尝试重新启动数据库，这次ora-600[4000]错误没有了，报的是<strong>ora-600[2662]</strong>错误，数据库无法open。2662就比较容易了，网上相关的<a href="http://www.eygle.com/archives/2005/12/oracle_diagnostics_howto_deal_2662_error.html">处理案例</a>已经一大堆了，将数据库启动到mount状态，设置<strong>10015</strong>事件来调整scn即可</p>
<pre>
alter session set events '10015 trace name adjust_scn level 1';
</pre>
<p>如果数据库已经处于open状态，则可以使用如下语句来调整SCN:</p>
<pre>
alter session set events 'IMMEDIATE trace name ADJUST_SCN level x';
</pre>
<p>再次重新打开库，报<strong>ora-600[2256]</strong>，继续使用10015事件，这次level设置为(ora-600错误的第三个参数+1)*4</p>
<pre>
alter session set event '10015 trace name adjust_scn level 20';
</pre>
<p>重启，报<strong>ora-600[4097]</strong>错误，好了，现在基本上恢复已经接近尾声了，使用下面几个隐藏参数，可以正常将数据库打开了。接下来就是告诉朋友做全库export/重建库/import了。</p>
<pre>
_allow_resetlogs_corruption=true
_allow_terminal_recovery_corruption=true
_corrupted_rollback_segments=(_SYSSMU1$, _SYSSMU2$, _SYSSMU3$, _SYSSMU4$, _SYSSMU5$, _SYSSMU6$,
 _SYSSMU7$, _SYSSMU8$, _SYSSMU9$, _SYSSMU10$)
</pre>
<p>年关将近，事故高发时期，阿弥陀佛，多求个平安吧。话说这个案例还没来得及些写呢，晚上回家的路上又接到说有人truncate了一张表。。。</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/how_to_link_oracle_bbed_tools.html" title="安装使用BBED">安装使用BBED</a></li><li><a href="http://www.ningoo.net/html/2009/ora-600_internal_error_categories.html" title="ora-600内部错误的类型">ora-600内部错误的类型</a></li><li><a href="http://www.ningoo.net/html/2009/in_memory_undo_and_logical_standby_database.html" title="In Memory Undo与logical standby database">In Memory Undo与logical standby database</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></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html'>http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/ora-600_4000_affter_redo_corruption.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/ora-600_4000_affter_redo_corruption.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>性能测试工具sysbench简介</title>
		<link>http://www.ningoo.net/html/2009/performance_test_tool_sysbench.html</link>
		<comments>http://www.ningoo.net/html/2009/performance_test_tool_sysbench.html#comments</comments>
		<pubDate>Sun, 08 Nov 2009 04:14:30 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[工具]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[sysbench]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1063</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net sysbench是一款开源的多线程性能测试工具，可以执行CPU/内存/线程/IO/数据库等方面的性能测试。数据库目前支持MySQL/Oracle/PostgreSQL。本文只是简单演示一下几种测试的... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/performance_test_tool_sysbench.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://sysbench.sourceforge.net">sysbench</a>是一款开源的多线程性能测试工具，可以执行CPU/内存/线程/IO/数据库等方面的性能测试。数据库目前支持MySQL/Oracle/PostgreSQL。本文只是简单演示一下几种测试的用法，后续准备利用sysbench来对MySQL进行一系列的测试。具体的一些参数设置，需要根据不同的测试要求来进行调整。</p>
<p><strong>下载</strong><br />
<a href="http://sourceforge.net/projects/sysbench/">http://sourceforge.net/projects/sysbench/</a><br />
<strong><br />
编译安装</strong><br />
默认支持MySQL，如果需要测试Oracle/PostgreSQL，则在configure时需要加上&#8211;with-oracle或者&#8211;with-pgsql参数</p>
<pre>
./configure --prefix=/u01/sysbench \
--with-mysql-includes=/opt/mysql/include/mysql \
--with-mysql-libs=/opt/mysql/lib/mysql

make &#038;&#038; make install
</pre>
<p><strong>参数</strong></p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench
Missing required command argument.
Usage:
  sysbench [general-options]... --test=<test-name> [test-options]... command

General options:
  --num-threads=N            number of threads to use [1]
  --max-requests=N           limit for total number of requests [10000]
  --max-time=N               limit for total execution time in seconds [0]
  --forced-shutdown=STRING   amount of time to wait after --max-time before forcing shutdown [off]
  --thread-stack-size=SIZE   size of stack per thread [32K]
  --init-rng=[on|off]        initialize random number generator [off]
  --test=STRING              test to run
  --debug=[on|off]           print more debugging info [off]
  --validate=[on|off]        perform validation checks where possible [off]
  --help=[on|off]            print help and exit
  --version=[on|off]         print version and exit

Compiled-in tests:
  fileio - File I/O test
  cpu - CPU performance test
  memory - Memory functions speed test
  threads - Threads subsystem performance test
  mutex - Mutex performance test
  oltp - OLTP test

Commands: prepare run cleanup help version
See 'sysbench --test=<name> help' for a list of options for each test.
</pre>
<p><strong>CPU测试</strong><br />
sysbench采用寻找最大素数的方式来测试CPU的性能</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=cpu --cpu-max-prime=2000 run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 1

Doing CPU performance benchmark

Threads started!
Done.

Maximum prime number checked in CPU test: 2000

Test execution summary:
    total time:                          2.3996s
    total number of events:              10000
    total time taken by event execution: 2.3917
    per-request statistics:
         min:                                  0.23ms
         avg:                                  0.24ms
         max:                                 27.44ms
         approx.  95 percentile:               0.24ms

Threads fairness:
    events (avg/stddev):           10000.0000/0.00
    execution time (avg/stddev):   2.3917/0.00
</pre>
<p><strong>线程测试</strong></p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=threads --num-threads=64 --thread-yields=100 \
--thread-locks=2 run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 64

Doing thread subsystem performance test
Thread yields per test: 100 Locks used: 2
Threads started!
Done.

Test execution summary:
    total time:                          4.3925s
    total number of events:              10000
    total time taken by event execution: 280.4418
    per-request statistics:
         min:                                  0.04ms
         avg:                                 28.04ms
         max:                                 72.81ms
         approx.  95 percentile:              52.29ms

Threads fairness:
    events (avg/stddev):           156.2500/1.43
    execution time (avg/stddev):   4.3819/0.01
</pre>
<p><strong><br />
文件IO性能测试</strong><br />
首先生成需要的测试文件，文件总大小300M，16个并发线程，随机读写模式。执行完后会在当前目录下生成一堆小文件。</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=fileio --num-threads=16  \
--file-total-size=300M --file-test-mode=rndrw prepare
sysbench 0.4.12:  multi-threaded system evaluation benchmark

128 files, 2400Kb each, 300Mb total
Creating files for the test...
</pre>
<p>执行测试</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=fileio --num-threads=16  \
--file-total-size=300M --file-test-mode=rndrw run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 16

Extra file open flags: 0
128 files, 2.3438Mb each
300Mb total file size
Block size 16Kb
Number of random requests for random IO: 10000
Read/Write ratio for combined random IO test: 1.50
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Threads started!
Done.

Operations performed:  5996 Read, 4004 Write, 12800 Other = 22800 Total
Read 93.688Mb  Written 62.562Mb  Total transferred 156.25Mb  (26.713Mb/sec)
 1709.66 Requests/sec executed

Test execution summary:
    total time:                          5.8491s
    total number of events:              10000
    total time taken by event execution: 12.5045
    per-request statistics:
         min:                                  0.01ms
         avg:                                  1.25ms
         max:                                373.28ms
         approx.  95 percentile:               0.03ms

Threads fairness:
    events (avg/stddev):           625.0000/109.60
    execution time (avg/stddev):   0.7815/0.29
</pre>
<p>清理现场</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=fileio --num-threads=16  \
--file-total-size=300M --file-test-mode=rndrw cleanup
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Removing test files...
</pre>
<p><strong>Mutex测试</strong></p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=mutex --num-threads=16 \
--mutex-num=1024 --mutex-locks=10000 --mutex-loops=5000 run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 16

Doing mutex performance test
Threads started!
Done.

Test execution summary:
    total time:                          1.1561s
    total number of events:              16
    total time taken by event execution: 18.3831
    per-request statistics:
         min:                               1084.60ms
         avg:                               1148.94ms
         max:                               1153.52ms
         approx.  95 percentile:         10000000.00ms

Threads fairness:
    events (avg/stddev):           1.0000/0.00
    execution time (avg/stddev):   1.1489/0.02
</pre>
<p><strong>内存测试</strong></p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=memory --num-threads=16 \
--memory-block-size=8192 --memory-total-size=1G run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

Running the test with following options:
Number of threads: 16

Doing memory operations speed test
Memory block size: 8K

Memory transfer size: 1024M

Memory operations type: write
Memory scope type: global
Threads started!
WARNING: Operation time (0.000000) is less than minimal counted value, counting as 1.000000
WARNING: Percentile statistics will be inaccurate
Done.

Operations performed: 131072 (114162.68 ops/sec)

1024.00 MB transferred (891.90 MB/sec)

Test execution summary:
    total time:                          1.1481s
    total number of events:              131072
    total time taken by event execution: 16.0448
    per-request statistics:
         min:                                  0.00ms
         avg:                                  0.12ms
         max:                                  3.60ms
         approx.  95 percentile:               0.01ms

Threads fairness:
    events (avg/stddev):           8192.0000/192.89
    execution time (avg/stddev):   1.0028/0.00
</pre>
<p><strong>MySQL数据库测试</strong><br />
首先需要创建默认的sbtest数据库,或者使用&#8211;mysql-db指定一个已经存在的数据库</p>
<p>生成测试数据，引擎为myisam,表大小为1000000条记录</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=oltp --mysql-table-engine=myisam --oltp-table-size=1000000 \
--mysql-user=root --mysql-socket=/opt/mysql/run/mysql.sock prepare
sysbench 0.4.12:  multi-threaded system evaluation benchmark

No DB drivers specified, using mysql
Creating table 'sbtest'...
Creating 1000000 records in table 'sbtest'...

root@sbtest 11:42:18>desc sbtest.sbtest;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| k     | int(10) unsigned | NO   | MUL | 0       |                |
| c     | char(120)        | NO   |     |         |                |
| pad   | char(60)         | NO   |     |         |                |
+-------+------------------+------+-----+---------+----------------+
</pre>
<p>执行测试</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=oltp --mysql-table-engine=myisam --oltp-table-size=1000000 \
--mysql-user=root --mysql-socket=/opt/mysql/run/mysql.sock run
sysbench 0.4.12:  multi-threaded system evaluation benchmark

No DB drivers specified, using mysql
Running the test with following options:
Number of threads: 1

Doing OLTP test.
Running mixed OLTP test
Using Special distribution (12 iterations,  1 pct of values are returned in 75 pct cases)
Using "LOCK TABLES WRITE" for starting transactions
Using auto_inc on the id column
Maximum number of requests for OLTP test is limited to 10000
Threads started!
Done.

OLTP test statistics:
    queries performed:
        read:                            140000
        write:                           50000
        other:                           20000
        total:                           210000
    transactions:                        10000  (336.64 per sec.)
    deadlocks:                           0      (0.00 per sec.)
    read/write requests:                 190000 (6396.11 per sec.)
    other operations:                    20000  (673.27 per sec.)

Test execution summary:
    total time:                          29.7056s
    total number of events:              10000
    total time taken by event execution: 29.6301
    per-request statistics:
         min:                                  2.27ms
         avg:                                  2.96ms
         max:                                 43.88ms
         approx.  95 percentile:               3.36ms

Threads fairness:
    events (avg/stddev):           10000.0000/0.00
    execution time (avg/stddev):   29.6301/0.00
</pre>
<p>清理现场</p>
<pre>
NinGoo:/u01/sysbench/bin>$sysbench --test=oltp --mysql-table-engine=myisam --oltp-table-size=1000000 \
--mysql-user=root --mysql-socket=/opt/mysql/run/mysql.sock cleanup
sysbench 0.4.12:  multi-threaded system evaluation benchmark

No DB drivers specified, using mysql
Dropping table 'sbtest'...
Done.
</pre>
<br/><b>Related Articles</b><p><ul class="related_post"><li><a href="http://www.ningoo.net/html/2008/mysql_benchmark_function.html" title="MySQL的benchmark函数">MySQL的benchmark函数</a></li><li><a href="http://www.ningoo.net/html/2008/mysql_load_test_tool_mysqlslap.html" title="MySQL压力测试工具mysqlslap">MySQL压力测试工具mysqlslap</a></li><li><a href="http://www.ningoo.net/html/2007/an_introduction_to_swingbench.html" title="压力测试和展示工具Swingbench简介">压力测试和展示工具Swingbench简介</a></li><li><a href="http://www.ningoo.net/html/2007/buying_a_hard_disk_from_360buy.html" title="京东购物记">京东购物记</a></li><li><a href="http://www.ningoo.net/html/2008/using_hscale_for_mysql_partition_.html" title="用HSCALE实现MySQL的数据分布式存储">用HSCALE实现MySQL的数据分布式存储</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/2009/performance_test_tool_sysbench.html'>http://www.ningoo.net/html/2009/performance_test_tool_sysbench.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/performance_test_tool_sysbench.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/2009/performance_test_tool_sysbench.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>InnoDB线程并发检查机制</title>
		<link>http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html</link>
		<comments>http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html#comments</comments>
		<pubDate>Sat, 07 Nov 2009 13:52:02 +0000</pubDate>
		<dc:creator>NinGoo</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[InnoDB]]></category>
		<category><![CDATA[innodb_thead_concurrency]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.ningoo.net/?p=1032</guid>
		<description><![CDATA[Author:NinGoo posted on NinGoo.net InnoDB在接受MySQL线程调用能时，有一个并发线程的检查机制，通过innodb_thread_concurrency参数进行控制。如果参数设置大于0，则表示检查机制开启，允许进入的线程数就... ]]></description>
			<content:encoded><![CDATA[<p>Author:<a href='http://www.ningoo.net'>NinGoo</a> posted on <a href='http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.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>InnoDB在接受MySQL线程调用能时，有一个并发线程的检查机制，通过<strong>innodb_thread_concurrency</strong>参数进行控制。如果参数设置大于0，则表示检查机制开启，允许进入的线程数就是参数的值。等于0则禁用并发检查。</p>
<p>在新的MySQL线程调用Innodb接口前，Innodb会检查已经接受的请求线程数，如已经超过innodb_thread_concurrency设置的限制，则该请求线程会等待<strong>innodb_thread_sleep_delay</strong>微秒后尝试重新请求，如果第二次请求还是无法获得，则该线程会进入线程队列休眠。重试两次的机制是为了减少CPU的上下文切换的次数，以降低CPU消耗，这和Oracle中latch的spin机制是同样的道理。如果请求被Innodb接受，则会获得一个次数为<strong>innodb_concurrency_tickets</strong>(默认500次)的通行证，在次数用完之前，该线程重新请求时无须再进行前面所说innodb_thread_concurrency的检查。</p>
<p>上述检查逻辑在源码<strong>storage/innobase/srv/srv0srv.c</strong>(Innodb很多参数都可以在该文件中找到定义)的<strong>srv_conc_enter_innodb</strong>函数中，有兴趣的可以仔细阅读一下，代码比较浅显，不难理解。另外，如果是一个已经持有lock的线程，则通过调用<strong>srv_conc_force_enter_innodb</strong>函数可以无视该检查，这是为了避免线程长时间持有锁影响性能，且可能增加死锁的机率。除此之外，slave线程也是有无视检查直接通行的权限。</p>
<p>简单思考一下上述机制，可以得出一个初步的推论：在数据库并发请求较小的情况下，从性能上来说禁用检查机制应该是更好的，毕竟执行检查机制本身也需要加锁(Mutex)。当并发线程很高的情况下，则开启检查机制对性能更有利。至于具体innodb_thread_concurrency设置为多少，可能就需要在不同的条件下实际的做一下测试了，不同的硬件环境，不同的MySQL版本和Innodb版本，应该都会有一些区别。</p>
<p>源代码中对于innodb_thread_concurrency参数的注释如下：</p>
<pre>
/* The following controls how many threads we let inside InnoDB concurrently:
threads waiting for locks are not counted into the number because otherwise
we could get a deadlock. MySQL creates a thread for each user session, and
semaphore contention and convoy problems can occur withput this restriction.
Value 10 should be good if there are less than 4 processors + 4 disks in the
computer. Bigger computers need bigger values. Value 0 will disable the
concurrency check. */

ulong   srv_thread_concurrency  = 0;
</pre>
<p>因为检查机制需要Mutex保护(Mutex-based Model)，所以开启检查本身也有性能消耗，并且扩展性也会受到限制，在MySQL5.4版本中引入了一种新的机制（Timer-based Model），这里就不讨论了，有兴趣的可以参考<a href="http://mikaelronstrom.blogspot.com/2009/05/mysql-54-patches-innodb-thread.html">这里</a>(需要翻墙)，实际上<a href="http://www.ningoo.net/html/2009/xtradb_storage_engine.html">XtraDB存储引擎</a>里已经包含Timer-based Model，通过参数<strong>innodb_thread_concurrency_timer_based</strong>可以开启，默认为OFF。在MySQL5.4的srv0srv.c的源代码中的注释中，可以看到Google和Percona的版权声明，看来MySQL5.4中吸引了很多第三方的改进代码，值得期待。</p>
<br/><b>Related Articles</b><p><ul class="related_post"><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/an_introduce_of_innodb_monitor%e4%bb%8b%e7%bb%8d.html" title="Innodb monitor介绍">Innodb monitor介绍</a></li><li><a href="http://www.ningoo.net/html/2009/using_xtrabackup_backup_mysql_database_2.html" title="使用Xtrabackup备份MySQL数据库(续)">使用Xtrabackup备份MySQL数据库(续)</a></li><li><a href="http://www.ningoo.net/html/2008/mysql_innodb_transaction_isolation_level.html" title="MySQL InnoDB存储引擎的事务隔离级别">MySQL InnoDB存储引擎的事务隔离级别</a></li><li><a href="http://www.ningoo.net/html/2008/mysql_innodb_parametes.html" title="MySQL InnoDB存储引擎的一些参数">MySQL InnoDB存储引擎的一些参数</a></li></ul></p><p><b>PermLink:</b> <a href='http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html'>http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.html</a></p><p><b><a href='http://www.ningoo.net/html/2009/about_innodb_thread_concurrency.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/about_innodb_thread_concurrency.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
