What the message looks like
ERROR 1146 (42S02): Table 'appdb.orders' doesn't exist
PHP Fatal error: Uncaught mysqli_sql_exception: Table 'appdb.wp_options' doesn't existCommon causes
- The connection uses a different database than expected, or none at all, so the unqualified table name points to the wrong place.
- The name does not match exactly. On Linux table names are case-sensitive by default (
lower_case_table_names=0), and apps with a table prefix (for examplewp_) fail when the prefix differs. - The table was never created: a migration failed halfway, the import stopped at an error, or the app runs against a database from another environment.
- The dump was restored into another database, or only partly.
- Data files (
.ibdor.frm) were copied by hand instead of using a dump. The server's data dictionary does not know such tables, and the message may readdoesn't exist in engine(error 1932).
Check
- Which databases exist?
mysql -e "SHOW DATABASES" - Where does a table with this name exist? (all schemas)
mysql -e "SELECT table_schema, table_name FROM information_schema.tables WHERE table_name LIKE '%orders%'" - Case sensitivity of table names (0 = case-sensitive, typical on Linux)
mysql -e "SHOW VARIABLES LIKE 'lower_case_table_names'" - Errors at startup or import in the server log
Fix
- Select the right database with
USE appdb;or qualify the name asappdb.orders, and check which database name the app configuration uses. - Match the name exactly, including case and prefix.
SHOW TABLES LIKE 'ord%';lists the candidates. - If the table was never created, run the migration or import the schema again and make sure the import finishes without error:
mysql appdb < dump.sql. - Do not use copied
.ibdfiles as a backup. Restore from a dump or a physical backup tool (mariabackup, Percona XtraBackup). A single orphaned file can only be brought back with the transportable tablespace procedure (ALTER TABLE ... DISCARD TABLESPACEandIMPORT TABLESPACE) on a table created with the identical definition. - For
doesn't exist in enginerestore the table from a backup. Attempt any recovery only on a copy of the data directory. - Do not try to fix case problems by changing
lower_case_table_nameson a running system. On MySQL 8.0 it can only be set when the data directory is initialized. Fix the name in the code instead.
Typical scenario
A developer moves an application from a Windows laptop to a Linux server. Every second page now fails with Table 'shop.Orders' doesn't exist, although SHOW TABLES lists the table as orders. The code asks with a capital O, which the laptop tolerated. Run SHOW TABLES LIKE '%orders%' first and then fix the name in the code.
FAQ
The table is there in the file listing, why does MySQL not see it?
For InnoDB, a file in the data directory is not enough. The table must also be registered in the server's data dictionary. Tables that were copied as files are not, which is why a dump or a proper backup tool is the way to move them.
It worked on my Mac or Windows machine, but not on the server.
Those systems treat table names case-insensitively by default, Linux does not. A query for Orders finds orders there, but not on a Linux server. Use the exact lowercase name everywhere.
How do I find a table when I do not know the database?
Through information_schema.tables: SELECT table_schema, table_name FROM information_schema.tables WHERE table_name LIKE '%orders%'; shows every database that holds such a table.
The error appeared right after an update, what now?
Then the migration of the update most likely stopped halfway. Look in the application log for the first error, fix the cause and run the migration again. Take a backup before.
Can a database user without privileges cause this error?
Usually a privilege error (1142) comes up, but not always: a user without any rights on a database does not see its tables. Check what the user may do with SHOW GRANTS.