可以写py脚本导出,但是感觉没有直接写sql灵活,于是写了一个函数,直接传一段sql,导出csv到指定目录:
(1)nas上挂载一个目录:
mount -t xfs -o defaults,rw,noatime,nodiratime,nobarrier,noikeep,attr2,largeio,inode64,swalloc,allocsize=1 172.18.x.x:/volume1/DBBackup /mnt/csv
(2)my.cnf添加配置(需要重启实例)
#启用LOAD DATA功能 local_infile=1 secure_file_priv=/mnt/csv
mysql8.0说最好不要开启 secure_file_priv 这个,因为不太安全,这个还是看管理情况了,我开启了,然后nas映射目录。然后直接数据库导出导指定目录
(3)写sql函数:
delimiter $$ use `report`$$ drop procedure if exists `proc_exp_select_tab`$$ create definer=`report`@`%` procedure `proc_exp_select_tab`(in i_sql varchar(2000),tab varchar(40)) begin declare tb_name varchar(40); set @tab_name=concat(tab,date_format(now(),'%h%i%s'),'.csv'); set tb_name=@tab_name; set @exe_sql=concat('with t as(',i_sql,')','select * into outfile \'/mnt/csv/',tb_name,'''',' character set gbk fields terminated by \',\' optionally enclosed by \'"\' lines terminated by \'\\n\' from t'); -- write the resultset to the file prepare stmt from @exe_sql; execute stmt; deallocate prepare stmt; select concat('请访问url : ','http://xxxxxxx',' : ',tb_name) as '文件信息'; end$$ delimiter ;age:
ege:
call proc_exp_select_tab('select xxxxx','test')
然后数据就存到操作系统 /mnt/csv/ 这个目录,nas上在配置一个前端nginx访问的的地址,直接web就可以下载刚才的导出的sql语句了。