<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>roga&#039;s blog &#187; MySQL</title>
	<atom:link href="http://blog.roga.tw/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.roga.tw</link>
	<description>walk away in slow motion.</description>
	<lastBuildDate>Tue, 31 Jan 2012 15:56:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19861</generator>
		<item>
		<title>MySQL 複製資料表筆記</title>
		<link>http://blog.roga.tw/2011/08/mysql-%e8%a4%87%e8%a3%bd%e8%b3%87%e6%96%99%e8%a1%a8%e7%ad%86%e8%a8%98/</link>
		<comments>http://blog.roga.tw/2011/08/mysql-%e8%a4%87%e8%a3%bd%e8%b3%87%e6%96%99%e8%a1%a8%e7%ad%86%e8%a8%98/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 08:49:03 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2546</guid>
		<description><![CDATA[要複製某個資料表，要先建立一個新的資料表，然後把原本的資料新增到剛建立的新資料表。 建立新資料表：CREATE TABLE new_table LIKE old_db.old_table; 複製資料：INSERT new_table SELECT * FROM old_db.old_table; 收工。]]></description>
			<content:encoded><![CDATA[<p>要複製某個資料表，要先建立一個新的資料表，然後把原本的資料新增到剛建立的新資料表。</p>
<p>建立新資料表：<code>CREATE TABLE new_table LIKE old_db.old_table;</code></p>
<p>複製資料：<code>INSERT new_table SELECT * FROM old_db.old_table;</code></p>
<p>收工。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2011/08/mysql-%e8%a4%87%e8%a3%bd%e8%b3%87%e6%96%99%e8%a1%a8%e7%ad%86%e8%a8%98/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debian Squeeze 安裝 MySQL HandlerSocket Plugin</title>
		<link>http://blog.roga.tw/2011/03/debian-squeeze-%e5%ae%89%e8%a3%9d-mysql-handlersocket-plugin/</link>
		<comments>http://blog.roga.tw/2011/03/debian-squeeze-%e5%ae%89%e8%a3%9d-mysql-handlersocket-plugin/#comments</comments>
		<pubDate>Sun, 13 Mar 2011 12:05:43 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2497</guid>
		<description><![CDATA[去年 10 月 20 YOSHINORI MATSUNOBU 寫了 Using MySQL as a NoSQL &#8211; A story for exceeding 750,000 qps on a commodity server 這篇文章，裡面介紹了 DeNA Co.,Ltd. 發表的 HandlerSocket-Plugin-for-MySQL，好處在原網站有講到很多，最大的優勢是速度比傳統的用 SQL 查詢 MySQL 還要快了七倍之多 (MySQL via SQL = 105,000 qps, MySQL via HandlerSocket = 750,000 qps). 以下簡單講一下在 Debian Squeeze 安裝 MySQL HandlerSocket Plugin 的方法。 記得用 root 執行以下指令 [...]]]></description>
			<content:encoded><![CDATA[<p>去年 10 月 20 <a href="http://www.blogger.com/profile/14180479977952026421">YOSHINORI MATSUNOBU</a> 寫了 <a href="http://yoshinorimatsunobu.blogspot.com/2010/10/using-mysql-as-nosql-story-for.html">Using MySQL as a NoSQL &#8211; A story for exceeding 750,000 qps on a commodity server</a> 這篇文章，裡面介紹了 DeNA Co.,Ltd. 發表的 <a href="https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL">HandlerSocket-Plugin-for-MySQL</a>，好處在原網站有講到很多，最大的優勢是速度比傳統的用 SQL 查詢 MySQL 還要快了七倍之多 (MySQL via SQL = 105,000 qps,  MySQL via HandlerSocket = 750,000 qps).<br />
<span id="more-2497"></span><br />
以下簡單講一下在 Debian Squeeze 安裝 MySQL HandlerSocket Plugin 的方法。</p>
<p>記得用 root 執行以下指令<br />
<code><br />
sudo su<br />
</code></p>
<p>切到放原始碼的目錄下<br />
<code><br />
cd /usr/src/<br />
</code></p>
<p>先取回 MySQL Server 原始碼<br />
<code><br />
apt-get source mysql-server-5.1<br />
</code></p>
<p>目前版本是 5.1-5.1.49<br />
<code><br />
cd mysql-5.1-5.1.49/<br />
</code></p>
<p>將所有 patch 加入<br />
<code><br />
dpatch apply-all<br />
</code></p>
<p>回到原本目錄<br />
<code><br />
cd -<br />
</code></p>
<p>拿回 HandlerSocket 原始碼，如果沒有 get 可以先安裝 apt-get install git-core<br />
<code><br />
git clone https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.git handlersocket<br />
</code></p>
<p>產生 Makefile<br />
<code><br />
cd handlersocket/<br />
sh autogen.sh<br />
./configure --with-mysql-source=../mysql-5.1-5.1.49/ --with-mysql-bindir=/usr/bin/ --with-mysql-plugindir=/usr/lib/mysql/plugin/<br />
</code></p>
<p>安裝到系統中 (/usr/lib/mysql/plugin/)<br />
<code><br />
make<br />
make install<br />
</code></p>
<p>到 MySQL 中安裝 handlersocket.so<br />
<code><br />
mysql -u root -p -h localhost</p>
<p>mysql> install plugin handlersocket soname 'handlersocket.so';<br />
Query OK, 0 rows affected (0.02 sec)<br />
</code> </p>
<p>接著建立 MySQL 的設定檔<br />
<code><br />
touch /etc/mysql/conf.d/handlersocket.cnf<br />
</code></p>
<p>設定檔內容</p>
<blockquote><p>
[mysqld]<br />
loose_handlersocket_port = 9998<br />
# the port number to bind to (for read requests)<br />
loose_handlersocket_port_wr = 9999<br />
# the port number to bind to (for write requests)<br />
loose_handlersocket_threads = 16<br />
# the number of worker threads (for read requests)<br />
loose_handlersocket_threads_wr = 1<br />
# the number of worker threads (for write requests)<br />
open_files_limit = 65535<br />
# to allow handlersocket accept many concurrent<br />
# connections, make open_files_limit as large as<br />
# possible.
</p></blockquote>
<p>修改 services<br />
<code><br />
vi /etc/services<br />
</code></p>
<p>加入兩個服務</p>
<blockquote><p>
mysql-hsr   9998/tcp    #   the port number to bind to (for read requests)<br />
mysql-hsw   9999/tcp    #   the port number to bind to (for write requests)
</p></blockquote>
<p>然後重新啟動 MySQL ，到這邊 Server 的 Plugin 就安裝完成了<br />
<code><br />
/etc/init.d/mysql restart<br />
</code></p>
<p>接著安裝 Client 端程式，這邊以 PHP 為例，安裝 <a href="http://code.google.com/p/php-handlersocket/">php-handlersocket</a></p>
<p>下載 tarball<br />
<code><br />
wget http://php-handlersocket.googlecode.com/files/php-handlersocket-0.0.7.tar.gz<br />
</code></p>
<p>解開，並且安裝<br />
<code><br />
tar -zxvf php-handlersocket-0.0.7.tar.gz<br />
cd php-handlersocket/<br />
phpize<br />
./configure<br />
make<br />
make install<br />
</code></p>
<p>然後新增設定檔加入模組<br />
<code><br />
touch /etc/php5/conf.d/handlersocket.ini<br />
</code></p>
<p>裡面加入</p>
<blockquote><p>
;php-handlersocket<br />
extension=handlersocket.so
</p></blockquote>
<p>這樣就差不多完成了，下一篇文章會簡介使用方法。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2011/03/debian-squeeze-%e5%ae%89%e8%a3%9d-mysql-handlersocket-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>將頻繁的查詢改放在 memcached 內</title>
		<link>http://blog.roga.tw/2010/09/%e5%b0%87%e9%a0%bb%e7%b9%81%e7%9a%84%e6%9f%a5%e8%a9%a2%e6%94%b9%e6%94%be%e5%9c%a8-memcached-%e5%85%a7/</link>
		<comments>http://blog.roga.tw/2010/09/%e5%b0%87%e9%a0%bb%e7%b9%81%e7%9a%84%e6%9f%a5%e8%a9%a2%e6%94%b9%e6%94%be%e5%9c%a8-memcached-%e5%85%a7/#comments</comments>
		<pubDate>Fri, 17 Sep 2010 18:27:33 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2431</guid>
		<description><![CDATA[最近改寫了一些以前的程式，目的是把一些不太重要，過期就用不到的東西丟到 memcached 裡面去放著。趁著週五的晚上有空，把新版的程式丟到伺服器上測試。跑了一段時間後，看 MySQL 的使用狀況，真是令人賞心悅目。 圖中大幅降低的值是 MySQL Server Status 的 Questions ，這個值的定義是 MySQL 執行過的 SQL 敘述次數，這個數據的定義可以看： 5.1.6. Server Status Variables#statvar_Questions。 也可以看到 open table / file 次數也降低一半 Open_tables 表示 被開啟的資料表數目 (The number of tables that are open.)， Opened_files 則是被 MySQL 內部函數 my_open() 打開的檔案個數，這兩個數據的定義可以看： 5.1.6. Server Status Variables#statvar_Open_tables。]]></description>
			<content:encoded><![CDATA[<p>最近改寫了一些以前的程式，目的是把一些不太重要，過期就用不到的東西丟到 memcached 裡面去放著。趁著週五的晚上有空，把新版的程式丟到伺服器上測試。跑了一段時間後，看 MySQL 的使用狀況，真是令人賞心悅目。<br />
<span id="more-2431"></span><br />
<img src="http://gallery.roga.tw/d/40675-1/graph_image_php+_1_.png" alt="" /></p>
<p>圖中大幅降低的值是  MySQL Server Status 的 Questions ，這個值的定義是 MySQL 執行過的 SQL 敘述次數，這個數據的定義可以看： <a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Questions">5.1.6. Server Status Variables#statvar_Questions</a>。</p>
<p>也可以看到 open table / file 次數也降低一半</p>
<p><img src="http://gallery.roga.tw/d/40678-1/graph_image_php.png" alt="" /></p>
<p>Open_tables 表示 被開啟的資料表數目 (The number of tables that are open.)， Opened_files 則是被 MySQL 內部函數 my_open() 打開的檔案個數，這兩個數據的定義可以看： <a href="http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html#statvar_Open_tables">5.1.6. Server Status Variables#statvar_Open_tables</a>。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2010/09/%e5%b0%87%e9%a0%bb%e7%b9%81%e7%9a%84%e6%9f%a5%e8%a9%a2%e6%94%b9%e6%94%be%e5%9c%a8-memcached-%e5%85%a7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Basic Tunning</title>
		<link>http://blog.roga.tw/2009/06/mysql-basic-tunning/</link>
		<comments>http://blog.roga.tw/2009/06/mysql-basic-tunning/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 08:40:18 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2228</guid>
		<description><![CDATA[環境說明：目前我用的是 64bit Intel 4 核心 2.4GHz CPU 的機器加上 8 GB 的 RAM ，硬碟選用一般的 SATA 介面，我的資料庫每小時會執行大概 30 萬個查詢 ( 295.73 k )，大約每秒 (80) 個查詢，每天 1000 萬次查詢，目前暫時沒有 Replication 機制 (想增加)。 最近常常 SHOW STATUS 看效能，而之前最常遭遇到的問題是： Handler_read_rnd_next (82K) 過大，解法：當進行大量排序查詢的時候不理想，或許可以調高 read_buffer_size 。 Handler_read_rnd (32G) 過大，描述：表示每次 query 都必須要直接掃整個 table 而且 index 可能沒有發揮作用，但是我不曉得是不是 Active Record 造成的，因為我 WHERE 和 ORDER BY 選用的欄位都有檢查過了，應該都建了 index [...]]]></description>
			<content:encoded><![CDATA[<p>環境說明：目前我用的是 64bit Intel 4 核心 2.4GHz CPU 的機器加上 8 GB 的 RAM ，硬碟選用一般的 SATA 介面，我的資料庫每小時會執行大概 30 萬個查詢 ( 295.73 k )，大約每秒 (80) 個查詢，每天 1000 萬次查詢，目前暫時沒有 Replication 機制 (想增加)。<br />
<span id="more-2228"></span><br />
最近常常 SHOW STATUS 看效能，而之前最常遭遇到的問題是：</p>
<ul>
<li>Handler_read_rnd_next (82K) 過大，解法：當進行大量排序查詢的時候不理想，或許可以調高 read_buffer_size 。</li>
<li>Handler_read_rnd (32G) 過大，描述：表示每次 query 都必須要直接掃整個 table 而且 index 可能沒有發揮作用，但是我不曉得是不是 Active Record 造成的，因為我 WHERE 和 ORDER BY 選用的欄位都有檢查過了，應該都建了 index</li>
<li>Qcache_lowmem_prunes (40K) 描述：我應該要把 query cache 調小，因為每次查出來結果都不同(鮮少重複的查詢)，所以不需要調太大。</li>
<li>Created_tmp_disk_tables (1,725 ) 過大，在磁碟上建立的臨時 table 太大，應該要調高 tmp_table_size 的數值(記憶體中建立臨時 table )，以改善效能。</li>
<li>Sort_merge_passes (24) 過大，應該要增加 sort_buffer 的值。</li>
<li>Opened_tables (2639) 過大，則要增加 table_cache 的值。</li>
<li>Table_locks_waited (112K) 過大，雖然我的  Table_locks_immediate 有 12M，而 Table_locks_waited 有 112K ，發生鎖定等待的情況大約是比例上為 1/100 (一百次鎖定中，可以直接鎖定 99 次，有一次要等待)</li>
</ul>
<p>今天在 Matt Yonkovit 的 Big DBA Head 看到了他提出 MySQL 設定檔的範例 ( MySQL 本身也都會附上不同環境的設定例範，我的機器之前是以 my-huge.cnf 作為參考設定。)：</p>
<blockquote><p>
64 bit system &#8211; 假設 64 bit CPU 以及作業系統<br />
8GB+ of memory &#8211; 超過 8 GB 記憶體<br />
Dedicated DB Box &#8211; 專職的資料庫伺服器<br />
All MyISAM &#8211; 使用 MyISAM 儲存引擎<br />
[mysqld]<br />
thread_cache_size = 256<br />
table_cache = 1024<br />
key_buffer = 4000M<br />
sort_buffer_size = 256K<br />
read_buffer_size = 256K<br />
read_rnd_buffer_size = 256K<br />
max_allowed_packet = 1M<br />
tmp_table_size=64M<br />
max_heap_table_size=64M<br />
query_cache_size=128M<br />
myisam_recover = backup<br />
myisam_sort_buffer_size=512M<br />
skip-innodb
</p></blockquote>
<p>參考網址：<a href="http://www.bigdbahead.com/?p=643">5 Minute DBA MyISAM Example Config Files</a></p>
<p>備註：Big DBA Head 的作者是 Matt Yonkovit ，之前是昇陽下面 MySQL 的資深顧問，有 11 年 Oracle 的經驗、9 年的 MySQL 的經驗、以及 8 年的 SQL Server 經驗。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2009/06/mysql-basic-tunning/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL 大量刪除資料的問題 (續)</title>
		<link>http://blog.roga.tw/2009/06/%e7%ba%8c%e6%98%a8%e5%a4%a9%e7%9a%84%e5%95%8f%e9%a1%8c/</link>
		<comments>http://blog.roga.tw/2009/06/%e7%ba%8c%e6%98%a8%e5%a4%a9%e7%9a%84%e5%95%8f%e9%a1%8c/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 07:13:44 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2220</guid>
		<description><![CDATA[剛剛和同事小菜哥討論了一下昨天我問的MySQL 大量刪除資料的問題，想到一個比較好的解法： 這個方法一解下去，一秒不到就弄好了，資料庫頓時少了 60 MB 的資料。不過我還是想知道，有沒有辦法用純 SQL 來解(感覺上難度不低)。 (最佳的解法應該是用 Store Procedure 存到 MySQL 內，這樣它就會自己 Maintain 數目在 1500 筆，但這方面我並不是很熟悉。)]]></description>
			<content:encoded><![CDATA[<p>剛剛和同事小菜哥討論了一下昨天我問的<a href="http://blog.roga.tw/2009/06/09/2218">MySQL 大量刪除資料的問題</a>，想到一個比較好的解法：<br />
<span id="more-2220"></span></p>
<pre class="brush: php; title: ; notranslate">
$sql = &quot;SELECT `url_id`, count( url_id ) FROM `url_detail_history` GROUP BY `url_id` HAVING count(id) &gt; 1499;&quot;;
$result = mysql_query($sql);

$i = 0;
while (list($url_id, $count) = mysql_fetch_row($result) )
{
    echo &quot;id = $url_id and, count =$count &lt;br /&gt;&quot;;

    /* 掃描資料表，先反向排序，再用 LIMIT 來取得第 1500 筆資料，這筆資料的 ID 就是臨界值 - 重點 - */
    $sql2 = &quot;SELECT `id` FROM `url_detail_history` WHERE `url_id` = $url_id ORDER BY `id` DESC LIMIT 1499,1500&quot;;
    $result2 = mysql_query($sql2);

    $row = mysql_fetch_array($result2);
    $critical = $row['id'];
    echo &quot;critical: $critical&quot;;

    /* 比臨界值小的都刪掉 */
    $sql3 = &quot;DELETE FROM `url_detail_history` WHERE `url_id` = $url_id and `id` &lt; $critical&quot;;
    mysql_query($sql3);

    echo &quot;&lt;br /&gt;&quot;;
    $i++;
}
echo &quot;&lt;hr /&gt; total = $i;&quot;
/* 假設資料保留 1500 筆資料 */
</pre>
<p>這個方法一解下去，一秒不到就弄好了，資料庫頓時少了 60 MB 的資料。不過我還是想知道，有沒有辦法用純 SQL 來解(感覺上難度不低)。</p>
<p>(最佳的解法應該是用 Store Procedure 存到 MySQL 內，這樣它就會自己 Maintain 數目在 1500 筆，但這方面我並不是很熟悉。)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2009/06/%e7%ba%8c%e6%98%a8%e5%a4%a9%e7%9a%84%e5%95%8f%e9%a1%8c/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL 大量刪除資料的問題</title>
		<link>http://blog.roga.tw/2009/06/mysql-%e5%a4%a7%e9%87%8f%e5%88%aa%e9%99%a4%e8%b3%87%e6%96%99%e7%9a%84%e5%95%8f%e9%a1%8c/</link>
		<comments>http://blog.roga.tw/2009/06/mysql-%e5%a4%a7%e9%87%8f%e5%88%aa%e9%99%a4%e8%b3%87%e6%96%99%e7%9a%84%e5%95%8f%e9%a1%8c/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 17:26:20 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2218</guid>
		<description><![CDATA[url_detail_history 資料表 +--------------+---------------------+------+ &#124; Field &#124; Type &#124; Null &#124; +--------------+---------------------+------+ &#124; id &#124; bigint(11) unsigned &#124; NO &#124; -> 有 Auto_increment &#124; url_id &#124; int(11) &#124; NO &#124; &#124; visitor_ip &#124; varchar(15) &#124; NO &#124; &#124; visitor_fqdn &#124; varchar(100) &#124; NO &#124; &#124; visitor_time &#124; timestamp &#124; NO &#124; -> CURRENT_TIMESTAMP +--------------+---------------------+------+ 現在裡面有許多筆資料，可以用 SELECT [...]]]></description>
			<content:encoded><![CDATA[<p><span id="more-2218"></span></p>
<pre>
    url_detail_history 資料表
    +--------------+---------------------+------+
    | Field        | Type                | Null |
    +--------------+---------------------+------+
    | id           | bigint(11) unsigned | NO   |  -> 有 Auto_increment
    | url_id       | int(11)             | NO   |
    | visitor_ip   | varchar(15)         | NO   |
    | visitor_fqdn | varchar(100)        | NO   |
    | visitor_time | timestamp           | NO   |  -> CURRENT_TIMESTAMP
    +--------------+---------------------+------+
</pre>
<p> 現在裡面有許多筆資料，可以用</p>
<p><code>SELECT COUNT(id), url_id FROM url_detail_history GROUP BY url_id</code></p>
<p>得知每個 url_id 有幾筆資料。</p>
<p>我前幾天寫錯了一個地方，導致資料表裡面長了大概一百萬筆資料。由於我 SQL 很弱，所以想知道有沒有辦法直接用 SQL 語法執行刪除，條件是：<strong>每個 url_id 只要總數超過 N 筆資料就刪除到只剩下 N 筆為止</strong>， 當然， id 越大代表資料越新，必須能從小的 id 開始刪起。</p>
<p>我有寫一段程式來跑但是效能不彰，系統 loading 飆到 100 多，拉牛上樹慘不忍睹..(以下是虛擬碼)</p>
<pre class="brush: php; title: ; notranslate">
/* 先取出符合條件的資料 */

SELECT count(id), url_id FROM url_detail_history GROUP BY url_id HAVING count(id) &gt; 1500

用此法個別取出 $url_id ，然後

foreach( $url_id ) {
     do {
            $id= SELECT min(id) FROM table WHERE url_id = $url_id
            DELETE FROM table WHERE id = $id
            $count = &quot;SELECT COUNT(id), url_id FROM url_detail_history WHERE url_id = $url_id
        } while( $count &lt; 1501)
}
</pre>
<p>這樣作法效能很差，不想這麼暴力解這個問題&#8230; <img src='http://blog.roga.tw/wp-includes/images/smilies/icon_cry.gif' alt=':cry:' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2009/06/mysql-%e5%a4%a7%e9%87%8f%e5%88%aa%e9%99%a4%e8%b3%87%e6%96%99%e7%9a%84%e5%95%8f%e9%a1%8c/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL 排序兩個欄位的方法 (使用IF)</title>
		<link>http://blog.roga.tw/2009/06/mysql-%e6%8e%92%e5%ba%8f%e5%85%a9%e5%80%8b%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/</link>
		<comments>http://blog.roga.tw/2009/06/mysql-%e6%8e%92%e5%ba%8f%e5%85%a9%e5%80%8b%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 06:46:01 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2213</guid>
		<description><![CDATA[討論區很常見的排法是這樣：「按照所有文章的發表時間排序，但是如果有最新回應的話，則是會把最新回應的時間一起排進去。」 照古早的寫法： SELECT * FROM `forum` ORDER BY `REPLY_TIME` DESC, `TIME` DESC; 這樣會爆掉，因為它會把 REPLY_TIME 都排完，才排 TIME ，而這並不是我們想要的。 MySQL 有提供 IF 的功能， 用法： IF (bool,value1,value2) 解釋：當第一個參數為真時，回傳 value1，否則傳回 value2 可以改寫成： SELECT IF(`REPLY_TIME` > `TIME`, `REPLY_TIME`, `TIME`) AS `TEMPTIME`, `ID`, `POST_TITLE`, `POST_CONTET` -- 這邊是其他欄位，而 TEMPTIME 則是產生的新欄位。 FROM `forum` WHERE 條件子句 ORDER BY `TEMPTIME` DESC; LIMIT 0, 資料筆數 重要的筆記&#8230;]]></description>
			<content:encoded><![CDATA[<p>討論區很常見的排法是這樣：「按照所有文章的發表時間排序，但是如果有最新回應的話，則是會把最新回應的時間一起排進去。」<br />
<span id="more-2213"></span><br />
照古早的寫法：<br />
<code><br />
SELECT * FROM `forum` ORDER BY `REPLY_TIME` DESC, `TIME` DESC;<br />
</code><br />
這樣會爆掉，因為它會把 REPLY_TIME 都排完，才排 TIME ，而這並不是我們想要的。</p>
<p>MySQL 有提供 IF 的功能，<br />
用法： IF (bool,value1,value2)<br />
解釋：當第一個參數為真時，回傳 value1，否則傳回 value2</p>
<p>可以改寫成：<br />
<code><br />
SELECT<br />
IF(`REPLY_TIME` > `TIME`, `REPLY_TIME`, `TIME`) AS `TEMPTIME`, `ID`, `POST_TITLE`, `POST_CONTET`<br />
-- 這邊是其他欄位，而 TEMPTIME 則是產生的新欄位。<br />
FROM `forum`<br />
WHERE 條件子句<br />
ORDER BY `TEMPTIME` DESC;<br />
LIMIT 0, 資料筆數<br />
</code><br />
重要的筆記&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2009/06/mysql-%e6%8e%92%e5%ba%8f%e5%85%a9%e5%80%8b%e6%ac%84%e4%bd%8d%e7%9a%84%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ubuntu 移動 MySQL datadir 的一個小技巧</title>
		<link>http://blog.roga.tw/2009/05/ubuntu-%e7%a7%bb%e5%8b%95-mysql-datadir-%e7%9a%84%e4%b8%80%e5%80%8b%e5%b0%8f%e6%8a%80%e5%b7%a7/</link>
		<comments>http://blog.roga.tw/2009/05/ubuntu-%e7%a7%bb%e5%8b%95-mysql-datadir-%e7%9a%84%e4%b8%80%e5%80%8b%e5%b0%8f%e6%8a%80%e5%b7%a7/#comments</comments>
		<pubDate>Wed, 27 May 2009 06:23:39 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=2205</guid>
		<description><![CDATA[一般來說，移動 MySQL 資料庫只要修改 /etc/mysql/my.cnf 裡面的 datadir 即可&#8230; 把原本的註解掉： #datadir = /var/lib/mysql 並且填上新的路徑 datadir = /data/mysql # 假設這是新路徑 然後複製資料庫資料 cp -R -p /var/lib/mysql /data/mysql 可是當我下指令 /etc/init.d/mysql restart 的時候，卻發現錯誤，我也找不出問題在哪(權限對了，路徑也對了，檔案也都在)。後來才發現原來 Ubuntu 還有另一個地方的設定必須要修改，就是 apparmor ：先修改 /etc/apparmor.d/usr.sbin.mysqld 這個設定檔，找出裡面原本指向 /var/lib/mysql 的目錄，全部改為新目錄。 然後重啟動就可以了！ /etc/init.d/apparmor restart /etc/init.d/mysql restart]]></description>
			<content:encoded><![CDATA[<p>一般來說，移動 MySQL 資料庫只要修改 /etc/mysql/my.cnf 裡面的 datadir 即可&#8230;<br />
<span id="more-2205"></span><br />
把原本的註解掉：<br />
#datadir        = /var/lib/mysql<br />
並且填上新的路徑<br />
datadir        = /data/mysql<br />
# 假設這是新路徑</p>
<p>然後複製資料庫資料 cp -R -p /var/lib/mysql /data/mysql 可是當我下指令 /etc/init.d/mysql restart 的時候，卻發現錯誤，我也找不出問題在哪(權限對了，路徑也對了，檔案也都在)。後來才發現原來 Ubuntu 還有另一個地方的設定必須要修改，就是 apparmor ：先修改 /etc/apparmor.d/usr.sbin.mysqld 這個設定檔，找出裡面原本指向 /var/lib/mysql 的目錄，全部改為新目錄。</p>
<p>然後重啟動就可以了！<br />
/etc/init.d/apparmor restart<br />
/etc/init.d/mysql restart</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2009/05/ubuntu-%e7%a7%bb%e5%8b%95-mysql-datadir-%e7%9a%84%e4%b8%80%e5%80%8b%e5%b0%8f%e6%8a%80%e5%b7%a7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Command Line Interface to import a MySQL database.</title>
		<link>http://blog.roga.tw/2008/11/using-command-line-interface-to-import-a-mysql-database/</link>
		<comments>http://blog.roga.tw/2008/11/using-command-line-interface-to-import-a-mysql-database/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 02:55:09 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=1467</guid>
		<description><![CDATA[Sometimes we don&#8217;t have a database management system(DBMS) in our server, so we need to use command line interface to CREATE and IMPORT a database. Here are some notes: in other way, we also can set my.cnf]]></description>
			<content:encoded><![CDATA[<p>Sometimes we don&#8217;t have a database management system(DBMS) in our server, so we need to use command line interface to CREATE and IMPORT a database.<br />
<span id="more-1467"></span><br />
Here are some notes:</p>
<pre class="brush: sql; title: ; notranslate">
-- Login to MySQL
debian:# mysql -u root -pPASSWORD

-- Create a utf8 encode database.
CREATE DATABASE DB_NAME DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

-- Use the utf8 connection collation
SET NAMES utf8;

-- Import a utf8 SQL file.
SOURCE filename-utf8.sql

-- or, after create the database, back to system, and use the &quot;mysql&quot; command.
debian:# mysql DB_NAME -u root -pPASSWORD &lt; filename-utf8.sql --default-character-set=utf8
</pre>
<p>in other way, we also can set my.cnf</p>
<pre class="brush: php; title: ; notranslate">
[client]
    default-character-set=utf8
[mysqld]
    default-character-set=utf8
    default-collation=utf8_general_ci
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2008/11/using-command-line-interface-to-import-a-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL 資料庫儲存引擎的選用</title>
		<link>http://blog.roga.tw/2008/11/mysql-%e8%b3%87%e6%96%99%e5%ba%ab%e5%84%b2%e5%ad%98%e5%bc%95%e6%93%8e%e7%9a%84%e9%81%b8%e7%94%a8/</link>
		<comments>http://blog.roga.tw/2008/11/mysql-%e8%b3%87%e6%96%99%e5%ba%ab%e5%84%b2%e5%ad%98%e5%bc%95%e6%93%8e%e7%9a%84%e9%81%b8%e7%94%a8/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 11:21:30 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=1288</guid>
		<description><![CDATA[最近在工作上遇到一個資料庫的問題，主要是 MySQL 儲存引擎的選用。但我對各種儲存引擎其實不甚瞭解，在應用上往往也是用預設值，現在面臨了嚴重的問題，只好查資料來幫我釐清觀念。 這篇文章主要討論 Memory, MyIASM, InnoDB 三種儲存引擎，因為我只選用這三種。首先，先看個我做了很久的表格吧！ 項目 MyIASM InnoDB Memory 空間限制 無 64TB 記憶體 transaction x 有 x 大量 Insert 速度 高 低 高 設置外來鍵 x 有 x 鎖定層級 資料表 資料列 資料表 二元樹索引 有 有 不知 雜湊索引 x 有 有 全文搜尋索引 有 x x 資料壓縮 有 x x 資料快取 x 有 有 索引快取 [...]]]></description>
			<content:encoded><![CDATA[<p>最近在工作上遇到一個資料庫的問題，主要是 MySQL 儲存引擎的選用。但我對各種儲存引擎其實不甚瞭解，在應用上往往也是用預設值，現在面臨了嚴重的問題，只好查資料來幫我釐清觀念。</p>
<p>這篇文章主要討論 Memory, MyIASM, InnoDB 三種儲存引擎，因為我只選用這三種。首先，先看個我做了很久的表格吧！<br />
<span id="more-1288"></span></p>
<table>
<tr>
<th>項目</th>
<th>MyIASM</th>
<th>InnoDB</th>
<th>Memory</th>
</tr>
<tr>
<td>空間限制</td>
<td>無</td>
<td>64TB</td>
<td>記憶體</td>
</tr>
<tr>
<td>transaction</td>
<td>x</td>
<td>有</td>
<td>x</td>
</tr>
<tr>
<td>大量 Insert 速度</td>
<td>高</td>
<td>低</td>
<td>高</td>
</tr>
<tr>
<td>設置外來鍵</td>
<td>x</td>
<td>有</td>
<td>x</td>
</tr>
<tr>
<td>鎖定層級</td>
<td>資料表</td>
<td>資料列</td>
<td>資料表</td>
</tr>
<tr>
<td>二元樹索引</td>
<td>有</td>
<td>有</td>
<td>不知</td>
</tr>
<tr>
<td>雜湊索引</td>
<td>x</td>
<td>有</td>
<td>有</td>
</tr>
<tr>
<td>全文搜尋索引</td>
<td>有</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>資料壓縮</td>
<td>有</td>
<td>x</td>
<td>x</td>
</tr>
<tr>
<td>資料快取</td>
<td>x</td>
<td>有</td>
<td>有</td>
</tr>
<tr>
<td>索引快取</td>
<td>有</td>
<td>有</td>
<td>有</td>
</tr>
<tr>
<td>記憶體佔用</td>
<td>低</td>
<td>高</td>
<td>中</td>
</tr>
<tr>
<td>磁碟佔用</td>
<td>低</td>
<td>高</td>
<td>x</td>
</tr>
</table>
<p>對於 MyIASM 來說，最大的好處是成本低，而且可以 create views ，這是其他儲存引擎辦不到的，但缺點就是鎖定層級以 table 為單位，而且不支援 transaction ，這些地方輸給 InnoDB 。不過 InnoDB 也是有缺點像是不支援 FULLTEXT 的索引，且記憶體佔用多、磁碟空間耗用大&#8230;等等。</p>
<p>我找到一篇文章針對 MyIASM, InnoDB 和 Falcon 來做比較，在這裡面 MyIASM和 InnoDB 表現都沒有差很多，唯獨在測 READ_PK_RANGE 和 READ_KEY_POINT 時候， MyIASM 爛掉了(不過主角其實是 Falcon 因為它被打趴了)。原因是：There MyISAM shows bad scalability with increasing count of thread. I think the reason is pread system call MyISAM uses to access data and retrieving from OS cache is not scaled.<br />
<a href="http://www.mysqlperformanceblog.com/2007/01/08/innodb-vs-myisam-vs-falcon-benchmarks-part-1/">InnoDB vs MyISAM vs Falcon benchmarks &#8211; part 1</a></p>
<p>而 Memory 儲存引擎的最大優點就是快、快、很快、不會對硬碟頻繁讀寫、並且用 HASH 雜湊索引(但不曉得有沒有 Btree 二元樹索引)！另外它有個特性，就是會在硬碟建立一個 .frm 檔，目的是為了存資料表的 scheme ，但是每一筆 record 還是儲存在記憶體中，這也意味著如果斷電或是關機，資料就會消失不見。</p>
<p>實際應用：</p>
<p>假設我有兩個不同類型的 Table ，分別儲存 App Data 和 Session ，我有大量連線，從伺服器上的紀錄看來，開機半天左右，總共處理近九百萬個連線(這是實際數據)。</p>
<table>
<tr>
<th colspan="4">系統開機至現在共進行 8,944,853 次查詢</th>
</tr>
<tr>
<td>總共</td>
<td>每小時</td>
<td>每分</td>
<td>每秒</td>
</tr>
<tr>
<td>8,945 k</td>
<td>806.51 k</td>
<td>12.73 k</td>
<td>224.19</td>
</tr>
</table>
<p>而 App Data 的資料表作用和 Session 資料表個作用分別如下：</p>
<p>每個 App 啟動時，都會有一個 Session ID，而每筆 Session 都被當成一筆 Record Insert 到 Table 中做紀錄，當 Session 起始/結束的時候，才把更新的資料寫到 App Data 中。</p>
<p>對於 Session 個資料表的處理，我採用 Memory 為儲存引擎，因為 Session 掉了並不可惜，但卻可以換來極佳的效率，而 App Data 的資料表，我則是採用 InnoDB ，雖然相較於 MyIASM 會花上更多的 Cost 而且效率較差，但是他提供很好的鎖定(以row為單位)以及安全的復原機制，另外也支援外來鍵的設定。</p>
<p>推薦閱讀文章：<br />
<a href="http://dev.mysql.com/tech-resources/articles/storage-engine/part_3.html">MySQL Storage Engine Architecture, Part 3: Details and Comparison</a><br />
<a href="http://dev.mysql.com/doc/refman/5.0/en/memory-storage-engine.html">13.4. The MEMORY (HEAP) Storage Engine</a><br />
<a href="http://www.softwareprojects.com/resources/programming/t-mysql-storage-engines-1470.html">MySQL Storage Engines</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2008/11/mysql-%e8%b3%87%e6%96%99%e5%ba%ab%e5%84%b2%e5%ad%98%e5%bc%95%e6%93%8e%e7%9a%84%e9%81%b8%e7%94%a8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>mysqldump 的一些注意事項</title>
		<link>http://blog.roga.tw/2008/11/mysql-dump-%e7%9a%84%e4%b8%80%e4%ba%9b%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85/</link>
		<comments>http://blog.roga.tw/2008/11/mysql-dump-%e7%9a%84%e4%b8%80%e4%ba%9b%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 04:20:04 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=1127</guid>
		<description><![CDATA[最近在工作上常常遇到一些以前古人建立的資料庫編碼混亂的問題，通常都發生在 MySQL 資料庫新舊轉換上面。 MySQL 預設的語系是 latin1 ，但是以現在的情況而言，非英語系國家最適合的當然是 utf8 。 通常我喜歡用 phpMyAdmin 來倒出資料庫和還原，但是如果語系(編碼)沒設定好，那在 phpMyAdmin 裡面不管怎麼弄都會變成亂碼。這時候我們可以靠 mysqldump 指令來解決。 語法如下： 注意，-p後面緊接著密碼，不需要空格，其餘進階用法請 man mysqldump 。如果有編碼混亂的情況，可以先試著用： 把資料倒出來看看，再用文字編輯器(Notepad++)開啟，如果沒有變成亂碼的話，即可把 CREATE TABLE `table` ( 略 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; 的 latin1 都取代為 utf8 ，請且重新儲存為 utf8 編碼的檔案(無BOM)，這樣就是一個 utf8 的完整 dump 檔了！ 除了修改資料庫的連線問題之外，也可以修改 my.cnf 其中[mysqld]的設定內容修改為 utf8 ，可以把資料庫預設的編碼從 latin1 改為 utf8 ，至於 client 的內容則是預設的查詢條件。要不要改就看個人意願了(我個人偏好不做修改，而是從程式那邊的查詢下手)。 [...]]]></description>
			<content:encoded><![CDATA[<p>最近在工作上常常遇到一些以前古人建立的資料庫編碼混亂的問題，通常都發生在 MySQL 資料庫新舊轉換上面。 MySQL 預設的語系是 latin1 ，但是以現在的情況而言，非英語系國家最適合的當然是 utf8 。</p>
<p><span id="more-1127"></span></p>
<p>通常我喜歡用 phpMyAdmin 來倒出資料庫和還原，但是如果語系(編碼)沒設定好，那在 phpMyAdmin 裡面不管怎麼弄都會變成亂碼。這時候我們可以靠 mysqldump 指令來解決。</p>
<p>語法如下：</p>
<pre class="brush: bash; title: ; notranslate">mysqldump -u USERNAME -pPASSWORD [DBname] &gt; [filepath/filename.sql]</pre>
<p>注意，-p後面緊接著密碼，不需要空格，其餘進階用法請 man mysqldump 。如果有編碼混亂的情況，可以先試著用：</p>
<pre class="brush: bash; title: ; notranslate">mysqldump -u USERNAME -pPASSWORD --default-character-set=latin1 [DBname] &gt; [filepath/filename.sql]</pre>
<p>把資料倒出來看看，再用文字編輯器(Notepad++)開啟，如果沒有變成亂碼的話，即可把</p>
<blockquote><p>
CREATE TABLE `table` (<br />
略<br />
) ENGINE=MyISAM DEFAULT CHARSET=<strong>latin1</strong> AUTO_INCREMENT=1 ;
</p></blockquote>
<p>的 latin1 都取代為 utf8 ，請且重新儲存為 utf8 編碼的檔案(無BOM)，這樣就是一個 utf8 的完整 dump 檔了！</p>
<p>除了修改資料庫的連線問題之外，也可以修改 my.cnf</p>
<pre class="brush: xml; title: ; notranslate">
[mysqld]
default-character-set=utf8

[client]
default-character-set=utf8
init_connect='SET NAMES utf8'
</pre>
<p>其中[mysqld]的設定內容修改為 utf8 ，可以把資料庫預設的編碼從 latin1 改為 utf8 ，至於 client 的內容則是預設的查詢條件。要不要改就看個人意願了(我個人偏好不做修改，而是從程式那邊的查詢下手)。</p>
<p>舉例而言，稍據規模的專案，幾乎都有 DB 的 Driver 可找，通常 Driver 內都有一個部份在做 prefetch ，在該區加入</p>
<pre class="brush: php; title: ; notranslate">mysql_query(&quot;SET NAMES 'UTF8'&quot;);</pre>
<p>這樣感覺就好多了！出來的文字編碼也不會錯了！當然，如果以後要再用 mysqldump 備份的話，記得指令要打完整</p>
<pre class="brush: bash; title: ; notranslate">mysqldump -u USERNAME =pPASSWORD --default-character-set=utf8 [DBname] &gt; [filepath/filename.sql]</pre>
<p>以防倒出來的編碼不小心又錯了。</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2008/11/mysql-dump-%e7%9a%84%e4%b8%80%e4%ba%9b%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL 驚魂記</title>
		<link>http://blog.roga.tw/2008/10/mysql-%e9%a9%9a%e9%ad%82%e8%a8%98/</link>
		<comments>http://blog.roga.tw/2008/10/mysql-%e9%a9%9a%e9%ad%82%e8%a8%98/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 15:59:11 +0000</pubDate>
		<dc:creator>roga</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.roga.tw/?p=1008</guid>
		<description><![CDATA[其實我很久沒去動伺服器了，前幾天放假時心血來潮，下幾個指令看看硬碟的狀況，不看還好，一看吐血&#8230; roga@Lab3:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/md0 229G 62G 168G 27% / tmpfs 507M 0 507M 0% /lib/init/rw udev 10M 44K 10M 1% /dev tmpfs 507M 0 507M 0% /dev/shm roga@Lab3:~$ 我明明硬碟上什麼都沒放，怎麼會活生生被吃掉 62G 呢！？ 用指令 root@Lab3:/# du -ckhs /* 一層一層慢慢查才發現原來 /var/log/mysql 裡面有一大堆 mysql-bin.0000XX ，原來這些都是 MySQL 的 Binary Log ，讓你在 MySQL [...]]]></description>
			<content:encoded><![CDATA[<p>其實我很久沒去動伺服器了，前幾天放假時心血來潮，下幾個指令看看硬碟的狀況，不看還好，一看吐血&#8230;</p>
<pre>
roga@Lab3:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/md0              229G   62G  168G  27% /
tmpfs                 507M     0  507M   0% /lib/init/rw
udev                   10M   44K   10M   1% /dev
tmpfs                 507M     0  507M   0% /dev/shm
roga@Lab3:~$
</pre>
<p>我明明硬碟上什麼都沒放，怎麼會活生生被吃掉 62G 呢！？</p>
<p>用指令 root@Lab3:/# du -ckhs /* 一層一層慢慢查才發現原來 /var/log/mysql 裡面有一大堆 mysql-bin.0000XX ，原來這些都是 MySQL 的 Binary Log ，讓你在 MySQL 爛掉的時候可以用 Binary Log 救回來。由於我都直接 dump 每日備份，所以這個 Replay 用的 Binary Log 當然這就用不到啦！上網找了方法，只要在 my.cnf 裡面找到以下兩行設定：</p>
<p>expire_logs_days = 10<br />
max_binlog_size = 100M</p>
<p>把 size 和保留天數調整一下就可以了。之後重新啟動 MySQL 即可見效，如果試了很多次 MySQL 都再起不能的話，直接把 /var/log/mysql 裡面的 binary 檔都移掉，這樣就可以正常重新啟動了。</p>
<p><strong>注意：執行這個作法之前，請確定資料庫備份方案足夠完善，以免哪天 crash 沒有東西可以備份還原。</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.roga.tw/2008/10/mysql-%e9%a9%9a%e9%ad%82%e8%a8%98/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

