<?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>alter &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/alter/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Sat, 07 Nov 2020 15:29:15 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>alter &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MySQL 排程(Event Scheduler)</title>
		<link>https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/</link>
					<comments>https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Sat, 29 Sep 2012 06:12:19 +0000</pubDate>
				<category><![CDATA[資料庫]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[event_scheduler]]></category>
		<category><![CDATA[general_log]]></category>
		<category><![CDATA[排程]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[mariadb]]></category>
		<guid isPermaLink="false">http://wazai.net/?p=2306</guid>

					<description><![CDATA[<p>一段陣子沒清vps上的東西，同步到Dropbox的檔案瞬間爆增，每隔一段時間都要定期手動清一下，今天一有時間索性加入crontab -e排程一勞永逸，還有另外一個問題，由於我的股票精算師只留七日內的文&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>一段陣子沒清vps上的東西，<strong><a rel="noreferrer noopener nofollow external" href="http://wazai.net/2166" target="_blank" data-wpel-link="external" class="wpel-icon-right">同步到Dropbox<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>的檔案瞬間爆增，每隔一段時間都要定期手動清一下，今天一有時間索性<strong><a rel="noreferrer noopener nofollow external" href="http://wazai.net/2000" target="_blank" data-wpel-link="external" class="wpel-icon-right">加入crontab -e排程<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>一勞永逸，還有另外一個問題，由於我的股票精算師只留七日內的文章，也面臨相同的問題，為了不想讓資料庫文章量太大，造成效能低落，因此希望可以把定期清理資料庫的動作改為自動化，要自動化有很多種做法，也可以用上面提到的 crontab -e 加入排程，再透過 php 或其他方式執行清理的 SQL，Google一下發現 MySQL 有 <strong><a rel="noreferrer noopener nofollow external" href="http://dev.mysql.com/doc/refman/5.1/en/create-event.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">Event<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong> 這個排程的東東，就來試用看看囉！ </p>



<span id="more-2306"></span>



<p>小蛙就直接記錄自己做了哪些事情，從<strong><a rel="noreferrer noopener nofollow external" href="http://dev.mysql.com/doc/refman/5.1/en/create-event.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">官網<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>上面以及參考資料中查到建立 event 的基本方法 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">CREATE
    [DEFINER = { user | CURRENT_USER }]
    EVENT
    [IF NOT EXISTS]
    event_name
    ON SCHEDULE schedule
    [ON COMPLETION [NOT] PRESERVE]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'comment']
    DO event_body;
schedule:
    AT timestamp [+ INTERVAL interval] ...
    | EVERY interval
    [STARTS timestamp [+ INTERVAL interval] ...]
    [ENDS timestamp [+ INTERVAL interval] ...]
interval:
    quantity {YEAR | QUARTER | MONTH | DAY | HOUR | MINUTE |
    WEEK | SECOND | YEAR_MONTH | DAY_HOUR | DAY_MINUTE |
    DAY_SECOND | HOUR_MINUTE | HOUR_SECOND | MINUTE_SECOND}</pre>



<h2 class="para wp-block-heading">建立 MySQL Event</h2>



<p>看起來有點複雜，用下面的簡單例子直接套用(<strong><a rel="noreferrer noopener nofollow external" href="http://blog.sina.com.cn/s/blog_548668f1010007pu.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">這個部落格<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>有非常詳細的說明)，小蛙要的功能是「<strong>馬上把7天以前的文章刪除，並且每天執行一次刪除動作</strong>」，這樣就簡單多了，不需要用到上面複雜的說明。 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">CREATE EVENT my_event
    ON SCHEDULE EVERY 1 DAY STARTS CURRENT_TIMESTAMP
&nbsp;&nbsp;&nbsp;&nbsp;DO&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DELETE FROM `mytable` WHERE `publish_date` &lt; DATE_SUB(now(), INTERVAL 7 DAY)</pre>



<p>Line 1: 建立一個叫做 my_event 的事件<br>Line 2: 從現在開始執行一次後，每天都會執行一次<br>Line 3: 要做的動作是刪除 mytable 中，小於現在時間減掉七天(也就是七天前)的資料 </p>



<p>到這邊看起來似乎很簡單，但小蛙為了測試把時間設定成 5 分鐘，遲遲不見 my_event 去做相對應的工作 &gt;&lt;。 </p>



<h2 class="para wp-block-heading">開啟 MySQL log (非必需) </h2>



<p>這邊順便紀錄一下把 MySQL log 打開的過程(參考自<strong><a rel="noreferrer noopener nofollow external" href="http://blog.permastyle.com/2010/12/mysql-%E5%95%9F%E7%94%A8query-log-%E8%88%87-slow-query-log%E5%8A%9F%E8%83%BD.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">這裡<span class="wpel-icon wpel-image wpel-icon-6"></span></a></strong>)</p>



<p>在 /etc/mysql/my.cnf 中的 [mysqld] 區塊內加入 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">general_log_file = /var/log/mysql.log
general_log = 0</pre>



<p>原本被註解掉的話，把他打開，把general_log = 0 改成 1，到上述路徑檢查是否有該 log 檔案，如果沒有的話，建立一個 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">touch /var/log/mysqld.log
chown mysql.mysql /var/log/mysqld.log</pre>



<p>為了避免 mysql 沒有權限寫入該檔案，順便把群組跟所有者改成 mysql，重新啟動 mysql 就可以看到 log 了。這邊要注意的是如果一天的流量很高的話，log 成長的速度會非常快速(會記錄每一條執行的SQL)。 </p>



<h2 class="para wp-block-heading">設定 MySQL Event Scheduler</h2>



<p>很多部落格包括官網提到開啟 event_scheduler 的方法，先利用以下指令檢查是否已開啟 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SELECT @@event_scheduler;</pre>



<p>如果是 OFF 的話，就用以下指令來開啟 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SET GLOBAL event_scheduler := 1;</pre>



<p>小蛙開啟了，但是這邊有一個很大的問題，下次去看的時候卻又是 OFF &#8230; 後來發現應該是因為重新啟動後，又會變回 OFF，這篇文章提到可以直接在 /etc/mysql/my.cnf 中直接設定。 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">event_scheduler = ON</pre>



<p>一定要放在 [mysqld] 區塊中。小蛙一開始放在最底下，結果完全毫無動靜 &gt;&lt;，停止 Event Scheduler </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">ALTER EVENT my_event DISABLE;</pre>



<p>修改 Scheduler </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">ALTER
    [DEFINER = { user | CURRENT_USER }]
    EVENT event_name
    [ON SCHEDULE schedule]
    [ON COMPLETION [NOT] PRESERVE]
    [RENAME TO new_event_name]
    [ENABLE | DISABLE | DISABLE ON SLAVE]
    [COMMENT 'comment']
    [DO event_body]</pre>



<p>顯示所有 Events </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">show events;</pre>



<p>顯示詳細 Events 內容 </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">SELECT * FROM INFORMATION_SCHEMA.EVENTS&nbsp;WHERE EVENT_NAME='mytable';</pre>



<p>刪除 Events </p>



<pre class="wp-block-preformatted wp-block-syntaxhighlighter-code">DELETE FROM mysql.event&nbsp;WHERE db = 'mydb' AND name = 'mytable';</pre>



<h2 class="para wp-block-heading">參考資料</h2>



<ol class="my-li bg-darkblue wp-block-list"><li>修改&nbsp;Event Scheduler @&nbsp;MySQL官網<br><a href="http://dev.mysql.com/doc/refman/5.1/en/alter-event.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://dev.mysql.com/doc/refman/5.1/en/alter-event.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>建立 Event Scheduler @&nbsp;MySQL官網<br><a href="http://dev.mysql.com/doc/refman/5.1/en/create-event.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://dev.mysql.com/doc/refman/5.1/en/create-event.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>MySQL: 事件排程器 (Event Scheduler) @&nbsp;Wax Way<br><a href="http://waxway.blogspot.tw/2011/12/mysql-event-schedule.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">http://waxway.blogspot.tw/2011/12/mysql-event-schedule.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>Enabling the MySQL Event Scheduler @&nbsp;LOGZILLA<br><a href="http://forum.logzilla.pro/index.php?topic=71.0" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">http://forum.logzilla.pro/index.php?topic=71.0<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>mysql event&#8212;-语法之CREATE EVENT @&nbsp;星期八的博客<br><a href="http://blog.sina.com.cn/s/blog_548668f1010007pu.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">http://blog.sina.com.cn/s/blog_548668f1010007pu.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>MySQL日期函數應用 &#8211; @&nbsp;No Money No Honey<br>h<a href="http://blog.infinity.idv.tw/index.php/2009/12/16/mysql%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B8%E6%87%89%E7%94%A8" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">ttp://blog.infinity.idv.tw/index.php/2009/12/16/mysql%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B8%E6%87%89%E7%94%A8<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li><li>[MySQL] 啟用query log 與 slow query log功能 @&nbsp;Allen開放源碼研究室<br><a href="http://blog.permastyle.com/2010/12/mysql-%E5%95%9F%E7%94%A8query-log-%E8%88%87-slow-query-log%E5%8A%9F%E8%83%BD.html" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external" class="wpel-icon-right">http://blog.permastyle.com/2010/12/mysql-%E5%95%9F%E7%94%A8query-log-%E8%88%87-slow-query-log%E5%8A%9F%E8%83%BD.html<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ol>



<p><strong>MySQL / MariaDB / Oracle 相關文章：</strong></p>



<ul class="my-li bg-darkblue wp-block-list">
<li><a href="https://noter.tw/12503/mariadb-json_extract-%e5%be%9e-json-%e6%ac%84%e4%bd%8d%e5%8f%96%e5%80%bc/" data-wpel-link="internal">MariaDB JSON_EXTRACT: 從 JSON 欄位取值</a></li>



<li><a href="https://noter.tw/12015/oracle-%e5%88%86%e7%b5%84%e7%b7%a8%e8%99%9f%e8%88%87%e5%85%a8%e6%8e%92%e5%ba%8f%e7%b7%a8%e8%99%9f-row_number-over-partition-by/" data-wpel-link="internal">Oracle, MySQL 分組編號與全排序編號 row_number, over, partition by</a></li>



<li><a href="https://noter.tw/3929/mysql-error-2003-hy000-%E7%84%A1%E6%B3%95%E9%81%A0%E7%AB%AF%E9%80%A3%E7%B7%9A/" data-wpel-link="internal">MySQL ERROR 2003 (HY000) 無法遠端連線</a></li>



<li><a href="https://noter.tw/2889/jsp-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">JSP 連結 MySQL</a></li>



<li><a href="https://noter.tw/2883/java-%E9%80%A3%E7%B5%90-mysql/" data-wpel-link="internal">Java 連結 MySQL</a></li>



<li><a href="https://noter.tw/2306/mysql%E6%8E%92%E7%A8%8Bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a></li>



<li><a href="https://noter.tw/1988/java%E9%80%A3%E7%B5%90%E9%81%A0%E7%AB%AFmysql/" data-wpel-link="internal">Java 連結遠端 MySQL</a></li>



<li><a href="https://noter.tw/4508/mariadb-replication-%E8%A8%AD%E5%AE%9A/" data-wpel-link="internal">MariaDB Replication 設定</a></li>



<li><a href="https://noter.tw/122/java-php-%E6%8F%92%E5%85%A5-emoji-%E8%B3%87%E6%96%99%E5%88%B0-mariadb/" data-wpel-link="internal">Java / PHP 插入 emoji 資料到 MariaDB</a></li>
</ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/" data-wpel-link="internal">MySQL 排程(Event Scheduler)</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/2306/mysql%e6%8e%92%e7%a8%8bevent-scheduler/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
