MySQL导出数据
概述
从MySQL导出数据对于备份、迁移和数据共享至关重要。MySQL提供了多种方法以不同格式导出数据。
导出方法
mysqldump:完整数据库导出 Text export / SELECT INTO OUTFILE:直接CSV/文本导出 MySQL Workbench:GUI导出 编程API:应用程序级导出
mysqldump导出
基本用法
bash
# Export single database
# Export all databases
# Export specific tables
# Export with structure only
# Export with data only常用选项
bash
# Export with routines (procedures, functions)
# Export with triggers
# Export without triggers
# Export with events
# Export with extended insert
# Export with lock tables
# Export with single transaction压缩导出
bash
# Export and compress with gzip
# Export and compress with bzip2
# Export with different compression levelsSELECT INTO OUTFILE / SELECT INTO OUTFILE
CSV导出
sql
-- Export to CSV
-- Export with headers自定义分隔符
sql
-- Export with tab delimiter
-- Export with pipe delimiter安全文件权限
sql
-- Check secure file priv
-- If ON, can only export to specific directory
-- Usually /var/lib/mysql-files/ or similar
-- Export to allowed directory导出格式
CSV格式
sql
-- Standard CSV exportJSON格式
sql
-- Export as JSON (MySQL 5.7.8+)XML格式
sql
-- Use mysqldump for XML
-- Export with specific XML format部分导出
过滤导出
sql
-- Export with WHERE clause
-- Export specific columns
-- Export with JOINmysqldump过滤器
bash
# Export with WHERE clause
# Export ignoring specific tables
--ignore-table=database_name.temp_data \
# Export excluding specific columns (requires --no-create-info)大数据导出
批量导出
sql
-- Export in chunks to avoid memory issues
-- Method 1: LIMIT and OFFSET
-- Method 2: Date range进度监控
bash
# Use pv to monitor progress
# Count exported rows从复制导出
一致导出
bash
# Export from slave to reduce load on master
# Ensure slave is consistent
# Export data
--single-transaction database_name > backup.sql
# Restart slave导出最佳实践
安全考虑
bash
# Encrypt exported data
# Set file permissions
# Export to secure location
# Remove sensitive data before export
-- Exclude password, credit_card, etc.导出计划
bash
# Create cron job for daily backup
# Edit crontab
# Weekly full backup小结
MySQL数据导出方法包括:
mysqldump:完整数据库转储 SELECT INTO OUTFILE:直接文件导出 多种格式:SQL、CSV、JSON、XML 过滤导出:部分数据导出 压缩导出:节省空间
根据您的需求选择适当的方法:完整备份、部分导出或格式要求。
上一个:SQL注入
下一个:导入数据