MySQL导入数据
概述
将数据导入MySQL对于数据迁移、批量加载和数据库初始化是必要的。MySQL提供了导入各种数据格式的高效方法。
导入方法
LOAD DATA INFILE:快速批量导入 Source命令:执行SQL文件 MySQL客户端:交互式导入 MySQL Workbench:GUI导入 编程API:应用程序级导入
LOAD DATA INFILE / LOAD DATA INFILE
基本用法
sql
-- Simple CSV import
-- Import from client machine (MySQL 8.0+)导入选项
sql
-- Import with column mapping
-- Skip header row
-- Replace existing data
-- Ignore duplicates高级导入选项
sql
-- Import with character set
-- Import with null handling
-- Import with starting line numberSource命令
执行SQL文件
sql
-- Source SQL file
-- Short form
-- From MySQL command line
-- From within MySQL导入多个文件
bash
# Import sequentially
# Or from MySQL clientMySQL客户端导入
命令行导入
bash
# Import single database
# Import all databases
# Import with compression
# Import with specific database
# Import with username prompt批量导入
bash
# Import all SQL files in directory
# Import with logging导入格式
CSV导入
sql
-- Standard CSV import
-- CSV with specific columns
-- CSV with different delimiterTSV导入
sql
-- Tab-separated valuesJSON导入
sql
-- MySQL 8.0+ JSON import
-- Method 1: Use JSON functions
-- Method 2: Load and parse
-- Then use JSON functions in UPDATE大数据导入
分块导入
sql
-- Split large CSV into chunks
-- Chunk 1
-- Chunk 2
-- Repeat for all chunks性能优化
sql
-- Disable indexes during import
-- Import data
-- Re-enable indexes
-- Or use unique checks批量导入选项
sql
-- Set buffer size
-- Import with transactions
-- Import data
-- Commit
-- Reset autocommit导入错误处理
错误日志
bash
# Import with error logging
# Check for errors
# Check LOAD DATA errors常见错误
sql
-- File not found
-- Error: Can't get stat of file
-- Solution: Check file path and permissions
-- Access denied
-- Error: Access denied for user
-- Solution: Check FILE privilege and file permissions
-- Syntax error
-- Error in SQL syntax
-- Solution: Check CSV format and delimiters
-- Data truncation
-- Warning: Data truncated
-- Solution: Check column data types验证
sql
-- Verify import
-- Compare with source
-- Check for NULLs
-- Check data integrity从复制导入
主导入
bash
# Import to master and setup replication
# Step 1: Stop slave
# Step 2: Import data to master
# Step 3: Configure replication on master
# Step 4: Start slave导入最佳实践
准备工作
sql
-- Backup existing data
-- Before import, backup current data
-- Validate data
-- Check for issues before importing
-- Check character encoding, delimiters, etc.
-- Clean target table
-- Or create new table and rename later
-- Or: CREATE TABLE users_new LIKE users;安全
bash
# Import as limited user
-- Don't import as root if possible
# Validate source file
-- Check for malicious SQL
-- Use prepared statements
-- For application-level imports性能
sql
-- Optimize import settings
-- Disable foreign key checks
-- Import data
-- Re-enable checks小结
MySQL数据导入方法包括:
LOAD DATA INFILE:快速批量加载 Source命令:执行SQL文件 多种格式:SQL、CSV、TSV、JSON 大数据:分块导入以优化 错误处理:验证和日志记录 性能:优化设置
根据数据大小、格式和性能需求选择适当的方法。
上一个:导出数据
下一个:MySQL函数