<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
	<channel>
		<title>meisw's blog</title>
		<link>http://meisw.wdlinux.cn//</link>
		<description>工作,学习,生活,这里将会有一些记录.     备用域名:http://meisw.wdlinux.cn</description>
		<copyright>Copyright (C) 2004 Security Angel Team [S4T] All Rights Reserved.</copyright>
		<generator>SaBlog-X Version 1.6 Build 20080806</generator>
		<lastBuildDate>Sat, 30 May 2026 00:56:09 +0000</lastBuildDate>
		<ttl>30</ttl>
		<item>
			<guid>http://meisw.wdlinux.cn//show-1077-1.html</guid>
			<title>mysql优化</title>
			<author>admin</author>
			<description><![CDATA[<div>查看mysql库大小，表大小，索引大小</div>
<p>&nbsp;https://www.cnblogs.com/xzlive/p/9884674.html</p>
<div><br />
<div>
<h2 class="header" style="box-sizing: inherit; font-family: &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Noto Sans CJK SC&quot;, Sathu, EucrosiaUPC, Arial, Helvetica, sans-serif; line-height: 1.28571em; margin: 0px 0px 1rem; padding: 0px; font-size: 1.71429rem; word-break: break-word; overflow-wrap: break-word; color: rgba(0, 0, 0, 0.87);">在内存中建立 MySQL 的临时目录</h2>
</div>
<div>https://www.oschina.net/question/12_79459</div>
<div>&nbsp;</div>
<div>MySQL高负载解决方案</div>
<div>https://www.cnblogs.com/pengai/articles/9190979.html</div>
</div>]]></description>
			<link>http://meisw.wdlinux.cn//show-1077-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2020-02-21 23:59</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-1041-1.html</guid>
			<title>MySQL 事务没有提交导致 锁等待 Lock wait timeout exceeded</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;</p>
<h3 style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 22px; color: rgb(79, 79, 79); line-height: 30px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">MySQL 5.5 -- innodb_lock_wait 锁 等待</h3>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><span style="box-sizing: border-box; outline: 0px; word-break: break-all;">记得以前，当出现：ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction，<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
要解决是一件麻烦的事情 ；<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
特别是当一个SQL执行完了，但未COMMIT，后面的SQL想要执行就是被锁，超时结束；<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
DBA光从数据库无法着手找出源头是哪个SQL锁住了；<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
有时候看看show engine innodb status , 并结合 show full processlist; 能暂时解决问题；但一直不能精确定位；</span></p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><span style="box-sizing: border-box; outline: 0px; word-break: break-all;">在5.5中，information_schema 库中增加了三个关于锁的表（MEMORY引擎）；<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
innodb_trx ## 当前运行的所有事务<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
innodb_locks ## 当前出现的锁<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
innodb_lock_waits ## 锁等待的对应关系</span></p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><span style="box-sizing: border-box; outline: 0px; word-break: break-all;">看到这个就非常激动 ； 这可是解决了一个大麻烦，先来看一下表结构</span></p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; line-height: 26px; text-align: justify; word-break: break-all;"><font color="#4f4f4f" face="-apple-system, SF UI Text, Arial, PingFang SC, Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei, SimSun"><span style="font-size: 16px;">https://blog.csdn.net/mangmang2012/article/details/9207007</span></font></p>]]></description>
			<link>http://meisw.wdlinux.cn//show-1041-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2018-07-04 00:36</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-1040-1.html</guid>
			<title>mysql中set autocommit=0与start transaction区别</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;<span style="color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">set&nbsp;autocommit=0指事务非自动提交，自此句执行以后，每个SQL语句或者语句块所在的事务都需要显示&quot;commit&quot;才能提交事务。</span></p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">&nbsp;</p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">&nbsp;</p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">1、不管autocommit 是1还是0&nbsp;<br />
&nbsp; &nbsp; &nbsp;START TRANSACTION 后，只有当commit数据才会生效，ROLLBACK后就会回滚。</p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">&nbsp;</p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">2、当autocommit 为 0 时<br />
&nbsp; &nbsp; 不管有没有START TRANSACTION。<br />
&nbsp; &nbsp; 只有当commit数据才会生效，ROLLBACK后就会回滚。</p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">&nbsp;</p>
<p style="line-height: 1.5; margin: 10px auto; color: rgb(75, 75, 75); font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;">3、如果autocommit 为1 ，并且没有START TRANSACTION 。<br />
&nbsp; &nbsp; 调用ROLLBACK是没有用的。即便设置了SAVEPOINT。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-1040-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2018-07-04 00:35</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-1027-1.html</guid>
			<title>分布式缓存--序列3--原子操作与CAS乐观锁</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;</p>
<h1 id="问题的提出" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 28px; color: rgb(79, 79, 79); line-height: 36px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">问题的提出</h1>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">我们知道，在单机的&ldquo;线程模型&ldquo;中，2个线程并发修改一个变量，是需要加锁的。这个在Java并发编程&ndash;序列1已经讲过，要么是悲观锁，要么是乐观锁。&nbsp;<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
<img src="https://img-blog.csdn.net/20161126004440560" alt="这里写图片描述" title="" style="box-sizing: border-box; outline: 0px; margin: 24px 0px; max-width: 100%; word-break: break-all; cursor: zoom-in;" /></p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">如果把单机的线程模型，改成有客户端/服务器的进程模型。服务器可以是Mysql/Redis/Memcached任何一种，那该问题又如何解决呢？</p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><img src="https://img-blog.csdn.net/20161126004620299" alt="这里写图片描述" title="" style="box-sizing: border-box; outline: 0px; margin: 24px 0px; max-width: 100%; word-break: break-all; cursor: zoom-in;" /></p>
<h1 id="方案1-单条命令的原子性" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 28px; color: rgb(79, 79, 79); line-height: 36px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">方案1 &ndash; 单条命令的原子性</h1>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">Mysql: 用类似update table set x = x + 1 where &hellip; 这样的单条语句就可解决上述问题，因为服务器内部会处理加锁的问题，不用客户端解决。</p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">Memcached: incr/decr命令</p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">Redis: incr/decr命令</p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">一句话：对于这种简单的整数加减的原子操作，只要是1条命令可以搞定，就不需要客户端解决互斥问题。</p>
<h1 id="方案2-memcachedredis的乐观锁" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 28px; color: rgb(79, 79, 79); line-height: 36px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><a name="t2" style="box-sizing: border-box; outline: 0px; color: rgb(78, 161, 219); cursor: pointer; word-break: break-all;"></a>方案2 &ndash; Memcached/Redis的乐观锁</h1>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">上面的方案1，必须是单条命令，但该方法有很大局限性。很多时候，如果我们需要执行复杂的计算逻辑，要先把数据get出来，执行复杂逻辑，再set回去。类似下面这种：</p>
<pre class="prettyprint" name="code" style="box-sizing: border-box; outline: 0px; padding: 8px 16px 4px 56px; margin-top: 0px; margin-bottom: 24px; position: relative; overflow-y: hidden; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; word-break: break-all; background-color: rgb(246, 248, 250); border: none;"><code class="hljs cs has-numbering" style="box-sizing: border-box; outline: 0px; display: block; padding: 0px; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; border-radius: 4px; overflow-x: auto; word-wrap: normal; word-break: break-all;"><span class="hljs-comment" style="box-sizing: border-box; outline: 0px; color: rgb(136, 0, 0); word-break: break-all;">//客户端1</span> x = <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(0, 0, 136); word-break: break-all;">get</span>(key) 对x执行复杂逻辑 <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(0, 0, 136); word-break: break-all;">set</span>(key,x)  <span class="hljs-comment" style="box-sizing: border-box; outline: 0px; color: rgb(136, 0, 0); word-break: break-all;">//客户端2</span> x = <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(0, 0, 136); word-break: break-all;">get</span>(key) 对x执行复杂逻辑 <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(0, 0, 136); word-break: break-all;">set</span>(key,x)</code><ul class="pre-numbering" style="box-sizing: border-box; outline: 0px; padding-top: 8px; padding-bottom: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style: none; word-break: break-all; position: absolute; width: 48px; background-color: rgb(238, 240, 244); top: 0px; left: 0px; text-align: right;"><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">1</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">2</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">3</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">4</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">5</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">6</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">7</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">8</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">9</li></ul></pre>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">此时有2条指令，没有办法保证2条语句的原子性，这个时候如何解决呢？</p>
<h2 id="mysql的乐观锁" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 24px; color: rgb(79, 79, 79); line-height: 32px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><a name="t3" style="box-sizing: border-box; outline: 0px; color: rgb(78, 161, 219); cursor: pointer; word-break: break-all;"></a>Mysql的乐观锁</h2>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">关于Mysql解决上述问题的乐观锁方案，此处不再详述，参见Java并发编程-序列1</p>
<h2 id="memcached乐观锁" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 24px; color: rgb(79, 79, 79); line-height: 32px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><a name="t4" style="box-sizing: border-box; outline: 0px; color: rgb(78, 161, 219); cursor: pointer; word-break: break-all;"></a>Memcached乐观锁</h2>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">Memcached提供了2个命令 gets + cas。gets取出数据的时候，同时返回版本号；修改之后, cas回去的时候，会比较该版本号和服务器上最新的版本号。如果不等，则cas失败。</p>
<h2 id="redis乐观锁" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 24px; color: rgb(79, 79, 79); line-height: 32px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><a name="t5" style="box-sizing: border-box; outline: 0px; color: rgb(78, 161, 219); cursor: pointer; word-break: break-all;"></a>Redis乐观锁</h2>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">Redis提供了watch命令，如下所示：</p>
<pre class="prettyprint" name="code" style="box-sizing: border-box; outline: 0px; padding: 8px 16px 4px 56px; margin-top: 0px; margin-bottom: 24px; position: relative; overflow-y: hidden; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; word-break: break-all; background-color: rgb(246, 248, 250); border: none;"><code class="hljs cs has-numbering" style="box-sizing: border-box; outline: 0px; display: block; padding: 0px; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; border-radius: 4px; overflow-x: auto; word-wrap: normal; word-break: break-all;">    watch  key1   <span class="hljs-comment" style="box-sizing: border-box; outline: 0px; color: rgb(136, 0, 0); word-break: break-all;">//修改数据之前，执行watch，意思监听此key</span>      multi     <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(0, 0, 136); word-break: break-all;">set</span> key1  foo  <span class="hljs-comment" style="box-sizing: border-box; outline: 0px; color: rgb(136, 0, 0); word-break: break-all;">//如果别的客户端在此期间修改了该key1，此处更新将失败</span>     exec  </code><ul class="pre-numbering" style="box-sizing: border-box; outline: 0px; padding-top: 8px; padding-bottom: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style: none; word-break: break-all; position: absolute; width: 48px; background-color: rgb(238, 240, 244); top: 0px; left: 0px; text-align: right;"><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">1</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">2</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">3</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">4</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">5</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">6</li></ul></pre>
<h1 id="方案3-redis事务" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 28px; color: rgb(79, 79, 79); line-height: 36px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><a name="t6" style="box-sizing: border-box; outline: 0px; color: rgb(78, 161, 219); cursor: pointer; word-break: break-all;"></a>方案3 &ndash; Redis事务</h1>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">Redis也提供了事务的概念，但它不能回滚。如果1条命令执行错误，会继续执行下面的。</p>
<pre class="prettyprint" name="code" style="box-sizing: border-box; outline: 0px; padding: 8px 16px 4px 56px; margin-top: 0px; margin-bottom: 24px; position: relative; overflow-y: hidden; overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; word-break: break-all; background-color: rgb(246, 248, 250); border: none;"><code class="hljs r has-numbering" style="box-sizing: border-box; outline: 0px; display: block; padding: 0px; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 22px; border-radius: 4px; overflow-x: auto; word-wrap: normal; word-break: break-all;">      multi        get foo        <span class="hljs-keyword" style="box-sizing: border-box; outline: 0px; color: rgb(0, 0, 136); word-break: break-all;">...</span>        incr foo        exec</code><ul class="pre-numbering" style="box-sizing: border-box; outline: 0px; padding-top: 8px; padding-bottom: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; list-style: none; word-break: break-all; position: absolute; width: 48px; background-color: rgb(238, 240, 244); top: 0px; left: 0px; text-align: right;"><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">1</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">2</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">3</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">4</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">5</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">6</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">7</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">8</li><li style="box-sizing: border-box; outline: 0px; padding: 0px 8px; margin: 0px; list-style: none; word-break: break-all; color: rgb(153, 153, 153);">9</li></ul></pre>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">此处的multi/extc，类似Mysql中的beganTransaction/endTransaction。&nbsp;<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
另外，由于redis是单线程的，因此事务里面的多条语句执行时，不会被打断。</p>
<h1 id="memcached的多线程-vs-redis单线程" style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 8px 0px 16px; font-size: 28px; color: rgb(79, 79, 79); line-height: 36px; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;"><a name="t7" style="box-sizing: border-box; outline: 0px; color: rgb(78, 161, 219); cursor: pointer; word-break: break-all;"></a>Memcached的多线程 vs. Redis单线程</h1>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">我们都知道, Memcached内部是多线程的，而Redis是单线程的。多线程好理解，但Redis为什么要搞成单线程呢？&nbsp;<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
个人认为，有以下几个原因：&nbsp;<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
（1）redis有各种复杂的数据结构list, has, set。也就是说，对于一个(key, value)，value的类型可以是list, hash, set。在实际应用场景中，很容易出现多个客户端对同一个key的这个复杂的value数据结构进行并发操作，如果是多线程，势必要引入锁，而锁却是性能杀手。&nbsp;<br style="box-sizing: border-box; outline: 0px; word-break: break-all;" />
相比较而言，memcached只有简单的get/set/add操作，没有复杂数据结构，在互斥这个问题上，没有redis那么严重。</p>
<p style="box-sizing: border-box; outline: 0px; margin-bottom: 16px; font-size: 16px; color: rgb(79, 79, 79); line-height: 26px; text-align: justify; word-break: break-all; font-family: -apple-system, &quot;SF UI Text&quot;, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, &quot;WenQuanYi Micro Hei&quot;, sans-serif, SimHei, SimSun;">（2）对于纯内存操作来说，cpu并不是瓶颈，瓶颈在网络IO上。所以即使单线程，也很快。另外，如果要利用多核的优势，可以在一个机器上开多个redis实例。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-1027-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2018-06-14 09:24</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-1026-1.html</guid>
			<title>乐观锁与悲观锁及应用举例</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">最近因为在工作中需要，学习了乐观锁与悲观锁的相关知识，这里我通过这篇文章，把我自己对这两个&ldquo;锁家&rdquo;兄弟理解记录下来;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="box-sizing: border-box; outline: 0px; font-weight: 700; word-break: break-all;">- 悲观锁</span>：正如其名，它指的是对数据被外界（包括本系统当前的其他事务，以及来自外部系统的事务处理）的修改持保守态度，因此，在整个数据处理过程中，将数据处于锁定状态。悲观锁的实现，往往依靠数据库提供的锁机制（也只有数据库层提供的锁机制才能真正保证数据访问的排他性，否则，即使在本系统中实现了加锁机制，也无法保证外部系统不会修改数据）。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;以常用的mysql InnoDB存储引擎为例：加入商品表items表中有一个字段status，status=1表示该商品未被下单，status=2表示该商品已经被下单，那么我们对每个商品下单前必须确保此商品的status=1。假设有一件商品，其id为10000；如果不使用锁，那么操作方法如下：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//查出商品状态</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;select status from items where id=10000;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//根据商品信息生成订单</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;insert into orders(id,item_id) values(null,10000);</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//修改商品状态为2</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;update Items set status=2 where id=10000;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;上述场景在高并发环境下可能出现问题：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;前面已经提到只有商品的status=1是才能对它进行下单操作，上面第一步操作中，查询出来的商品status为1。但是当我们执行第三步update操作的时候，有可能出现其他人先一步对商品下单把Item的status修改为2了，但是我们并不知道数据已经被修改了，这样就可能造成同一个商品被下单2次，使得数据不一致。所以说这种方式是不安全的。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;使用悲观锁来实现：在上面的场景中，商品信息从查询出来到修改，中间有一个处理订单的过程，使用悲观锁的原理就是，当我们在查询出items信息后就把当前的数据锁定，直到我们修改完毕后再解锁。那么在这个过程中，因为items被锁定了，就不会出现有第三者来对其进行修改了。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp; 注：要使用悲观锁，我们必须关闭mysql数据库的自动提交属性，因为MySQL默认使用autocommit模式，也就是说，当你执行一个更新操作后，MySQL会立刻将结果进行提交。我们可以使用命令设置MySQL为非autocommit模式：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;set autocommit=0;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;设置完autocommit后，我们就可以执行我们的正常业务了。具体如下：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//开始事务</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;begin;/begin work;/start transaction; (三者选一就可以)</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//查询出商品信息</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;select status from items where id=10000 for update;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//根据商品信息生成订单</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;insert into orders (id,item_id) values (null,10000);</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//修改商品status为2</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;update items set status=2 where id=10000;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//提交事务</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;commit;/commit work;</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;注：上面的begin/commit为事务的开始和结束，因为在前一步我们关闭了mysql的autocommit，所以需要手动控制事务的提交，在这里就不细表了。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;上面的第一步我们执行了一次查询操作：select status from items where id=10000 for update;与普通查询不一样的是，我们使用了select&hellip;for update的方式，这样就通过数据库实现了悲观锁。此时在items表中，id为10000的 那条数据就被我们锁定了，其它的事务必须等本次事务提交之后才能执行。这样我们可以保证当前的数据不会被其它事务修改。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;注：需要注意的是，在事务中，只有SELECT ... FOR UPDATE 或LOCK IN SHARE MODE 同一笔数据时会等待其它事务结束后才执行，一般SELECT ... 则不受此影响。拿上面的实例来说，当我执行select status from items where id=10000 for update;后。我在另外的事务中如果再次执行select status from items where id=10000 for update;则第二个事务会一直等待第一个事务的提交，此时第二个查询处于阻塞的状态，但是如果我是在第二个事务中执行select status from items where id=10000;则能正常查询出数据，不会受第一个事务的影响。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;上面我们提到，使用select&hellip;for update会把数据给锁住，不过我们需要注意一些锁的级别，MySQL InnoDB默认Row-Level Lock，所以只有明确地指定主键，MySQL 才会执行Row lock (只锁住被选取的数据) ，否则MySQL 将会执行Table Lock (将整个数据表单给锁住)。除了主键外，使用索引也会影响数据库的锁定级别。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;悲观锁并不是适用于任何场景，它也有它存在的一些不足，因为悲观锁大多数情况下依靠数据库的锁机制实现，以保证操作最大程度的独占性。如果加锁的时间过长，其他用户长时间无法访问，影响了程序的并发访问性，同时这样对数据库性能开销影响也很大，特别是对长事务而言，这样的开销往往无法承受。所以与悲观锁相对的，我们有了乐观锁，乐观锁的概念如下：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="box-sizing: border-box; outline: 0px; font-weight: 700; word-break: break-all;">- 乐观锁</span>（ Optimistic Locking ） 相对悲观锁而言，乐观锁假设认为数据一般情况下不会造成冲突，所以在数据进行提交更新的时候，才会正式对数据的冲突与否进行检测，如果发现冲突了，则让返回用户错误的信息，让用户决定如何去做。那么我们如何实现乐观锁呢，一般来说有以下2种方式：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;1.使用数据版本（Version）记录机制实现，这是乐观锁最常用的一种实现方式。何谓数据版本？即为数据增加一个版本标识，一般是通过为数据库表增加一个数字类型的 &ldquo;version&rdquo; 字段来实现。当读取数据时，将version字段的值一同读出，数据每更新一次，对此version值+1。当我们提交更新的时候，判断数据库表对应记录的当前版本信息与第一次取出来的version值进行比对，如果数据库表当前版本号与第一次取出来的version值相等，则予以更新，否则认为是过期数据。用下面的一张图来说明：</span></p>
<div style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, SimHei, Arial, SimSun; text-align: center;"><a href="http://img1.tbcdn.cn/L1/461/1/aba656681527f77a6c28ab596e8f1938bacb043c" target="_blank" style="box-sizing: border-box; outline: 0px; color: rgb(66, 139, 202); text-decoration-line: none; word-break: break-all; font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;"><img src="http://img1.tbcdn.cn/L1/461/1/aba656681527f77a6c28ab596e8f1938bacb043c" alt="1" title="1" style="box-sizing: border-box; outline: 0px; margin: 0px 0px 24px; max-width: 100%; word-break: break-all; cursor: zoom-in; border: 0px; vertical-align: middle;" /></a></div>
<p><span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;如上图所示，如果更新操作顺序执行，则数据的版本（version）依次递增，不会产生冲突。但是如果发生有不同的业务操作对同一版本的数据进行修改，那么，先提交的操作（图中B）会把数据version更新为2，当A在B之后提交更新时发现数据的version已经被修改了，那么A的更新操作会失败。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;2.乐观锁定的第二种实现方式和第一种差不多，同样是在需要乐观锁控制的table中增加一个字段，名称无所谓，字段类型使用时间戳（timestamp）, 和上面的version类似，也是在更新提交的时候检查当前数据库中数据的时间戳和自己更新前取到的时间戳进行对比，如果一致则OK，否则就是版本冲突。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;以mysql InnoDB存储引擎为例，还是拿之前的例子商品表items表中有一个字段status，status=1表示该商品未被下单，status=2表示该商品已经被下单，那么我们对每个商品下单前必须确保此商品的status=1。假设有一件商品，其id为10000；</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;下单操作包括3步骤：</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//查询出商品信息</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;select (status,version) from items where id=#{id}</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//根据商品信息生成订单</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;//修改商品status为2</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;update items set status=2,version=version+1 where id=#{id} and version=#{version};</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;为了使用乐观锁，我们需要首先修改items表，增加一个version字段，数据默认version可设为1；</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;其实我们周围的很多产品都有乐观锁的使用，比如我们经常使用的分布式存储引擎XXX，XXX中存储的每个数据都有版本号，版本号在每次更新后都会递增，相应的，在XXX put接口中也有此version参数，这个参数是为了解决并发更新同一个数据而设置的，这其实就是乐观锁；</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;很多情况下，更新数据是先get，修改get回来的数据，然后put回系统。如果有多个客户端get到同一份数据，都对其修改并保存，那么先保存的修改就会被后到达的修改覆盖，从而导致数据一致性问题,在大部分情况下应用能够接受，但在少量特殊情况下，这个是我们不希望发生的。</span><br style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;" />
<span style="box-sizing: border-box; outline: 0px; word-break: break-all; color: rgb(51, 51, 51); font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft Yahei&quot;, sans-serif; font-size: 16px; letter-spacing: 0.36px; line-height: 27.2px;">&nbsp; &nbsp; &nbsp; &nbsp;比如系统中有一个值&rdquo;1&rdquo;, 现在A和B客户端同时都取到了这个值。之后A和B客户端都想改动这个值，假设A要改成12，B要改成13，如果不加控制的话，无论A和B谁先更新成功，它的更新都会被后到的更新覆盖。XXX引入的乐观锁机制避免了这样的问题。刚刚的例子中，假设A和B同时取到数据，当时版本号是10，A先更新，更新成功后，值为12，版本为11。当B更新的时候，由于其基于的版本号是10，此时服务器会拒绝更新，返回version error，从而避免A的更新被覆盖。B可以选择get新版本的value，然后在其基础上修改，也可以选择强行更新。</span></p>]]></description>
			<link>http://meisw.wdlinux.cn//show-1026-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2018-06-14 09:20</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-1006-1.html</guid>
			<title>mysql处理高并发，防止库存超卖</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;<span style="color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial;">今天王总又给我们上了一课，其实mysql处理高并发，防止库存超卖的问题，在去年的时候，王总已经提过；但是很可惜，即使当时大家都听懂了，但是在现实开发中，还是没这方面的意识。今天就我的一些理解，整理一下这个问题，并希望以后这样的课程能多点。</span></p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">先来就库存超卖的问题作描述：一般电子商务网站都会遇到如团购、秒杀、特价之类的活动，而这样的活动有一个共同的特点就是访问量激增、上千甚至上万人抢购一个商品。然而，作为活动商品，库存肯定是很有限的，如何控制库存不让出现超买，以防止造成不必要的损失是众多电子商务网站程序员头疼的问题，这同时也是最基本的问题。</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">从技术方面剖析，很多人肯定会想到事务，但是事务是控制库存超卖的必要条件，但不是充分必要条件。</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">举例：</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">总库存：4个商品</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">请求人：a、1个商品 b、2个商品 c、3个商品</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">程序如下：</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">beginTranse(开启事务)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">try{</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp; $result = $dbca-&gt;query('select amount from s_store where postID = 12345');</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp; if(result-&gt;amount &gt; 0){</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//quantity为请求减掉的库存数量</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$dbca-&gt;query('update s_store set amount = amount -&nbsp;quantity&nbsp;where postID = 12345');</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">}catch($e Exception){</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;rollBack(回滚)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">}</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">commit(提交事务)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">以上代码就是我们平时控制库存写的代码了，大多数人都会这么写，看似问题不大，其实隐藏着巨大的漏洞。数据库的访问其实就是对磁盘文件的访问，数据库中的表其实就是保存在磁盘上的一个个文件，甚至一个文件包含了多张表。例如由于高并发，当前有三个用户a、b、c三个用户进入到了这个事务中，这个时候会产生一个共享锁，所以在select的时候，这三个用户查到的库存数量都是4个，同时还要注意，mysql innodb查到的结果是有版本控制的，再其他用户更新没有commit之前(也就是没有产生新版本之前)，当前用户查到的结果依然是就版本；</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">然后是update，假如这三个用户同时到达update这里，这个时候update更新语句会把并发串行化，也就是给同时到达这里的是三个用户排个序，一个一个执行，并生成排他锁，在当前这个update语句commit之前，其他用户等待执行，commit后，生成新的版本；这样执行完后，库存肯定为负数了。但是根据以上描述，我们修改一下代码就不会出现超买现象了，代码如下：</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">beginTranse(开启事务)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">try{</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;//quantity为请求减掉的库存数量<br style="box-sizing: border-box;" />
&nbsp;&nbsp;&nbsp; $dbca-&gt;query('update s_store set amount = amount -&nbsp;quantity&nbsp;where postID = 12345');</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp; $result = $dbca-&gt;query('select amount from s_store where postID = 12345');</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp; if(result-&gt;amount &lt; 0){</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw new Exception('库存不足');</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">}catch($e Exception){</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;rollBack(回滚)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">}</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">commit(提交事务)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">另外，更简洁的方法：</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">beginTranse(开启事务)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">try{</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;//quantity为请求减掉的库存数量<br style="box-sizing: border-box;" />
&nbsp;&nbsp;&nbsp; $dbca-&gt;query('update s_store set amount = amount -&nbsp;quantity&nbsp;where&nbsp;<span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 21px;"><strong style="box-sizing: border-box;">amount&gt;=</strong><span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 21px;"><strong style="box-sizing: border-box;">quantity and</strong></span></span><strong style="box-sizing: border-box;">&nbsp;</strong>postID = 12345');</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">}catch($e Exception){</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;&nbsp;&nbsp;&nbsp;rollBack(回滚)</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">}</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">commit(提交事务)</p>
<p>&nbsp;</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">=====================================================================================</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;"><span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">1、在秒杀的情况下，肯定不能如此高频率的去读写数据库，会严重造成性能问题的</span><br style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;" />
<span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">必须使用缓存，将需要秒杀的商品放入缓存中，并使用锁来处理其并发情况。当接到用户秒杀提交订单的情况下，先将商品数量递减（加锁/解锁）后再进行其他方面的处理，处理失败在将数据递增1（加锁/解锁），否则表示交易成功。</span><br style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;" />
<span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">当商品数量递减到0时，表示商品秒杀完毕，拒绝其他用户的请求。</span></p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">&nbsp;</p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;">2、<span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">这个肯定不能直接操作数据库的，会挂的。直接读库写库对数据库压力太大，要用缓存。</span><br style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;" />
<span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">把你要卖出的商品比如10个商品放到缓存中；然后在memcache里设置一个计数器来记录请求数，这个请求书你可以以你要秒杀卖出的商品数为基数，比如你想卖出10个商品，只允许100个请求进来。那当计数器达到100的时候，后面进来的就显示秒杀结束，这样可以减轻你的服务器的压力。然后根据这100个请求，先付款的先得后付款的提示商品以秒杀完。</span></p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;"><span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;"><br style="box-sizing: border-box;" />
</span></p>
<p style="box-sizing: border-box; margin-bottom: 0px; word-wrap: break-word; word-break: normal; color: rgb(69, 69, 69); font-family: tahoma, helvetica, arial; line-height: 21px;"><span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">3、</span><span style="box-sizing: border-box; margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px; line-height: 24.7px;">首先，多用户并发修改同一条记录时，肯定是后提交的用户将覆盖掉前者提交的结果了。</span></p>
<p style="box-sizing: border-box; margin: 10px auto; word-wrap: break-word; word-break: normal; list-style: none; border: none; line-height: 24.7px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px;">这个直接可以使用加锁机制去解决，乐观锁或者悲观锁。<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">乐观锁</strong>，就是在数据库设计一个版本号的字段，每次修改都使其+1，这样在提交时比对提交前的版本号就知道是不是并发提交了，但是有个缺点就是只能是应用中控制，如果有跨应用修改同一条数据乐观锁就没办法了，这个时候可以考虑悲观锁。<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">悲观锁</strong>，就是直接在数据库层面将数据锁死，类似于oralce中使用select xxxxx from xxxx where xx=xx for update，这样其他线程将无法提交数据。<br style="box-sizing: border-box;" />
除了加锁的方式也可以使用接收锁定的方式，思路是在数据库中设计一个状态标识位，用户在对数据进行修改前，将状态标识位标识为正在编辑的状态，这样其他用户要编辑此条记录时系统将发现有其他用户正在编辑，则拒绝其编辑的请求，类似于你在操作系统中某文件正在执行，然后你要修改该文件时，系统会提醒你该文件不可编辑或删除。</p>
<p style="box-sizing: border-box; margin: 10px auto; word-wrap: break-word; word-break: normal; list-style: none; border: none; line-height: 24.7px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px;">&nbsp;</p>
<p style="box-sizing: border-box; margin: 10px auto; word-wrap: break-word; word-break: normal; list-style: none; border: none; line-height: 24.7px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px;">4、<span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 24.7px;">不建议在数据库层面加锁，建议通过服务端的内存锁（锁主键）。<span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 24.7px;">当某个用户要修改某个id的数据时，把要修改的id存入memcache，若其他用户触发修改此id的数据时，读到memcache有这个id的值时，就阻止那个用户修改。</span></span></p>
<p style="box-sizing: border-box; margin: 10px auto; word-wrap: break-word; word-break: normal; list-style: none; border: none; line-height: 24.7px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px;"><span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 24.7px;"><br style="box-sizing: border-box;" />
</span></p>
<p style="box-sizing: border-box; margin: 10px auto; word-wrap: break-word; word-break: normal; list-style: none; border: none; line-height: 24.7px; color: rgb(51, 51, 51); font-family: &quot;Lucida Grande&quot;, Verdana, &quot;Bitstream Vera Sans&quot;, Arial, sans-serif; font-size: 13px;"><span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 24.7px;"><span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 24.7px;">5、<span style="box-sizing: border-box; margin: 0px; padding: 0px; line-height: 24.7px;">实际应用中，并不是让mysql去直面大并发读写，会借助&ldquo;外力&rdquo;，比如缓存、利用主从库实现读写分离、分表、使用队列写入等方法来降低并发读写。</span></span></span></p>]]></description>
			<link>http://meisw.wdlinux.cn//show-1006-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2017-12-24 15:53</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-945-1.html</guid>
			<title>mysql强制修改密码</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;mysqld_safe --skip-grant-tables --socket=/tmp/mysql.sock &gt; /dev/null 2&gt;&amp;1 &amp;</p>
<div>。。。。</div>
<div>killall -q mysqld_safe mysqld</div>
<div>&nbsp;</div>
<div>-------</div>
<div>&gt; /dev/null 2&gt;&amp;1 &amp; 与 &amp; &gt; /dev/null 2&gt;&amp;1 是有区别的</div>
<div>---</div>
<div>-q</div>]]></description>
			<link>http://meisw.wdlinux.cn//show-945-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2017-05-11 17:10</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-885-1.html</guid>
			<title>mysql_up5712</title>
			<author>admin</author>
			<description><![CDATA[<p>&nbsp;wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12.tar.gz</p>
<div>&nbsp;</div>
<div>yum install -y gcc gcc-c++ make sudo autoconf libtool-ltdl-devel gd-devel &nbsp; &nbsp; &nbsp; &nbsp; freetype-devel libxml2-devel libjpeg-devel libpng-devel openssl-devel &nbsp; &nbsp; &nbsp; &nbsp; curl-devel patch libmcrypt-devel libmhash-devel ncurses-devel bzip2 &nbsp; &nbsp; &nbsp; &nbsp; libcap-devel ntp sysklogd diffutils sendmail iptables unzip cmake wget &nbsp; &nbsp; &nbsp; &nbsp; re2c bison icu libicu libicu-devel net-tools psmisc vim-enhanced</div>
<div>&nbsp;</div>
<div>cmake . -DCMAKE_INSTALL_PREFIX=/mysql \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DMYSQL_DATADIR=/mysql/data \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DSYSCONFDIR=/mysql/etc \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_INNOBASE_STORAGE_ENGINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_PARTITION_STORAGE_ENGINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_FEDERATED_STORAGE_ENGINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_MYISAM_STORAGE_ENGINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_ARCHIVE_STORAGE_ENGINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_READLINE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DENABLED_LOCAL_INFILE=1 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DENABLE_DTRACE=0 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DDEFAULT_CHARSET=utf8mb4 \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DDEFAULT_COLLATION=utf8mb4_general_ci \</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; -DWITH_EMBEDDED_SERVER=1 \</div>
<div><span class="Apple-tab-span" style="white-space:pre">	</span>-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr -DENABLE_DOWNLOADS=1</div>
<div>&nbsp;</div>
<div>groupadd -g 27 mysql</div>
<div>useradd -g 27 -u 27 -d /dev/null -s /sbin/nologin mysql</div>
<div>&nbsp;</div>
<div>/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/mysql --datadir=/mysql/data</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>groupadd -g 27 mysql</div>
<div>useradd -g 27 -u 27 -d /dev/null -s /sbin/nologin mysql</div>
<div>&nbsp;</div>
<div>chown mysql.mysql /mysql/data -R</div>
<div>&nbsp;</div>
<div>cp support-files/mysql.server /etc/rc.d/init.d/mysqld</div>
<div>chmod 755 /etc/rc.d/init.d/mysqld</div>
<div>chkconfig --level 35 mysqld on</div>
<div>&nbsp;</div>]]></description>
			<link>http://meisw.wdlinux.cn//show-885-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2016-12-16 11:07</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-827-1.html</guid>
			<title>SQL注入</title>
			<author>admin</author>
			<description><![CDATA[<p>./sqlmap.py -r 3.txt --dbms=mysql --dbs -v 0 --level 5<br />
./sqlmap.py -r 3.txt --dbms=mysql --tables -v 0 --level 5</p>
<p>&nbsp;</p>
<p>3.txt为header,firefox header,修改 header</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-827-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-08-07 10:49</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-821-1.html</guid>
			<title>mysql 开启慢查询日志</title>
			<author>admin</author>
			<description><![CDATA[<p style="font: 14px/26px Arial; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">优化MySQL最重要的一部分工作是先确定&rdquo;有问题&rdquo;的查询语句。只有先找出这些查询较慢的sql查询(执行时间较长)，我们才能进一步分析原因并且优化它。MySQL为我们提供了Slow Query Log记录功能，它能记录执行时间超过了特定时长的查询。分析Slow Query Log有助于帮我们找到&rdquo;问题&rdquo;查询。记录slow queries</p>
<p style="font: 14px/26px Arial; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">首先，我们需要查看mysql server版本号，以及是否配置启用了slow query log。</p>
<p style="font: 14px/26px Arial; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">#打开服务<br />
log_slow_queries = ON</p>
<p style="font: 14px/26px Arial; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">当log_slow_queries是ON时，才表示已经启用了记录slow query功能。默认是不记录slow query的。<br />
启用slow query日志<br />
#//将下列配置放到my.cnf中<br />
[mysqld]<br />
log-slow-queries = /var/lib/mysql/slow-queries.log</p>
<div style="font: 14px/26px Arial; text-align: left; color: rgb(102, 102, 102); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; visibility: visible !important; -ms-word-break: break-all; -ms-zoom: 1 !important; -ms-word-wrap: break-word; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">//新增加的参数<br />
long_query_time = 3<br />
log-queries-not-using-indexes<br />
log-slow-admin-statements
<p>&nbsp;</p>
<p>上面的配置打开了slow query日志,将会捕获了执行时间超过了3秒的查询，包括执行速度较慢的管理命令(比如OPTIMEZE TABLE),并且记录了没有使用索引的查询。这些SQL，都会被记录到log-slow-queries指定的文件/var/lib/mysql/slow-queries.log文件中。<br />
log-slow-queries &lt;slow_query_log_file&gt;<br />
存放slow query日志的文件。你必须保证mysql server进程mysqld_safe进程用户对该文件有w权限。<br />
long_query_time<br />
如果query time超过了该值，则认为是较慢查询，并被记录下来。单位是秒，最小值是1,默认值是10秒。10秒对于大多数应用来讲，太长了。我们推荐从3秒开始， 依次减少，每次都找出最&rdquo;昂贵&rdquo;的10条SQL语句并且优化他们。日复一日，一步一步优化。一次性找出很多条SQL语句，对于优化来讲，意义并不大。<br />
log-queries-not-using-indexes<br />
MySQL会将没有使用索引的查询记录到slow query日志中。无论它执行有多快，查询语句没有使用索引，都会被记录。有的时候，有些没有使用引索的查询非常快(例如扫描很小的表)，但也有可能导致服务器变慢，甚至还会使用大量的磁盘空间。<br />
log-slow-admin-statements<br />
一些管理指令，也会被记录。比如OPTIMEZE TABLE, ALTER TABLE等等。<br />
日志文件</p>
<p>我们可以通过tail -f查看日志文件。<br />
$tail -f /var/lib/mysql/slow-queries.log<br />
# Time: 110107 16:22:11<br />
# User@Host: root[root] @ localhost []<br />
# Query_time: 9.869362 Lock_time: 0.000035 Rows_sent: 1 Rows_examined: 6261774<br />
SET timestamp=1294388531;<br />
select count(*) from ep_friends;</p>
<p>第一行,SQL查询执行的时间<br />
第二行,执行SQL查询的连接信息<br />
第三行记录了一些我们比较有用的信息<br />
Query_time SQL执行的时间,越长则越慢<br />
Lock_time 在MySQL服务器阶段(不是在存储引擎阶段)等待表锁时间<br />
Rows_sent 查询返回的行数<br />
Rows_examined 查询检查的行数<br />
Slow Query日志，虽然帮助你记录了那些执行过了的SQL语句。但它不是万能的，意义可能没有你想象的那么大。它只告诉了你哪些语句慢，但是为什么慢?具体 原因，还是需要你自己去分析，不断的调试。也许，你只需要换一条更有效的sql语句，也许你只需简单地增加一个索引，但也有可能你需要调整你应用程序的设 计方案。比如，上面那条语句是很明显，它检查了600多万行数据。不幸的是，并不是每条语句都这么明显。也许还有别的原因，比如:<br />
*锁表了，导致查询处于等态状态。lock_time显示了查询等待锁被翻译的时间<br />
*数据或索引没有被缓存。常见于第一次启动服务器或者服务器没有调优<br />
*备份数据库，I/O变慢<br />
*也许同时运行了其它的查询，减少了当前查询</p>
<p>所以,不要过于紧张日志文件某条记录，而应该理性地审记，找出真正的原因。如果经常出现的slow query需要特别注意。如果个别出现，则做一些常规检查即可。我们建议，统计并且形成基准报告，进行比较排除，比胡乱瞎撞有用。希望大家不要在这部分过于浪费时间与精力。<br />
线上记录slow query</p>
<p>上文的配置需要重启mysql server进程mysqld才会生效。但是很多时候，尤其是产品运营环境，不希望每次修改都需要重新启动mysql服务器，也希望能在某些特定时间记 录。MySQL5.1给我们提供了更为灵活的运行时控制，使得你不必重新启动mysql服务器，也能选择性地记录或者不记录某些slow queries。</p>
<p>MySQL5.1中，提供了全局变量slow_query_log、slow_query_log_file可以灵活地控制enable/disable慢查询。同时可以通过long_query_time设置时间<br />
#//停用slow query记录<br />
#注意:设置了slow_query_log全局变量, log_slow_queries也会隐性地跟着改变<br />
mysql&gt;set global slow_query_log=OFF</p>
<p>不幸运的是,在MySQL5.0并没有提供类似的全局变量来灵活控制，但是我们可以通过将long_query_time设置得足够大来避免记录某些查询语句。比如<br />
mysql&gt;set global long_query_time = 3600;</p>
<p>MySQL5.0, 不关服务的情况下，希望不记录日志的办法是将日志文件成为/dev/null的符号链接(symbolic link)。注意:你只需要在改变后运行FLUSH LOGS以确定MYSQL释放当前的日志文件描述符，重新把日志记录到/dev/null</p>
<p>和MySQL5.0不同,MySQL5.1可以在运行时改变日记行为，将日志记录到数据库表中。只要将mysql全局变量log_output设置为 TABLE即可。MySQL会将日志分别记录到表mysql.gengera_log和mysql.slow_log二张表中。但是，我们推荐将日志记录 在日记文件中。<br />
mysql&gt; show variables like &lsquo;log_output&rsquo;\G<br />
Variable_name: log_output<br />
Value: FILE<br />
mysql&gt;set global log_output=&rsquo;table&rsquo;;<br />
缺陷与审记</p>
<p>虽然记录了slow query能够帮助你优化产品。但是MySQL目前版本，还有几大蹩足的地方。<br />
1.MySQL5.0版本, long_query_time时间粒度不够细,最小值为1秒。对于高并发性能的网页脚本而言，1秒出现的意义不大。即出现1秒的查询比较少。直到mysql5.1.21才提供更细粒度的long_query_time设定.<br />
2.不能将服务器执行的所有查询记录到慢速日志中。虽然MySQL普通日志记录了所有查询，但是它们是解析查询之前就记录下来了。这意味着普通日志没办法包含诸如执行时间，锁表时间，检查行数等信息。<br />
3.如果开启了log_queries_not_using_indexes选项，slow query日志会充满过多的垃圾日志记录，这些快且高效的全表扫描查询(表小)会冲掉真正有用的slow queries记录。比如select * from category这样的查询也会被记录下来。</p>
<p>通过microslow-patch补丁可使用更细的时间粒度，和记录所有执行过的sql语句。不过，使用这个补订不得不自己编译MySQL，出于稳定性考滤，我们推荐在开发测试环境，可以打上这个补丁，享受这个补丁带来的便利。在运营环境尽量不要这么做&hellip;</p>
<p>MySQL自带了mysqldumpslow工具用来分析slow query日志，除此之外，还有一些好用的开源工具。比如MyProfi、mysql-log-filter，当然还有mysqlsla</p>
</div>]]></description>
			<link>http://meisw.wdlinux.cn//show-821-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-06-20 09:10</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-814-1.html</guid>
			<title>Mysql 慢查询和慢查询日志分析</title>
			<author>admin</author>
			<description><![CDATA[<p style="font: 13px/19px verdana, sans-serif; margin: 10px auto; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">众所周知，大访问量的情况下，可添加节点或改变架构可有效的缓解数据库压力，不过一切的原点，都是从单台mysql开始的。下面总结一些使用过或者研究过的经验，从配置以及调节索引的方面入手，对mysql进行一些优化。<br />
第一步应该做的就是排查问题，找出瓶颈，所以，先从日志入手<br />
<strong>开启慢查询日志</strong><br />
mysql&gt;show variables like &ldquo;%slow%&rdquo;; 查看慢查询配置，没有则在my.cnf中添加，如下</p>
<div class="wp_syntax" style="font: 13px/19px verdana, sans-serif; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">
<div class="code">
<pre class="python" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; -ms-word-wrap: break-word;">log-slow-queries = /data/mysqldata/slowquery.<span style="color: black;">log</span>    <span style="color: rgb(12, 120, 35); font-style: normal;">#日志目录</span> long_query_time = <span style="color: rgb(255, 69, 0);">1</span>                          <span style="color: rgb(12, 120, 35); font-style: normal;">#记录下查询时间查过1秒</span> log-queries-not-using-indexes     <span style="color: rgb(12, 120, 35); font-style: normal;">#表示记录下没有使用索引的查询</span></pre>
</div>
</div>
<p style="font: 13px/19px verdana, sans-serif; margin: 10px auto; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;"><strong>分析日志 &ndash; mysqldumpslow</strong><br />
分析日志，可用mysql提供的mysqldumpslow，使用很简单，参数可&ndash;help查看</p>
<div class="wp_syntax" style="font: 13px/19px verdana, sans-serif; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">
<div class="code">
<pre class="perl" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; -ms-word-wrap: break-word;"><span style="color: rgb(12, 120, 35); font-style: normal;"># -s：排序方式。c , t , l , r 表示记录次数、时间、查询时间的多少、返回的记录数排序；</span> <span style="color: rgb(12, 120, 35); font-style: normal;">#                             ac , at , al , ar 表示相应的倒叙；</span> <span style="color: rgb(12, 120, 35); font-style: normal;"># -t：返回前面多少条的数据；</span> <span style="color: rgb(12, 120, 35); font-style: normal;"># -g：包含什么，大小写不敏感的；</span> mysqldumpslow <span style="color: rgb(51, 153, 51);">-</span><span style="color: rgb(0, 0, 102);">s</span> r <span style="color: rgb(51, 153, 51);">-</span>t <span style="color: rgb(204, 102, 204);">10</span>  <span style="color: rgb(51, 153, 51);">/</span>slowquery<span style="color: rgb(51, 153, 51);">.</span><span style="color: rgb(0, 0, 102);">log</span>     <span style="color: rgb(12, 120, 35); font-style: normal;">#slow记录最多的10个语句</span> mysqldumpslow <span style="color: rgb(51, 153, 51);">-</span><span style="color: rgb(0, 0, 102);">s</span> t <span style="color: rgb(51, 153, 51);">-</span>t <span style="color: rgb(204, 102, 204);">10</span> <span style="color: rgb(51, 153, 51);">-</span>g <span style="color: rgb(255, 0, 0);">&quot;left join&quot;</span>  <span style="color: rgb(51, 153, 51);">/</span>slowquery<span style="color: rgb(51, 153, 51);">.</span><span style="color: rgb(0, 0, 102);">log</span>     <span style="color: rgb(12, 120, 35); font-style: normal;">#按照时间排序前10中含有&quot;left join&quot;的</span></pre>
</div>
</div>
<p style="font: 13px/19px verdana, sans-serif; margin: 10px auto; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;"><strong>推荐用分析日志工具 &ndash;<span class="Apple-converted-space">&nbsp;</span><a style="color: rgb(68, 68, 68); text-decoration: none;" href="http://hackmysql.com/mysqlsla" target="blank"><font color="#444444">mysqlsla</font></a></strong></p>
<div class="wp_syntax" style="font: 13px/19px verdana, sans-serif; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">
<div class="code">
<pre class="bash" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; -ms-word-wrap: break-word;"><span style="color: rgb(194, 12, 185); font-weight: bold;">wget</span> http:<span style="color: rgb(0, 0, 0); font-weight: normal;">//</span>hackmysql.com<span style="color: rgb(0, 0, 0); font-weight: normal;">/</span>scripts<span style="color: rgb(0, 0, 0); font-weight: normal;">/</span>mysqlsla-2.03.tar.gz <span style="color: rgb(194, 12, 185); font-weight: bold;">tar</span> zvxf mysqlsla-2.03.tar.gz <span style="color: rgb(122, 8, 116); font-weight: bold;">cd</span> mysqlsla-<span style="color: rgb(0, 0, 0);">2.03</span> <span style="color: rgb(194, 12, 185); font-weight: bold;">perl</span> Makefile.PL <span style="color: rgb(194, 12, 185); font-weight: bold;">make</span> <span style="color: rgb(194, 12, 185); font-weight: bold;">make</span> <span style="color: rgb(194, 12, 185); font-weight: bold;">install</span> mysqlsla <span style="color: rgb(0, 0, 0); font-weight: normal;">/</span>data<span style="color: rgb(0, 0, 0); font-weight: normal;">/</span>mysqldata<span style="color: rgb(0, 0, 0); font-weight: normal;">/</span>slow.log <span style="color: rgb(12, 120, 35); font-style: normal;"># mysqlsla会自动判断日志类型，为了方便可以建立一个配置文件&ldquo;~/.mysqlsla&rdquo;</span> <span style="color: rgb(12, 120, 35); font-style: normal;"># 在文件里写上：top=100，这样会打印出前100条结果。</span></pre>
<pre class="bash" style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; -ms-word-wrap: break-word;"><span style="color: rgb(12, 120, 35); font-style: normal;"><pre class="text" style="color: rgb(0, 0, 0); text-transform: none; line-height: 19px; text-indent: 0px; letter-spacing: normal; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; margin-top: 0px; margin-bottom: 0px; word-spacing: 0px; white-space: pre-wrap; -ms-word-wrap: break-word; background-color: rgb(255, 255, 255); -webkit-text-stroke-width: 0px;">【说明】 queries total: 总查询次数 unique:去重后的sql数量 sorted by : 输出报表的内容排序 最重大的慢sql统计信息, 包括 平均执行时间, 等待锁时间, 结果行的总数, 扫描的行总数. Count, sql的执行次数及占总的slow log数量的百分比. Time, 执行时间, 包括总时间, 平均时间, 最小, 最大时间, 时间占到总慢sql时间的百分比. 95% of Time, 去除最快和最慢的sql, 覆盖率占95%的sql的执行时间. Lock Time, 等待锁的时间. 95% of Lock , 95%的慢sql等待锁时间. Rows sent, 结果行统计数量, 包括平均, 最小, 最大数量. Rows examined, 扫描的行数量. Database, 属于哪个数据库 Users, 哪个用户,IP, 占到所有用户执行的sql百分比 Query abstract, 抽象后的sql语句 Query sample, sql语句</pre>
</span></pre>
</div>
</div>]]></description>
			<link>http://meisw.wdlinux.cn//show-814-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-06-04 10:30</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-803-1.html</guid>
			<title>mysql innodb引擎的优化方法</title>
			<author>admin</author>
			<description><![CDATA[<p><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">innodb</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">_buffer_pool_size：这是InnoDB最重要的设置，对InnoDB</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">性能</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">有决定性的影响。默认的设置只有8M，所以默认的数据库设置下面InnoDB性能很差。在只有InnoDB存储引擎的数据库服务器上面，可以设置60-80%的</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">内存</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">。更精确一点，在内存容量允许的情况下面设置比InnoDB tablespaces大10%的内存大小。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_data_file_path：指定表数据和索引存储的空间，可以是一个或者多个</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">文件</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">。最后一个数据文件必须是自动扩充的，也只有最后一个文件允许自动扩充。这样，当空间用完后，自动扩充数据文件就会自动增长（以8MB为单位）以容纳额外的数据。例如： innodb_data_file_path=/disk1 /ibdata1:900M;/disk2/ibdata2:50M:autoextend两个数据文件放在不同的磁盘上。数据首先放在ibdata1 中，当达到900M以后，数据就放在ibdata2中。一旦达到50MB，ibdata2将以8MB为单位自动增长。如果磁盘满了，需要在另外的磁盘上面增加一个数据文件。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_data_home_dir：放置表空间数据的目录，默认在</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">mysql</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">的数据目录，设置到和MySQL</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">安装</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">文件不同的分区可以提</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">高性能</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">log</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">_file_size：该参数决定了recovery speed。太大的话recovery就会比较慢，太小了影响查询性能，一般取256M可以兼顾性能和recovery的速度</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_log_buffer_size：磁盘速度是很慢的，直接将log写道磁盘会影响InnoDB的性能，该参数设定了log buffer的大小，一般4M。如果有大的blob操作，可以适当增大。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_flush_logs_at_trx_commit=2： 该参数设定了事务提交时内存中log信息的处理。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 1) =1时，在每个事务提交时，</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">日志</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">缓冲被写到日志文件，对日志文件做到磁盘操作的刷新。Truly ACID。速度慢。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 2) =2时，在每个事务提交时，日志缓冲被写到文件，但不对日志文件做到磁盘操作的刷新。只有操作</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">系统</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">崩溃或掉电才会删除最后一秒的事务，不然不会丢失事务。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 3) =0时， 日志缓冲每秒一次地被写到日志文件，并且对日志文件做到磁盘操作的刷新。任何mysqld进程的崩溃会删除崩溃前最后一秒的事务</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_file_per_table：可以存储每个InnoDB表和它的索引在它自己的文件中。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">transaction-isolation=READ-COMITTED: 如果应用程序可以运行在READ-COMMITED隔离级别，做此设定会有一定的性能提升。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_flush_method： 设置InnoDB同步IO的方式：</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 1) Default &ndash;<span class="Apple-converted-space">&nbsp;</span></span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">使用</span><span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">fsync（）。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 2) O_SYNC 以sync模式打开文件，通常比较慢。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 3) O_DIRECT，在Linux上使用Direct IO。可以显著提高速度，特别是在RAID系统上。避免额外的数据复制和double buffering（mysql buffering 和OS buffering）。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">innodb_thread_concurrency： InnoDB kernel最大的线程数。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 1) 最少设置为(num_disks+num_cpus)*2。</span><br style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">&nbsp; &nbsp; 2) 可以通过设置成1000来禁止这个</span><span class="t_tag" style="font: 14px/22px 微软雅黑, Verdana, sans-serif, 宋体; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" onclick="tagshow(event)">限制</span></p>]]></description>
			<link>http://meisw.wdlinux.cn//show-803-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-04-03 19:41</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-792-1.html</guid>
			<title>mysql的innodb_flush_log_at_trx_commit</title>
			<author>admin</author>
			<description><![CDATA[<p style="font: 14px/25.18px Helvetica, Tahoma, Arial, sans-serif; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><strong style="font-weight: bold;">innodb_buffer_pool_size<br />
</strong>如 果用Innodb，那么这是一个重要变量。相对于MyISAM来说，Innodb对于buffer size更敏感。MySIAM可能对于大数据量使用默认的key_buffer_size也还好，但Innodb在大数据量时用默认值就感觉在爬了。 Innodb的缓冲池会缓存数据和索引，所以不需要给系统的缓存留空间，如果只用Innodb，可以把这个值设为内存的70%-80%。和 key_buffer相同，如果数据量比较小也不怎么增加，那么不要把这个值设太高也可以提高内存的使用率。</p>
<p style="font: 14px/25.18px Helvetica, Tahoma, Arial, sans-serif; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><strong style="font-weight: bold;">innodb_additional_pool_size</strong><span class="Apple-converted-space">&nbsp;</span><br />
这个的效果不是很明显，至少是当操作系统能合理分配内存时。但你可能仍需要设成20M或更多一点以看Innodb会分配多少内存做其他用途。</p>
<p style="font: 14px/25.18px Helvetica, Tahoma, Arial, sans-serif; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><strong style="font-weight: bold;">innodb_log_file_size<br />
</strong>对于写很多尤其是大数据量时非常重要。要注意，大的文件提供更高的性能，但数据库恢复时会用更多的时间。我一般用64M-512M，具体取决于服务器的空间。</p>
<p style="font: 14px/25.18px Helvetica, Tahoma, Arial, sans-serif; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><strong style="font-weight: bold;">innodb_log_buffer_size</strong><span class="Apple-converted-space">&nbsp;</span><br />
默认值对于多数中等写操作和事务短的运用都是可以的。如 果经常做更新或者使用了很多blob数据，应该增大这个值。但太大了也是浪费内存，因为1秒钟总会 flush（这个词的中文怎么说呢？）一次，所以不需要设到超过1秒的需求。8M-16M一般应该够了。小的运用可以设更小一点。</p>
<p style="font: 14px/25.18px Helvetica, Tahoma, Arial, sans-serif; margin: 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><span style="color: rgb(255, 0, 0);"><strong style="font-weight: bold;">innodb_flush_log_at_trx_commit&nbsp; （这个很管用）</strong><span class="Apple-converted-space">&nbsp;</span></span><br />
抱怨Innodb比MyISAM慢 100倍？那么你大概是忘了调整这个值。默认值1的意思是每一次事务提交或事务外的指令都需要把日志写入（flush）硬盘，这是很费时的。特别是使用电 池供电缓存（Battery backed up cache）时。设成2对于很多运用，特别是从MyISAM表转过来的是可以的，它的意思是不写入硬盘而是写入系统缓存。日志仍然会每秒flush到硬 盘，所以你一般不会丢失超过1-2秒的更新。设成0会更快一点，但安全方面比较差，即使MySQL挂了也可能会丢失事务的数据。而值2只会在整个操作系统 挂了时才可能丢数据。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-792-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-03-20 17:30</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-788-1.html</guid>
			<title>mysql 5.5安装提示ncurses问题</title>
			<author>admin</author>
			<description><![CDATA[<p><strong><font color="#333333" face="Times New Roman">安装mysql5.5.17的时候报错如下：<br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
</font></strong><span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">-- Could NOT find OpenSSL (missing: OPENSSL_LIBRARIES OPENSSL_INCLUDE_DIR)<span class="Apple-converted-space">&nbsp;</span></span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">-- Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)<span class="Apple-converted-space">&nbsp;</span></span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">CMake Error at cmake/readline.cmake:83 (MESSAGE):</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">Curses library not found. Please install appropriate package,</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">Call Stack (most recent call first):</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">cmake/readline.cmake:118 (FIND_CURSES)</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">cmake/readline.cmake:214 (MYSQL_USE_BUNDLED_READLINE)</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">CMakeLists.txt:257 (MYSQL_CHECK_READLINE)</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">-- Configuring incomplete, errors occurred!</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<strong style="color: rgb(51, 51, 51); text-transform: none; line-height: 23px; text-indent: 0px; letter-spacing: normal; font-family: &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">解决办法：</strong><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">[root@vps mysql-5.5.17]# rm -f CMakeCache.txt</span><br style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" />
<span style="font: 14px/23px &quot;Times New Roman&quot;, Arial, Helvetica, sans-serif; color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; float: none; display: inline !important; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">[root@vps mysql-5.5.17]# yum -y install ncurses-devel</span></p>]]></description>
			<link>http://meisw.wdlinux.cn//show-788-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-03-19 15:25</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-785-1.html</guid>
			<title>Incorrect key file for table mysql表损坏的修复方法</title>
			<author>admin</author>
			<description><![CDATA[<p>今天查询mysql的时候，报这样的错误，Incorrect key file for table './tg/dxad.MYI'; try to repair it. mysql表损坏的情况是很少见的，下面的方法适用于myisam，其他存储引擎，不知道能不能这样修复。<br />
1，myisamchk修改表<br />
查看复制打印?<br />
[root@localhost tg]# myisamchk -of ./dxad.MYI&nbsp;&nbsp;&nbsp;&nbsp; //修复第一步&nbsp; <br />
- recovering (with keycache) MyISAM-table './dxad.MYI'&nbsp; <br />
Data records: 12597637&nbsp; <br />
Found block that points outside data file at 1630252996&nbsp; <br />
Data records: 12597456&nbsp; <br />
[root@localhost tg]# myisamchk -r ./dxad.MYI&nbsp;&nbsp;&nbsp;&nbsp; //修复第二步&nbsp; <br />
- recovering (with sort) MyISAM-table './dxad.MYI'&nbsp; <br />
Data records: 12597456&nbsp; <br />
- Fixing index 1&nbsp; <br />
[root@localhost tg]# myisamchk ./dxad.MYI&nbsp;&nbsp;&nbsp;&nbsp; //修复第三步&nbsp; <br />
&nbsp; <br />
Checking MyISAM file: ./dxad.MYI&nbsp; <br />
Data records: 12597456&nbsp;&nbsp; Deleted blocks:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp; <br />
- check file-size&nbsp; <br />
- check record delete-chain&nbsp; <br />
- check key delete-chain&nbsp; <br />
- check index reference&nbsp; <br />
- check data record references index: 1&nbsp; <br />
- check record links&nbsp; <br />
myisamchk带的参数，可以用man看一下。操作后重新启动一下数据库。<br />
[root@localhost tg]# /etc/init.d/mysqld restart&nbsp; <br />
这样操作后，还是有问题，会报 #145 - Table &quot;XXXXX&quot; is marked as crashed and should be repaired。如下图：</p>
<p>myisamchk 修复表后报的错误<br />
2，命令行下repair修复表<br />
查看复制打印?<br />
mysql&gt; repair table dxad;&nbsp; //dxad是表名&nbsp; <br />
如下图</p>
<p>命令行下repair修复表成功<br />
到这儿，myisam表损坏就修复好了。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-785-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-03-16 18:16</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-784-1.html</guid>
			<title>MySQL MyISAM 表转换为InnoDB的方法</title>
			<author>admin</author>
			<description><![CDATA[<p>1.备份数据库： mysqldump -u[user] -p[password] [databasename] &gt; [dbfile.sql] # 备份数据库。<br />
2. /usr/local/mysql/bin/mysqladmin -u root -p shutdown # 停止数据库 或者 service mysql stop。<br />
3. InnoDB 表不支持全文搜索(fulltext search)，那么，记得要将备份出来的数据库sql，删掉有关 Fulltext 的索引。<br />
4. cd /usr/local/mysql/support-files/ 找寻适合主机内存的设定文件，必将设定文件拷贝到 /etc/my.cnf。<br />
5. vi /etc/my.cnf ，将以下几项批注取消掉。以 my-large.cnf 为例。</p>
<p>　　innodb_data_file_path = ibdata1:10M:autoextend<br />
innodb_buffer_pool_size = 256M<br />
innodb_additional_mem_pool_size = 20M<br />
innodb_log_file_size = 5M<br />
innodb_log_buffer_size = 8M<br />
innodb_flush_log_at_trx_commit = 1<br />
innodb_lock_wait_timeout = 50<br />
&nbsp;&nbsp;&nbsp; 加上 default-storage-engine=innodb　<br />
加上这段之后，以后新增的数据表型态都即是 InnoDB，不然每次新增一次数据表，SQL 后面得加上 ENGINE=InnoDB;</p>
<p>6 .将刚刚备份出来的sql，将ENGINE=MyISAM改成ENGINE=InnoDB。<br />
7. /usr/local/mysql/bin/safe_mysqld --user=mysql &amp;&nbsp; ，或service mysql start 启动数据库<br />
8. 建立一个新的数据库(数据库名称跟备份出来的数据库名称一样)。<br />
9. mysql -u[user] -p[password] [database_name] &lt; [dbfile] # 将改好的数据汇入数据库中！</p>
<p>说明：<br />
* 设定文件的选择是参照内存大小来选择。<br />
my-huge.cnf - 1G~2G 、my-large.cnf - 512M 、 my-medium.cnf - 32M - 64M 、my-small.cnf &lt;= 64M 。<br />
InnoDB：my-innodb-heavy-4G.cnf <br />
* 假如不会将备份出来的数据库改型态，那么您可以用下面这个指令，直接改变数据表的型态。</p>
<p>　ALTER TABLE [tablename] ENGINE=InnoDB 如有存放全文索引功能的话，转换会失败的。</p>
<p>* 如你有一批数据表要改，可以用下面的指令：<br />
&nbsp;mysql_convert_table_format [opt] -- ENGINE=InnoDB dbname [tablename] <br />
但千万注意不要改变 mysql 数据库的数据型态，因为 mysql数据库存放的是 MySQL 内部的管理信息，所以必须保持 MyISAM 的格式。</p>
<p>* 加大 tablespace 空间：<br />
innodb_data_file_path = ibdata1:1G;ibdata2:1G:autoextend:max2G</p>
<p>　上面的意思是，tablespace 包含 ibdata1 &amp; ibdata2 两个文件，若文件不存在，则建立容量各为1G的文件。一旦未来 InnoDB 需要，更多的空间，则 ibdata2 将每次自动增加 8MB，直到2G为止。</p>
<p>　* MySQL 3.23.n，innodb_data_home &amp; innodb_data_file_path设定是必须要有的，MySQL 4.0.0 之后的版本则是非必须的。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-784-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-03-16 17:45</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-783-1.html</guid>
			<title>mysql myisam转innodb的2种方法</title>
			<author>admin</author>
			<description><![CDATA[<p>mysql myisam转innodb的2种方法</p>
<p>一，用alter table来实现。<br />
mysql&gt; alter table merchant type=innodb;</p>
<p>ALTER TABLE&nbsp; `block_view` ENGINE = INNODB;</p>
<p>&nbsp;</p>
<p>二，导出数据，重新建表<br />
1，导出表结构以及数据，删除老表，重新建表<br />
查看复制打印?<br />
//方法1，我们可以把把创建这个表的sql拷贝出来，把ENGINE=MyISAM换成，ENGINE=INNODB&nbsp; <br />
mysql&gt; show create table merchant\G;&nbsp; <br />
*************************** 1. row ***************************&nbsp; <br />
&nbsp;Table: merchant&nbsp; <br />
Create Table: CREATE TABLE `merchant` (&nbsp; <br />
&nbsp;`btid` int(5) unsigned zerofill NOT NULL auto_increment,&nbsp; <br />
&nbsp;`btname` varchar(100) NOT NULL,&nbsp; <br />
&nbsp;`btnote` text,&nbsp; <br />
&nbsp;PRIMARY KEY&nbsp; (`btid`),&nbsp; <br />
&nbsp;UNIQUE KEY `btname` (`btname`)&nbsp; <br />
) ENGINE=MyISAM DEFAULT CHARSET=utf8&nbsp; <br />
1 row in set (0.00 sec)&nbsp; <br />
&nbsp; <br />
//方法2，直接导出表结构，在文件里面替换一个myisam&nbsp; <br />
mysqldump -udba -p -d test merchant &gt;/tmp/test/mytest/merchant.sql;&nbsp; <br />
2，导出数据<br />
mysqldump -udba -p --no-create-info test merchant &gt;/tmp/test/mytest/merchant_data.sql;&nbsp; <br />
3，删除老表，重新建表，导入数据，这个就不说了，linux 简单的mysql备份和导入，以及文件的备份和导入</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-783-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-03-16 17:44</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-782-1.html</guid>
			<title>mysql innodb启动问题</title>
			<author>admin</author>
			<description><![CDATA[<p># Uncomment the following if you are using InnoDB tables<br />
innodb_data_home_dir = /www/wdlinux/mysql-5.5.30/data<br />
innodb_data_file_path = ibdata1:10M:autoextend<br />
innodb_log_group_home_dir = /www/wdlinux/mysql-5.5.30/data<br />
# You can set .._buffer_pool_size up to 50 - 80 %<br />
# of RAM but beware of setting memory usage too high<br />
innodb_buffer_pool_size = 1024M<br />
innodb_additional_mem_pool_size = 20M<br />
# Set .._log_file_size to 25 % of buffer pool size<br />
#innodb_log_file_size = 64M&nbsp; //启用这个,就不能启动<br />
innodb_log_buffer_size = 8M<br />
innodb_flush_log_at_trx_commit = 1<br />
innodb_lock_wait_timeout = 50</p>
<p>&nbsp;</p>
<p>--------</p>
<p>要把ib_log*的文件删除,再启动就正常了</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-782-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2013-03-16 14:03</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-753-1.html</guid>
			<title>Copying to tmp table</title>
			<author>admin</author>
			<description><![CDATA[<p style="font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; margin: 0px 20px 0px 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">MySQL may use temporary tables during query execution. Ideally you would want to avoid this, since its an expensive and slow operation. It can be avoided by optimizing queries. Sometimes it can&rsquo;t be completely avoided &ndash; in that case you want to make sure the temporary table is created as a &ldquo;memory&rdquo; storage engine table, since its very fast, as it is never written to disk and remains, as the name states, in memory. But, as the&nbsp;<a style="color: rgb(0, 102, 204); text-decoration: none;" href="http://dev.mysql.com/doc/refman/5.1/en/internal-temporary-tables.html" target="_blank"><font color="#0066cc">manual explains</font></a>, there are some conditions, such as TEXT/BLOB columns, or a combination of GROUP BY/ORDER BY clauses that makes MySQL write the temporary table to disk as a MyISAM table. One can spot these queries by the EXPLAIN output:<br />
<code style="font-family: &quot;Courier New&quot;, Courier, monospace; font-size: 1em; white-space: pre;">[...] Using where; Using temporary; Using filesort</code><br />
In that case performance depends on disk I/O speed. If there are multiple similar queries running simultaneously, they try to read/write a lot of information to the disk, and will become extremely slow.</p>
<p style="font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; margin: 0px 20px 0px 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">Solution? TMPFS!</p>
<p style="font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; margin: 0px 20px 0px 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">tmpfs is a filesystem, that resides in RAM/Swap, so if your server has enough available RAM, files written there will bypass disk I/O completely, and will perform significantly faster.</p>
<p style="font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; margin: 0px 20px 0px 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">Now, &ldquo;High Performance MySQL, Second Edition&rdquo; claims that this solution is still not as good as a MEMORY table, since it requires MySQL to use some expensive OS calls to write &amp; read the temporary table, but it is still faster than the disk based temporary table.</p>
<p style="font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; margin: 0px 20px 0px 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">To set it up, just mount a tmpfs system on an empty directory (you should also add this to fstab):<br />
<code style="font-family: &quot;Courier New&quot;, Courier, monospace; font-size: 1em; white-space: pre;">mount tmpfs /tmpfs -t tmpfs</code><br />
and edit my.cnf to make MySQL use that directory as a temporary directory:<br />
<code style="font-family: &quot;Courier New&quot;, Courier, monospace; font-size: 1em; white-space: pre;">tmpdir = /tmpfs</code><br />
Be careful though, there is&nbsp;<a style="color: rgb(0, 102, 204); text-decoration: none;" href="http://bugs.mysql.com/bug.php?id=30287" target="_blank"><font color="#0066cc">a bug</font></a>&nbsp;in some versions that prevents this from working properly.</p>
<p style="font: 14px/25px Helvetica, Tahoma, Arial, sans-serif; margin: 0px 20px 0px 0px; padding: 0px; text-align: left; color: rgb(0, 0, 0); text-transform: none; text-indent: 0px; letter-spacing: normal; word-spacing: 0px; white-space: normal; orphans: 2; widows: 2; background-color: rgb(255, 255, 255); -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">For more information, see&nbsp;<a style="color: rgb(0, 102, 204); text-decoration: none;" href="http://www.mysqlperformanceblog.com/2007/08/16/how-much-overhead-is-caused-by-on-disk-temporary-tables/" target="_blank"><font color="#0066cc">this blog</font></a>.</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-753-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2012-11-29 23:10</pubDate>
		</item>
		<item>
			<guid>http://meisw.wdlinux.cn//show-726-1.html</guid>
			<title>MySQL错误“Specified key was too long; max key length is 1000 bytes”的解决办法</title>
			<author>admin</author>
			<description><![CDATA[<p>今天在为数据库中的某两个字段设置unique索引的时候，出现了Specified key was too long; max key length is 1000 bytes错误，经过查询才知道，是Mysql的字段设置的太长了，于是我把这两个字段的长度改了一下就好了。</p>
<p>建立索引时，数据库计算key的长度是累加所有Index用到的字段的char长度后再按下面比例乘起来不能超过限定的key长度1000：<br />
latin1 = 1 byte = 1 character<br />
uft8 = 3 byte = 1 character<br />
gbk = 2 byte = 1 character<br />
举例能看得更明白些，以GBK为例：<br />
CREATE UNIQUE INDEX `unique_record` ON reports (`report_name`, `report_client`, `report_city`);<br />
其中report_name varchar(200), report_client varchar(200), report_city varchar(200)<br />
(200 + 200 +200) * 2 = 1200 &gt; 1000，所有就会报1071错误，只要将report_city改为varchar(100)那么索引就能成功建立。<br />
如果表是UTF8字符集，那索引还是建立不了。</p>]]></description>
			<link>http://meisw.wdlinux.cn//show-726-1.html</link>
			<category domain="http://meisw.wdlinux.cn//category-4-1.html">mysql/db</category>
			<pubDate>2012-08-15 21:26</pubDate>
		</item>
	</channel>
</rss>
