定時執行mysql數據庫任務方案

命題: 每日 02:00 am 定時刪除 表 t (id int primary key,ptime TIMESTAMP DEFAULT CURRENT_TIMESTAMP) 中三天之前的記錄。html


=======================================
方法一:操做系統
=======================================


(Windows)

在WINDOWS 操做系統中 的計劃任務中定時操做,  
進入WINDOWS的命令行, (開始->運行-> 輸入 cmd 回車)
mysql

C:\>at 02:00 /every:M,T,W,Th,F,S,Su "C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe" -u root -p123 csdn -e "delete from t where ptime <date_sub(curdate(),interval 3 day)"
Added a new job with job ID = 1


C:\>at
Status ID   Day           Time      Command Line
-------------------------------------------------------------------------------
    1   Each Monday...      上午 02:00  "C:\Program Files\MySQL\MySQL
Server 5.1\bin\mysql.exe" -u root -p123 csdn -e "delete from t where ptime <date_
sub(curdate(),interval 3 day)"


linux

C:\>sql

linux
windows

一、建立backup.sh文件


#!/bin/sh


export MYSQLBAKDATE=`date +%Y%m%d`
# echo $MYSQLBAKDATE
export OUTFILE=$MYSQLBAKDATE'.TestDB.dbf'
# echo $OUTFILE
rm -f /bak/$OUTFILE
/usr/local/mysql/bin/mysqldump -uroot -p****** -R TestDB > /bak/$OUTFILE
gzip /bak/$OUTFILE


#END




若是考慮得再周全點,能夠考慮刪除一個星期或一個月以前的備份文件。


二、使用crontab -e命令編輯計劃任務,加入如下一條記錄




30 02 * * * sh /usr/local/mysql/backup.sh




表示在凌晨2點30分作備份
spa

=======================================
方法二:MySQL事件 Event
=======================================

MySQL 5.1.6 以後,你能夠直接使用相似於oralce job的mysql event

首先,檢查一下你的MYSQL是否打開了這項功能。
操作系統

mysql> show variables like '%sc%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | OFF   |
+-----------------+-------+
1 row in set (0.00 sec)
.net


默認下是未打開的,你能夠到 my.ini (windows)/ my.cnf (linux/unix) 參數文件中定義後重啓mysqld
或者直接 set global event_scheduler=on;
命令行

mysql> show variables like '%sc%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | ON    |
+-----------------+-------+
1 row in set (0.00 sec)
unix


建立這個event


mysql> CREATE EVENT myevent
    -> ON SCHEDULE EVERY 1 DAY STARTS '2009-10-24 02:00:00'
    -> DO
    ->  delete from t where ptime <date_sub(curdate(),interval 3 day);
Query OK, 0 rows affected (0.09 sec)


mysql> show events;
+------+---------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+--
| Db   | Name    | Definer        | Time zone | Type      | Execute at | Interval value | Interval field | Starts              | Ends | Status  | O
+------+---------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+--
| csdn | myevent | root@localhost  | SYSTEM    | RECURRING | NULL       | 1              | DAY            | 2009-10-24 02:00:00 | NULL | ENABLED |  
+------+---------+----------------+-----------+-----------+------------+----------------+----------------+---------------------+------+---------+--
1 row in set (0.00 sec)


mysql>


關於EVENT的介紹(很遺憾,沒有中文版的)

http://dev.mysql.com/doc/refman/5.1/en/events.html
19.4. Using the Event Scheduler
19.4.1. Event Scheduler Overview  
19.4.2. Event Scheduler Configuration  
19.4.3. Event Syntax  
19.4.4. Event Metadata  
19.4.5. Event Scheduler Status  
19.4.6. The Event Scheduler and MySQL Privileges  

關於create event 的語法和例子。
http://dev.mysql.com/doc/refman/5.1/en/create-event.html
12.1.11. CREATE EVENT Syntax

相關文章
相關標籤/搜索