Ffile2fix
Sign in Get started

How to fix MySQL "ERROR 1146: Table doesn't exist"

ERROR 1146 (42S02): Table 'appdb.wp_options' doesn't exist

The query refers to a table that MySQL cannot find in that database. Usually the table was never created or imported, the table prefix or database name is wrong, or the name's letter case does not match on a Linux server.

Also appears as: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'appdb.users' doesn't exist · WordPress database error Table 'appdb.wp_posts' doesn't exist for query SELECT ... · #1146 - Table 'appdb.Users' doesn't exist · Table 'appdb.sessions' doesn't exist in engine

Common causes

  • A SQL import stopped partway, so some tables were never created
  • Wrong table prefix, such as $table_prefix = 'wp_' when tables use 'wp2_'
  • The app connects to the wrong database
  • Case mismatch: Users vs users on Linux where lower_case_table_names = 0
  • Database migrations were not run (Laravel, Symfony, Django)
  • 'doesn't exist in engine' means InnoDB data files are missing or out of sync

How to fix it

  1. List the tables that exist. Run SHOW TABLES FROM appdb; and compare the names to the one in the error, including prefix and letter case.
  2. Fix the prefix or database name. In WordPress, set $table_prefix in wp-config.php to the prefix your tables actually use. In other apps, check DB_DATABASE and any prefix setting in .env.
  3. Re-run the import. If tables are missing, re-import the dump and watch for the first error, for example mysql -u appuser -p appdb < backup.sql. A failure halfway skips every table after it.
  4. Run migrations. For framework apps run the migration command, such as php artisan migrate, so missing tables are created.
  5. Match the name's case. On Linux, table names are case-sensitive by default. Rename the table with RENAME TABLE Users TO users; or fix the query. lower_case_table_names can only be set when the MySQL 8 data directory is first initialized.
  6. Handle 'doesn't exist in engine'. This points to InnoDB damage, often after copying raw files from /var/lib/mysql. Restore the table from a proper SQL dump instead.

Check what tables exist and the prefix in use

SHOW TABLES FROM appdb LIKE '%options';
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'appdb'
ORDER BY table_name;

How to stop it happening again

  • Back up with mysqldump instead of copying raw data files
  • Check import logs for errors before going live
  • Use lowercase table names everywhere
  • Keep the table prefix in config in sync after migrations

Frequently asked questions

Why does it work on Windows but not Linux?

Windows and macOS treat table names as case-insensitive by default; Linux does not. A query for Users fails when the table is users.

Can I copy table files from /var/lib/mysql to restore?

Not for InnoDB in a simple way. Copying .ibd files without the right steps causes 'doesn't exist in engine'. Use mysqldump or a proper backup tool.

My WordPress shows the install screen. Is this related?

Yes. If WordPress cannot find its wp_options table, often due to a wrong $table_prefix, it thinks it is not installed.