ChatGPT解决这个技术问题 Extra ChatGPT

Schrödingers MySQL table: exists, yet it does not

I am having the weirdest error of all.

Sometimes, when creating or altering tables, I get the 'table already exists' error. However, DROP TABLE returns '#1051 - unknown table'. So I got a table I cannot create, cannot drop.

When I try to drop the database, mysqld crashes. Sometimes it helps to create another db with different name, sometimes it does not.

I use a DB with ~50 tables, all InnoDB. This problem occurs with different tables.

I experienced this on Windows, Fedora and Ubuntu, MySQL 5.1 and 5.5. Same behaviour, when using PDO, PHPMyAdmin or commandline. I use MySQL Workbench to manage my schema - I saw some related errors (endlines and stuff), however none of them were relevant for me.

No, it is not a view, it is a table. All names are lowercase.

I tried everything I could google - flushing tables, moving .frm files from db to db, reading mysql log, nothing helped but reinstalling the whole damn thing.

'Show tables' reveals nothing, 'describe' table says 'table doesn't exist,' there is no .frm file, yet 'create table' still ends with an error (and so does 'create table if not exists') and dropping database crashes mysql

Related, yet unhelpful questions:

Mysql 1050 Error "Table already exists" when in fact, it does not

MySQL Table does not exist error, but it does exist

Edit:

mysql> use askyou;
Database changed

mysql> show tables;
Empty set (0.00 sec)

mysql> create table users_has_friends (id int primary key);
ERROR 1050 (42S01): Table '`askyou`.`users_has_friends`' already exists

mysql> drop table users_has_friends;
ERROR 1051 (42S02): Unknown table 'users_has_friends'

And such, all the same: table doesn't exist, yet cannot be created;

mysql> drop database askyou;
ERROR 2013 (HY000): Lost connection to MySQL server during query

Names change, this is not the only table / database I've run into problems with

Can you open a MySQL client, and type some commands demonstrating the problem, then copy and paste an exact copy of the commands and output here. It's great that you've described your problem in a lot of detail, but it would be even better if you posted the exact commands and messages.
If there's definitely no view of the same name, I'd wager MySQL's data file structure is wonky.
What do you get in response to SHOW FULL TABLES IN askyou and SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA LIKE 'askyou'?
Are you using innodb_file_per_table?
@RafaelBarros: Quite right. Typo. Thanks for clarifying.

s
sreimer

I've seen this issue when the data file is missing in the data directory but the table definition file exists or vise-versa. If you're using innodb_file_per_table, check the data directory to make sure you have both an .frm file and .ibd file for the table in question. If it's MYISAM, there should be a .frm, .MYI and a .MYD file.

The problem can usually be resolved by deleting the orphaned file manually.


I am not using innodb_file_per_table; however, when I turn it on and try to recreate the table, it only creates the .ibd file. .frm is nowhere to be found. This only applies to a certain table (10+ others are created with correct files). Deleting that orphan ibd doesn't help anything anyway
I am using innodb_file_per_table ; but if i delete the orphaned .frm, it is recreated when i run the create statement (but the create statement returns an error, the .ibd file isn't created, and i still can't drop it)
E
ESG

Going on a wild guess here, but it seems like innodb still has an entry for your tables in a tablespace, probably in ibdata. If you really don't need any of the data, or if you have backups, try the following:

Delete all schemas (excluding mysql) shut down the database Make sure that all folders in your data directory have been removed properly (again, excluding mysql) delete ibdata and log files restart the database. It should recreate the tablespace and logs from scratch.


Fantastic: Stopping mysql, deleting 'ibdata1', 'ib_logfile1', 'ib_logfile0' and restarting mysql solved my problem. Thanks a lot!
BACK UP the entire database directory before you do this and after you've stopped the service.
R
RJTatWork

The fix turns out to be easy; at least what I worked out, worked for me. Create a table "zzz" on another MySQL instance, where zzz is the problem table name. (i.e. if the table is called schrodinger, substitute that for zzz whever written.) It does not matter what the definition of the table is. It's a temporary dummy; Copy the zzz.frm file to the database directory on server where the table should be, making sure file ownership and permissions are still correct on the file. On MySQL, you can now do "show tables;", and the table zzz will be there. mysql> drop table zzz; ...should now works. Clear any zzz.MYD or ZZZ.MYI files in the directory if necessary.


Actually this solution saved my life, thank you! I did copy the FRM and IDB file from another database but on the same server (same MySQL version etc...) and it seems to work gracefully.
I confirm, this is the way to go copy the .frm file from the other instance (master/slave) and put it into the directory, drop table and you will be able to create the table again.
o
ozataman

I doubt this is a direct answer to the question case here, but here is how I solved this exact perceived problem on my OS X Lion system.

I frequently create/drop tables for some analytics jobs I have scheduled. At some point, I started getting table already exists errors half-way through my script. A server restart typically solved the issue, but that was too annoying of a solution.

Then I noticed in the local error log file this particular line:

[Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive

This gave me the idea that maybe if my tables contained capital letters, MySQL would be fooled into thinking they are still there even after I had dropped them. That turned out to be the case and switching to using only lowercase letters for table names made the problem go away.

It is likely the result of some misconfiguration in my case, but hopefully this error case will help someone waste less time trying to find a solution.


C
Community

This is an old question but I just hit the same issue and an answer in one of the related issues linked at the top was just what I needed and far less drastic than deleting files, tables, shutting down the server etc.

mysqladmin -uxxxxxx -pyyyyy flush-tables

j
joao albuquerque

If will are stock with this error 1051 and you only want to delete the database and import this again do this steps and all gonna be just fine....

in Unix envoriment AS root:

rm -rf /var/lib/mysql/YOUR_DATABASE;

OPTIONAL -> mysql_upgrade --force

mysqlcheck -uUSER -pPASS YOUR_DATABASE

mysqladmin -uUSER -pPASS drop YOUR_DATABASE

mysqladmin -uUSER -pPASS create YOUR_DATABASE

mysql -uUSER -pPASS YOUR_DATABASE < IMPORT_FILE

Regards, Christus


S
Sebastian

In my case the problem was solved by changing the ownership of the mysql data directory to the user that ran the application. (In my case it was a Java application running Jetty webserver.)

Even though mysql was running and other apps could use it properly, this app had a problem with that. After changing the data directory ownership and resetting the user's password, everything worked properly.


R
Rafael Barros

I was having this problem with one particular table. Reading the possible solutions i've did some steps like:

Search for orphan files: didn't exist anyone;

execute: show full tables in database;: didn't see the problematic one;

execute: describe table;: returned table doesn't exist;

execute: SELECT * FROM information_schema.TABLES WHERE TABLE_NAME='table';: returned Empty set;

Search by the phpMyAdmin manually the query above: didn't exist;

And, after those steps, i check again with the show tables; and... vualá! the problematic table was gone. I could create it and drop it with the same problematic name with no problem, and i didn't have even to restart the server! Weird...


M
MoChaMan

I had this problem and hoped deleting the IBD file would help as posted above but it made no difference . MySQL only recreated a new IBD file . In my case, there are actually similar tables in other databases in the same MySQL instance . Since the FRM file was missing , I copied the FRM file from the similar table in another database , restarted MySQL and the table worked correctly .


u
user2911374

I ran into this error after I created a table and deleted it, then wanted to create it again. In my case, I had a self-contained dump file so I dropped my schema, recreated it and imported tables and data using the dump file.


P
Pete Kelley

It happens at our site (but rarely) usually when an "event" happens while running certain scripts that do a lot of rebuilding. Events include network outages or power problems. What I do for this on the very rare occasions it happens - I use the heavy-handed approach:

I needed to simply get rid-of-and-rebuild the particular table. I'm usually in a position that this is OK since the table is being built. (Your situation may be different if you need to recover data)

As an admin, go into the mysql installation (on windows its may be "...program files/mysql/MySQL Server xx/data/

Find the offending file with the table name in the folder - and delete it.

Check for orphaned temporary files and delete them too. #...frm files if they happen to be there.

MySQL will let you CREATE the table again

I've had this problem on a couple of different databases over a long time (years). It was a stumper because the contradicting messages. The first time I did a variation of the deleting/rebuilding/renaming database as described in the other answers and managed to get things going, but it definitely takes longer that way. Lucky for me it's always happened to reference tables that are being rebuilt - DROP'd and CREATEd - typically in the morning. Rarely got the problem but came to recognize it as a special quirky case. (I'll restate : if you need to recover the data look to the other solutions.)

it isn't a table belonging to another user, or in another database

it isn't the upper/lower case issue, I use all lower-case, but that was an interesting issue!

it was extra extra frustrating seeing responses with variations of "it definitely was and you're just not doing it right" :)

the table didn't show on "show tables"

the table was (always was/had been) an INNODB table.

trying to DROP the table gave the error message that the table doesn't exist.

but trying to CREATE the table gave the error message that the table already exists.

using mysql 5.0 or 5.1

REPAIR is ineffective for this problem