Skip to content

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 number

Source命令

执行SQL文件

sql
-- Source SQL file

-- Short form

-- From MySQL command line

-- From within MySQL

导入多个文件

bash
# Import sequentially

# Or from MySQL client

MySQL客户端导入

命令行导入

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 delimiter

TSV导入

sql
-- Tab-separated values

JSON导入

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函数