Incident Response

MySQL Error 1064 "You have an error in your SQL syntax": How to Fix It

Error 1064 is the parser telling you it got lost. The quoted fragment shows exactly where. Here is how to read it, the causes we see most often, and why the same query starts failing right after a version upgrade.

· 13 min read · AG Data Team

MySQL error 1064, "You have an error in your SQL syntax", means the server could not parse your statement. Nothing was executed and no data was touched. The message reads like a shrug, but the fragment after near tells you exactly where the parser gave up. Usually the mistake is the word just before it.

How to read the message

The error is ER_PARSE_ERROR, number 1064, SQLSTATE 42000, with the format %s near '%s' at line %d (MySQL error reference). MariaDB uses the same number and format (MariaDB error codes). Here is the example from the MySQL manual:

mysql> CREATE TABLE interval (begin INT, end INT);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to use
near 'interval (begin INT, end INT)' at line 1

The quoted text is the rest of the statement, starting at the token the parser could not accept. Here it starts at interval, because INTERVAL is a reserved word and cannot be a table name unquoted (keywords and reserved words). begin and end are keywords too, but not reserved, so they are fine.

Three practical rules:

The causes we see most often

  1. A reserved word used as a name. Columns called rank, groups, system, lead or window worked on MySQL 5.7. They became reserved in 8.0 together with RECURSIVE, LATERAL, OVER, ROW_NUMBER and others (8.0 keywords). MySQL 8.4 adds QUALIFY and TABLESAMPLE as reserved words (8.4 keywords). Quote such names with backticks wherever they appear. Better, rename them while you can.
  2. Double quotes around identifiers. Without the ANSI_QUOTES SQL mode, MySQL treats "name" as a string, not a column (schema object names). Queries ported from PostgreSQL or generated by tools for another database then fail, or worse, return a constant.
  3. Stored procedures and triggers without DELIMITER. The mysql client splits input on ;, so it sends the procedure body to the server in pieces, and the first piece fails with 1064 (defining stored programs). DELIMITER is a client command, not SQL (mysql client commands). GUI tools and drivers that send the whole statement in one call do not need it. If DELIMITER itself reaches the server, the server returns 1064, because it is not SQL.
  4. Syntax that the server version removed. See the next section. This is the cause that bites production systems.
  5. Strings built by hand. An unescaped apostrophe in a name like O'Brien ends the string literal early. The rest of the statement then fails to parse. If you see this in application logs, you have an SQL injection hole as well as a bug: use prepared statements with bound parameters.
  6. Typos and copy-paste damage. Smart quotes from documents, non-breaking spaces, a trailing comma before ) in CREATE TABLE.
-- INTERVAL is reserved: quote it with backticks
CREATE TABLE `interval` (begin INT, end INT);

-- after a period it is always an identifier, no quotes needed
SELECT t.interval FROM schedule AS t;

-- double quotes are string literals unless ANSI_QUOTES is set
SELECT "id" FROM orders;        -- returns the text 'id'
SET SESSION sql_mode = CONCAT(@@sql_mode, ',ANSI_QUOTES');
DELIMITER //
CREATE PROCEDURE archive_orders(IN days INT)
BEGIN
  INSERT INTO orders_archive
    SELECT * FROM orders WHERE created_at < NOW() - INTERVAL days DAY;
  DELETE FROM orders WHERE created_at < NOW() - INTERVAL days DAY;
END //
DELIMITER ;

"It works on my machine." When the same statement succeeds in one place and fails with 1064 in another, compare SELECT VERSION(), @@sql_mode; on both. A different server version or a missing ANSI_QUOTES explains most of these cases in our experience.

Upgrades: when 1064 is a version problem

Each major version removes old syntax, and a removed statement is simply unknown to the parser. MySQL 8.0 removed the PASSWORD() function, so SET PASSWORD … = PASSWORD('…') no longer exists, and also removed changing account authentication through GRANT (What is new in MySQL 8.0). MySQL 8.4 removed the old replication statements: CHANGE MASTER TO, RESET MASTER, SHOW MASTER STATUS, START SLAVE, SHOW SLAVE STATUS and the rest of that family (What is new in MySQL 8.4).

-- removed in MySQL 8.0
SET PASSWORD FOR 'app'@'%' = PASSWORD('secret');
GRANT SELECT ON app.* TO 'app'@'%' IDENTIFIED BY 'secret';
-- use instead
ALTER USER 'app'@'%' IDENTIFIED BY 'secret';
CREATE USER 'app'@'%' IDENTIFIED BY 'secret';
GRANT SELECT ON app.* TO 'app'@'%';

-- removed in MySQL 8.4
SHOW MASTER STATUS;      -- SHOW BINARY LOG STATUS
SHOW SLAVE STATUS;       -- SHOW REPLICA STATUS
START SLAVE;             -- START REPLICA
CHANGE MASTER TO ...;    -- CHANGE REPLICATION SOURCE TO ...
RESET MASTER;            -- RESET BINARY LOGS AND GTIDS

Application code is rarely the problem here. The usual victims are the things nobody reads before an upgrade: monitoring checks that run SHOW SLAVE STATUS, backup scripts that run SHOW MASTER STATUS to record a position, provisioning playbooks with CHANGE MASTER TO, and old user-management scripts. After an upgrade they fail quietly, and a replica lag alert that cannot parse its own query never fires. See our MySQL 8.0 to 8.4 upgrade guide for the full list of changes to plan for.

What changes on large systems

# MySQL Shell: run against the OLD server before upgrading
mysqlsh -- util check-for-server-upgrade \
  { --user=admin --host=old-primary --port=3306 } \
  --target-version=8.4.0 --output-format=JSON > upgrade-check.json
-- parse errors by account since restart (or since the table was truncated)
SELECT USER, HOST, SUM_ERROR_RAISED, FIRST_SEEN, LAST_SEEN
FROM performance_schema.events_errors_summary_by_account_by_error
WHERE ERROR_NUMBER = 1064 AND SUM_ERROR_RAISED > 0
ORDER BY SUM_ERROR_RAISED DESC;

Key takeaways

MySQL Errors SQL Upgrades
Work with us

Queries broke after a MySQL upgrade?

We plan and run MySQL upgrades for production systems: compatibility checks, application query review, replicas as test targets and a rollback plan.

Get in touch More from the blog