时间:2021-07-01 10:21:17 帮助过:32人阅读
error_reporting(E_ALL & ~E_NOTICE);
//接收参数
$mysql_host = XX.XX.XX.XX;
$mysql_user = XXX;
$mysql_pass = XX;
$mysql_port = 3306;
$mysql_db = 'test';
$table_pre = 'log_';
$gz_log_file = a.gz;
//脚本执行日志
$exec_log = '/data_log/record.txt';
file_put_contents ($exec_log,'*****************************************START***********************************'."\r\n",FILE_APPEND );
file_put_contents ($exec_log,'param is mysql_host='.$mysql_host.' mysql_user='.$mysql_user.' mysql_pass='.$mysql_pass.' mysql_port='.$mysql_port.' mysql_db='.$mysql_db.' table_pre='.$table_pre.' gz_log_file='.$gz_log_file.' start_time='.date("Y-m-d H:i:s")."\r\n",FILE_APPEND );
//读日志入库
$z_handle = gzopen($gz_log_file,'r');
$time_start = microtime_float();
$mysql_value_ary = array();
//链接数据库
$conn = mysql_connect("$mysql_host:$mysql_port",$mysql_user,$mysql_pass);
if (!$conn) {
file_put_contents ($exec_log,'Could not connect database error, error='.mysql_error()."\r\n",FILE_APPEND );
exit;
}
$selec_db = mysql_select_db($mysql_db);
if(!$selec_db){
file_put_contents ($exec_log,'select database error, database='.$mysql_db."\r\n",FILE_APPEND );
exit;
}
while(!gzeof($z_handle)){
$each_gz_line = gzgets($z_handle, 4096);
$line_to_array = explode("\t",$each_gz_line);
//过滤无效日志
if(!empty($line_to_array[3]) && !empty($line_to_array[2]) && !empty($line_to_array[4])){
$insert_value = "('".$line_to_array[3]."','".$line_to_array[2]."','".$line_to_array[1]."','".$line_to_array[4]."','".$line_to_array[0]."') ";
$insert_sql = "insert into $table_name (uid,email,ip,ctime) values $insert_value ";
$table_id = abs(crc32($line_to_array[2]) % 1000);
$table_name = $table_pre.$table_id;
$result = mysql_query($insert_sql);
if(!$result){
//如果插入错误,则记录日志
file_put_contents ($exec_log,'table_name='.$table_name.' email='.$line_to_array[2]."\r\n",FILE_APPEND );
}
}
}
$time_end = microtime_float();
$diff = $time_end - $time_start;
file_put_contents ($exec_log,'success to insert database,log_file is '.$gz_log_file.' time-consuming is='.$diff."s \r\n",FILE_APPEND );
file_put_contents ($exec_log,'*******************************************END***********************************'."\r\n",FILE_APPEND );
gzclose($z_handle);