Incident Response

MySQL Error 1146 "Table doesn't exist": Causes and Fixes

The table is right there in SHOW TABLES, or it was there yesterday, and MySQL still says it does not exist. Here is how to tell a wrong schema from a letter-case problem, a broken view, a half-copied tablespace or a replica that skipped the CREATE.

· 11 min read · AG Data Team

MySQL error 1146, "Table doesn't exist", means the server found no table with that exact schema and name. The error message spells out both, Table 'db.name' doesn't exist, and that is the first thing to read carefully. In our experience the table usually does exist, just in another schema, on another server, or with different letter case.

What the error means

The error is ER_NO_SUCH_TABLE, SQLSTATE 42S02 (MySQL error reference). Several neighbouring errors mean something else, and it helps not to mix them up:

mysql> SELECT * FROM Orders LIMIT 1;
ERROR 1146 (42S02): Table 'shop.Orders' doesn't exist

Step 1: confirm where you are

SELECT DATABASE(), @@hostname, @@port, @@lower_case_table_names;

-- every table with that name, in any schema and any letter case
SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
FROM information_schema.TABLES
WHERE LOWER(TABLE_NAME) = LOWER('Orders');

This one query answers most tickets. If the table shows up under another TABLE_SCHEMA, the connection's default schema or the application's config is wrong. If it shows up with different letter case, read the next section. If it does not show up at all, check @@hostname and @@port: a client pointed at a replica, a staging instance or a local MySQL on 3306 will tell you the truth about the wrong server.

Cause 1: letter case and lower_case_table_names

Table names map to files, so case sensitivity depends on the file system. The defaults for lower_case_table_names are 0 on Unix, 1 on Windows and 2 on macOS. With 0, names are compared case-sensitively, so Orders and orders are different tables on Linux (identifier case sensitivity).

-- Linux, lower_case_table_names=0 (default on Unix)
CREATE TABLE Orders (id INT PRIMARY KEY);
SELECT * FROM orders;   -- ERROR 1146: Table 'shop.orders' doesn't exist
SELECT * FROM Orders;   -- works

The classic version: code written on a Windows or macOS laptop says orders, the table was created as Orders, and everything breaks after the first deploy to Linux. The fix is to make code and schema agree. Do not plan on flipping the setting: lower_case_table_names can only be set when the server is initialized, and changing it afterwards is prohibited (docs). If you need case-insensitive names on Linux, that means a new instance initialized with lower_case_table_names=1 and a logical dump and reload.

Cause 2: a view points to a table that is gone

Dropping or renaming a table does not drop the views built on it. Queries against the view then fail with 1356, and it is easy to misread as a missing table. CHECK TABLE on the view reports tables that its definition refers to but that no longer exist (CHECK TABLE).

mysql> SELECT * FROM v_daily_sales;
ERROR 1356 (HY000): View 'shop.v_daily_sales' references invalid table(s)
or column(s) or function(s) or definer/invoker of view lack rights to use them

CHECK TABLE v_daily_sales;
SHOW CREATE VIEW v_daily_sales\G

Cause 3: files copied into the data directory

Since MySQL 8.0, table metadata lives in a transactional data dictionary instead of metadata files (data dictionary). In our experience, a .ibd file dropped into a schema directory does not become a table, and a restore that copied only some files leaves you with 1146 or worse. To move a single InnoDB table between servers, use transportable tablespaces (importing InnoDB tables):

-- destination: same definition as the source table
CREATE TABLE t1 (...) ENGINE=InnoDB;
ALTER TABLE t1 DISCARD TABLESPACE;

-- source: keep this session open until the copy is done
FLUSH TABLES t1 FOR EXPORT;
-- copy t1.ibd and t1.cfg to the destination schema directory
UNLOCK TABLES;

-- destination
ALTER TABLE t1 IMPORT TABLESPACE;

The rules that bite in practice:

For whole databases, a physical backup tool or a logical dump is the safer route; see our mysqldump vs XtraBackup comparison.

Cause 4: temporary tables and connection pools

A TEMPORARY table is visible only within the session that created it and disappears when that session closes (CREATE TEMPORARY TABLE). With a connection pool, the next query may run on a different connection. In our experience, code that creates a temp table in one call and reads it in the next works on a developer's machine and fails under load with 1146.

Cause 5: the replica never created the table

On a replica, 1146 stops the SQL thread, and SHOW REPLICA STATUS shows Last_SQL_Errno: 1146. A frequent reason is database-level replication filters. With statement-based logging, replicate-do-db checks the default database set by USE. With row-based logging, it checks the database of the changed table. DDL is always logged as a statement, even with binlog_format=ROW (replication filtering rules). So a CREATE TABLE shop.x issued while another database is selected can be filtered out, while the rows that follow are applied and fail.

-- replica started with replicate-do-db=shop, source runs:
USE tools;
CREATE TABLE shop.audit_log (...);   -- DDL is logged as a statement,
                                     -- checked against "tools": skipped
INSERT INTO shop.audit_log ...;      -- row event, checked against "shop":
                                     -- applied, fails with 1146

SHOW REPLICA STATUS\G   -- Last_SQL_Errno: 1146

Do not just skip the event: every later write to that table will fail the same way. Create the table on the replica with the exact definition from the source, or reseed the replica, and fix the filter or the habit that caused it. Our guide to replication error 1062 covers why skipping errors leaves a replica quietly diverged.

What changes on large systems

Key takeaways

MySQL Errors Replication Migrations
Work with us

Tables missing after a restore or migration?

We restore, move and repair MySQL databases in production: transportable tablespaces, cross-platform migrations and replicas that drifted from the source.

Get in touch More from the blog