How to Fix MySQL Error 3684 (ER_REGEXP_STRING_NOT_TERMINATED)

Published
Length
3 min read

Luis Felipe Dias

All posts

You see mysql error 3684 when a regular expression output string exceeds its buffer size. To fix it, reduce the replacement text size or adjust the packet size limit. The database returns the symbol ER_REGEXP_STRING_NOT_TERMINATED along with the general SQLSTATE code HY000. The official message states that an output string could not be zero-terminated in memory. This happens because the string length goes past the available buffer space during execution. When the engine runs out of room, it sto

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 see mysql error 3684 when a regular expression output string exceeds its buffer size. To fix it, reduce the replacement text size or adjust the packet size limit.

The database returns the symbol ER_REGEXP_STRING_NOT_TERMINATED along with the general SQLSTATE code HY000. The official message states that an output string could not be zero-terminated in memory. This happens because the string length goes past the available buffer space during execution. When the engine runs out of room, it stops the query right away. This safeguard keeps the database from writing past its memory bounds and crashing.

What causes mysql error 3684 in the database engine

MySQL uses the International Components for Unicode library to run regular expression tasks. When you run a regex replace query, the engine builds a new output string. It places this new string inside a fixed memory buffer before returning results. Every string in this buffer needs a null byte at the very end. That final zero byte tells the engine where the text stops in memory.

If your replacement pattern expands the original text too much, the buffer fills up. The output string takes every last byte of space in the allocated memory block. As a result, the engine has no room left to write the zero terminator. Rather than return broken text, MySQL stops the query and throws error 3684.

This failure often shows up when queries process huge text columns or long logs. Repeating capture groups inside a replacement pattern can also multiply string length fast. Multi-byte character sets make the problem worse because each character takes more memory space. Even a short text string can grow past buffer limits if patterns repeat.

How to find and fix the regex buffer failure in production

Start by finding the exact query and column that trigger the buffer overflow. Check your query logs to see which regex replace call failed during the run. Do not grant broad admin roles to developers just to debug live data. Instead, check the text length of the target rows using a safe read role. You can measure character length and byte size without reading sensitive row content.

Once you find the oversized rows, look closely at your regular expression pattern. Broad wildcard patterns often match far more text than the author planned to replace. Make the match rule more specific so it only replaces small text segments. If you duplicate matched groups in the output, check how much the string grows. Trimming the replacement text keeps the final result inside the buffer capacity.

MySQL Access Governance + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Sometimes the query must process large text blocks as part of a normal job. In that case, break the update or select task into smaller text chunks. You can run the regex function on substrings instead of the whole document column. You should also check the server limit for the maximum allowed packet size. Raising that memory limit gives string functions more room to finish their work. Always test these server setting changes in a staging setup before updating production.

How to prevent regex buffer overflows in future queries

Good data rules stop regex buffer errors before they reach your live database. Set strict length limits on text columns when you design your database tables. Do not store giant raw log files inside a single text column. Break large documents into smaller rows so string queries use less memory per row.

Teams should also review complex string queries before running them on production tables. When analysts clean up sensitive fields like emails or phone numbers, keep patterns simple. Data masking rewrites matched fields in results, so replacement strings should stay short. Replacing a match with a short fixed label uses very little buffer space. This simple habit keeps memory use low and prevents error 3684 during heavy workloads.

Inspect every MySQL query and response in flight

Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev sits inline in the MySQL protocol as a sidecar for your MySQL. For every plain query, prepared statement, or cursor an agent or human runs in production, hoop.dev decides to pass, mask, block, or hold it for review.

First, hoop.dev guardrails block banned actions before MySQL runs them, and an AI Analyzer you configure can score intent or escalate for approval. Next, hoop.dev data masking rewrites sensitive string columns in results in memory, and every verdict goes to the audit trail. Find the open-source binary on GitHub.

Frequently asked questions

What does ER_REGEXP_STRING_NOT_TERMINATED mean in MySQL?

It means a regular expression function created a string too big for its buffer. Because the buffer is full, MySQL cannot add the required ending zero byte.

Does mysql error 3684 corrupt data in my tables?

No, this error does not harm or corrupt your stored table data. The engine cancels the query before writing any incomplete strings to disk.

Can character sets trigger mysql error 3684 more often?

Yes, multi-byte character sets use more buffer memory for every single character. Switching to a wide character set can push large regex outputs past buffer limits.

The query runs. The emails stay hidden.