﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-SIMONE-随笔分类-mysql</title><link>http://www.blogjava.net/wangxinsh55/category/49905.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 02 Jul 2016 00:55:14 GMT</lastBuildDate><pubDate>Sat, 02 Jul 2016 00:55:14 GMT</pubDate><ttl>60</ttl><item><title>MySQL分库分表的全局唯一ID生成器方案</title><link>http://www.blogjava.net/wangxinsh55/archive/2016/06/28/431035.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 28 Jun 2016 10:48:00 GMT</pubDate><guid>http://www.blogjava.net/wangxinsh55/archive/2016/06/28/431035.html</guid><wfw:comment>http://www.blogjava.net/wangxinsh55/comments/431035.html</wfw:comment><comments>http://www.blogjava.net/wangxinsh55/archive/2016/06/28/431035.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxinsh55/comments/commentRss/431035.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxinsh55/services/trackbacks/431035.html</trackback:ping><description><![CDATA[<div>http://lztian.com/blog/5921.html</div><br /><div><div> 						<p>借用MySQL 的 auto_increment 特性可以产生唯一的可靠ID。</p> <p>表定义，关键在于auto_increment，和UNIQUE KEY的设置：</p> <div><div id="highlighter_394886"  sql"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div><div number3="" index2=""  alt2"="">3</div><div number4="" index3=""  alt1"="">4</div><div number5="" index4=""  alt2"="">5</div><div number6="" index5=""  alt1"="">6</div></td><td><div><div number1="" index0=""  alt2"=""><code keyword"="">CREATE</code> <code keyword"="">TABLE</code> <code plain"="">`Tickets64` (</code></div><div number2="" index1=""  alt1"=""><code spaces"="">&nbsp;&nbsp;</code><code plain"="">`id` </code><code keyword"="">bigint</code><code plain"="">(20) unsigned </code><code color1"="">NOT</code> <code color1"="">NULL</code> <code plain"="">auto_increment,</code></div><div number3="" index2=""  alt2"=""><code spaces"="">&nbsp;&nbsp;</code><code plain"="">`stub` </code><code keyword"="">char</code><code plain"="">(1) </code><code color1"="">NOT</code> <code color1"="">NULL</code> <code keyword"="">default</code> <code string"="">''</code><code plain"="">,</code></div><div number4="" index3=""  alt1"=""><code spaces"="">&nbsp;&nbsp;</code><code keyword"="">PRIMARY</code> <code keyword"="">KEY</code>&nbsp; <code plain"="">(`id`),</code></div><div number5="" index4=""  alt2"=""><code spaces"="">&nbsp;&nbsp;</code><code keyword"="">UNIQUE</code> <code keyword"="">KEY</code> <code plain"="">`stub` (`stub`)</code></div><div number6="" index5=""  alt1"=""><code plain"="">) ENGINE=MyISAM</code></div></div></td></tr></tbody></table></div></div> <p>需要使用时，巧用replace into语法来获取值，结合表定义的UNIQUE KEY，确保了一条记录就可以满足ID生成器的需求：</p> <div><div id="highlighter_696647"  sql"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div></td><td><div><div number1="" index0=""  alt2"=""><code color2"="">REPLACE</code> <code keyword"="">INTO</code> <code plain"="">Tickets64 (stub) </code><code keyword"="">VALUES</code> <code plain"="">(</code><code string"="">'a'</code><code plain"="">);</code></div><div number2="" index1=""  alt1"=""><code keyword"="">SELECT</code> <code plain"="">LAST_INSERT_ID();</code></div></div></td></tr></tbody></table></div></div> <p>以上方式中，通过MySQL的机制，可以确保此ID的唯一和自增，且适用于多并发的场景。官方对此的描述：https://dev.mysql.com/doc/refman/5.0/en/information-functions.html</p> <div><div id="highlighter_815008"  plain"=""><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td><div number1="" index0=""  alt2"="">1</div><div number2="" index1=""  alt1"="">2</div><div number3="" index2=""  alt2"="">3</div></td><td><div><div number1="" index0=""  alt2"=""><code plain"="">It is multi-user safe because multiple clients can issue the UPDATE statement and </code></div><div number2="" index1=""  alt1"=""><code plain"="">get their own sequence value with the SELECT statement (or mysql_insert_id()), </code></div><div number3="" index2=""  alt2"=""><code plain"="">without affecting or being affected by other clients that generate their own sequence values.</code></div></div></td></tr></tbody></table></div></div> <p>需要注意的是，若client采用PHP，则不能使用mysql_insert_id()获取ID，原因见《mysql_insert_id()  在bigint型AI字段遇到的问题》：http://kaifage.com/notes/99/mysql-insert-id-issue- with-bigint-ai-field.html。</p> <p>Flickr 采取了此方案： http://code.flickr.net/2010/02/08/ticket-servers-distributed-unique-primary-keys-on-the-cheap/</p> <p>相关：</p> <p>http://www.zhihu.com/question/30674667</p> <p>http://my.oschina.net/u/142836/blog/174465</p> 											</div></div><img src ="http://www.blogjava.net/wangxinsh55/aggbug/431035.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxinsh55/" target="_blank">SIMONE</a> 2016-06-28 18:48 <a href="http://www.blogjava.net/wangxinsh55/archive/2016/06/28/431035.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>MYSQL之表分区----按日期分区 </title><link>http://www.blogjava.net/wangxinsh55/archive/2016/06/07/430822.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 07 Jun 2016 10:06:00 GMT</pubDate><guid>http://www.blogjava.net/wangxinsh55/archive/2016/06/07/430822.html</guid><wfw:comment>http://www.blogjava.net/wangxinsh55/comments/430822.html</wfw:comment><comments>http://www.blogjava.net/wangxinsh55/archive/2016/06/07/430822.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxinsh55/comments/commentRss/430822.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxinsh55/services/trackbacks/430822.html</trackback:ping><description><![CDATA[<div>http://blog.sina.com.cn/s/blog_888269b20100w7kf.html</div><br /><div><div id="sina_keyword_ad_area2"  "=""> 			<p>mysql 5.1已经到了beta版，官方网站上也陆续有一些文章介绍，比如上次看到的<a href="http://www.ooso.net/index.php/archives/154">Improving Database Performance with Partitioning</a>。在使用分区的前提下，可以用mysql实现非常大的数据量存储。今天在mysql的站上又看到一篇进阶的文章 &#8212;&#8212; <a href="http://dev.mysql.com/tech-resources/articles/mysql_5.1_partitioning_with_dates.html"> 按日期分区存储</a>。如果能够实现按日期分区，这对某些时效性很强的数据存储是相当实用的功能。下面是从这篇文章中摘录的一些内容。</p> <h3>错误的按日期分区例子</h3> <p>最直观的方法，就是直接用年月日这种日期格式来进行常规的分区：</p> <div><a href="http://www.ooso.net/index.php/archives/217#"><strong>PLAIN TEXT</strong></a></div> <div><strong>CODE:</strong> <div> <div> <ol><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt;&nbsp;<wbr> create table rms <span style="font-weight: bold; color: #006600">(</span>d date<span style="font-weight: bold; color: #006600">)</span></div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> partition by range <span style="font-weight: bold; color: #006600">(</span>d<span style="font-weight: bold; color: #006600">)</span></div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; <span style="font-weight: bold; color: #006600">(</span>partition p0 values less than <span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'1995-01-01'</span><span style="font-weight: bold; color: #006600">)</span>,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> partition p1 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2010-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span>;</div> </li></ol> </div> </div> </div> <p>&nbsp;<wbr></p> <p>上面的例子中，就是直接用"Y-m-d"的格式来对一个table进行分区，可惜想当然往往不能奏效，会得到一个错误信息:</p> <blockquote> <p>ERROR 1064 (42000): VALUES value must be of same type as partition function near '),<br /> partition p1 VALUES LESS THAN ('2010-01-01'))' at line 3</p> </blockquote> <p>上述分区方式没有成功，而且明显的不经济，老练的DBA会用整型数值来进行分区：</p> <div><a href="http://www.ooso.net/index.php/archives/217#"><strong>PLAIN TEXT</strong></a></div> <div><strong>CODE:</strong> <div> <div> <ol><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt; CREATE TABLE part_date1</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> <span style="font-weight: bold; color: #006600">(</span>&nbsp;<wbr> c1 int default NULL,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> c2 varchar<span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">30</span><span style="font-weight: bold; color: #006600">)</span> default NULL,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> c3 date default NULL<span style="font-weight: bold; color: #006600">)</span> engine=myisam</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> partition by range <span style="font-weight: bold; color: #006600">(</span>cast<span style="font-weight: bold; color: #006600">(</span>date_format<span style="font-weight: bold; color: #006600">(</span>c3,<span style="color: #cc0000">'%Y%m%d'</span><span style="font-weight: bold; color: #006600">)</span> as signed<span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span></div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; <span style="font-weight: bold; color: #006600">(</span>PARTITION p0 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">19950101</span><span style="font-weight: bold; color: #006600">)</span>,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p1 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">19960101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p2 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">19970101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p3 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">19980101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p4 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">19990101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p5 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">20000101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p6 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">20010101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p7 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">20020101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p8 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">20030101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p9 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">20040101</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p10 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">20100101</span><span style="font-weight: bold; color: #006600">)</span>,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p11 VALUES LESS THAN MAXVALUE <span style="font-weight: bold; color: #006600">)</span>;</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> Query OK, <span style="color: #800000">0</span> rows affected <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">0</span>.<span style="color: #800000">01</span> sec<span style="font-weight: bold; color: #006600">)</span></div> </li></ol> </div> </div> </div> <p>&nbsp;<wbr></p> <p>搞定？接着往下分析</p> <div><a href="http://www.ooso.net/index.php/archives/217#"><strong>PLAIN TEXT</strong></a></div> <div><strong>CODE:</strong> <div> <div> <ol><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt; explain partitions</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; select count<span style="font-weight: bold; color: #006600">(</span>*<span style="font-weight: bold; color: #006600">)</span> from part_date1 where</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> c3&gt; date <span style="color: #cc0000">'1995-01-01'</span> and c3 &lt;date <span style="color: #cc0000">'1995-12-31'</span>\G</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> *************************** <span style="color: #800000">1</span>. row ***************************</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>id: <span style="color: #800000">1</span></div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> select_type: SIMPLE</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> table: part_date1</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr>partitions: p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>type: ALL</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> possible_keys: NULL</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> key: NULL</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> key_len: NULL</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> ref: NULL</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>rows: <span style="color: #800000">8100000</span></div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> Extra: Using where</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> <span style="color: #800000">1</span> row in set <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">0</span>.<span style="color: #800000">00</span> sec<span style="font-weight: bold; color: #006600">)</span></div> </li></ol> </div> </div> </div> <p>&nbsp;<wbr></p> <p>万恶的mysql居然对上面的sql使用全表扫描，而不是按照我们的日期分区分块查询。原文中解释到<a href="http://www.ooso.net/index.php/archives/category/mysql/">MYSQL</a>的优化器并不认这种日期形式的分区，花了大量的篇幅来引诱俺走上歧路，过分。</p> <h3>正确的日期分区例子</h3> <p>mysql优化器支持以下两种内置的日期函数进行分区：</p> <ul><li>TO_DAYS()</li><li>YEAR()</li></ul> <p>看个例子：</p> <div><a href="http://www.ooso.net/index.php/archives/217#"><strong>PLAIN TEXT</strong></a></div> <div><strong>CODE:</strong> <div> <div> <ol><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt; CREATE TABLE part_date3</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> <span style="font-weight: bold; color: #006600">(</span>&nbsp;<wbr> c1 int default NULL,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> c2 varchar<span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">30</span><span style="font-weight: bold; color: #006600">)</span> default NULL,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> c3 date default NULL<span style="font-weight: bold; color: #006600">)</span> engine=myisam</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> partition by range <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span>c3<span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span></div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; <span style="font-weight: bold; color: #006600">(</span>PARTITION p0 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'1995-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span>,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p1 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'1996-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p2 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'1997-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p3 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'1998-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p4 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'1999-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p5 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2000-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p6 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2001-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p7 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2002-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p8 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2003-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p9 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2004-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span> ,</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p10 VALUES LESS THAN <span style="font-weight: bold; color: #006600">(</span>to_days<span style="font-weight: bold; color: #006600">(</span><span style="color: #cc0000">'2010-01-01'</span><span style="font-weight: bold; color: #006600">)</span><span style="font-weight: bold; color: #006600">)</span>,</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; PARTITION p11 VALUES LESS THAN MAXVALUE <span style="font-weight: bold; color: #006600">)</span>;</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> Query OK, <span style="color: #800000">0</span> rows affected <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">0</span>.<span style="color: #800000">00</span> sec<span style="font-weight: bold; color: #006600">)</span></div> </li></ol> </div> </div> </div> <p>&nbsp;<wbr></p> <p>以to_days()函数分区成功，我们分析一下看看：</p> <div><a href="http://www.ooso.net/index.php/archives/217#"><strong>PLAIN TEXT</strong></a></div> <div><strong>CODE:</strong> <div> <div> <ol><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt; explain partitions</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt; select count<span style="font-weight: bold; color: #006600">(</span>*<span style="font-weight: bold; color: #006600">)</span> from part_date3 where</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> c3&gt; date <span style="color: #cc0000">'1995-01-01'</span> and c3 &lt;date <span style="color: #cc0000">'1995-12-31'</span>\G</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> *************************** <span style="color: #800000">1</span>. row ***************************</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>id: <span style="color: #800000">1</span></div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> select_type: SIMPLE</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> table: part_date3</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr>partitions: p1</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>type: ALL</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> possible_keys: NULL</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> key: NULL</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> key_len: NULL</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> ref: NULL</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr>rows: <span style="color: #800000">808431</span></div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> Extra: Using where</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> <span style="color: #800000">1</span> row in set <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">0</span>.<span style="color: #800000">00</span> sec<span style="font-weight: bold; color: #006600">)</span></div> </li></ol> </div> </div> </div> <p>&nbsp;<wbr></p> <p>可以看到，<a href="http://www.ooso.net/index.php/archives/category/mysql/">mysql</a>优化器这次不负众望，仅仅在p1分区进行查询。在这种情况下查询，真的能够带来提升查询效率么？下面分别对这次建立的part_date3和之前分区失败的part_date1做一个查询对比：</p> <div><a href="http://www.ooso.net/index.php/archives/217#"><strong>PLAIN TEXT</strong></a></div> <div><strong>CODE:</strong> <div> <div> <ol><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt; select count<span style="font-weight: bold; color: #006600">(</span>*<span style="font-weight: bold; color: #006600">)</span> from part_date3 where</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> c3&gt; date <span style="color: #cc0000">'1995-01-01'</span> and c3 &lt;date <span style="color: #cc0000">'1995-12-31'</span>;</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> +----------+</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> | count<span style="font-weight: bold; color: #006600">(</span>*<span style="font-weight: bold; color: #006600">)</span> |</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> +----------+</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> |&nbsp;<wbr> &nbsp;<wbr><span style="color: #800000">805114</span> |</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> +----------+</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> <span style="color: #800000">1</span> row in set <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">4</span>.<span style="color: #800000">11</span> sec<span style="font-weight: bold; color: #006600">)</span></div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr></div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> mysql&gt; select count<span style="font-weight: bold; color: #006600">(</span>*<span style="font-weight: bold; color: #006600">)</span> from part_date1 where</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> &nbsp;<wbr> &nbsp;<wbr> -&gt;&nbsp;<wbr> &nbsp;<wbr> &nbsp;<wbr> c3&gt; date <span style="color: #cc0000">'1995-01-01'</span> and c3 &lt;date <span style="color: #cc0000">'1995-12-31'</span>;</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> +----------+</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> | count<span style="font-weight: bold; color: #006600">(</span>*<span style="font-weight: bold; color: #006600">)</span> |</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> +----------+</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> |&nbsp;<wbr> &nbsp;<wbr><span style="color: #800000">805114</span> |</div> </li><li style="font-weight: bold; color: #26536a"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> +----------+</div> </li><li style="font-weight: normal; color: #3a6a8b; font-style: normal; font-family: -Courier new-, Courier, monospace"> <div style="font-weight: normal; font-family: -Courier new-, Courier, monospace"> <span style="color: #800000">1</span> row in set <span style="font-weight: bold; color: #006600">(</span><span style="color: #800000">40</span>.<span style="color: #800000">33</span> sec<span style="font-weight: bold; color: #006600">)</span></div> </li></ol> </div> </div> </div> <p>&nbsp;<wbr></p> <p> 可以看到，分区正确的话query花费时间为4秒，而分区错误则花费时间40秒（相当于没有分区），效率有90％的提升！所以我们千万要正确的使用分区功能，分区后务必用explain验证，这样才能获得真正的性能提升。</p> <p><br /></p> <p>注意：</p> <p>在mysql5.1中建立分区表的语句中，只能包含下列函数：<br /> ABS()<br /> CEILING() and FLOOR() （在使用这2个函数的建立分区表的前提是使用函数的分区键是INT类型），例如</p> <div style="padding-bottom: 0px;"> <div> <pre style="font-family: monospace;">mysql<span style="color: #cc0099;">&gt;</span> <span style="font-weight: bold; color: #990099;">CREATE</span> <span style="font-weight: bold; color: #990099;">TABLE</span> t <span style="color: #ff00ff;">(</span>c <span style="font-weight: bold; color: #999900;">FLOAT</span><span style="color: #ff00ff;">)</span> PARTITION BY LIST<span style="color: #ff00ff;">(</span> <span style="color: #000099;">FLOOR</span><span style="color: #ff00ff;">(</span>c<span style="color: #ff00ff;">)</span> <span style="color: #ff00ff;">)</span><span style="color: #ff00ff;">(</span>     <span style="color: #cc0099;">-&gt;</span> PARTITION p0 <span style="font-weight: bold; color: #990099;">VALUES</span> <span style="font-weight: bold; color: #990099;">IN</span> <span style="color: #ff00ff;">(</span><span style="color: #008080;">1</span><span style="color: #000033;">,</span><span style="color: #008080;">3</span><span style="color: #000033;">,</span><span style="color: #008080;">5</span><span style="color: #ff00ff;">)</span><span style="color: #000033;">,</span>     <span style="color: #cc0099;">-&gt;</span> PARTITION p1 <span style="font-weight: bold; color: #990099;">VALUES</span> <span style="font-weight: bold; color: #990099;">IN</span> <span style="color: #ff00ff;">(</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span><span style="color: #008080;">4</span><span style="color: #000033;">,</span><span style="color: #008080;">6</span><span style="color: #ff00ff;">)</span>     <span style="color: #cc0099;">-&gt;</span> <span style="color: #ff00ff;">)</span><span style="color: #000033;">;;</span> ERROR <span style="color: #008080;">1491</span> <span style="color: #ff00ff;">(</span>HY000<span style="color: #ff00ff;">)</span>: The PARTITION <span style="font-weight: bold; color: #990099;">function</span> <span style="font-weight: bold; color: #990099;">returns</span> the wrong <span style="font-weight: bold; color: #990099;">type</span> &nbsp;<wbr> mysql<span style="color: #cc0099;">&gt;</span> <span style="font-weight: bold; color: #990099;">CREATE</span> <span style="font-weight: bold; color: #990099;">TABLE</span> t <span style="color: #ff00ff;">(</span>c <span style="font-weight: bold; color: #999900;">int</span><span style="color: #ff00ff;">)</span> PARTITION BY LIST<span style="color: #ff00ff;">(</span> <span style="color: #000099;">FLOOR</span><span style="color: #ff00ff;">(</span>c<span style="color: #ff00ff;">)</span> <span style="color: #ff00ff;">)</span><span style="color: #ff00ff;">(</span>     <span style="color: #cc0099;">-&gt;</span> PARTITION p0 <span style="font-weight: bold; color: #990099;">VALUES</span> <span style="font-weight: bold; color: #990099;">IN</span> <span style="color: #ff00ff;">(</span><span style="color: #008080;">1</span><span style="color: #000033;">,</span><span style="color: #008080;">3</span><span style="color: #000033;">,</span><span style="color: #008080;">5</span><span style="color: #ff00ff;">)</span><span style="color: #000033;">,</span>     <span style="color: #cc0099;">-&gt;</span> PARTITION p1 <span style="font-weight: bold; color: #990099;">VALUES</span> <span style="font-weight: bold; color: #990099;">IN</span> <span style="color: #ff00ff;">(</span><span style="color: #008080;">2</span><span style="color: #000033;">,</span><span style="color: #008080;">4</span><span style="color: #000033;">,</span><span style="color: #008080;">6</span><span style="color: #ff00ff;">)</span>     <span style="color: #cc0099;">-&gt;</span> <span style="color: #ff00ff;">)</span><span style="color: #000033;">;</span> Query OK<span style="color: #000033;">,</span> <span style="color: #008080;">0</span> rows affected <span style="color: #ff00ff;">(</span><span style="color: #008080;">0.01</span> sec<span style="color: #ff00ff;">)</span> </pre></div> </div> <p>DAY()<br /> DAYOFMONTH()<br /> DAYOFWEEK()<br /> DAYOFYEAR()<br /> DATEDIFF()<br /> EXTRACT()<br /> HOUR()<br /> MICROSECOND()<br /> MINUTE()<br /> MOD()<br /> MONTH()<br /> QUARTER()<br /> SECOND()<br /> TIME_TO_SEC()<br /> TO_DAYS()<br /> WEEKDAY()<br /> YEAR()<br /> YEARWEEK()</p>							 		</div></div><img src ="http://www.blogjava.net/wangxinsh55/aggbug/430822.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxinsh55/" target="_blank">SIMONE</a> 2016-06-07 18:06 <a href="http://www.blogjava.net/wangxinsh55/archive/2016/06/07/430822.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【MYSQL数据库】MYSQL学习笔记-mysql分区基本操作</title><link>http://www.blogjava.net/wangxinsh55/archive/2013/02/22/395592.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Fri, 22 Feb 2013 09:43:00 GMT</pubDate><guid>http://www.blogjava.net/wangxinsh55/archive/2013/02/22/395592.html</guid><wfw:comment>http://www.blogjava.net/wangxinsh55/comments/395592.html</wfw:comment><comments>http://www.blogjava.net/wangxinsh55/archive/2013/02/22/395592.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxinsh55/comments/commentRss/395592.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxinsh55/services/trackbacks/395592.html</trackback:ping><description><![CDATA[<div>http://www.0773linji.com/web/mysql_partitions.html</div><br /><div><div>                         	<p> 	【MySQL使用分区表的好处】<br /> 	1.可以把一些归类的数据放在一个分区中，可以减少服务器检查数据的数量加快查询。<br /> 	2.方便维护，通过删除分区来删除老的数据。<br /> 	3.分区数据可以被分布到不同的物理位置，可以做分布式有效利用多个硬盘驱动器。</p> <p> 	【MySQL可以建立四种分区类型的分区】<br /> 	RANGE 分区：基于属于一个给定连续区间的列值，把多行分配给分区。<br /> 	LIST 分区：类似于按RANGE分区，区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择。<br /> 	HASH分区：基于用户定义的表达式的返回值来进行选择的分区，该表达式使用将要插入到表中的这些行的列值进行计算。这个函数可以包含MySQL 中有效的、产生非负整数值的任何表达式。<br /> 	KEY 分区：类似于按HASH分区，区别在于KEY分区只支持计算一列或多列，且MySQL 服务器提供其自身的哈希函数。必须有一列或多列包含整数值。<br /> 	一般用得多的是range分区和list分区。</p> <p> 	<br /> 	【查看是否支持分区】</p> <blockquote> 	<p> 		show variables like "%part%";</p> </blockquote> <p> 	【如何查看mysql分区信息】</p> <blockquote> 	<p> 		select * from INFORMATION_SCHEMA.PARTITIONS where table_name='user'; （这里查看user表的分区信息）</p> </blockquote> <p> 	【查看执行一条查询SQL会扫描的分区】</p> <blockquote> 	<p> 		explain partitions select * from user where id=10;</p> </blockquote> <p> 	【如何创建分区】<br /> 	1.建表时就分区：</p> <blockquote> 	<p> 		CREATE TABLE IF NOT EXISTS `user` (&nbsp;<br /> 		&nbsp;&nbsp; `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',&nbsp;<br /> 		&nbsp;&nbsp; `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名称',&nbsp;<br /> 		&nbsp;&nbsp; `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0为男，1为女',&nbsp;<br /> 		&nbsp;&nbsp; PRIMARY KEY (`id`)&nbsp;<br /> 		) ENGINE=MyISAM&nbsp; DEFAULT CHARSET=utf8 AUTO_INCREMENT=1&nbsp;<br /> 		PARTITION BY RANGE (id) (&nbsp;<br /> 		&nbsp;&nbsp;&nbsp;&nbsp; PARTITION p0 VALUES LESS THAN (3),&nbsp;<br /> 		&nbsp;&nbsp;&nbsp;&nbsp; PARTITION p1 VALUES LESS THAN (6),&nbsp;<br /> 		&nbsp;&nbsp;&nbsp;&nbsp; PARTITION p2 VALUES LESS THAN (9),&nbsp;<br /> 		&nbsp;&nbsp;&nbsp;&nbsp; PARTITION p3 VALUES LESS THAN (12),&nbsp;<br /> 		&nbsp;&nbsp;&nbsp;&nbsp; PARTITION p4 VALUES LESS THAN MAXVALUE&nbsp;<br /> 		);</p> </blockquote> <p> 	2.对现有表进行分区：可以对现有表进行分区,并且会按規则自动的将表中的数据分配相应的分区中</p> <blockquote> 	<p> 		alter table 表名 partition by RANGE(字段)&nbsp;<br /> 		(PARTITION p1 VALUES less than (1),&nbsp;<br /> 		PARTITION p2 VALUES less than (5),&nbsp;<br /> 		PARTITION p3 VALUES less than MAXVALUE);</p> </blockquote> <p> 	【删除一个分区】<br /> 	（当删除了一个分区，也同时删除了该分区中所有的数据）<br /> 	ALTER TABLE 表名 DROP PARTITION p2;</p> <p> 	【分区合并】<br /> 	下面的SQL，将p201001 - p201009 合并为3个分区p2010Q1 - p2010Q3</p> <blockquote> 	<p> 		ALTER TABLE sale_data<br /> 		&nbsp;&nbsp;&nbsp; REORGANIZE PARTITION p201001,p201002,p201003,<br /> 		&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; p201004,p201005,p201006,<br /> 		&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; p201007,p201008,p201009 INTO<br /> 		&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; (<br /> 		&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; PARTITION p2010Q1 VALUES LESS THAN (201004),<br /> 		&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; PARTITION p2010Q2 VALUES LESS THAN (201007),<br /> 		&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; PARTITION p2010Q3 VALUES LESS THAN (201010)<br /> 		&nbsp;<br /> 		&nbsp;&nbsp; );</p> </blockquote> <p> 	【分区的拆分】<br /> 	下面的SQL，将p2010Q1 分区，拆分为s2009 与s2010 两个分区</p> <blockquote> 	<p> 		ALTER TABLE sale_data REORGANIZE PARTITION p2010Q1 INTO (<br /> 		&nbsp;<br /> 		&nbsp;&nbsp; PARTITION s2009 VALUES LESS THAN (201001),<br /> 		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> 		&nbsp;&nbsp;&nbsp; PARTITION s2010 VALUES LESS THAN (201004)<br /> 		&nbsp;<br /> 		&nbsp;&nbsp; );</p> </blockquote> <p> 	【分区索引的局限】<br /> 	1.所有分区都要使用同样的引擎。<br /> 	2.分区表的每一个唯一索引必须包含由分区函数引用的列。<br /> 	3.mysql能避免查询所有的分区，但仍然锁定了所有分区。<br /> 	4.分区函数能使用的函数和表达式有限，例如函数有上面的4种。<br /> 	5.分区不支持外键。<br /> 	6.不能使用LOAD INDEX INTO CACHE<br /> 	7.分区并不能总是改善性能，要进行性能评测。</p>                          </div></div><img src ="http://www.blogjava.net/wangxinsh55/aggbug/395592.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxinsh55/" target="_blank">SIMONE</a> 2013-02-22 17:43 <a href="http://www.blogjava.net/wangxinsh55/archive/2013/02/22/395592.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ubuntu 11.10 下安装mysql5.5.17 (个人安装过程)</title><link>http://www.blogjava.net/wangxinsh55/archive/2011/10/25/361995.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Tue, 25 Oct 2011 06:16:00 GMT</pubDate><guid>http://www.blogjava.net/wangxinsh55/archive/2011/10/25/361995.html</guid><wfw:comment>http://www.blogjava.net/wangxinsh55/comments/361995.html</wfw:comment><comments>http://www.blogjava.net/wangxinsh55/archive/2011/10/25/361995.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxinsh55/comments/commentRss/361995.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxinsh55/services/trackbacks/361995.html</trackback:ping><description><![CDATA[首先我用ubuntu的软件中心通过界面安装MySQL，安装后发现其版本比较低5.1的版本，目前MySQL的版本是5.5.17，想使用最新的MySQL版本；所以又将其卸载了。<br />开始我卸载是直接使用软件中心的界面&#8220;卸载&#8221;按扭来卸载的。由于使用界面的&#8220;卸载&#8221;按钮卸载，卸载的不干净；在后边的安装过程中引起了不小的麻烦。<br />所以必须使用下边的命令来完成卸载：<br /><div></div><div><strong>删除 mysql</strong><br />sudo apt-get autoremove --purge mysql-server-5.0<br />sudo apt-get remove mysql-server<br />sudo apt-get autoremove mysql-server<br /><span style="color: red;">sudo apt-get remove mysql-common (非常重要)</span><br />上面的其实有一些是多余的，建议还是按照顺序执行一遍<br /><br /><strong style="color: red;">清理残留数据</strong><br style="color: red;" />dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P</div><br />上边的操作来自http://www.blogjava.net/yjhmily/articles/336926.html这个站点<br /><br />=====================================================================<br />清理工作完成之后，下边开始安装：<br />我参考的地址列表<br />http://dev.mysql.com/doc/refman/5.5/en/binary-installation.html<br /><div>http://forum.ubuntu.org.cn/viewtopic.php?t=301214</div><div><div>http://www.cnblogs.com/wuhou/archive/2008/09/28/1301071.html</div><br />下边的是我的操作：<br /><div><div>groupadd mysql<br />useradd -r -g mysql mysql<br />tar zxvf /path/to/mysql-VERSION-OS.tar.gz<br />#将下载到的二进制MySQL文件解压到/opt/mysql_5_5_17目录下，即将MySQL安装到/opt/mysql_5_5_17目录下。网上好多教程都是安装到/usr/local/mysql目录下，我这里做了修改<br />cp /path/to/mysql-VERSION-OS.tar.gz /opt/mysql_5_5_17<br />#将当前目录的所有者改为mysql用户&nbsp; 要加上-R参数。这个参数表示循环遍历子节点。子节点都起作用<br />chown -R mysql .<br />#将当前目录的所有组改为mysql组<br />chgrp -R mysql .<br />#这里的--basedir=/opt/mysql_5_5_17参数是我参考其他站点加上的；如果安装到/usr/local/mysql应该不用加这个参数<br />scripts/mysql_install_db --user=mysql --basedir=/opt/mysql_5_5_17<br />chown -R root .<br />chown -R mysql data<br />#关于my.cnf：mysql按照下列顺序搜索my.cnf:/etc,mysql安装目录，安装目录下的data。/etc下的是全局设置。<br />#网上一些教程说放到/etc/my.cnf下；我这里放到/opt/mysql_5_5_17/my.cnf下了<br />cp support-files/my-medium.cnf ./my.cnf<br />#修改mysql最大连接数增加或修改max_connections=1024<br />#[mysqld]处添加服务器的默认字符编码character-set-server=utf8<br />#[mysql]处添加默认字符编码default-character-set=utf8<br />#编辑完保存退出<br />vim my.cnf<br />#启动MySQL<br />bin/mysqld_safe --user=mysql &amp;<br />#让程序随服务器启动而自动启动<br />cp support-files/mysql.server /etc/init.d/mysql<br /><div>cd /etc/init.d<br />#编辑mysql.server文件，修改MySQL默认的安装路径，即修改basedir值：basedir=/opt/mysql_5_5_17</div>#如果要修改数据目录，也可修改datadir路径值<br />vim mysql<br /><div>chmod +x /etc/init.d/mysql<br />update-rc.d mysql defaults </div><div>关于Ubuntu自动启动见<a href="http://wangyan.org/blog/ubuntu-update-rc-d.html">《Ubuntu系统update-rc.d命令详解》</a>，官方文档见<a href="http://dev.mysql.com/doc/refman/5.1/zh/installing.html#automatic-start">自动启动和停止 MySQL</a>。</div><br /><br /><br />至此启动MySQL方法：<br />bin/mysqld_safe --user=mysql &amp;<br />或<br />/etc/init.d/mysql start<br /><br />关闭MySQL的方法<br />bin/mysqladmin -uroot shutdown<br />或<br />/etc/init.d/mysql stop</div></div></div><img src ="http://www.blogjava.net/wangxinsh55/aggbug/361995.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxinsh55/" target="_blank">SIMONE</a> 2011-10-25 14:16 <a href="http://www.blogjava.net/wangxinsh55/archive/2011/10/25/361995.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ubuntu 11.10 下安装mysql5.5.17</title><link>http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361913.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Mon, 24 Oct 2011 11:14:00 GMT</pubDate><guid>http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361913.html</guid><wfw:comment>http://www.blogjava.net/wangxinsh55/comments/361913.html</wfw:comment><comments>http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361913.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxinsh55/comments/commentRss/361913.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxinsh55/services/trackbacks/361913.html</trackback:ping><description><![CDATA[<div><span style="font-size: medium">下载最新的mysql-5.5.10-linux2.6-i686.tar.gz <br />
<br />
shell切换到root用户 <br />
<br />
安装在/usr/local目录下面 <br />
解压缩文件 <br />
$cd /usr/local <br />
$tar zxvf path/mysql-5.5.10-linux2.6-i686.tar.gz <br />
$ln -s mysql-5.5.10-linux2.6-i686 mysql <br />
添加用户组 <br />
$groupadd mysql <br />
给用户组添加用户 <br />
$useradd -r -g mysql mysql <br />
给文件夹赋予用户权限 <br />
$cd mysql <br />
$chown -R mysql . (后面有点) <br />
$chgrp -R mysql . <br />
安装数据库 <br />
$scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql <br />
<br />
&nbsp; 注释 如果这一步遇见error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory <br />
请给系统安装libaio-dev <br />
$apt-get install libaio-dev <br />
安装完成之后再次执行数据库安装命令 <br />
<br />
$chown -R root . <br />
$chown -R mysql data <br />
$cp support-files/my-medium.cnf /etc/my.cnf <br />
启动数据库 <br />
$bin/mysqld_safe --user=mysql &amp; <br />
<br />
进入数据库管理命令 <br />
$bin/mysql -u -root -p <br />
<br />
自动启动 <br />
$cp support-files/mysql.server&nbsp; /etc/init.d/mysql <br />
以后启动直接输入 <br />
$/etc/init.d/mysql restart|start，，</span></div>
<br />
<br />
=============================================================<br />
<div><strong>使用二进制安装的好处</strong>
<p>mysql安装为什么大部分使用二进制安装。而不是源码手动去编译？<br />
<br />
mysql使用的glibc进行开发的。glibc库是一个底层api,所以只要是linux，都会有glibc库。所以，mysql安装不需要考虑环境是否符合要求。移植性很方便。直接将编译好的二进制代码复制到另外一个机器上，也是可以用的。<br />
<br />
原 理性东西：一般在linux下安装一个软件，不同软件所需要的函数库不同。于是会遇到一个现象:一个软件复制到另外一个平台可能无法运行。安装软件需要先 侦测环境就是出于此考虑。因为mysql使用的是通用的glibc函数库。没有对其他东西的依赖性。所以，从一个平台复制到另外一个平台能够通用。并不需 要去侦测环境。直接使用编译好的二进制即可<br />
<br />
手动编译安装，显得步骤麻烦。基于mysql的特点,二进制安装完全可以。</p>
<p>注:mysql5.5安装的时候，需要用到cmake命令，所以需要保证你的机器上安装了该命令。没有的话，还要去安装该cmake。也显得麻烦。我暂时不想去安装最新版本的了。<br />
<br />
<br />
如果使用二进制安装升级怎么进行？<br />
<br />
升级获取的还是二进制压缩包。那么，直接将指向目录切换到新的目录即可。<br />
<br />
不需要涉及到像php一样，手动编译可以达到自己定制模块的好处。而mysql安装的时候不需要涉及到模块<br />
<br />
mysql官方建议安装方式：二进制安装。</p>
<p><strong>实践过程</strong></p>
<p>大致思路就是：解压二进制文件放到指定的目录。然后安装基本的数据库,权限方面会用到。<br />
然后，启动mysql就行了。<br />
<br />
第一步：创建一个用户组和用户。<br />
<br />
组名和用户名的名字都为mysql。<br />
<br />
命令：1.groupadd mysql<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2.useradd -r -g mysql mysql<br />
<br />
<br />
第二步:解压二进制压缩包。<br />
一般放到"/usr/local/src"目录下去。<br />
结果会变成:/usr/local/src/mysql安装目录<br />
<br />
有很多种方式。一般可以直接进去要解压要存放的目录。<br />
运行命令:tar zxvf 压缩包源&nbsp; 注释：第二个参数可以填要解压到的位置。第二个参数不填，就会将压缩包源解压到运行tar命令的当前目录。一般都是使用这种特性进行解压:你要解压到哪里就先进入哪里运行tar命令。<br />
<br />
<br />
<br />
<br />
第三步:建立一个软链接。<br />
<br />
在"usr/local"下建立一个软链接(自己定义名为mysql)。软链接的目录是为了指向"/usr/local/src/mysql安装目录"<br />
<br />
命令：ln -s /usr/local/src/mysql安装目录 /usr/local/mysql&nbsp;&nbsp;&nbsp; 参数说明：-s参数表示建立软链接<br />
<br />
去local目录下查看，看是否成功生成了一个软链接。使用"ls -al"查看，特点是软连接上会有个箭头符号。<br />
<br />
其实这一步就是为了提供一种快捷操作的方式。其实不建立软连接也是可以用的，为什么要这样做？<br />
升级管理有用：新版本单独建立一个目录存放的。假如你升级mysql了。只要将这个软连接指向的目录修改即可。<br />
<br />
<br />
第四步:权限检查与修改<br />
主要是保证"mysql安装目录"的所有者为mysql用户，所属组也为mysql组。<br />
<br />
1.chown -R mysql . //将当前目录的所有者改为mysql用户&nbsp; 要加上-R参数。这个参数表示循环遍历子节点。子节点都起作用<br />
2.chgrp -R mysql . //将当前目录的所有组改为mysql组<br />
<br />
常见的问题：忘记加上-R参数。结果下面节点没有权限。启动错误，访问错误等等一系列的问题。<br />
<br />
<br />
<br />
<br />
第五步：安装初始化数据库<br />
运行mysql安装目录下的文件:/scripts/mysql_install_db<br />
<br />
命令备忘：scripts/mysql_install_db --user=mysql //这是进入mysql安装目录下进行运行的，所以需要输入路径"scripts/"<br />
注意:必须在安装目录下执行。不能切换到scripts目录下，然后执行"./mysql_install_db --user=mysql",否则会报如下错误：</p>
<p>FATAL ERROR: Could not find ./bin/my_print_defaults</p>
<p>因为脚本mysql_install_db里面所使用的是这种形式的相对路径"./bin/my_print_defaults",所以必须是在"mysql安装目录"下，才能找得到所需文件。</p>
<p>第二次试验的时候，还报出一个错误：./bin/my_print_defaults: cannot execute binary file<br />
<br />
<br />
<br />
<br />
第六步：my.cnf配置文件拷贝<br />
将my.cnf文件移到etc/目录下去。<br />
命令：cp support-files/my-medium.cnf /etc/my.cnf<br />
<br />
<br />
<br />
<br />
<br />
<br />
第七步：启动mysql<br />
<br />
<br />
马上启动mysql的命令是：bin/mysqld_safe --user=mysql &amp; //"&amp;"表示在后台运行<br />
这里使用&amp;的经验备忘：执行上面命令后，一直定在一个地方。没有任何反应了。并没有报错信息。还是成功的。此时使用ctrl+c。然后去看看<br />
<br />
<br />
之后，想让服务器启动的时候就跟着启动mysql服务器，在一个目录下放一个文件：<br />
<br />
cp support-files/mysql.server /etc/init.d/mysql.server<br />
support-files/mysql.server在二进制安装目录下可以找到的。<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
完成后进行的步骤：<br />
第一步：如何查看mysql是否安装成功。<br />
一个方法是：去查看3306端口是否开启<br />
命令：netstat -ntl&nbsp; 可以列出所有侦听的端口<br />
<br />
第二个方法:使用mysql命令连接一次服务器<br />
命令: mysql -u root</p>
<p>小技巧：为了省去每次执行客户端都要进入目录的麻烦。将客户端的路径加入环境变量中去。<br />
<br />
shell命令：export PATH=$PATH:/usr/local/src/mysql-5.1/bin/</p>
<p>export命令备忘: export 变量名=变量值，这里变量名是PATH 变量值设置成在原来的$PATH基础上加,有多个值,那么值之间以分号":"隔开。<br />
这样，在任意路径中。都可以通过:"mysql -u 用户名 -p" 的形式使用客户端了。</p>
<p><strong>所遇问题总结</strong></p>
<p>第一个问题：<br />
<br />
问题描述：SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/usr/local/mysql/tmp/mysql.sock'<br />
<br />
<br />
问题经验：<br />
<br />
不能通过 '/tmp/mysql.sock'连到服务器，而php标准配置正是用过'/tmp/mysql.sock'<br />
<br />
1.php.ini中可以配置通过哪个sock去连接mysql<br />
2.而mysql的配置文件中也可以自由配置使用哪个位置的sock文件<br />
<br />
错误提示中显示了错误2002号。结合手册如下说明：<br />
<br />
错误：2002 (CR_CONNECTION_ERROR) <br />
<br />
消息：不能通过套接字'%s' (%d)连接到本地MySQL服务器。<br />
<br />
<br />
第二个问题：权限问题。<br />
<br />
解决问题的经验是：如果遇到启动mysql错误。首先应该去看看权限是否有。设置对了没。<br />
<br />
比如启动使用"service mysqld start"常常看到如下错误提示：<br />
<br />
mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdomain.err'.<br />
110306 23:17:20 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data<br />
110306 23:17:20 mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended<br />
<br />
看不懂意思。这些信息会在data/*.err文件中记录的。不知道上述报错信息的原因。<br />
<br />
查看目录下的子节点，发现所有者和所属组并不都是mysql。我直接设置好目录的所有者和所属组都是mysql后。就行了。</p>
</div>
<br />
====================================================================<br />
个人测试成功过程：<br />
<div>
<pre>shell&gt; <strong><code>groupadd mysql</code></strong> <br />shell&gt; <strong><code>useradd -r -g mysql mysql</code></strong> <br />shell&gt; <strong><code>cd /usr/local</code></strong> <br />shell&gt; <strong><code>tar zxvf <em><code>/path/to/mysql-VERSION-OS</code></em>.tar.gz</code></strong> <br />shell&gt; <strong><code>ln -s <em><code>full-path-to-mysql-VERSION-OS</code></em> mysql</code></strong> <br />shell&gt; <strong><code>cd mysql</code></strong> <br />shell&gt; <strong><code>chown -R mysql .</code></strong> <br />shell&gt; <strong><code>chgrp -R mysql .</code></strong> <br />shell&gt; <strong><code>scripts/mysql_install_db </code></strong><span style="color: red;">--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data</span><strong><code> --user=mysql</code></strong> <br />shell&gt; <strong><code>chown -R root .</code></strong> <br />shell&gt; <strong><code>chown -R mysql data</code></strong> <br /># Next command is optional <br />shell&gt; <strong><code>cp support-files/my-medium.cnf /etc/my.cnf</code></strong> <br />shell&gt; <strong><code>bin/mysqld_safe </code></strong>--basedir=/usr/local/mysql --datadir=/usr/local/mysql/data<strong><code> --user=mysql &amp;</code></strong> <br /># Next command is optional <br />shell&gt; <strong><code>cp support-files/mysql.server /etc/init.d/mysql.server<br />
</code></strong></pre>
</div>
<div>
<pre><div>
启动后的日志写在/var/log/mysql/error.log文件下，可直接到这个文件下去查看。
</div>
</pre>
</div><img src ="http://www.blogjava.net/wangxinsh55/aggbug/361913.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxinsh55/" target="_blank">SIMONE</a> 2011-10-24 19:14 <a href="http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361913.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql数据库备份及恢复</title><link>http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361874.html</link><dc:creator>SIMONE</dc:creator><author>SIMONE</author><pubDate>Mon, 24 Oct 2011 03:46:00 GMT</pubDate><guid>http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361874.html</guid><wfw:comment>http://www.blogjava.net/wangxinsh55/comments/361874.html</wfw:comment><comments>http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361874.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangxinsh55/comments/commentRss/361874.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangxinsh55/services/trackbacks/361874.html</trackback:ping><description><![CDATA[<div><p><span style="color: #000000;">还原一个数据库:mysql -h localhost -u root -p123456 www</span></p> <p><span style="color: #000000;">备份一个数据库:mysqldump -h localhost -u root -p123456 www &gt; d:\www2008-2-26.sql</span></p> <p><span style="color: #000000;">其中WWW为数据库名</span></p> <p><span style="color: #000000;">//以下是在程序中进行测试</span></p> <p><span style="color: #000000;">//$command = &#8220;mysqldump &#8211;opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip &gt; $backupFile&#8221;;</span><br /> 	<span style="color: #000000;">$command=&#8221;mysqldump -h localhost -u root -p123456 guestbook &gt; guestbook2-29.sql&#8221;;</span><br /> 	<span style="color: #000000;">system($command);</span><br /> 	<span style="color: #000000;">echo &#8220;success&#8221;;</span></p> <p><span style="color: #000000;">************************************************</span></p> <p><span style="color: #000000;">备份MySQL数据库的命令</span></p> <p><span style="color: #000000;">mysqldump -hhostname -uusername -ppassword databasename &gt; backupfile.sql</span><br /> 	<span style="color: #000000;">备份MySQL数据库为带删除表的格式</span><br /> 	<span style="color: #000000;">备份MySQL数据库为带删除表的格式，能够让该备份覆盖已有数据库而不需要手动删除原有数据库。</span></p> <p><span style="color: #000000;">mysqldump &#8211;add-drop-table -uusername -ppassword databasename &gt; backupfile.sql</span><br /> 	<span style="color: #000000;">直接将MySQL数据库压缩备份</span></p> <p><span style="color: #000000;">mysqldump -hhostname -uusername -ppassword databasename | gzip &gt; backupfile.sql.gz</span><br /> 	<span style="color: #000000;">备份MySQL数据库某个(些)表</span></p> <p><span style="color: #000000;">mysqldump -hhostname -uusername -ppassword databasename specific_table1 specific_table2 &gt; backupfile.sql</span><br /> 	<span style="color: #000000;">同时备份多个MySQL数据库</span></p> <p><span style="color: #000000;">mysqldump -hhostname -uusername -ppassword &#8211;databases databasename1 databasename2 databasename3 &gt; multibackupfile.sql</span><br /> 	<span style="color: #000000;">仅仅备份数据库结构</span></p> <p><span style="color: #000000;">mysqldump &#8211;no-data &#8211;databases databasename1 databasename2 databasename3 &gt; structurebackupfile.sql</span><br /> 	<span style="color: #000000;">备份服务器上所有数据库</span></p> <p><span style="color: #000000;">mysqldump &#8211;all-databases &gt; allbackupfile.sql</span><br /> 	<span style="color: #000000;">还原MySQL数据库的命令</span></p> <p><span style="color: #000000;">mysql -hhostname -uusername -ppassword databasename &lt; backupfile.sql</span><br /> 	<span style="color: #000000;">还原压缩的MySQL数据库</span></p> <p><span style="color: #000000;">gunzip &lt; backupfile.sql.gz | mysql -uusername -ppassword databasename</span><br /> 	<span style="color: #000000;">将数据库转移到新服务器</span></p> <p><span style="color: #000000;">mysqldump -uusername -ppassword databasename | mysql &#8211;host=*.*.*.* -C databasename</span></p> <p><span style="color: #000000;">几个常用用例：</span><br /> 	<span style="color: #000000;">1.导出整个数据库</span><br /> 	<span style="color: #000000;">mysqldump -u 用户名 -p 数据库名 &gt; 导出的文件名</span><br /> 	<span style="color: #000000;">mysqldump -u root -p dataname &gt;dataname.sql</span><br /> 	<span style="color: #000000;">这个时候会提示要你输入root用户名的密码,输入密码后dataname数据库就成功备份在mysql/bin/目录中.</span></p> <p><span style="color: #000000;">2.导出一个表</span><br /> 	<span style="color: #000000;">mysqldump -u 用户名 -p 数据库名 表名&gt; 导出的文件名</span><br /> 	<span style="color: #000000;">mysqldump -u root -p dataname users&gt; dataname_users.sql</span></p> <p><span style="color: #000000;">3.导出一个数据库结构</span><br /> 	<span style="color: #000000;">mysqldump -u wcnc -p -d &#8211;add-drop-table smgp_apps_wcnc &gt;d:\wcnc_db.sql</span><br /> 	<span style="color: #000000;">-d 没有数据 &#8211;add-drop-table 在每个create语句之前增加一个drop table</span></p> <p><span style="color: #000000;">4.导入数据库</span><br /> 	<span style="color: #000000;">常用source 命令</span><br /> 	<span style="color: #000000;">进入mysql数据库控制台，</span><br /> 	<span style="color: #000000;">如mysql -u root -p</span></p> <p><span style="color: #000000;">mysql&gt;use 数据库</span></p> <p><span style="color: #000000;">然后使用source命令，后面参数为脚本文件（如这里用到的.sql）</span><br /> 	<span style="color: #000000;">mysql&gt;source d:\wcnc_db.sql</span></p> <p><span style="color: #000000;">mysqldump支持下列选项：</span><br /> 	<span style="color: #000000;">&#8211;add-locks</span><br /> 	<span style="color: #000000;">在每个表导出之前增加LOCK TABLES并且之后UNLOCK TABLE。(为了使得更快地插入到MySQL)。</span></p> <p><span style="color: #000000;">&#8211;add-drop-table</span><br /> 	<span style="color: #000000;">在每个create语句之前增加一个drop table。</span></p> <p><span style="color: #000000;">&#8211;allow-keywords</span><br /> 	<span style="color: #000000;">允许创建是关键词的列名字。这由表名前缀于每个列名做到。</span></p> <p><span style="color: #000000;">-c, &#8211;complete-insert</span><br /> 	<span style="color: #000000;">使用完整的insert语句(用列名字)。</span></p> <p><span style="color: #000000;">-C, &#8211;compress</span><br /> 	<span style="color: #000000;">如果客户和服务器均支持压缩，压缩两者间所有的信息。</span></p> <p><span style="color: #000000;">&#8211;delayed</span><br /> 	<span style="color: #000000;">用INSERT DELAYED命令插入行。</span></p> <p><span style="color: #000000;">-e, &#8211;extended-insert</span><br /> 	<span style="color: #000000;">使用全新多行INSERT语法。（给出更紧缩并且更快的插入语句）</span></p> <p><span style="color: #000000;">-#, &#8211;debug[=option_string]</span><br /> 	<span style="color: #000000;">跟踪程序的使用(为了调试)。</span></p> <p><span style="color: #000000;">&#8211;help</span><br /> 	<span style="color: #000000;">显示一条帮助消息并且退出。</span></p> <p><span style="color: #000000;">&#8211;fields-terminated-by=&#8230;</span></p> <p><span style="color: #000000;">&#8211;fields-enclosed-by=&#8230;</span></p> <p><span style="color: #000000;">&#8211;fields-optionally-enclosed-by=&#8230;</span></p> <p><span style="color: #000000;">&#8211;fields-escaped-by=&#8230;</span></p> <p><span style="color: #000000;">&#8211;fields-terminated-by=&#8230;</span></p> <p><span style="color: #000000;">这些选择与-T选择一起使用，并且有相应的LOAD DATA INFILE子句相同的含义。</span><br /> 	<span style="color: #000000;">LOAD DATA INFILE语法。</span></p> <p><span style="color: #000000;">-F, &#8211;flush-logs</span><br /> 	<span style="color: #000000;">在开始导出前，洗掉在MySQL服务器中的日志文件。</span></p> <p><span style="color: #000000;">-f, &#8211;force,</span><br /> 	<span style="color: #000000;">即使我们在一个表导出期间得到一个SQL错误，继续。</span></p> <p><span style="color: #000000;">-h, &#8211;host=..</span><br /> 	<span style="color: #000000;">从命名的主机上的MySQL服务器导出数据。缺省主机是localhost。</span></p> <p><span style="color: #000000;">-l, &#8211;lock-tables.</span><br /> 	<span style="color: #000000;">为开始导出锁定所有表。</span></p> <p><span style="color: #000000;">-t, &#8211;no-create-info</span><br /> 	<span style="color: #000000;">不写入表创建信息(CREATE TABLE语句）</span></p> <p><span style="color: #000000;">-d, &#8211;no-data</span><br /> 	<span style="color: #000000;">不写入表的任何行信息。如果你只想得到一个表的结构的导出，这是很有用的！</span></p> <p><span style="color: #000000;">&#8211;opt</span><br /> 	<span style="color: #000000;">同&#8211;quick &#8211;add-drop-table &#8211;add-locks &#8211;extended-insert &#8211;lock-tables。</span><br /> 	<span style="color: #000000;">应该给你为读入一个MySQL服务器的尽可能最快的导出。</span></p> <p><span style="color: #000000;">-pyour_pass, &#8211;password[=your_pass]</span><br /> 	<span style="color: #000000;">与服务器连接时使用的口令。如果你不指定&#8220;=your_pass&#8221;部分，mysqldump需要来自终端的口令。</span></p> <p><span style="color: #000000;">-P port_num, &#8211;port=port_num</span><br /> 	<span style="color: #000000;">与一台主机连接时使用的TCP/IP端口号。（这用于连接到localhost以外的主机，因为它使用 Unix套接字。）</span></p> <p><span style="color: #000000;">-q, &#8211;quick</span><br /> 	<span style="color: #000000;">不缓冲查询，直接导出至stdout；使用mysql_use_result()做它。</span></p> <p><span style="color: #000000;">-S /path/to/socket, &#8211;socket=/path/to/socket</span><br /> 	<span style="color: #000000;">与localhost连接时（它是缺省主机)使用的套接字文件。</span></p> <p><span style="color: #000000;">-T, &#8211;tab=path-to-some-directory</span><br /> 	<span style="color: #000000;">对于每个给定的表，创建一个table_name.sql文件，它包含SQL  CREATE  命令，和一个table_name.txt文件，它包含数据。注意：这只有在mysqldump运行在mysqld守护进程运行的同一台机器上的时候才工 作。.txt文件的格式根据&#8211;fields-xxx和 &#8211;lines&#8211;xxx选项来定。</span></p> <p><span style="color: #000000;">-u user_name, &#8211;user=user_name</span><br /> 	<span style="color: #000000;">与服务器连接时，MySQL使用的用户名。缺省值是你的Unix登录名。</span></p> <p><span style="color: #000000;">-O var=option, &#8211;set-variable var=option</span><br /> 	<span style="color: #000000;">设置一个变量的值。可能的变量被列在下面。</span></p> <p><span style="color: #000000;">-v, &#8211;verbose</span><br /> 	<span style="color: #000000;">冗长模式。打印出程序所做的更多的信息。</span></p> <p><span style="color: #000000;">-V, &#8211;version</span><br /> 	<span style="color: #000000;">打印版本信息并且退出。</span></p> <p><span style="color: #000000;">-w, &#8211;where=&#8217;where-condition&#8217;</span><br /> 	<span style="color: #000000;">只导出被选择了的记录；注意引号是强制的！</span><br /> 	<span style="color: #000000;">&#8220;&#8211;where=user=&#8217;jimf&#8217;&#8221; &#8220;-wuserid&gt;1&#8243; &#8220;-wuserid&lt;1&#8243;</span></p> <p><span style="color: #000000;">最常见的mysqldump使用可能制作整个数据库的一个备份：</span><br /> 	<span style="color: #000000;">mysqldump &#8211;opt database &gt; backup-file.sql</span></p> <p><span style="color: #000000;">但是它对用来自于一个数据库的信息充实另外一个MySQL数据库也是有用的：</span><br /> 	<span style="color: #000000;">mysqldump &#8211;opt database | mysql &#8211;host=remote-host -C database</span><br /> 	<span style="color: #000000;">由于mysqldump导出的是完整的SQL语句，所以用mysql客户程序很容易就能把数据导入了：</span></p> <p><span style="color: #000000;">shell&gt; mysqladmin create target_db_name</span><br /> 	<span style="color: #000000;">shell&gt; mysql target_db_name &lt; backup-file.sql</span><br /> 	<span style="color: #000000;">就是</span><br /> 	<span style="color: #000000;">shell&gt; mysql 库名 &lt; 文件名</span> </p></div><br /><br /><div>最近这两天，因为之前设计<a href="http://www.2cto.com/database/" target="_blank">数据库</a>的 时候没有注意到有的表是要建在另外一台服务器上面的（测试服务器数据库是分布式的，不同的表可能在不同服务器上）。现在里面已经有很多测试数据了，又不想 重新添加一遍。所以就想能不能备份出来，再恢复到目标机器上去。然后昨天的话就折腾了一下，请教同事，上网查资料。这里把用法记录一下。<br /> 	<a href="http://www.2cto.com/database/mysql/" target="_blank">mysql</a>dump命令:MySQL数据库备份还原<br /> 	&nbsp;<br /> 	一、常用操作:<br /> 	备份整个数据库<br /> 	格式：<br /> 	mysqldump -h主机名&nbsp; -P端口 -u用户名 -p密码 (&#8211;database) 数据库名 &gt; 文件名.sql<br /> 	mysqldump -h{hostname} -P{port} -u{username} -p{password} {databasename} &gt; {backupfile.sql}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	mysqldump -hlocalhost -P3306 -uzhuchao -p123456 db_test &gt; backfile1.sql<br /> 	备份MySQL数据库为带删除表的格式<br /> 	备份MySQL数据库为带删除表的格式，能够让该备份覆盖已有数据库而不需要手动删除原有数据库。<br /> 	格式：mysqldump -&#8211;add-drop-table -u{username} -p{password} {databasename} &gt; {backfile.sql}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	mysqldump -&#8211;add-drop-table &#8211;uzhuchao -p123456 db_test &gt; backfile2.sql<br /> 	直接将MySQL数据库压缩备份<br /> 	格式：mysqldump -h{hostname} -u{username} -p{password} {databasename} | gzip &gt; {backfile.sql.gz}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	mysqldump &#8211;hlocalhost &#8211;uzhuchao &#8211;p123456 db_test1 | gzip &gt; backfile3.sql.gz<br /> 	备份MySQL数据库某个(些)表<br /> 	格式：mysqldump -h主机名&nbsp; -P端口 -u用户名 -p密码 (&#8211;tables | &#8211;quick) 数据库名 表名1 (表名2 &#8230;) &gt; 文件名.sql (括号中的可缺省)。<br /> 	mysqldump -h{hostname} (-P{port}) -u{user} -p{password} (&#8211;tables | &#8211;quick) {databasename} {table1} {table2} &gt; {backfile.sql}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	2<br /> 	3<br /> 	4<br /> 	mysqldump -hlocalhost -uzhuchao -p123456 db_test tbl_test &gt; backfile4-1.sql<br /> 	mysqldump -hlocalhost -P3306 -uzhuchao -p123456 db_test tbl_test &gt; backfile4-2.sql<br /> 	mysqldump -hlocalhost -P3306 -uzhuchao -p123456 --quick db_test tbl_test &gt; backfile4-3.sql<br /> 	mysqldump -hlocalhost -P3306 -uzhuchao -p123456 --tables db_test tbl_test1 tbl_test2 &gt; backfile4-4.sql<br /> 	同时备份多个MySQL数据库<br /> 	格式：mysqldump -h{hostname} (-P{port}) -u{username} -p{password}  &#8211;databases {databasename1} {databasename2} {databasename3} &gt;  multibackfile.sql<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	mysqldump -hlocalhost -uzhuchao -p123456 &#8211;databases db_test1 db_test2 db_test3 &gt; multibackfile.sql<br /> 	仅仅备份数据库结构<br /> 	格式：mysqldump &#8211;no-data &#8211;databases {databasename1} {databasename2} &gt; {structurebackfile.sql}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	mysqldump &#8211;no-data &#8211;databases db_test1 db_test2 &gt; structurebackfile.sql<br /> 	备份服务器上所有数据库<br /> 	格式：mysqldump &#8211;all-databases &gt; allbackupfile.sql<br /> 	&nbsp;<br /> 	========================================================================<br /> 	&nbsp;<br /> 	还原MySQL数据库的命令<br /> 	格式：mysql -h{hostname} -u{username} -p{password} {databasename} &lt; {backfile.sql}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	mysql -hlocalhost -uroot -p123456 db_test4 &lt; back_file1.sql<br /> 	还原压缩的MySQL数据库<br /> 	格式：gunzip &lt; {backfile.sql.gz} | mysql &#8211;u{username} &#8211;p{password} {databasename}<br /> 	例如：<br /> 	&nbsp;<br /> 	代码如下：<br /> 	1<br /> 	gunzip &lt; backfile.sql.gz | mysql &#8211;uzhuchao &#8211;p123456 db_test5<br /> 	将数据库转移到新服务器<br /> 	mysqldump &#8211;u{username} &#8211;p{password} {databasename} | mysql &#8211;host=*.*.*.* &#8211;C {databasename}<br /> 	&nbsp;<br /> 	二、其他：<br /> 	1、如果端口为默认的3306时，可省略 -P {端口号} 这一项。<br /> 	&nbsp;<br /> 	2、命令行格式中 { } 中的内容都是变量<br /> 	主机名 : {hostname}<br /> 	端&nbsp;&nbsp; 口：{port}&nbsp;&nbsp;&nbsp; （一般默认3306，可缺省）<br /> 	用户名：{user} {username} （如root）<br /> 	密&nbsp;&nbsp; 码：{password}<br /> 	数据库名 ：{databasename}<br /> 	表&nbsp;&nbsp; 名：{table}&nbsp; {table1}&nbsp; {table2}<br /> 	文件名：{backfile.sql}</div><img src ="http://www.blogjava.net/wangxinsh55/aggbug/361874.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangxinsh55/" target="_blank">SIMONE</a> 2011-10-24 11:46 <a href="http://www.blogjava.net/wangxinsh55/archive/2011/10/24/361874.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>