简体   繁体   English

MySQL清除shell脚本

[英]MySQL purging shell script

I have a mysql table with 120 million rows and I want to write a shell script to start purging some of that useless information in that table that's not needed. 我有一个具有1.2亿行的mysql表,我想编写一个shell脚本来开始清除该表中不需要的一些无用信息。 Problem is I'm super new to shell scripting. 问题是我是Shell脚本的超级新手。

I have a datetime column with a unix timestamp in it. 我有一个datetime列,里面有一个unix时间戳。 I want to delete every row that's not within the last 2 months since I've recently enacted a data retention policy that will allow me to only keep 2 months of certain data. 自最近制定了一项数据保留政策以来,我想删除过去两个月内未出现的每一行,该政策只允许我保留2个月的某些数据。

TL;DR Need to build a shell script that deletes all rows up until the last 2 months of data by using the unix timestamp in the datetime column. TL; DR需要构建一个shell脚本,该脚本通过使用datetime列中的unix时间戳来删除直到最后两个月数据为止的所有行。

UPDATE: Here's my new shell script 更新:这是我的新shell脚本

#!/bin/sh

i=1
while [ "$i" -ne 0 ]
do
  i=mysql -h 127.0.0.1 -u halo_ylh -pCa5d8a88 halo_twittstats < mysqlpurge.sql
  sleep 10
done

Wouldn't it be easier to just use the current_timestamp and unix_timestamp function to execute: 仅使用current_timestampunix_timestamp函数执行会不会更容易:

DELETE FROM Table1 
 WHERE datetime < unix_timestamp(current_timestamp - interval 2 month)

To run it from the shell you could put that command in a file script1.sql and run it using the mysql command line tool: 要从外壳运行它,可以将该命令放在文件script1.sql ,然后使用mysql命令行工具运行它:

mysql db_name < script1.sql
mysql --host ? -user ? -password ? {DB_NAME} < dump_script.sql > data.dat 

dump_script.sql will have your SELECT statememt to retrieve the data you want archived. dump_script.sql将具有SELECT状态存储器,以检索要归档的数据。 it will store the output in data.dat 它将输出存储在data.dat中

then 然后

mysql --host ? -user ? -password ? {DB_NAME} < delete_script.sql

delete_script.sql will cotain the DELETE statement with the same WHERE clause as in dump_script.sql. delete_script.sql将使用与dump_script.sql中相同的WHERE子句包含DELETE语句。

Make sure you lock the table so that nothing writes in between the two script exections that could make it into the WHERE clause to avoid phantom inserts that would be deleted by the delete script yet not be included in the dump script. 确保锁定表,以免在两个脚本执行之间写入任何可能使它进入WHERE子句的脚本,以避免幻影插入被删除脚本删除但不包含在转储脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM