<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Orzdba&#039;s Blog</title>
	<atom:link href="http://orzdba.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://orzdba.wordpress.com</link>
	<description>Think then do it...</description>
	<lastBuildDate>Mon, 29 Aug 2011 04:19:17 +0000</lastBuildDate>
	<language>zh</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='orzdba.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Orzdba&#039;s Blog</title>
		<link>http://orzdba.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://orzdba.wordpress.com/osd.xml" title="Orzdba&#039;s Blog" />
	<atom:link rel='hub' href='http://orzdba.wordpress.com/?pushpress=hub'/>
		<item>
		<title>【Perl】fork子进程</title>
		<link>http://orzdba.wordpress.com/2011/04/08/perl_fork/</link>
		<comments>http://orzdba.wordpress.com/2011/04/08/perl_fork/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 04:58:12 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[fork]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55699</guid>
		<description><![CDATA[Perl中可以通过fork函数来创建一个子进程。如果fork成功的话，这个时候就存在父进程和子进程，接下来2个进程会同时开始执行perl脚本中fork函数后面的代码。例子如下： #!/usr/local/bin/perl use Term::ANSIColor; use strict; use warnings; $SIG{CHLD} = &#8216;IGNORE&#8217;; #忽略SIGCHLD信号,回收僵尸子进程 print color(&#8216;green&#8217;),&#8221;Program started&#8230; pid = $$ \n\n&#8221;; print &#8220;fork process&#8230;\n&#8221;; # 如果fork失败，则返回undefined defined ( my $child = fork() ) or die &#8220;Fork Error : $!\n&#8221; ; # 如果fork成功: # 如果是父进程，则把生成的子进程PID赋值给$child。 # 如果是子进程，则把0赋值给$child。 if ($child == 0){ print color(&#8216;yellow&#8217;),&#8221;\n-&#62; child process running &#8230;\n&#8221;; print [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55699&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Perl中可以通过<a href="http://perldoc.perl.org/functions/fork.html" target="_blank">fork</a>函数来创建一个子进程。如果fork成功的话，这个时候就存在父进程和子进程，接下来2个进程会同时开始执行perl脚本中fork函数后面的代码。例子如下：</p>
<blockquote>
<div id="_mcePaste">#!/usr/local/bin/perl</div>
<div id="_mcePaste">use Term::ANSIColor;</div>
<div id="_mcePaste">use strict;</div>
<div id="_mcePaste">use warnings;</div>
<div id="_mcePaste"><strong>$SIG{CHLD} = &#8216;IGNORE&#8217;;</strong> <span style="color:#008000;">#忽略SIGCHLD信号,回收僵尸子进程</span></div>
<div>print color(&#8216;green&#8217;),&#8221;Program started&#8230; pid = $$ \n\n&#8221;;</div>
<div>print &#8220;fork process&#8230;\n&#8221;;</div>
<div><span style="color:#008000;"># 如果fork失败，则返回undefined</span></div>
<div id="_mcePaste"><strong>defined ( my $child = <span style="color:#ff0000;">fork() </span>) or die &#8220;Fork Error : $!\n&#8221; ;</strong></div>
<div id="_mcePaste"><span style="color:#008000;"># 如果fork成功:</span></div>
<div><span style="color:#008000;"># 如果是父进程，则把生成的子进程PID赋值给$child。</span></div>
<div><span style="color:#008000;"><span style="color:#000000;"><span style="color:#008000;"># 如果是子进程</span></span></span><span style="color:#008000;">，则把0赋值给$child。</span></div>
<div>if ($child == 0){</div>
<div id="_mcePaste">print color(&#8216;yellow&#8217;),&#8221;\n-&gt; child process running &#8230;\n&#8221;;</div>
<div id="_mcePaste">print &#8220;child process pid : <strong>$$</strong> !\n&#8221;;</div>
<div><span style="color:#008000;"># 退出子进程，如果前面没有对子进程处理的话，那么在父进程还没退出期间，子进程就变成</span><span style="color:#008000;">僵尸子进程。</span></div>
<div id="_mcePaste"><strong>exit 0;</strong></div>
<div id="_mcePaste">} else {</div>
<div id="_mcePaste">print color(&#8216;blue&#8217;),&#8221;\n-&gt; parent process running &#8230;\n&#8221;;</div>
<div id="_mcePaste">print &#8220;child process pid : $child ; parent process pid : <strong>$$</strong> !\n&#8221;,color(&#8216;reset&#8217;);</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">#sleep(10);</div>
<div id="_mcePaste">print &#8220;\nHello World!\n&#8221;;</div>
</blockquote>
<p>执行结果如下：</p>
<p><span id="more-55699"></span></p>
<blockquote><p><span style="color:#888888;">[zhuxu@xentest8-vm1 my_perl_test]$ ./fork.pl</span></p>
<p><span style="color:#008000;">Program started&#8230; pid = <strong><em>30881</em></strong></span></p>
<p><span style="color:#008000;">fork process&#8230;</span></p>
<p><span style="color:#ff9900;"><span style="color:#ff9900;">-&gt; child process running &#8230;</span></span></p>
<p><span style="color:#ff9900;"><span style="color:#ff9900;">child process pid : <strong><em>30882 </em></strong>!</span></span></p>
<p><span style="color:#0000ff;">-&gt; parent process running &#8230;</span></p>
<p><span style="color:#0000ff;">child process pid : <strong><em>30882 </em></strong>; parent process pid : <strong><em>30881 </em></strong>!</span></p>
<p>Hello World!</p>
<p><span style="color:#888888;">[zhuxu@xentest8-vm1 my_perl_test]$</span></p></blockquote>
<p>【参考】：<a href="http://hi.baidu.com/yafeiie/blog/item/540112a87ab3d8bacb130cbc.html" target="_blank">Perl fork()</a></p>
<p>&#8211;EOF&#8211;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55699/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55699/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55699/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55699/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55699/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55699/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55699/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55699/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55699&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/04/08/perl_fork/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【MySQL】LOAD DATA</title>
		<link>http://orzdba.wordpress.com/2011/03/17/mysql_load-data/</link>
		<comments>http://orzdba.wordpress.com/2011/03/17/mysql_load-data/#comments</comments>
		<pubDate>Wed, 16 Mar 2011 16:51:13 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[LOAD DATA]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55697</guid>
		<description><![CDATA[在使用LOAD DATA到MySQL的时候，有2种情况： （1）在远程客户端（需要添加选项：&#8211;local-infile=1）导入远程客户端文本到MySQL，需指定LOCAL（默认就是ignore）,加ignore选项会放弃数据，加replace选项会更新数据，都不会出现唯一性约束问题。 （2）在本地服务器导入本地服务器文本到MySQL，不指定LOACL，出现唯一性约束冲突，会失败回滚，数据导入不进去，这个时候就需要加ignore或者replace来导入数据。 测试如下： （1）本地服务器导入本地服务器文本 mysql&#62; show create table tmp_loaddata\G; *************************** 1. row *************************** Table: tmp_loaddata Create Table: CREATE TABLE `tmp_loaddata` ( `id` int(11) NOT NULL, `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified mysql&#62; select * from tmp_loaddata; +&#8212;-+&#8212;&#8212;+ &#124; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55697&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;">在使用<span lang="EN-US"><a style="color:blue;text-decoration:underline;" href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">LOAD DATA</a></span>到<span lang="EN-US">MySQL</span>的时候，有<span lang="EN-US">2</span>种情况：<span lang="EN-US"> </span></span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;">（<span lang="EN-US">1</span>）在远程客户端（需要添加选项：<strong><span style="color:red;" lang="EN-US">&#8211;local-infile=1</span></strong>）导入远程客户端文本到<span lang="EN-US">MySQL</span>，需指定<span lang="EN-US">LOCAL</span>（默认就是<span lang="EN-US">ignore</span>）<span lang="EN-US">,</span>加<span lang="EN-US">ignore</span>选项会放弃数据，加<span lang="EN-US">replace</span>选项会更新数据，都不会出现唯一性约束问题。<span lang="EN-US"> </span></span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;">（<span lang="EN-US">2</span>）在本地服务器导入本地服务器文本到<span lang="EN-US">MySQL</span>，不指定<span lang="EN-US">LOACL</span>，出现唯一性约束冲突，会失败回滚，数据导入不进去，这个时候就需要加<span lang="EN-US">ignore</span>或者<span lang="EN-US">replace</span>来导入数据。</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;">测试如下：</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;"><span id="more-55697"></span><br />
</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><strong><span style="font-family:幼圆;">（<span lang="EN-US">1</span>）本地服务器导入本地服务器文本</span></strong></p>
<blockquote>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; show create table tmp_loaddata\G;</span><span style="color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">*************************** 1. row ***************************</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="color:#a6a6a6;" lang="EN-US"> </span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> Table: tmp_loaddata</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">Create Table: </span><span style="font-family:幼圆;" lang="EN-US">CREATE TABLE `tmp_loaddata` (</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> `id` int(11) NOT NULL,</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> `name` varchar(10) DEFAULT NULL,</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> <span style="color:red;">PRIMARY KEY (`id`)</span></span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">) ENGINE=InnoDB DEFAULT CHARSET=latin1</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">1 row in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">ERROR:</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">No query specified</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">| id | name |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | test |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">1 row in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">system cat /home/zhuxu/1.txt</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">1,new update</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">2,new update</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><strong><span style="font-family:幼圆;" lang="EN-US">LOAD DATA INFILE &#8216;/home/zhuxu/1.txt&#8217; INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;;</span></strong></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">ERROR 1062 (23000): Duplicate entry &#8217;1&#8242; for key &#8216;PRIMARY&#8217;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US"># </span><span style="font-family:幼圆;color:#00b050;">出现唯一性约束冲突，会失败回滚</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">| id | name |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | test |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">1 row in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><strong><span style="font-family:幼圆;" lang="EN-US">LOAD DATA INFILE &#8216;/home/zhuxu/1.txt&#8217; <span style="color:red;">IGNORE</span> INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;;</span></strong></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">Query OK, </span><span style="font-family:幼圆;color:red;" lang="EN-US">1 row affected </span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">(0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">Records: 2</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Deleted: 0</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Skipped: 1</span><span style="color:#a6a6a6;" lang="EN-US"> </span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> Warnings: 0</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US"># </span><span style="font-family:幼圆;color:#00b050;">使用<span lang="EN-US">IGNORE</span>对于冲突的数据丢弃掉。</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">| id | name</span><span style="color:#a6a6a6;" lang="EN-US"> </span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | test</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 2 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">2 rows in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><strong><span style="font-family:幼圆;" lang="EN-US">LOAD DATA INFILE &#8216;/home/zhuxu/1.txt&#8217; <span style="color:red;">REPLACE</span> INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;; </span></strong><span style="color:#a6a6a6;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">Query OK, </span><span style="font-family:幼圆;color:red;" lang="EN-US">3 rows affected</span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">Records: 2</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Deleted: 1</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Skipped: 0</span><span style="color:#a6a6a6;" lang="EN-US"> </span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> Warnings: 0</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US"># </span><span style="font-family:幼圆;color:#00b050;">使用<span lang="EN-US">REPLACE</span>对于冲突的数据进行更新。</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">| id | name</span><span style="color:#a6a6a6;" lang="EN-US"> </span><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 2 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#a6a6a6;" lang="EN-US">2 rows in set (0.00 sec)</span></p>
</blockquote>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><strong><span style="font-family:幼圆;">（<span lang="EN-US">2</span>）远程客户端导入远程客户端文本</span></strong></p>
<blockquote>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">[zhuxu@xentest9-vm1 tmp]$ </span><span style="font-family:幼圆;" lang="EN-US">mysql -uzhuxu -pzhuxu test -h10.254.5.151</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Welcome to the MySQL monitor.</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> Commands end with ; or \g.</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Your MySQL connection id is 15</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Server version: 5.1.47-log Source distribution</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">This software comes with ABSOLUTELY NO WARRANTY. This is free software,</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">and you are welcome to modify and redistribute it under the GPL v2 license</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Type &#8216;help;&#8217; or &#8216;\h&#8217; for help. Type &#8216;\c&#8217; to clear the current input statement.</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">| id | name |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | test |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">1 row in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">system cat /tmp/2.txt</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">1,new update</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">2,new update</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">3,new update</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt;</span><span style="font-family:幼圆;" lang="EN-US"> LOAD DATA INFILE &#8216;/tmp/2.txt&#8217; INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">ERROR 13 (HY000): Can&#8217;t get stat of &#8216;/tmp/2.txt&#8217; (Errcode: 2)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US"># </span><span style="font-family:幼圆;color:#00b050;">由于数据库服务器没有对应的文本文件，所以报错。</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">LOAD DATA <span style="color:red;">LOCAL</span> INFILE &#8216;/tmp/2.txt&#8217; INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;; </span><span style="color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">ERROR 1148 (42000): The used command is not allowed with this MySQL version</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US"># </span><span style="font-family:幼圆;color:#00b050;">进去<span lang="EN-US">mysql</span>远程客户端，还需要加<strong><span lang="EN-US">&#8211;local-infile=1</span></strong>参数指定。</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; exit</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Bye</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">[zhuxu@xentest9-vm1 tmp]$ </span><span style="font-family:幼圆;" lang="EN-US">mysql -uzhuxu -pzhuxu test -h10.254.5.151<strong><span style="color:red;"> &#8211;local-infile=1 </span></strong>&#8211;show-warnings -v -v -v \</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">&gt; -e &#8220;LOAD DATA LOCAL INFILE &#8216;/tmp/2.txt&#8217; INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;&#8221;;</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">LOAD DATA LOCAL INFILE &#8216;/tmp/2.txt&#8217; INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Query OK, </span><span style="font-family:幼圆;color:red;" lang="EN-US">2 rows affected </span><span style="font-family:幼圆;color:gray;" lang="EN-US">(0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">Records: 3</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Deleted: 0</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Skipped: 1</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> Warnings: 0</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Bye</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">| id | name</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | test</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 2 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 3 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">3 rows in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US">#</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">[zhuxu@xentest9-vm1 tmp]$</span><span style="font-family:幼圆;" lang="EN-US"> mysql -uzhuxu -pzhuxu test -h10.254.5.151 <strong><span style="color:red;">&#8211;local-infile=1</span></strong> &#8211;show-warnings -v -v -v \</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">&gt; -e &#8220;LOAD DATA LOCAL INFILE &#8216;/tmp/2.txt&#8217; <span style="color:red;">IGNORE</span> INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;&#8221;;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">LOAD DATA LOCAL INFILE &#8216;/tmp/2.txt&#8217; IGNORE INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Query OK, </span><span style="font-family:幼圆;color:red;" lang="EN-US">0 rows affected</span><span style="font-family:幼圆;color:gray;" lang="EN-US"> (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">Records: 3</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Deleted: 0</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Skipped: 3</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> Warnings: 0</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Bye</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; </span><span style="font-family:幼圆;" lang="EN-US">select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">| id | name</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 1 | test</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 2 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">|</span><span lang="EN-US"> </span><span style="font-family:幼圆;" lang="EN-US"> 3 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">3 rows in set (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:#00b050;" lang="EN-US">#</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">[zhuxu@xentest9-vm1 tmp]$ </span><span style="font-family:幼圆;" lang="EN-US">mysql -uzhuxu -pzhuxu test -h10.254.5.151 <strong><span style="color:red;">&#8211;local-infile=1</span></strong> &#8211;show-warnings -v -v -v \</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">&gt; -e &#8220;LOAD DATA LOCAL INFILE &#8216;/tmp/2.txt&#8217; <span style="color:red;">REPLACE</span> INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;&#8221;;</span><span lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">LOAD DATA LOCAL INFILE &#8216;/tmp/2.txt&#8217; REPLACE INTO TABLE tmp_loaddata FIELDS TERMINATED BY &#8216;,&#8217;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">&#8212;&#8212;&#8212;&#8212;&#8211;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Query OK, </span><span style="font-family:幼圆;color:red;" lang="EN-US">4 rows affected</span><span style="font-family:幼圆;color:gray;" lang="EN-US"> (0.00 sec)</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">Records: 3</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Deleted: 1</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> Skipped: 0</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> Warnings: 0</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">Bye</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US"> </span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">mysql&gt; select * from tmp_loaddata;</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">| id | name</span><span style="color:gray;" lang="EN-US"> </span><span style="font-family:幼圆;color:gray;" lang="EN-US"> |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">|</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> 1 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">|</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> 2 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:red;" lang="EN-US">|</span><span style="color:red;" lang="EN-US"> </span><span style="font-family:幼圆;color:red;" lang="EN-US"> 3 | new update |</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">+&#8212;-+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;color:gray;" lang="EN-US">3 rows in set (0.00 sec)</span></p>
</blockquote>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US"><br />
</span></p>
<p style="text-align:justify;font-size:10.5pt;font-family:Calibri, sans-serif;line-height:normal;margin:0 0 .0001pt;"><span style="font-family:幼圆;" lang="EN-US">&#8211;EOF&#8211; </span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55697/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55697/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55697/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55697&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/03/17/mysql_load-data/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【MySQL】关于table cache的相关参数</title>
		<link>http://orzdba.wordpress.com/2011/03/15/mysql_table_cache/</link>
		<comments>http://orzdba.wordpress.com/2011/03/15/mysql_table_cache/#comments</comments>
		<pubDate>Tue, 15 Mar 2011 04:51:32 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[lsof]]></category>
		<category><![CDATA[Table Cache]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55695</guid>
		<description><![CDATA[一、状态值和变量： 1.1 在MySQL的show status中有2个状态值：Open_tables和Opened_tables。这2个值代表的意思如下： Open_tables  ：代表当前打开表的数量。 Opened_tables：代表自从MySQL启动后，打开表的数量。 关于MySQL怎么打开关闭表的具体细节参考文档：&#60;How MySQL Opens and Closes Tables&#62;。 （1）对于myisam存储引擎，打开1张表需要2个文件描述符（一个.MYD文件，一个.MYI文件）。 （2）对于innodb存储引擎，开启表的独立表空间（innodb_file_per_table）打开1张表只需要1个文件描述符（一个.ibd文件）。 【MySQL Variable】 对于上面的状态值，对应的5.1.3版本后的MySQL变量参数为table_open_cache，而早期版本为：table_cache，该参数值的代表MySQL可以缓存的打开表时候的最大文件描述符。 1.2 在MySQL 5.1.3之后，还添加了2个状态值：Open_table_definitions和Opened_table_definitions。这2个值代表的意思如下： Open_table_definitions  ：代表当前缓存了多少.frm文件。 Opened_table_definitions：代表自从MySQL启动后，缓存了.frm文件的数量。 需要注意的是.frm文件是MySQL用于存放表结构的文件，对应myisam和innodb存储引擎都必须有的。 【MySQL Variable】 对于上面的状态值，对应的5.1.3版本后的MySQL变量参数为table_definition_cache，该参数值的代表MySQL可以缓存的表定义的数量。和前面的table cache不同的是，表定义的缓存占用空间很小，而且不需要使用文件描述符，也就是只要打开.frm文件，缓存表定义，然后就可以关闭.frm文件。 1.3 另外，还有2个状态值：Open_files和Opened_files。这2个值的意思同上类似： Open_files  ：代表当前打开的文件。对应存储引擎（如：innodb）使用存储引擎自己内部函数打开的话，这个值是不会增加的。 Opened_files：代表使用MySQL的my_open()函数打开过的文件数。如果不是使用这个函数打开文件的话，这个值是不会增加的。 【MySQL Variable】 对于上面的状态值，对应的MySQL变量参数为open_files_limit。 二、测试 测试的MySQL版本： mysql&#62; select version(); +&#8212;&#8212;&#8212;&#8212;+ &#124; version()  &#124; +&#8212;&#8212;&#8212;&#8212;+ &#124; 5.1.47-log &#124; +&#8212;&#8212;&#8212;&#8212;+ 1 row in set [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55695&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>一、状态值和变量：</strong></p>
<p><strong>1.1</strong></p>
<p>在MySQL的show status中有2个状态值：<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Open_tables">Open_tables</a>和<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Opened_tables">Opened_tables</a>。这2个值代表的意思如下：</p>
<p>Open_tables  ：代表当前打开表的数量。</p>
<p>Opened_tables：代表自从MySQL启动后，打开表的数量。</p>
<p>关于MySQL怎么打开关闭表的具体细节参考文档：&lt;<a href="http://dev.mysql.com/doc/refman/5.1/en/table-cache.html">How MySQL Opens and Closes Tables</a>&gt;。</p>
<p><span style="color:#ff0000;">（1）对于myisam存储引擎，打开1张表需要2个文件描述符（一个.MYD文件，一个.MYI文件）。</span></p>
<p><span style="color:#ff0000;">（2）对于innodb存储引擎，开启表的独立表空间（<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-parameters.html#sysvar_innodb_file_per_table">innodb_file_per_table</a>）打开1张表只需要1个文件描述符（一个.ibd文件）。</span></p>
<p>【MySQL Variable】</p>
<p>对于上面的状态值，对应的5.1.3版本后的MySQL变量参数为<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_table_open_cache">table_open_cache</a>，而早期版本为：<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_table_cache">table_cache</a>，该参数值的代表MySQL可以缓存的打开表时候的最大<a href="http://en.wikipedia.org/wiki/File_descriptor">文件描述符</a>。</p>
<p><span id="more-55695"></span></p>
<p><strong>1.2</strong></p>
<p>在MySQL 5.1.3之后，还添加了2个状态值：<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Open_table_definitions">Open_table_definitions</a>和<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Opened_table_definitions">Opened_table_definitions</a>。这2个值代表的意思如下：</p>
<p>Open_table_definitions  ：代表当前缓存了多少.frm文件。</p>
<p>Opened_table_definitions：代表自从MySQL启动后，缓存了.frm文件的数量。</p>
<p>需要注意的是.frm文件是MySQL用于存放表结构的文件，对应myisam和innodb存储引擎都必须有的。</p>
<p>【MySQL Variable】</p>
<p>对于上面的状态值，对应的5.1.3版本后的MySQL变量参数为<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_table_definition_cache">table_definition_cache</a>，该参数值的代表MySQL可以缓存的表定义的数量。<span style="color:#ff0000;">和前面的table cache不同的是，表定义的缓存占用空间很小，而且不需要使用文件描述符，也就是只要打开.frm文件，缓存表定义，然后就可以关闭.frm文件。</span></p>
<p><strong>1.3</strong></p>
<p>另外，还有2个状态值：<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Open_files">Open_files</a>和<a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Opened_files">Opened_files</a>。这2个值的意思同上类似：</p>
<p>Open_files  ：代表当前打开的文件。对应存储引擎（如：innodb）使用存储引擎自己内部函数打开的话，这个值是不会增加的。</p>
<p>Opened_files：代表使用MySQL的my_open()函数打开过的文件数。如果不是使用这个函数打开文件的话，这个值是不会增加的。</p>
<p>【MySQL Variable】</p>
<p>对于上面的状态值，对应的MySQL变量参数为<a href="http://dev.mysql.com/doc/refman/5.1/en/server-system-variables.html#sysvar_open_files_limit">open_files_limit</a>。</p>
<p><strong>二、测试</strong></p>
<p>测试的MySQL版本：</p>
<blockquote><p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">select version();</span></span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p><span style="color:#999999;">| version()  |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p><span style="color:#999999;">|<span style="color:#000000;"> 5.1.47-log</span> |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;+</span></p>
<p><span style="color:#999999;">1 row in set (0.00 sec)</span></p></blockquote>
<p><strong> </strong></p>
<p><strong>2.1 myisam</strong><strong>存储引擎</strong></p>
<p><strong>2.1.1</strong></p>
<p>重启MySQL服务器，可以看到启动时候MySQL已经自动打开相关的系统表。</p>
<blockquote><p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">show global status like &#8216;Open%_table_definitions&#8217;;show global status like &#8216;Open%_tables&#8217;;show global status like &#8216;Open%_files&#8217;;</span><strong> </strong></span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name            | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_table_definitions   | 15    |</span></p>
<p><span style="color:#000000;">| Opened_table_definitions | 15    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_tables   | 8     |</span></p>
<p><span style="color:#000000;">| Opened_tables | 15    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_files    | 19    |</span></p>
<p><span style="color:#000000;">| Opened_files  | 62    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p></blockquote>
<p>在上面的状态输出，可以看到Open_tables的值为8，代表了当前打开了8张表，同样的通过mysqladmin status命令也可以看到：</p>
<blockquote><p><span style="color:#999999;">[mysql@xentest8-vm1 test]$ <span style="color:#000000;">mysqladmin status</span></span></p>
<p><span style="color:#999999;">Uptime: 631  Threads: 2  Questions: 14  Slow queries: 0  Opens: 15  Flush tables: 1  <span style="color:#000000;">Open tables: 8 </span>Queries per second avg: 0.22</span></p></blockquote>
<p>可以通过<a href="http://dev.mysql.com/doc/refman/5.1/en/show-open-tables.html">SHOW OPEN TABLES</a>命令查看，当前的table cache到底打开了哪些表：</p>
<blockquote><p><span style="color:#999999;">mysql&gt;<span style="color:#000000;"> show open tables;</span></span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Database | Table        | In_use | Name_locked |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| mysql    | servers      |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | db           |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | host         |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | columns_priv |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | user         |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | procs_priv   |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | event        |      0 |           0 |</span></p>
<p><span style="color:#999999;">| mysql    | tables_priv  |      0 |           0 |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;"><span style="color:#000000;">8 rows in set</span> (0.00 sec)</span></p></blockquote>
<p>可以看到打开了mysql库里面的8张系统表（都是myisam存储引擎），另外，还可以通过lsof（lsof的用法参见&lt;<a href="http://www.ibm.com/developerworks/cn/aix/library/au-lsof.html">使用lsof查找打开的文件</a>&gt;）查看MySQL打开的对应文件：</p>
<blockquote><p><span style="color:#999999;">[zhuxu@xentest8-vm1 ~]$<span style="color:#000000;"> sudo lsof -u mysql | grep /home/mysql/mysql-5.1.47/data/<span style="color:#ff0000;">mysql</span>/</span></span></p>
<p><span style="color:#000000;">mysqld    14683 mysql   16u   REG              253,1      2048 87889757 /home/mysql/mysql-5.1.47/data/mysql/host.MYI</span></p>
<p><span style="color:#000000;">mysqld    14683 mysql   17u   REG              253,1         0 87889758 /home/mysql/mysql-5.1.47/data/mysql/host.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   18u   REG              253,1      2048 87889760 /home/mysql/mysql-5.1.47/data/mysql/user.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   19u   REG              253,1       380 87889761 /home/mysql/mysql-5.1.47/data/mysql/user.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   20u   REG              253,1      5120 87889754 /home/mysql/mysql-5.1.47/data/mysql/db.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   21u   REG              253,1       880 87889755 /home/mysql/mysql-5.1.47/data/mysql/db.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   22u   REG              253,1      4096 87889772 /home/mysql/mysql-5.1.47/data/mysql/tables_priv.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   23u   REG              253,1         0 87889773 /home/mysql/mysql-5.1.47/data/mysql/tables_priv.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   24u   REG              253,1      4096 87889775 /home/mysql/mysql-5.1.47/data/mysql/columns_priv.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   25u   REG              253,1         0 87889776 /home/mysql/mysql-5.1.47/data/mysql/columns_priv.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   26u   REG              253,1      4096 87889808 /home/mysql/mysql-5.1.47/data/mysql/procs_priv.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   27u   REG              253,1         0 87889809 /home/mysql/mysql-5.1.47/data/mysql/procs_priv.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   28u   REG              253,1      1024 87889769 /home/mysql/mysql-5.1.47/data/mysql/servers.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   29u   REG              253,1         0 87889770 /home/mysql/mysql-5.1.47/data/mysql/servers.MYD</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   30u   REG              253,1      2048 87889817 /home/mysql/mysql-5.1.47/data/mysql/event.MYI</span></p>
<p><span style="color:#999999;">mysqld    14683 mysql   31u   REG              253,1         0 87889818 /home/mysql/mysql-5.1.47/data/mysql/event.MYD</span></p></blockquote>
<p><strong><span style="color:#999999;"> </span></strong></p>
<p><strong>2.1.2</strong></p>
<p>接着打开一张myisam存储引擎的表：</p>
<blockquote><p><span style="color:#999999;">mysql&gt;<span style="color:#000000;"> select count(*) from myisam_1;</span></span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| count(*) |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">|        0 |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">1 row in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">show global status like &#8216;Open%_table_definitions&#8217;;show global status like &#8216;Open%_tables&#8217;;show global status like &#8216;Open%_files&#8217;;</span></span></p>
<p><span style="color:#008000;"># 可以看到Opened_table_definitions/Opened_table_definitions/Open_tables/Opened_tables的值都加1</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name            | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_table_definitions   | 16    |</span></p>
<p><span style="color:#000000;">| Opened_table_definitions | 16    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_tables   | 9     |</span></p>
<p><span style="color:#000000;">| Opened_tables | 16    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#008000;"># 可以看到Open_files的值（19+2）加2，Opened_files的值（62+3）加3</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_files    | 21    |</span></p>
<p><span style="color:#000000;">| Opened_files  | 65    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">show create table myisam_1\G</span></span></p>
<p><span style="color:#999999;">*************************** 1. row ***************************</span></p>
<p><span style="color:#999999;">Table: myisam_1</span></p>
<p><span style="color:#000000;">Create Table: CREATE TABLE `myisam_1` (</span></p>
<p><span style="color:#000000;">`a` int(11) DEFAULT NULL,</span></p>
<p><span style="color:#000000;">KEY `idx_myisam_1` (`a`)</span></p>
<p><span style="color:#000000;">) ENGINE=MyISAM DEFAULT CHARSET=latin1</span></p>
<p><span style="color:#999999;">1 row in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">show open tables from test;</span></span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Database | Table    | In_use | Name_locked |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| test     | myisam_1 |      0 |           0 |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">1 row in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">[zhuxu@xentest8-vm1 ~]$ <span style="color:#000000;">sudo lsof -u mysql | grep /home/mysql/mysql-5.1.47/data/<span style="color:#ff0000;">test</span>/</span></span></p>
<p><span style="color:#000000;">mysqld    14683 mysql  <span style="color:#ff0000;"> 33u</span> REG              253,1      1024 87851510 /home/mysql/mysql-5.1.47/data/test/myisam_1.MYI</span></p>
<p><span style="color:#000000;">mysqld    14683 mysql   <span style="color:#ff0000;">34u</span> REG              253,1         0 87851511 /home/mysql/mysql-5.1.47/data/test/myisam_1.MYD</span></p>
<p><span style="color:#008000;"># 可见，除了需要打开.frm文件（打开，缓存后，就关闭），对于myisam存储引擎，打开1张表需要2个文件描述符（一个.MYD文件，一个.MYI文件）</span></p></blockquote>
<p><strong>2.1.3</strong></p>
<p>对应table cache中的缓存的表，可以通过<a href="http://dev.mysql.com/doc/refman/5.1/en/flush.html">flush tables</a>命令来把它人工移出table cache:</p>
<blockquote><p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">flush tables myisam_1;</span></span></p>
<p><span style="color:#999999;">Query OK, 0 rows affected (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">show open tables from test;</span></span></p>
<p><span style="color:#999999;"><span style="color:#000000;">Empty set</span> (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">mysql&gt; <span style="color:#000000;">show global status like &#8216;Open%_table_definitions&#8217;;show global status like &#8216;Open%_tables&#8217;;show global status like &#8216;Open%_files&#8217;;</span></span></p>
<p><span style="color:#008000;"># 可以看到Open_table_definitions和Open_tables的值都减1</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name            | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_table_definitions   | 15    |</span></p>
<p><span style="color:#999999;">| Opened_table_definitions | 16    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_tables   | 8     |</span></p>
<p><span style="color:#999999;">| Opened_tables | 16    |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#008000;"># 可以看到Open_files的值（21-2）减2</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">| Variable_name | Value |</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_files    | 19    |</span></p>
<p><span style="color:#999999;">| Opened_files  | <span style="color:#000000;">65 </span>|</span></p>
<p><span style="color:#999999;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#999999;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#999999;"><br />
</span></p>
<p><span style="color:#999999;">[zhuxu@xentest8-vm1 ~]$<span style="color:#000000;"> sudo lsof -u mysql | grep /home/mysql/mysql-5.1.47/data/test/ | wc -l</span></span></p>
<p><span style="color:#000000;">0</span></p></blockquote>
<p><strong>2.2 innodb</strong><strong>存储引擎</strong></p>
<p><strong>2.2.1</strong></p>
<p>打开一张innodb存储引擎的表：</p>
<blockquote><p><span style="color:#888888;">mysql&gt;<span style="color:#000000;"> show variables like &#8216;innodb_file_per_table&#8217;;</span></span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">| Variable_name         | Value |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| innodb_file_per_table | ON    |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">1 row in set (0.00 sec)</span></p>
<p><span style="color:#888888;">mysql&gt; <span style="color:#000000;">select count(*) from t0001;</span></span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">| count(*) |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">|        2 |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">1 row in set (0.01 sec)</span></p>
<p><span style="color:#888888;">mysql&gt; <span style="color:#000000;">show global status like &#8216;Open%_table_definitions&#8217;;show global status like &#8216;Open%_tables&#8217;;show global status like &#8216;Open%_files&#8217;;</span></span></p>
<p><span style="color:#008000;"># 可以看到Opened_table_definitions/Opened_table_definitions/Open_tables/Opened_tables的值都加1</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">| Variable_name            | Value |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_table_definitions   | 16    |</span></p>
<p><span style="color:#000000;">| Opened_table_definitions | 17    |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">| Variable_name | Value |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_tables   | 9     |</span></p>
<p><span style="color:#000000;">| Opened_tables | 17    |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#008000;"># 可以看到Open_files的值不变（即innodb使用存储引擎自己内部函数打开的话，这个值是不会增加）</span></p>
<p><span style="color:#008000;"># Opened_files的值（65+1）加1（需要打开.frm文件，缓存后，就关闭）</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">| Variable_name | Value |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| Open_files    | 19    |</span></p>
<p><span style="color:#000000;">| Opened_files  | 66    |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">2 rows in set (0.00 sec)</span></p>
<p><span style="color:#888888;">mysql&gt;<span style="color:#000000;"> show open tables from test;</span></span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">| Database | Table | In_use | Name_locked |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#000000;">| test     | t0001 |      0 |           0 |</span></p>
<p><span style="color:#888888;">+&#8212;&#8212;&#8212;-+&#8212;&#8212;-+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;-+</span></p>
<p><span style="color:#888888;">1 row in set (0.00 sec)</span></p>
<p><span style="color:#888888;">mysql&gt; <span style="color:#000000;">show create table t0001\G</span></span></p>
<p><span style="color:#888888;">*************************** 1. row ***************************</span></p>
<p><span style="color:#888888;">Table: t0001</span></p>
<p><span style="color:#000000;">Create Table: CREATE TABLE `t0001` (</span></p>
<p><span style="color:#000000;">`a` int(11) DEFAULT NULL</span></p>
<p><span style="color:#000000;">) ENGINE=InnoDB DEFAULT CHARSET=latin1</span></p>
<p><span style="color:#888888;">1 row in set (0.00 sec)</span></p>
<p><span style="color:#888888;">[zhuxu@xentest8-vm1 ~]$ <span style="color:#000000;">sudo lsof -u mysql | grep /home/mysql/mysql-5.1.47/data/test/</span></span></p>
<p><span style="color:#000000;">mysqld    14683 mysql   <span style="color:#ff0000;">33uW </span>REG              253,1     98304 87851074 /home/mysql/mysql-5.1.47/data/test/t0001.ibd</span></p></blockquote>
<p><strong>2.2.2</strong></p>
<p>对于innodb存储引擎，table cache对其影响其实不是很大，innodb有自己的数据字典，可以缓存相关表信息。</p>
<p>其他相关资料：</p>
<p>（1）在&lt;<a href="http://oreilly.com/catalog/9780596101718">High.Performance.MySQL</a>&gt;中的The InnoDB Data Dictionary（P280页）中描述：</p>
<p>InnoDB has its own per-table cache, variously called a table definition cache or data dictionary, which you cannot configure. When InnoDB opens a table, it adds a corresponding object to the data dictionary. Each table can take up 4 KB or more of memory (although much less space is required in MySQL 5.1). Tables are not removed from the data dictionary when they are closed.</p>
<p>The main performance issue—besides memory requirements—is opening and computing statistics for the tables, which is expensive because it requires a lot of I/O. In contrast to MyISAM, InnoDB doesn’t store statistics in the tables permanently; it recomputes them each time it starts. This operation is serialized by a global mutex in current versions of MySQL, so it can’t be done in parallel. If you have a lot of tables, your server can take hours to start and fully warm up, during which time it might not be doing much other than waiting for one I/O operation after another. We mention this to make sure you know about it, even though there’s nothing you can do to change it. It’s normally a problem only when you have many (thousands or tens of thousands) large tables, which cause the process to be I/O-bound.</p>
<p>If you use InnoDB’s innodb_file_per_table option (described later in “Configuring the tablespace” on page 291), there’s also a separate limit on the number of .ibd files InnoDB can keep open at any time. This is handled by the InnoDB storage engine, not the MySQL server, and is controlled by innodb_open_files. InnoDB doesn’t open files the same way MyISAM does: whereas MyISAM uses the table cache to hold file descriptors for open tables, in InnoDB there is no direct relationship between open tables and open files. InnoDB uses a single, global file descriptor for each .ibd file. If you can afford it, it’s best to set innodb_open_files large enough that the server can keep all .ibd files open simultaneously.</p>
<p>（2）&lt;<a href="http://www.mysqlperformanceblog.com/2009/11/18/how-innodb_open_files-affects-performance/">How innodb_open_files affects performance</a>&gt;</p>
<p>（3）&lt;<a href="http://www.mysqlperformanceblog.com/2008/12/14/show-open-tables-what-is-in-your-table-cache/">SHOW OPEN TABLES – what is in your table cache</a>&gt;里面有一句描述：</p>
<p>Note however if you’re starting MySQL Command line client without “-A” option it opens all tables in the active database to allow tab completion which can screw results.</p>
<p>在实际的测试中，对于5.0.67-log版本会出现上面的情况，对于5.1版本的测试，上面的现象是不存在。</p>
<p>&#8211;EOF&#8211;</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:幼圆;" lang="EN-US"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:幼圆;" lang="EN-US"> </span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55695/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55695/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55695/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55695/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55695/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55695/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55695/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55695/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55695&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/03/15/mysql_table_cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【Linux】free命令</title>
		<link>http://orzdba.wordpress.com/2011/03/10/free_buffers_cached/</link>
		<comments>http://orzdba.wordpress.com/2011/03/10/free_buffers_cached/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 05:12:42 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[Linux & Unix]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55694</guid>
		<description><![CDATA[记录linux下，free命令的执行结果的意思： [zhuxu@xentest8-vm1 ~]$ free total       used       free     shared    buffers     cached Mem:       4194304    4164812      29492          0     248892    2148968 -/+ buffers/cache:    1766952    2427352 Swap:       524280     343820 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55694&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">记录linux下，free命令的执行结果的意思：</div>
<blockquote>
<div id="_mcePaste"><span style="color:#c0c0c0;">[zhuxu@xentest8-vm1 ~]$</span> <strong>free</strong></div>
<div id="_mcePaste">total       used       free     shared    buffers     cached</div>
<div id="_mcePaste">Mem:       4194304    4164812      29492          0     248892    2148968</div>
<div id="_mcePaste">-/+ buffers/cache:    1766952    2427352</div>
<div id="_mcePaste"><span style="color:#888888;">Swap:       524280     343820     180460</span></div>
</blockquote>
<div id="_mcePaste"><strong>（1）第一行（Mem）中，</strong></div>
<div id="_mcePaste">total：代表总的内存大小。</div>
<div id="_mcePaste">used ：代表已经使用的内存大小，其中buffers和cached的值已经包含在used里面。</div>
<div id="_mcePaste">free ：代表未分配使用的内存大小。</div>
<div id="_mcePaste"><span style="color:#008000;">其中：</span></div>
<div id="_mcePaste"><span style="color:#008000;">total = used + free = 4164812 + 29492 = 4194304</span></div>
<div id="_mcePaste"><span style="color:#008000;">这边的used可以理解为从操作系统层面使用的内存大小（包括缓存）。</span></div>
<div id="_mcePaste"><span id="more-55694"></span></div>
<div>shared ：代表共享的内存大小。</div>
<div id="_mcePaste">buffers：代表Buffer Cache使用内存的大小。</div>
<div id="_mcePaste">cached ：代表Page Cache使用内存的大小。</div>
<div id="_mcePaste"><span style="color:#008000;">其中：</span></div>
<div id="_mcePaste"><span style="color:#008000;">Buffer Cache和Page Cache都是为了操作系统为了提高磁盘读取的速度，进行的缓存，是可以释放使用的内存。</span></div>
<div id="_mcePaste"><span style="color:#008000;">二者区别（来自<a href="http://www.penglixun.com/tech/system/buffer_and_cache_diff.html" target="_blank">http://www.penglixun.com/tech/system/buffer_and_cache_diff.html</a>）：</span></div>
<div id="_mcePaste"><span style="color:#008000;">Buffer Cache以块形式缓冲了块设备的操作，定时或手动的同步到硬盘，它是为了缓冲写操作然后一次性将很多改动写入硬盘，避免频繁写硬盘，提高写入效率。</span></div>
<div id="_mcePaste"><span style="color:#008000;">Page Cache以页面形式缓存了文件系统的文件，给需要使用的程序读取，它是为了给读操作提供缓冲，避免频繁读硬盘，提高读取效率。</span></div>
<div id="_mcePaste"><strong>（2）第二行（-/+ buffers/cache）中，</strong></div>
<div id="_mcePaste">used：即 &#8211; buffers/cache 的内存大小：used（第一行） –buffers –cached = 4164812 &#8211; 248892 &#8211; 2148968 = 1766952</div>
<div id="_mcePaste">free：即 + buffers/cache 的内存大小: free（第一行） + buffers + cached = 29492 + 248892 + 2148968 = 2427352</div>
<div id="_mcePaste"><span style="color:#008000;">其中：</span></div>
<div id="_mcePaste"><span style="color:#008000;">这边的used可以理解为应用程序使用的内存大小（不包括缓存）。</span></div>
<div id="_mcePaste"><span style="color:#008000;">这边的free也就是真正的可以使用的剩余内存大小。</span></div>
<div>另外，可以通过watch命令来动态的显示，下面每0.5s执行一次free命令，并高亮显示变化的地方：</div>
<blockquote>
<div><span style="color:#c0c0c0;"> [zhuxu@dbadb1 ~]$</span> <strong>watch &#8211;help</strong></div>
<div><span style="color:#c0c0c0;">Usage: watch [-dhntv] [--differences[=cumulative]] [--help] [--interval=&lt;n&gt;] [--no-title] [--version] &lt;command&gt;</span></div>
<div><span style="color:#c0c0c0;"> <span style="color:#000000;"> -d, &#8211;differences[=cumulative]        highlight changes between updates</span></span></div>
<div><span style="color:#c0c0c0;"> (cumulative means highlighting is cumulative)</span></div>
<div><span style="color:#c0c0c0;"> -h, &#8211;help                            print a summary of the options</span></div>
<div><span style="color:#c0c0c0;"> <span style="color:#000000;">-n, &#8211;interval=&lt;seconds&gt;              seconds to wait between updates</span></span></div>
<div><span style="color:#c0c0c0;"> -v, &#8211;version                         print the version number</span></div>
<div><span style="color:#c0c0c0;"> -t, &#8211;no-title                        turns off showing the header</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@dbadb1 ~]$ </span></div>
<div><span style="color:#c0c0c0;">[zhuxu@dbadb1 ~]$</span> <strong><span style="color:#ff0000;">watch -d -n 0.5 free</span></strong></div>
</blockquote>
<div>&#8211;</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55694/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55694/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55694/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55694/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55694/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55694/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55694/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55694/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55694&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/03/10/free_buffers_cached/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【Linux】内存段页式管理笔记</title>
		<link>http://orzdba.wordpress.com/2011/03/08/linux_memory/</link>
		<comments>http://orzdba.wordpress.com/2011/03/08/linux_memory/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 11:22:09 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[Linux & Unix]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55688</guid>
		<description><![CDATA[<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55688&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55688&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/03/08/linux_memory/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【MySQL】Innodb的多版本一致性读</title>
		<link>http://orzdba.wordpress.com/2011/03/08/innodb_multi_version/</link>
		<comments>http://orzdba.wordpress.com/2011/03/08/innodb_multi_version/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 17:17:06 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[InnoDB-multi_version]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55683</guid>
		<description><![CDATA[在前面Innodb事务隔离级别中可以看到，在Innodb默认的的重复读（repeatable read）事物隔离级别下，可以实现重复读。和串行读（serializable）事物隔离级别实现重复读不同的是，串行读（serializable）为实现重复读，会在SELECT的时候隐式的对行添加共享读锁。而Innodb在实现过程并不对行加锁，而是通过使用多版本并发控制（MVCC,Multiviersion Concurrency Control）的技术来实现Consistent Nonlocking Reads。 关于Innodb的多版本一致性读的实现机制和Oracle的实现很类似，都是通过rollback segment来实现的，具体如下说明： 1、rollback segment的存放位置 在Innodb的逻辑存储结构中也有表空间tablespace/segment/extent的概念，具体结构层次见下图（图片来自&#60;InnoDB Internals: InnoDB File Formats and Source Code Structure&#62;）。默认情况下，所有的数据都放到共享表空间ibdata1中，如果启用了参数innodb_file_per_table的话，每张表内的数据可以放到自己独立的放到一个表空间中。不管是否开启该参数，rollback segment都是存放在共享表空间中。 2、rollback segment的作用 rollback segment中存放发生改变行的老版本信息，过程如下图（来自Oracle的一张示图）： rollback segment主要用于： （1）当发生事物回滚，需要使用rollback segment中的这些信息用于进行undo操作。 （2）通过这些信息，可以实现对应版本的一致性读，从而实现了在innodb默认隔离级别下的可重复读。 在实现多版本一致性读的时候，Innodb的行会额外增加三个字段DB_TRX_ID/DB_ROLL_PTR/DB_ROW_ID来完成。要查看这3个字段参考之前笔记：《MySQL查看InnoDB存储引擎的表和索引内部结构》。 其他的可以参考： http://www.mysqlsupport.cn/innodb-multi-version/ 另外，看到网上关于Innodb的共享表空间增长迅速，其中主要原因是产生大量的回滚信息: Running Transaction which does a lot of changes. Running Very Long Transaction. Purge Thread Falling Behind. 具体见&#60;Reasons for run-away main Innodb Tablespace&#62;,&#60;Purge [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55683&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>在前面<a href="http://chenxu.yo2.cn/articles/transaction_isolation_levels.html">Innodb事务隔离级别</a>中可以看到，在Innodb默认的的重复读（<a href="http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html#isolevel_repeatable-read" target="_blank">repeatable read</a>）事物隔离级别下，可以实现重复读。和串行读（<a href="http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html#isolevel_serializable">serializable</a>）事物隔离级别实现重复读不同的是，串行读（<a href="http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html#isolevel_serializable">serializable</a>）为实现重复读，会在SELECT的时候隐式的对行添加共享读锁。而Innodb在实现过程并不对行加锁，而是通过使用多版本并发控制（MVCC,<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-multi-versioning.html">Multiviersion</a> Concurrency Control）的技术来实现<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-consistent-read.html">Consistent Nonlocking Reads</a>。</p>
<p>关于Innodb的多版本一致性读的实现机制和Oracle的实现很类似，都是通过<a href="http://dev.mysql.com/doc/innodb-plugin/1.0/en/glossary.html#glos_rollback_segment">rollback segment</a>来实现的，具体如下说明：</p>
<p><strong>1</strong><strong>、rollback segment</strong><strong>的存放位置</strong></p>
<p>在Innodb的逻辑存储结构中也有表空间tablespace/segment/extent的概念，具体结构层次见下图（图片来自&lt;<a href="http://www.innodb.com/wp/wp-content/uploads/2009/05/innodb-file-formats-and-source-code-structure.pdf">InnoDB</a> <a href="http://www.innodb.com/wp/wp-content/uploads/2009/05/innodb-file-formats-and-source-code-structure.pdf">Internals: InnoDB File Formats and Source Code Structure</a>&gt;）。默认情况下，所有的数据都放到<a href="http://dev.mysql.com/doc/innodb-plugin/1.0/en/glossary.html#glos_system_tablespace">共享表空间</a>ibdata1中，如果启用了参数<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-parameters.html#sysvar_innodb_file_per_table">innodb_file_per_table</a>的话，每张表内的数据可以放到自己独立的放到一个<a href="http://dev.mysql.com/doc/innodb-plugin/1.0/en/glossary.html#glos_tablespace">表空间</a>中。不管是否开启该参数，<a href="http://dev.mysql.com/doc/innodb-plugin/1.0/en/glossary.html#glos_rollback_segment">rollback segment</a>都是存放在共享表空间中。</p>
<p><img class="alignnone size-full wp-image-55684" title="innodb_tablespace" src="http://files.blogcn.com/wp01/M00/00/69/wKgKaE11DnkAAAAAAACp8F90ucE664.png" alt="" width="480" height="369" /></p>
<p><span id="more-55683"></span></p>
<p><strong>2</strong><strong>、rollback segment</strong><strong>的作用</strong></p>
<p style="text-align:left;">rollback segment中存放发生改变行的老版本信息，过程如下图（来自Oracle的一张示图）：</p>
<p style="text-align:left;"><img class="alignnone size-full wp-image-55685" title="undo" src="http://files.blogcn.com/wp01/M00/00/69/wKgKaE11D6EAAAAAAAAgWSbthtk638.jpg" alt="" width="264" height="207" /></p>
<p>rollback segment主要用于：</p>
<p>（1）当发生事物回滚，需要使用rollback segment中的这些信息用于进行undo操作。</p>
<p>（2）通过这些信息，可以实现对应版本的一致性读，从而实现了在innodb默认隔离级别下的可重复读。</p>
<p>在实现多版本一致性读的时候，Innodb的行会额外增加三个字段DB_TRX_ID/DB_ROLL_PTR/DB_ROW_ID来完成。要查看这3个字段参考之前笔记：《<a href="http://chenxu.yo2.cn/articles/innodb-table-index-internal.html">MySQL查看InnoDB存储引擎的表和索引内部结构</a>》。</p>
<p>其他的可以参考：</p>
<p><a href="http://dev.mysql.com/doc/innodb-plugin/1.0/en/glossary.html#glos_rollback_segment">http://www.mysqlsupport.cn/innodb-multi-version/</a></p>
<p>另外，看到网上关于Innodb的共享表空间增长迅速，其中主要原因是产生大量的回滚信息:</p>
<p>Running Transaction which does a lot of changes.</p>
<p>Running Very Long Transaction.</p>
<p>Purge Thread Falling Behind.</p>
<p>具体见&lt;<a href="http://www.mysqlperformanceblog.com/2010/06/10/reasons-for-run-away-main-innodb-tablespace/">Reasons for run-away main Innodb Tablespace</a>&gt;,&lt;<a href="http://www.mysqlperformanceblog.com/2010/06/10/purge-thread-spira-of-death/">Purge Thread Spiral of Death</a>&gt;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55683/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55683/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55683/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55683&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/03/08/innodb_multi_version/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>

		<media:content url="http://files.blogcn.com/wp01/M00/00/69/wKgKaE11DnkAAAAAAACp8F90ucE664.png" medium="image">
			<media:title type="html">innodb_tablespace</media:title>
		</media:content>

		<media:content url="http://files.blogcn.com/wp01/M00/00/69/wKgKaE11D6EAAAAAAAAgWSbthtk638.jpg" medium="image">
			<media:title type="html">undo</media:title>
		</media:content>
	</item>
		<item>
		<title>【MySQL】Innodb事务隔离级别</title>
		<link>http://orzdba.wordpress.com/2011/03/03/transaction_isolation_levels/</link>
		<comments>http://orzdba.wordpress.com/2011/03/03/transaction_isolation_levels/#comments</comments>
		<pubDate>Wed, 02 Mar 2011 16:36:02 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Transaction Isolation]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55681</guid>
		<description><![CDATA[一、事务隔离级别 ANSI/ISO SQL标准定义了4中事务隔离级别：未提交读（read uncommitted），提交读（read committed），重复读（repeatable read），串行读（serializable）。 对于不同的事务，采用不同的隔离级别分别有不同的结果。不同的隔离级别有不同的现象。主要有下面3种现在： 1、脏读（dirty read）：一个事务可以读取另一个尚未提交事务的修改数据。 2、非重复读（nonrepeatable read）：在同一个事务中，同一个查询在T1时间读取某一行，在T2时间重新读取这一行时候，这一行的数据已经发生修改，可能被更新了（update），也可能被删除了（delete）。 3、幻像读（phantom read）：在同一事务中，同一查询多次进行时候，由于其他插入操作（insert）的事务提交，导致每次返回不同的结果集。 不同的隔离级别有不同的现象，并有不同的锁定/并发机制，隔离级别越高，数据库的并发性就越差，4种事务隔离级别分别表现的现象如下表： 隔离级别 脏读 非重复读 幻像读 read uncommitted 允许 允许 允许 read committed 允许 允许 repeatable read 允许 serializable 二、数据库中的默认事务隔离级别 在Oracle中默认的事务隔离级别是提交读（read committed）。 对于MySQL的Innodb的默认事务隔离级别是重复读（repeatable read）。可以通过下面的命令查看： mysql&#62; SELECT @@GLOBAL.tx_isolation, @@tx_isolation; +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+ &#124; @@GLOBAL.tx_isolation &#124; @@tx_isolation  &#124; +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+ &#124; REPEATABLE-READ &#124; REPEATABLE-READ &#124; +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+ 1 row [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55681&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>一、事务隔离级别</strong></p>
<p>ANSI/ISO SQL标准定义了4中事务隔离级别：未提交读（read uncommitted），提交读（read committed），重复读（repeatable read），串行读（serializable）。</p>
<p>对于不同的事务，采用不同的隔离级别分别有不同的结果。不同的隔离级别有不同的现象。主要有下面3种现在：</p>
<p>1、脏读（dirty read）：一个事务可以读取另一个尚未提交事务的修改数据。</p>
<p>2、非重复读（nonrepeatable read）：在同一个事务中，同一个查询在T1时间读取某一行，在T2时间重新读取这一行时候，这一行的数据已经发生修改，可能被更新了（update），也可能被删除了（delete）。</p>
<p>3、幻像读（phantom read）：在同一事务中，同一查询多次进行时候，由于其他插入操作（insert）的事务提交，导致每次返回不同的结果集。</p>
<p>不同的隔离级别有不同的现象，并有不同的锁定/并发机制，隔离级别越高，数据库的并发性就越差，4种事务隔离级别分别表现的现象如下表：</p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="148" valign="top"><strong> </strong><strong>隔离级别</strong></td>
<td width="148" valign="top"><strong>脏读</strong></td>
<td width="148" valign="top"><strong>非重复读</strong></td>
<td width="148" valign="top"><strong>幻像读</strong></td>
</tr>
<tr>
<td width="148" valign="top">read   uncommitted</td>
<td width="148" valign="top">允许</td>
<td width="148" valign="top">允许</td>
<td width="148" valign="top">允许</td>
</tr>
<tr>
<td width="148" valign="top">read   committed</td>
<td width="148" valign="top"></td>
<td width="148" valign="top">允许</td>
<td width="148" valign="top">允许</td>
</tr>
<tr>
<td width="148" valign="top">repeatable   read</td>
<td width="148" valign="top"></td>
<td width="148" valign="top"></td>
<td width="148" valign="top">允许</td>
</tr>
<tr>
<td width="148" valign="top">serializable</td>
<td width="148" valign="top"></td>
<td width="148" valign="top"></td>
<td width="148" valign="top"></td>
</tr>
</tbody>
</table>
<p><strong>二、数据库中的默认事务隔离级别</strong></p>
<p>在Oracle中默认的事务隔离级别是提交读（read committed）。</p>
<p>对于<strong>MySQL的Innodb的默认事务隔离级别是<span style="color:#ff0000;">重复读（</span></strong><a href="http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html#isolevel_repeatable-read"><strong><span style="color:#ff0000;">repeatable read</span></strong></a><strong><span style="color:#ff0000;">）</span></strong>。可以通过下面的命令查看：</p>
<blockquote><p><span style="color:#c0c0c0;">mysql&gt;<span style="color:#ff0000;"> SELECT @@GLOBAL.tx_isolation, @@tx_isolation;</span></span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span></p>
<p><span style="color:#c0c0c0;">| @@GLOBAL.tx_isolation | @@tx_isolation  |</span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span></p>
<p><span style="color:#c0c0c0;">| <span style="color:#ff0000;">REPEATABLE-READ</span> | REPEATABLE-READ |</span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+</span></p>
<p><span style="color:#c0c0c0;">1 row in set (0.00 sec)</span></p></blockquote>
<p>下面进行一下测试：</p>
<p><span id="more-55681"></span></p>
<table border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="56" valign="top"><strong>Time</strong></td>
<td width="274" valign="top"><strong>Session 1</strong></td>
<td width="321" valign="top"><strong>Session 2</strong></td>
</tr>
<tr>
<td width="56" valign="top">T1</td>
<td width="274" valign="top">set   autocommit=0;</td>
<td width="321" valign="top">set   autocommit=0;</td>
</tr>
<tr>
<td width="56" valign="top">T2</td>
<td width="274" valign="top">mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |         1 |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>1   row in set (0.00 sec)</td>
<td width="321" valign="top"></td>
</tr>
<tr>
<td width="56" valign="top">T3</td>
<td width="274" valign="top"></td>
<td width="321" valign="top">mysql&gt;   <span style="color:#ff0000;">update tmp_test set version=2 where id=1;</span></p>
<p>Query   OK, 1 row affected (0.02 sec)</p>
<p>Rows   matched: 1  Changed: 1  Warnings: 0</p>
<p>mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |         2 |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>1   row in set (0.00 sec)</td>
</tr>
<tr>
<td width="56" valign="top">T4</td>
<td width="274" valign="top">mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |         <span style="color:#ff0000;">1</span> |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>1   row in set (0.00 sec)</p>
<p><span style="color:#339966;"><strong>【说明】</strong></span><br />
<span style="color:#339966;"><strong>Session   2未提交，看到数据不变，无脏读。</strong></span></td>
<td width="321" valign="top"></td>
</tr>
<tr>
<td width="56" valign="top">T5</td>
<td width="274" valign="top"></td>
<td width="321" valign="top">commit;</td>
</tr>
<tr>
<td width="56" valign="top">T6</td>
<td width="274" valign="top">mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |      <span style="color:#ff0000;"> 1</span> |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>1   row in set (0.00 sec)</p>
<p><strong><span style="color:#339966;">【说明】</span></strong><br />
<strong><span style="color:#339966;">Session   2已经提交，还是看到数据不变，即可以重复读。</span></strong></td>
<td width="321" valign="top"></td>
</tr>
<tr>
<td width="56" valign="top">T7</td>
<td width="274" valign="top">commit;</td>
<td width="321" valign="top"></td>
</tr>
<tr>
<td width="56" valign="top">T8</td>
<td width="274" valign="top">mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |         <span style="color:#ff0000;">2</span> |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>1   row in set (0.00 sec)</p>
<p><strong><span style="color:#339966;">【说明】</span></strong><br />
<strong><span style="color:#339966;">提交事务，看到最新数据。</span></strong></td>
<td width="321" valign="top"></td>
</tr>
<tr>
<td width="56" valign="top">T9</td>
<td width="274" valign="top"></td>
<td width="321" valign="top">mysql&gt;   <span style="color:#ff0000;">insert into tmp_test values(2,1);</span></p>
<p>Query   OK, 1 row affected (0.00 sec)</p>
<p>mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |         2 |<br />
|    2 |         1 |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>2   rows in set (0.00 sec)</p>
<p>mysql&gt;   commit;</p>
<p>Query   OK, 0 rows affected (0.00 sec)</td>
</tr>
<tr>
<td width="56" valign="top">T10</td>
<td width="274" valign="top">mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|      1 |       2 |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>1   row in set (0.00 sec)</p>
<p><strong><span style="color:#339966;">【说明】</span></strong><br />
<strong><span style="color:#339966;">Session   2的insert事务已经提交，看到的数据和T8的时候一样，即未发生幻象读。</span></strong></td>
<td width="321" valign="top"></td>
</tr>
<tr>
<td width="56" valign="top">T11</td>
<td width="274" valign="top">mysql&gt;   commit;</p>
<p>Query   OK, 0 rows affected (0.00 sec)</p>
<p>mysql&gt;   select * from tmp_test;</p>
<p>+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|   id   | version |<br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+<br />
|    1 |         2 |<br />
<span style="color:#ff0000;">|    2 |         1 |</span><br />
+&#8212;&#8212;+&#8212;&#8212;&#8212;+</p>
<p>2   rows in set (0.00 sec)</p>
<p><strong><span style="color:#339966;">【说明】</span></strong><br />
<strong><span style="color:#339966;">事务提交，看到最新数据。</span></strong></td>
<td width="321" valign="top"></td>
</tr>
</tbody>
</table>
<p>上面的结果可以看到Innodb的重复读（<a href="http://dev.mysql.com/doc/refman/5.1/en/set-transaction.html#isolevel_repeatable-read">repeatable read</a>）不允许脏读，不允许非重复读（即可以重复读，Innodb使用<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-consistent-read.html">多版本一致性读</a>来实现）和不允许幻象读（这点和ANSI/ISO SQL标准定义的有所区别）。</p>
<p>另外，同样的测试：</p>
<p>1、当session 2进行truncate表的时候，这个时候session 1再次查询就看不到数据。</p>
<p>2、当session 2进行alter表的时候，这个时候session 1再次查询就看不到数据。</p>
<p>MySQL官方文档中的<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-consistent-read.html">多版本一致性读</a>中说明了原因：<strong>Consistent read does not work over certain DDL statements</strong><strong>。</strong></p>
<p>关于Innodb的<a href="http://dev.mysql.com/doc/refman/5.1/en/innodb-consistent-read.html">多版本一致性读</a>将下一篇再来记录。</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55681/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55681/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55681/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55681/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55681/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55681/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55681/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55681/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55681&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/03/03/transaction_isolation_levels/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【Perl】安装Perl的DBI和DBD:mysql模块</title>
		<link>http://orzdba.wordpress.com/2011/02/24/perl_dbi_dbdmysql/</link>
		<comments>http://orzdba.wordpress.com/2011/02/24/perl_dbi_dbdmysql/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 12:26:20 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[DBI/DBD::mysql]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55678</guid>
		<description><![CDATA[首先，来一段代码来自NinGoo的检测系统中已经按照的Perl模块: [zhuxu@xentest9-vm1 ~]$ cat ./check_module.pl #!/usr/bin/perl use ExtUtils::Installed; my $inst = ExtUtils::Installed-&#62;new(); print join &#8220;\n&#8221;,$inst-&#62;modules(); print &#8220;\n&#8221;; [zhuxu@xentest9-vm1 ~]$ [zhuxu@xentest9-vm1 ~]$ ./check_module.pl Perl 手动安装模块一般步骤只要到CPAN查找下载模块包，然后：[生成makefile] perl Makefile.PL　[建立模块] make　[测试模块] make test　[安装模块] make　install 。 下面进行Perl的DBI和DBD:mysql模块的安装,模块安装在自定义目录下： (一) DBI模块的安装 1、下载安装DBI模块 DBI模块的具体下载地址：http://search.cpan.org/~timb/DBI-1.616/，具体如下： [zhuxu@xentest9-vm1 ~]$ wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.616.tar.gz &#8211;22:11:19&#8211;  http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.616.tar.gz Resolving search.cpan.org&#8230; 212.117.177.118, 194.106.223.155 Connecting to search.cpan.org&#124;212.117.177.118&#124;:80&#8230; connected. HTTP request sent, awaiting response&#8230; 302 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55678&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>首先，来一段代码来自<a href="http://www.ningoo.net/html/2008/install_perl_dbi_module.html" target="_blank">NinGoo</a>的检测系统中已经按照的Perl模块:</p>
<blockquote>
<div id="_mcePaste"><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ cat ./check_module.pl</span></div>
<div id="_mcePaste">#!/usr/bin/perl</div>
<div id="_mcePaste">use ExtUtils::Installed;</div>
<div id="_mcePaste">my $inst = ExtUtils::Installed-&gt;new();</div>
<div id="_mcePaste">print join &#8220;\n&#8221;,$inst-&gt;modules();</div>
<div id="_mcePaste">print &#8220;\n&#8221;;</div>
<div id="_mcePaste"><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ ./check_module.pl</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Perl</span></div>
</blockquote>
<div>手动安装模块一般步骤只要到<a href="http://search.cpan.org/" target="_blank">CPAN</a>查找下载模块包，然后：[生成makefile] perl Makefile.PL　[建立模块] make　[测试模块] make test　[安装模块] make　install 。</div>
<div>下面进行Perl的DBI和DBD:mysql模块的安装,模块安装在自定义目录下：</div>
<div><span id="more-55678"></span></div>
<div><strong>(一) DBI模块的安装</strong></div>
<div>1、下载安装DBI模块</div>
<div>DBI模块的具体下载地址：<a href="http://search.cpan.org/~timb/DBI-1.616/">http://search.cpan.org/~timb/DBI-1.616/</a>，具体如下：</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$</span> <strong>wget http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.616.tar.gz</strong></div>
<div><span style="color:#c0c0c0;">&#8211;22:11:19&#8211;  http://search.cpan.org/CPAN/authors/id/T/TI/TIMB/DBI-1.616.tar.gz</span></div>
<div><span style="color:#c0c0c0;">Resolving search.cpan.org&#8230; 212.117.177.118, 194.106.223.155</span></div>
<div><span style="color:#c0c0c0;">Connecting to search.cpan.org|212.117.177.118|:80&#8230; connected.</span></div>
<div><span style="color:#c0c0c0;">HTTP request sent, awaiting response&#8230; 302 Found</span></div>
<div><span style="color:#c0c0c0;">Location: http://ftp.belnet.be/mirror/ftp.cpan.org/authors/id/T/TI/TIMB/DBI-1.616.tar.gz [following]</span></div>
<div><span style="color:#c0c0c0;">&#8211;22:11:35&#8211;  http://ftp.belnet.be/mirror/ftp.cpan.org/authors/id/T/TI/TIMB/DBI-1.616.tar.gz</span></div>
<div><span style="color:#c0c0c0;">Resolving ftp.belnet.be&#8230; 193.190.67.15, 2001:6a8:a40::21</span></div>
<div><span style="color:#c0c0c0;">Connecting to ftp.belnet.be|193.190.67.15|:80&#8230; connected.</span></div>
<div><span style="color:#c0c0c0;">HTTP request sent, awaiting response&#8230; 200 OK</span></div>
<div><span style="color:#c0c0c0;">Length: 576803 (563K) [application/x-gzip]</span></div>
<div><span style="color:#c0c0c0;">Saving to: `DBI-1.616.tar.gz&#8217;</span></div>
<div><span style="color:#c0c0c0;">100%[===============================&gt;] 576,803     20.1K/s   in 28s</span></div>
<div><span style="color:#c0c0c0;">22:12:06 (19.8 KB/s) &#8211; `DBI-1.616.tar.gz&#8217; saved [576803/576803]</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$</span> <strong>mkdir -p My_Perl_Module/DBI</strong></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span><strong>tar -zxvf DBI-1.616.tar.gz</strong></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$</span><strong> cd DBI-1.616</strong></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI-1.616]$</span><strong><span style="color:#c0c0c0;"> </span>perl Makefile.PL<span style="color:#ff0000;"> PREFIX=&#8221;/home/zhuxu/My_Perl_Module/DBI&#8221;</span></strong></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI-1.616]$</span> <strong>make</strong></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI-1.616]$</span> <strong>make test</strong></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI-1.616]$</span> <strong>make install</strong></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBI/dbi_sql.h</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBI/DBI.so</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBI/dbivport.h</span></div>
<div><span style="color:#c0c0c0;">&#8230;</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBI/bin/dbiproxy</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBI/bin/dbiprof</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBI/bin/dbilogstrip</span></div>
<div><span style="color:#c0c0c0;">Writing /home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBI/.packlist</span></div>
<div><span style="color:#c0c0c0;">Appending installation info to /home/zhuxu/My_Perl_Module/DBI/lib64/perl5/5.8.8/x86_64-linux-thread-multi/perllocal.pod</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI-1.616]$</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI]$ <strong><span style="color:#000000;">cd ~/My_Perl_Module/DBI/</span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBI]$<strong><span style="color:#000000;"> ll</span></strong></span></div>
<div><span style="color:#c0c0c0;">total 24</span></div>
<div>
<div><span style="color:#c0c0c0;">drwxr-xr-x 6 zhuxu users 4096 Feb 22 22:23 .</span></div>
<div><span style="color:#c0c0c0;">drwxr-xr-x 3 zhuxu users 4096 Feb 22 22:19 ..</span></div>
<div><span style="color:#c0c0c0;">drwxr-xr-x 2 zhuxu users 4096 Feb 22 22:23 bin</span></div>
<div><span style="color:#c0c0c0;">drwxr-xr-x 3 zhuxu users 4096 Feb 22 22:23 lib</span></div>
<div><span style="color:#c0c0c0;">drwxr-xr-x 3 zhuxu users 4096 Feb 22 22:23 lib64</span></div>
<div><span style="color:#c0c0c0;">drwxr-xr-x 3 zhuxu users 4096 Feb 22 22:23 share</span></div>
</div>
</blockquote>
<div>2、测试使用DBI</div>
</div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ cat 1.pl </span></div>
<div>
<div><span style="color:#000000;">#!/usr/bin/perl -w</span></div>
<div><span style="color:#000000;">use strict;</span></div>
<div><span style="color:#000000;"><strong>use DBI;</strong></span></div>
<div><span style="color:#888888;"><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$</span><strong> <span style="color:#000000;">./1.pl </span></strong></span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t locate DBI.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .) at ./1.pl line 3.</span></div>
<div><span style="color:#c0c0c0;">BEGIN failed&#8211;compilation aborted at ./1.pl line 3.</span></div>
<div>
<div><span style="color:#888888;"><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$</span> <strong><span style="color:#000000;">./check_module.pl </span></strong></span></div>
</div>
</div>
<div><span style="color:#c0c0c0;">Perl</span></div>
</blockquote>
<div>由于不是安装在默认的路径，这样Perl在默认的模块和库的按照路径下找不到。查看Perl查找的默认路径有那些可以通过命令：<strong><span style="color:#ff0000;">perl -e &#8216;print join &#8220;\n&#8221;,@INC&#8217; </span></strong>进行查看。</div>
<div>所以，若要使用安装在非标准目录中的模块，必须使用命令<strong><span style="color:#ff0000;">use lib</span></strong>。这时Perl在搜索它自己的目录之前，首先搜索该目录，找出它要的模块。另外，不能将模块在不同操作系统的计算机之间移动。经过编译的模块只能在一种类型的操作系统上运行。</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<strong><span style="color:#000000;"> find /home/zhuxu/My_Perl_Module/ -name &#8220;*pm&#8221; | grep -i DBI.pm</span></strong></span></div>
<div><span style="color:#c0c0c0;">/home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/Bundle/DBI.pm</span></div>
<div><span style="color:#c0c0c0;">/home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBI.pm</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span></div>
<div><span style="color:#000000;">&#8211;这样就能执行成功</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ cat 1.pl </span></div>
<div><span style="color:#000000;">#!/usr/bin/perl -w</span></div>
<div><span style="color:#000000;">use strict;</span></div>
<div><span style="color:#000000;"><strong><span style="color:#ff0000;">use lib &#8220;/home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi&#8221;;</span></strong></span></div>
<div><span style="color:#000000;"><strong><span style="color:#ff0000;">require DBI;</span></strong></span></div>
<div><span style="color:#000000;">print &#8220;Hello World!\n&#8221;;</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong>./1.pl </strong></span></div>
<div><span style="color:#000000;">Hello World!</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span></div>
</blockquote>
</div>
<div>上面说明DBI模块可以正常使用。</div>
<div><strong>（二）安装Perl DBD::mysql驱动</strong></div>
<div>1、下载安装DBD::mysql模块</div>
<div>Perl连接数据库需要通过DBI模块和相应数据库的DBD驱动，这些可以从CPAN下载相应DBD驱动。我想要连接MySQL,就还需要安装Perl DBD::mysql驱动，下载地址：<a href="http://search.cpan.org/~capttofu/DBD-mysql-4.018/" target="_blank">http://search.cpan.org/~capttofu/DBD-mysql-4.018/</a>，具体如下:</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong><span style="color:#000000;">wget http://search.cpan.org/CPAN/authors/id/C/CA/CAPTTOFU/DBD-mysql-4.018.tar.gz</span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<strong><span style="color:#000000;"> mkdir -p My_Perl_Module/DBD_MySQL</span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <span style="color:#000000;"><strong>tar -zxvf DBD-mysql-4.018.tar.gz</strong></span></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong><span style="color:#000000;">cd DBD-mysql-4.018</span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$ <strong><span style="color:#000000;">perl Makefile.PL PREFIX=&#8221;/home/zhuxu/My_Perl_Module/DBD_MySQL&#8221;</span></strong></span></div>
<div><span style="color:#c0c0c0;"><span style="color:#ff0000;">Can&#8217;t locate DBI/DBD.pm in @INC</span> (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.8/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.7/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.6/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi /usr/lib/perl5/5.8.8 .)<span style="color:#ff0000;"> at Makefile.PL line 24.</span></span></div>
</blockquote>
<div>这时候报错，Makefile.pl在24行报错，说找不到DBI/DBD.pm模块，这个模块在我们上面自己按照的DBI目录下，需要把路径指定一下。只要在Makefile.pl的第24行添加下面的use lib语句：</div>
</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;"> 23 use File::Basename;</span></div>
<div><strong> 24 use lib &#8220;/home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi&#8221;;</strong></div>
<div><span style="color:#c0c0c0;"> 25 require DBI::DBD;</span></div>
</blockquote>
</div>
<div>再执行一次：</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$<span style="color:#000000;"> </span><strong><span style="color:#000000;">perl Makefile.PL PREFIX=&#8221;/home/zhuxu/My_Perl_Module/DBD_MySQL&#8221;</span></strong></span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t exec &#8220;mysql_config&#8221;: No such file or directory at Makefile.PL line 83.</span></div>
<div><span style="color:#c0c0c0;"><br />
</span></div>
<div><span style="color:#c0c0c0;"><span style="color:#ff0000;">Cannot find the file &#8216;mysql_config&#8217;! </span>Your execution PATH doesn&#8217;t seem </span></div>
<div><span style="color:#c0c0c0;">not contain the path to mysql_config. Resorting to guessed values!</span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t exec &#8220;mysql_config&#8221;: No such file or directory at Makefile.PL line 465.</span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t find mysql_config. Use &#8211;mysql_config option to specify where mysql_config is located</span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t exec &#8220;mysql_config&#8221;: No such file or directory at Makefile.PL line 465.</span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t find mysql_config. Use &#8211;mysql_config option to specify where mysql_config is located</span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t exec &#8220;mysql_config&#8221;: No such file or directory at Makefile.PL line 465.</span></div>
<div><span style="color:#c0c0c0;">Can&#8217;t find mysql_config. Use &#8211;mysql_config option to specify where mysql_config is located</span></div>
<div><span style="color:#c0c0c0;">&#8230;.</span></div>
</blockquote>
</div>
<div>这个时候可以执行了，不过报新错误。安装DBD::mysql需要配置<strong><span style="color:#ff0000;">mysql_config</span></strong>，参考<a href="http://www.anysql.net/mysql/compile-static-dbd-mysql.html" target="_blank">编译x86_64下静态DBD::MySQL模块</a>，安装<a href="http://chenxu.yo2.cn/articles/install-mysql-source-distribution.html" target="_blank">MYSQL</a>。安装完成后，查找MYSQL安装目录下的mysql_config</div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <span style="color:#000000;">sudo ls -l /home/mysql/mysql-5.1.47/bin/mysql_config</span></span></div>
<div><span style="color:#c0c0c0;">-rwxr-xr-x 1 mysql mysql 6188 Feb 23 20:48 /home/mysql/mysql-5.1.47/bin/mysql_config</span></div>
</blockquote>
<div>接下来就可以进行正常的安装了：</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$ <strong><span style="color:#ff0000;"><span style="color:#000000;">sudo perl Makefile.PL</span> PREFIX=&#8221;/home/zhuxu/My_Perl_Module/DBD_MySQL&#8221; \</span></strong></span></div>
<div><span style="color:#c0c0c0;"><strong><span style="color:#ff0000;">&gt; &#8211;mysql_config=/home/mysql/mysql-5.1.47/bin/mysql_config</span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$ <span style="color:#000000;"><strong>sudo make</strong></span></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$<strong> <span style="color:#000000;">sudo make test</span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$<span style="color:#000000;"> </span><strong><span style="color:#000000;">sudo make install</span></strong></span></div>
<div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/mysql.bs</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/mysql.so</span></div>
<div><span style="color:#c0c0c0;">Files found in blib/arch: installing files in blib/lib into architecture dependent library tree</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/Bundle/DBD/mysql.pm</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/mysql.pm</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/mysql/INSTALL.pod</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/mysql/GetInfo.pm</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/share/man/man3/Bundle::DBD::mysql.3pm</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/share/man/man3/DBD::mysql::INSTALL.3pm</span></div>
<div><span style="color:#c0c0c0;">Installing /home/zhuxu/My_Perl_Module/DBD_MySQL/share/man/man3/DBD::mysql.3pm</span></div>
<div><span style="color:#c0c0c0;">Writing /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/.packlist</span></div>
<div><span style="color:#c0c0c0;">Appending installation info to /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/5.8.8/x86_64-linux-thread-multi/perllocal.pod</span></div>
</div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 DBD-mysql-4.018]$ </span></div>
</blockquote>
</div>
<div>在安装目录下查询模块：</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong><span style="color:#000000;">find /home/zhuxu/My_Perl_Module/DBD_MySQL -name &#8220;*.pm&#8221; | grep DBD</span></strong></span></div>
<div><span style="color:#c0c0c0;">/home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/Bundle/DBD/mysql.pm</span></div>
<div><span style="color:#c0c0c0;">/home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/mysql.pm</span></div>
<div><span style="color:#c0c0c0;">/home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/DBD/mysql/GetInfo.pm</span></div>
</blockquote>
</div>
<div>2、测试使用DBD::mysql</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ cat 2.pl </span></div>
<div>#!/usr/bin/perl -w</div>
<div>use strict;</div>
<div><span style="color:#ff0000;">use lib &#8220;/home/zhuxu/My_Perl_Module/DBI/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi&#8221;;</span></div>
<div><span style="color:#ff0000;">require DBI;</span></div>
<div><span style="color:#ff0000;">use lib &#8220;/home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi&#8221;;</span></div>
<div>require DBD::mysql;     <span style="color:#008000;"># 这句其实写不写都可以. DBI在需要使用DBD::mysql时候会自己搜索使用.</span></div>
<div>my $user = &#8220;root&#8221;;</div>
<div>my $passwd;</div>
<div>my $host = &#8220;10.254.5.151&#8243;;</div>
<div>my $db = &#8220;test&#8221;;</div>
<div>my $data_source = &#8220;DBI:mysql:database=$db;host=$host&#8221;;</div>
<div>my $dbh = DBI-&gt;connect($data_source,$user,$passwd,{&#8216;RaiseError&#8217;=&gt;1}) ;</div>
<div>my $sql = qq{select now()};</div>
<div>my $sth = $dbh-&gt;prepare($sql);</div>
<div>$sth-&gt;execute();</div>
<div>my $set;</div>
<div>$sth-&gt;bind_columns(undef,\$set);</div>
<div>while($sth-&gt;fetch()){</div>
<div>print $set.&#8221;\n&#8221;;</div>
<div>}</div>
<div>$sth-&gt;finish();</div>
<div>$dbh-&gt;disconnect();</div>
<div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong><span style="color:#000000;">./2.pl </span></strong></span></div>
<div><span style="color:#c0c0c0;"><strong>Can&#8217;t load &#8216;<span style="color:#ff0000;">/home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/mysql.so</span>&#8216; for module DBD::mysql: libmysqlclient.so.16: cannot open shared object file: No such file or directory at /usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/DynaLoader.pm line 230.</strong></span></div>
<div><span style="color:#c0c0c0;"> at ./2.pl line 6</span></div>
</div>
<div><span style="color:#c0c0c0;"><br />
</span></div>
<div><span style="color:#c0c0c0;">Compilation failed in require at ./2.pl line 6.</span></div>
</blockquote>
</div>
<div>执行编译错误，说找不到对应的libmysqlclient.so.16，使用ldd命令查看修复：</div>
<div>
<blockquote>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<span style="color:#ff0000;"> </span><strong><span style="color:#000000;"><span style="color:#ff0000;">ldd</span> /home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/mysql.so</span></strong></span></div>
<div><span style="color:#c0c0c0;"> <span style="color:#ff0000;">libmysqlclient.so.16 =&gt; not found</span></span></div>
<div><span style="color:#c0c0c0;"> libz.so.1 =&gt; /usr/lib64/libz.so.1 (0x00002ad042acc000)</span></div>
<div><span style="color:#c0c0c0;"> libcrypt.so.1 =&gt; /lib64/libcrypt.so.1 (0x00002ad042ce0000)</span></div>
<div><span style="color:#c0c0c0;"> libnsl.so.1 =&gt; /lib64/libnsl.so.1 (0x00002ad042f19000)</span></div>
<div><span style="color:#c0c0c0;"> libm.so.6 =&gt; /lib64/libm.so.6 (0x00002ad043131000)</span></div>
<div><span style="color:#c0c0c0;"> libc.so.6 =&gt; /lib64/libc.so.6 (0x00002ad0433b4000)</span></div>
<div><span style="color:#c0c0c0;"> /lib64/ld-linux-x86-64.so.2 (0x00000039df600000)</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong><span style="color:#000000;">sudo ls -l /home/mysql/mysql-5.1.47/lib/mysql/libmysqlclient.so.16</span></strong></span></div>
<div><span style="color:#000000;">lrwxrwxrwx 1 mysql mysql 24 Feb 23 20:48 /home/mysql/mysql-5.1.47/lib/mysql/libmysqlclient.so.16 -&gt; libmysqlclient.so.16.0.0</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<strong><span style="color:#000000;"> ll /usr/lib64/libmysqlclient.so.16 </span></strong></span></div>
<div><span style="color:#ff0000;">ls: libmysqlclient.so.16: No such file or directory</span></div>
<div><span style="color:#000000;"><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ <strong><span style="color:#ff0000;">sudo cp /home/mysql/mysql-5.1.47/lib/mysql/libmysqlclient.so.16 /usr/lib64/libmysqlclient.so.16 </span></strong></span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<span style="color:#000000;"> </span><strong><span style="color:#000000;">ll /usr/lib64/libmysqlclient.so.16 </span></strong></span></div>
<div><span style="color:#000000;">-rwxr-xr-x 1 root root 1498934 Feb 23 21:48 /usr/lib64/libmysqlclient.so.16</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<strong><span style="color:#000000;"> <span style="color:#ff0000;">ldd </span>/home/zhuxu/My_Perl_Module/DBD_MySQL/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi/auto/DBD/mysql/mysql.so</span></strong></span></div>
<div><span style="color:#c0c0c0;"> <span style="color:#ff0000;">libmysqlclient.so.16 =&gt; /usr/lib64/libmysqlclient.so.16 (0x00002ae9549a2000)</span></span></div>
<div><span style="color:#c0c0c0;"> libz.so.1 =&gt; /usr/lib64/libz.so.1 (0x00002ae954c42000)</span></div>
<div><span style="color:#c0c0c0;"> libcrypt.so.1 =&gt; /lib64/libcrypt.so.1 (0x00002ae954e56000)</span></div>
<div><span style="color:#c0c0c0;"> libnsl.so.1 =&gt; /lib64/libnsl.so.1 (0x00002ae95508f000)</span></div>
<div><span style="color:#c0c0c0;"> libm.so.6 =&gt; /lib64/libm.so.6 (0x00002ae9552a7000)</span></div>
<div><span style="color:#c0c0c0;"> libc.so.6 =&gt; /lib64/libc.so.6 (0x00002ae95552a000)</span></div>
<div><span style="color:#c0c0c0;"> /lib64/ld-linux-x86-64.so.2 (0x00000039df600000)</span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$ </span></div>
<div><span style="color:#c0c0c0;">[zhuxu@xentest9-vm1 ~]$<strong><span style="color:#000000;"> ./2.pl </span></strong></span></div>
<div><span style="color:#000000;">2011-02-23 21:52:52</span></div>
</blockquote>
</div>
<div>终于可以执行了。</div>
<div>为啥我安装模块这么折腾，这么费劲，真蛋疼。=_=~</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55678/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55678/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55678/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55678&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2011/02/24/perl_dbi_dbdmysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【Shell】awk列汇总/正则匹配变量</title>
		<link>http://orzdba.wordpress.com/2010/11/28/shell_awk_if/</link>
		<comments>http://orzdba.wordpress.com/2010/11/28/shell_awk_if/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 09:07:16 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[Linux & Unix]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[shell-if]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55676</guid>
		<description><![CDATA[记录一下，本周工作中用到的Shell处理脚本： eg1：AWK匹配列汇总： &#8211; (1) [zhuxu@dbadb1 tmp]$ cat temp.log &#124; grep &#8220;Query OK&#8221; &#124;head Query OK, 21401 rows affected (10.00 sec) Query OK, 15560 rows affected (1.40 sec) Query OK, 23588 rows affected (14.07 sec) Query OK, 15249 rows affected (1.80 sec) Query OK, 22519 rows affected (13.02 sec) Query OK, 15535 rows affected (1.51 sec) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55676&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>记录一下，本周工作中用到的Shell处理脚本：</p>
<div id="_mcePaste"><strong>eg1：AWK匹配列汇总：</strong></div>
<blockquote>
<div id="_mcePaste">&#8211; (1)</div>
<div id="_mcePaste">[zhuxu@dbadb1 tmp]$ <strong>cat temp.log | grep &#8220;Query OK&#8221; |head</strong></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 21401 rows affected (10.00 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 15560 rows affected (1.40 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 23588 rows affected (14.07 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 15249 rows affected (1.80 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 22519 rows affected (13.02 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 15535 rows affected (1.51 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 23665 rows affected (12.40 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 15676 rows affected (1.57 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 24608 rows affected (12.65 sec)</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Query OK, 15159 rows affected (1.85 sec)</span></div>
<div id="_mcePaste">&#8211; (2)</div>
<div id="_mcePaste">[zhuxu@dbadb1 tmp]$ <strong>cat temp.log | grep &#8220;Query OK&#8221; |<span style="color:#ff0000;"> awk -F&#8221;[ (]&#8221; &#8216;BEGIN{rows=0;times=0} {rows+=$3;times+=$7} END{print &#8220;\nTotal rows: &#8220;rows,&#8221;\nTotal Times(min): &#8220;times/60&#8243;\n&#8221;}&#8217;</span></strong></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Total rows: 9990253</span></div>
<div id="_mcePaste"><span style="color:#c0c0c0;">Total Times(min): 76.8903</span></div>
<div id="_mcePaste">[zhuxu@dbadb1 tmp]$</div>
</blockquote>
<div id="_mcePaste"><strong>eg2：AWK列分组GRUP BY汇总SUM：</strong></div>
<div>
<blockquote>
<div>$<strong>head zx.log </strong></div>
<div><span style="color:#888888;">2011-01-19      10</span></div>
<div><span style="color:#888888;">2011-01-19      10</span></div>
<div><span style="color:#888888;">2011-01-19      100</span></div>
<div><span style="color:#888888;">2011-01-19      1042</span></div>
<div><span style="color:#888888;">2011-01-19      10422</span></div>
<div><span style="color:#888888;">2011-01-19      1059</span></div>
<div><span style="color:#888888;">2011-01-19      1059</span></div>
<div><span style="color:#888888;">2011-01-19      10887</span></div>
<div><span style="color:#888888;">2011-01-19      1140</span></div>
<div><span style="color:#888888;">2011-01-19      11724</span></div>
<div>$<strong><span style="color:#ff0000;">awk &#8216;{name[$1]+=$2;sum+=$2} END {for(i in name) print i,name[i];print &#8220;=====&gt; Total:&#8221;,sum,&#8221;&lt;=====&#8221;;}&#8217; zx.log </span>| sort -k1 -M</strong></div>
<div><span style="color:#888888;">2011-01-19 496582</span></div>
<div><span style="color:#888888;">2011-01-20 1039690</span></div>
<div><span style="color:#888888;">2011-01-21 364867</span></div>
<div><span style="color:#888888;">2011-01-22 1200747</span></div>
<div><span style="color:#888888;">2011-01-23 112981</span></div>
<div><span style="color:#888888;">2011-01-24 465766</span></div>
<div><span style="color:#888888;">2011-01-25 1081170</span></div>
<div><span style="color:#888888;">2011-01-26 1111863</span></div>
<div><span style="color:#888888;">2011-01-27 335430</span></div>
<div><span style="color:#888888;">2011-01-28 450190</span></div>
<div><span style="color:#888888;">2011-01-29 120003</span></div>
<div><span style="color:#888888;">2011-01-30 99402</span></div>
<div><span style="color:#888888;">2011-01-31 262238</span></div>
<div><span style="color:#888888;">2011-02-01 240861</span></div>
<div><span style="color:#888888;">2011-02-02 181618</span></div>
<div><span style="color:#888888;">2011-02-03 145767</span></div>
<div><span style="color:#888888;">2011-02-04 133216</span></div>
<div><span style="color:#888888;">2011-02-05 100355</span></div>
<div><span style="color:#888888;">2011-02-06 99856</span></div>
<div><span style="color:#888888;">2011-02-07 138638</span></div>
<div><span style="color:#888888;">2011-02-08 142822</span></div>
<div><span style="color:#888888;">2011-02-09 511951</span></div>
<div><span style="color:#888888;">2011-02-10 496962</span></div>
<div><span style="color:#888888;">2011-02-11 669015</span></div>
<div><span style="color:#888888;">2011-02-12 1750215</span></div>
<div><span style="color:#888888;">2011-02-13 629162</span></div>
<div><span style="color:#888888;">=====&gt; Total: 12381367 &lt;=====</span></div>
</blockquote>
</div>
<div><strong>eg3：脚本正则匹配变量：</strong></div>
<div id="_mcePaste">
<blockquote>
<div id="_mcePaste">#!/usr/local/bin/bash</div>
<div id="_mcePaste"># Func : Match</div>
<div id="_mcePaste">v_host=abcdef123.cm3.cm4</div>
<div id="_mcePaste"><strong><span style="color:#ff0000;">if [[ $v_host =~ '\.cm3$' ]] ;</span> </strong> <strong><span style="color:#008000;">#查看变量是否以&#8221;.cm3&#8243;字符串结束。</span></strong></div>
<div id="_mcePaste">then</div>
<div id="_mcePaste">echo &#8216;This is cm3!&#8217;;</div>
<div id="_mcePaste">elif [[ $v_host =~ '\.cm4$' ]] ;</div>
<div id="_mcePaste">then</div>
<div id="_mcePaste">echo &#8216;This is cm4!&#8217;;</div>
<div id="_mcePaste">else</div>
<div id="_mcePaste">echo &#8216;Error! Wrong Host&#8217;;</div>
<div id="_mcePaste">fi</div>
</blockquote>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55676/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55676/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55676/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55676&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2010/11/28/shell_awk_if/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
		<item>
		<title>【MySQL】INSERT &#8230; ON DUPLICATE KEY UPDATE/REPLACE</title>
		<link>http://orzdba.wordpress.com/2010/11/28/insert-on-duplicate-key-updatereplace/</link>
		<comments>http://orzdba.wordpress.com/2010/11/28/insert-on-duplicate-key-updatereplace/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 08:48:12 +0000</pubDate>
		<dc:creator>orzdba</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[INSERT ... ON DUPLICATE KEY UPDATE/REPLACE]]></category>

		<guid isPermaLink="false">http://chenxu.yo2.cn/?p=55673</guid>
		<description><![CDATA[INSERT &#8230; ON DUPLICATE KEY UPDATE/REPLACE都可以处理插入一条记录碰到主键或唯一性约束冲突的情况，不过2者的实现方式不同： 1、INSERT &#8230; ON DUPLICATE KEY UPDATE方式遇到冲突会update一下。 2、REPLACE方式遇到冲突会把原来的记录delete，然后再insert进去。 具体见下面的测试： &#8211; (1)表结构和数据 mysql&#62;show create table test\G; *************************** 1. row *************************** Table: test Create Table: CREATE TABLE `test` ( `pk_col` int(11) NOT NULL AUTO_INCREMENT, `uk_col` int(11) DEFAULT NULL, `col` varchar(10) DEFAULT NULL, PRIMARY KEY (`pk_col`), UNIQUE KEY `uk_test` (`uk_col`) ) ENGINE=InnoDB DEFAULT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55673&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html" target="_blank">INSERT &#8230; ON DUPLICATE KEY UPDATE</a>/<a href="http://dev.mysql.com/doc/refman/5.1/en/replace.html" target="_blank">REPLACE</a>都可以处理插入一条记录碰到主键或唯一性约束冲突的情况，不过2者的实现方式不同：<br />
1、INSERT &#8230; ON DUPLICATE KEY UPDATE方式遇到冲突会update一下。<br />
2、REPLACE方式遇到冲突会把原来的记录delete，然后再insert进去。</p>
<p>具体见下面的测试：</p>
<blockquote><p><strong><span style="color:#008000;">&#8211; (1)</span></strong><strong><span style="color:#008000;">表结构和数据</span></strong></p>
<p>mysql&gt;<strong>show create table test\G;</strong></p>
<p><span style="color:#c0c0c0;">*************************** 1. row ***************************</span></p>
<p><span style="color:#c0c0c0;">Table: test</span></p>
<p><span style="color:#c0c0c0;">Create Table: CREATE TABLE `test` (</span></p>
<p><span style="color:#c0c0c0;">`pk_col` int(11) NOT NULL</span><span style="color:#ff0000;"><strong><span style="color:#c0c0c0;"> </span></strong><strong>AUTO_INCREMENT</strong></span><span style="color:#c0c0c0;">,</span></p>
<p><span style="color:#c0c0c0;">`uk_col` int(11) DEFAULT NULL,</span></p>
<p><span style="color:#c0c0c0;">`col` varchar(10) DEFAULT NULL,</span></p>
<p><strong><span style="color:#ff0000;">PRIMARY KEY (`pk_col`),</span></strong></p>
<p><strong><span style="color:#ff0000;">UNIQUE KEY `uk_test` (`uk_col`)</span></strong></p>
<p><span style="color:#c0c0c0;">) ENGINE=InnoDB DEFAULT CHARSET=gbk</span></p>
<p><span style="color:#c0c0c0;">1 row in set (0.00 sec)</span></p>
<p>mysql&gt;<strong>select * from test;</strong></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">| pk_col | uk_col | col  |</span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">|      1 |      1 | a    |</span></p>
<p><span style="color:#c0c0c0;">|      2 |      2 | b    |</span></p>
<p><strong>|      3 |      3 | c    |</strong></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">3 rows in set (0.00 sec)</span></p>
<p><strong><span style="color:#008000;">&#8211; (2)</span></strong><strong><span style="color:#008000;">插入重复值</span></strong></p>
<p>mysql&gt;<strong>insert into test(uk_col,col) values(3,&#8217;d');</strong></p>
<p><span style="color:#c0c0c0;">ERROR 1062 (23000): <span style="color:#ff0000;"><em>Duplicate entry &#8217;3&#8242; for key &#8216;uk_test&#8217;</em></span></span></p>
<p><strong><span style="color:#008000;">&#8211; (3)on duplicate key update</span></strong><strong><span style="color:#008000;">方式插入，这时候记录的自增主键值不变，其他字段更新。</span></strong></p>
<p>mysql&gt;<strong>insert into test(uk_col,col) values(3,&#8217;d') <span style="color:#ff0000;">on duplicate key update </span></strong><strong>col=&#8217;d';</strong></p>
<p><span style="color:#c0c0c0;">Query OK, 2 rows affected (0.00 sec)</span></p>
<p>mysql&gt;<strong>select * from test;</strong></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">| pk_col | uk_col | col  |</span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">|      1 |      1 | a    |</span></p>
<p><span style="color:#c0c0c0;">|      2 |      2 | b    |</span></p>
<p><strong>|     <span style="color:#ff0000;"> 3</span> |      3 | <span style="color:#ff0000;">d </span></strong><strong> | </strong></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">3 rows in set (0.00 sec)</span></p>
<p><strong><span style="color:#008000;">&#8211; (4)replace</span></strong><strong><span style="color:#008000;">方式插入，这时候记录的自增主键值改变，原记录被删除。pk_col</span></strong><strong><span style="color:#008000;">变成6</span></strong><strong><span style="color:#008000;">不是4</span></strong><strong><span style="color:#008000;">，是由于上次2</span></strong><strong><span style="color:#008000;">次insert</span></strong><strong><span style="color:#008000;">语句，导致auto_crement</span></strong><strong><span style="color:#008000;">增加。</span></strong></p>
<p>mysql&gt;<strong><span style="color:#ff0000;">replace</span></strong><strong> test(uk_col,col) values(3,&#8217;d');</strong></p>
<p><span style="color:#c0c0c0;">Query OK, 2 rows affected (0.00 sec)</span></p>
<p><span style="color:#c0c0c0;">mysql&gt;select * from test;</span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">| pk_col | uk_col | col  |</span></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p>
<p><span style="color:#c0c0c0;">|      1 |      1 | a    |</span></p>
<p><span style="color:#c0c0c0;">|      2 |      2 | b    |</span></p>
<p><strong>|     <span style="color:#ff0000;"> 6</span></strong><strong> |      3 | <span style="color:#ff0000;">d</span></strong><strong> | </strong></p>
<p><span style="color:#c0c0c0;">+&#8212;&#8212;&#8211;+&#8212;&#8212;&#8211;+&#8212;&#8212;+</span></p></blockquote>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/orzdba.wordpress.com/55673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/orzdba.wordpress.com/55673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/orzdba.wordpress.com/55673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/orzdba.wordpress.com/55673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/orzdba.wordpress.com/55673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/orzdba.wordpress.com/55673/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/orzdba.wordpress.com/55673/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/orzdba.wordpress.com/55673/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=orzdba.wordpress.com&amp;blog=12958461&amp;post=55673&amp;subd=orzdba&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://orzdba.wordpress.com/2010/11/28/insert-on-duplicate-key-updatereplace/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d0417e0935d0284c0680d600a0a8426e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">orzdba</media:title>
		</media:content>
	</item>
	</channel>
</rss>
