How to Fix MySQL Error 4133 (ER_LOAD_BULK_DATA_UNSORTED)
You trigger mysql error 4133 when a bulk load expects primary key order, but rows arrive unsorted. You fix it by sorting the input files by the primary key before running the import. Database teams use bulk data loads to import huge file sets at high speed. The MySQL engine can skip extra sorting work when you tell it the input is pre-sorted. If a single row breaks that sort order, the engine stops the job right away. It returns symbol ER_LOAD_BULK_DATA_UNSORTED with SQLSTATE HY000 and fails th
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You trigger mysql error 4133 when a bulk load expects primary key order, but rows arrive unsorted. You fix it by sorting the input files by the primary key before running the import.
Database teams use bulk data loads to import huge file sets at high speed. The MySQL engine can skip extra sorting work when you tell it the input is pre-sorted. If a single row breaks that sort order, the engine stops the job right away. It returns symbol ER_LOAD_BULK_DATA_UNSORTED with SQLSTATE HY000 and fails the load into the target table.
What causes mysql error 4133 in the database engine
MySQL stores table rows in a tree structure based on the primary key. Building this tree from scratch is much faster when rows arrive in exact ascending order. When you tell the bulk loader that data is pre-sorted, the engine trusts your input. It writes rows straight into leaf pages and checks each new key against the last one.
If the next key is smaller than or equal to the prior key, the check fails. The engine throws error code 4133 and halts the import to protect the table index. This issue often comes from a few common pipeline mistakes.
First, the export tool and the target table might use different text sort rules. One system may sort uppercase letters before lowercase letters, while the database expects the reverse. Second, multi-column primary keys require sorting by every key column in the exact table order. Sorting only by the first column leaves secondary key columns out of order.
Third, parallel export jobs often split data into chunks that overlap or lack global order. Fourth, text files can sort numbers as strings, placing ten before two during export. Any of these mismatches will break the strict ascending order that the bulk loader requires.
How to diagnose and fix unsorted bulk load failures
Start by reading the full error message in your database log. The message names the failed table and gives details from the storage engine. Next, check the primary key columns and sort rules on that target table. Compare those rules with the query or tool that created your source files.
MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
You have two safe ways to fix this failure in production. The best fix is to sort the source data before you run the bulk load. Run your export job with an explicit sort clause that matches the target primary key. Make sure the export step uses the exact same character set and sort rules as MySQL. If you use external text tools to sort files, set numeric flags for number columns.
The second option is to remove the primary key order hint from your load command. Without that hint, the database does the sorting work itself during the import. This approach takes more memory and time, but it handles unsorted files safely.
Keeping production bulk loads safe and controlled
Never disable primary keys or unique checks on live tables just to force a load through. Skipping index checks on bad data can corrupt your table tree and cause outages. Always test your fix on a staging table with a small sample file first.
You should also limit who can run bulk load commands in production. Bulk loads read raw files and use heavy disk and memory resources. Grant bulk load rights only to specific service roles that run verified data pipelines. If engineers must inspect source files containing user data, apply data masking to the query results. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in results.
Add wire guardrails and masking beside MySQL
Database grants and TLS setup decide who can reach MySQL. Even so, an open session can run broad updates or read raw personal data from allowed tables. In practice, hoop.dev sits inline in the wire protocol beside the database as a sidecar for your MySQL. On every production query an agent or engineer sends, hoop.dev decides to pass, mask, block, or escalate it for human review.
With hoop.dev MySQL support, guardrails refuse bad queries before they run or hold risky calls for approval, while data masking rewrites matched sensitive values in result rows in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for GDPR, HIPAA, and PCI DSS programs. Read the docs and source on GitHub.
Frequently asked questions
What does ER_LOAD_BULK_DATA_UNSORTED mean in MySQL?
It means your bulk load command promised data sorted by the primary key, but the engine found out-of-order rows. MySQL stops the load with error 4133 and SQLSTATE HY000 to prevent index damage.
Why does my pre-sorted CSV file still trigger error 4133?
Your sorting tool likely used different rules than the target MySQL table. Check for text versus number sorting, character set differences, or missing columns in a multi-part primary key.
Can I run a bulk load without sorting the input files first?
Yes, you can omit the primary key order option from your bulk load statement. The database will sort the rows for you, though the import will use more time and memory.
Related guides
In this post