Drop screwed up table in Mysql db - mysql

I've managed to corrupt (or something) the 'sessions' table in a mysql db i have (which is called "e_learning_resource_prelive"). This wouldn't be a problem normally as i could just go back to a backup dump of the db. However, the corrupted table seems to be stopping me deleting the database:
> mysqladmin -u root drop e_learning_resource_prelive
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'e_learning_resource_prelive' database [y/N] y
mysqladmin: DROP DATABASE e_learning_resource_prelive failed;
error: 'Unknown table 'sessions''
When i go into the db the sessions table shows up in show_tables (it's the only one there, the mysqladmin drop deleted the rest) but i can't drop it:
mysql> show tables;
+---------------------------------------+
| Tables_in_e_learning_resource_prelive |
+---------------------------------------+
| sessions |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> drop table sessions;
ERROR 1051 (42S02): Unknown table 'sessions'
Can anyone tell me how i can delete this table, or the whole db? I need to delete the db and then rebuild it from my backup dump.

Figured it out, seems kind of obvious now. The dbs all just have a folder which can be deleted like anything else.
sudo rm -r /var/lib/mysql/e_learning_resource_prelive
Thanks anyone who looked, anyway :)
max

session is a reserved keyword (http://developer.mimer.com/validator/sql-reserved-words.tml), I think that is why your database is corrupt.
I had the same issue using a reserved keyword (references in my case), and I also had the problem that renaming, dropping or truncating the table was giving an sql error.
To fix this problem, use backtick-characters in the alter table query.
ALTER TABLE `session` RENAME TO newname
This way the query won't fail, and your data are still there (thank god!). I hope someone finds this useful!

Use the GUI interface. There is probably some not-very-printable character in the sessions name.
Or maybe the underlying file on the filesystem was deleted? If so, try creating an empty file named sessions there.

Related

MariaDb Gone Away On Spesefic Row

in my database when select from 2 of 10 tables, in spesefic rows , i give this message :
ERROR 2006 (HY000): MySQL server has gone away
for example whene i try :
select * from records where id=238
every thing is ok , and even this:
select * from records where id=270
is ok.
but when try this one:
select * from records where id=239
return error :
i already search in stackoverflow and google for this and all refrence mention that increace buffer size and...
but i dont think this problem relate to this subjects , its seems that some data is currept the table.
p.s : engine that use is innodb
any clue please
MySQL closes itself when a operation tries to read from a corrupted InnoDB table.
This is very annoying but is the only way I know by heart.
Is possible to recover all data in most of times, the procedure is the same for MySQL, MariaDB and XtraDB.
There is a lot of explanation on the official manual https://dev.mysql.com/doc/refman/8.0/en/forcing-innodb-recovery.html.
But, essentially, you need to add an option named innodb_force_recovery on MySQL configuration file, restart MySQL and try to SELECT that corrupted data. This options goes from number 1 to 6, but above 3 you could lose some data.
When you found a number that allow you to make a SELECT on that rows, mysqldump the entire table to recover after.
Depending on table state you'll need to remove the file manually before start MySQL without innodb_force_recovery:
rm /var/lib/mysql/myschema/corrupted_table.ibd
This will cause other problems, but once the database start you'll need to drop the old tablespace, create "the same table" in another database and swap the tablespaces:
USE a;
ALTER TABLE corrupted_table DISCARD TABLESPACE;
CREATE DATABASE b;
-- import the dump inside database b
-- fisically move the "ibd" file from "b" to "a"
-- mv /var/lib/mysql/a/corrupted_table.idb /var/lib/mysql/b/corrupted_table.idb
USE a;
ALTER TABLE corrupted_table IMPORT TABLESPACE;
DROP TABLE b.corrupted_table;

MySQL can't select from existing table because it doesn't exist?

I have no idea what is going on. I have a table called project_share_invite. A few hours ago (in our production environment) I could no longer issue SELECTs against this table. MySQL claims the table does not exist, though it shows on show tables. The only noteworthy event that has happened on the machine today is a routine package upgrade (via apt).
mysql> use analytics;
Database changed
mysql> show tables like 'project_share_invite';
+--------------------------------------------+
| Tables_in_analytics (project_share_invite) |
+--------------------------------------------+
| project_share_invite |
+--------------------------------------------+
1 row in set (0.00 sec)
mysql> select count(*) from project_share_invite;
ERROR 1146 (42S02): Table 'analytics.project_share_invite' doesn't exist
Ideas? This doesn't make any sense to me.
Update: The files for the table are still present on disk (project_share_invite.frm and project_share_invite.idb respectively) and have content in them.
A quick restart of MySQL has not fixed this.
Update: Same results when using root account instead of specific user account.
Update: I am unable to recreate the tables either.
CREATE TABLE `analytics`.`project_share_invite` ( ... )
ERROR 1146 (42S02): Table 'analytics.project_share_invite' doesn't exist
Update: Should have checked the error logs first:
InnoDB: Load table 'analytics/project_share_invite' failed, the table has missing foreign key indexes.
Though I've no idea how it's got in this state.
Looks like you hit a known bug in MySQL where a foreign key constraint exists, but the associated index was dropped. See: http://bugs.mysql.com/bug.php?id=68148
Depending on the version of MySQL (Seems like you need 5.6 or >) you can fix this problem by turning off foreign key checking and then recreating the missing index(es).
SET FOREIGN_KEY_CHECKS=0;
You should check the structure using SHOW CREATE TABLE table name
Then use CREATE INDEX to recreate the missing indexes.
This error is usually caused by moving files around at the filesystem level.
Keep in mind that SHOW TABLES just reads the .frm file, but once you query the table, MySQL invokes the storage engine. InnoDB has its own internal way of managing metadata, in a "data dictionary" which is always stored in ibdata1.
So if you moved the datadir but forgot the ibdata1 file (or copied an ibdata1 from another instance), then the InnoDB data dictionary wouldn't know about the table, even though SHOW TABLES does.
Another possibility is that you copied data files around, and now they don't have the write ownership or file permissions. So for example the .frm file is readable but the .ibd is not. They should be owned and writeable by mysql:mysql.
If your apt upgrade changed file locations or file permissions, that could cause it too. I would advise using ls -l to verify the permissions on the files.

unable to alter table, Table 'xxx/#sql-ib265' already exists

I have a mysql table y in database xxx which I attempted to change compression type before using
alter table y row_format=compressed key_block_size=8
the process stopped half way. I removed temp file '#sql-ib265.frm and #sql-ib265' in mysql lib directory and restarted the server. However
Now when I attempt the alter table y (with the same command above) again I get error.
ERROR 1050 (42S01) at line 1: Table 'xxx/#sql-ib265' already exists
I can't drop table 'xxx/#sql-ib265' because it can't be found.
what should I do?
Edit
Solution:
I ended up dropping the old database and recreate the database.
Try to restart mysql client with the --skip-auto-rehash option and try DROP TABLE again.
If above does not work, try this from MySQL Manual:
You have a corrupt innodb data dictionary..
https://dev.mysql.com/doc/refman/5.0/en/innodb-troubleshooting-datadict.html
Problem with Temporary Table
If MySQL crashes in the middle of an ALTER TABLE operation, you may end up with an orphaned temporary table inside the InnoDB tablespace. Using the Table Monitor, you can see listed a table with a name that begins with #sql-. You can perform SQL statements on tables whose name contains the character “#” if you enclose the name within backticks. Thus, you can drop such an orphaned table like any other orphaned table using the method described earlier. To copy or rename a file in the Unix shell, you need to put the file name in double quotation marks if the file name contains “#”.
There are two ways to fix this problem.
As other answer suggests, official MySQL documentation suggests to drop a specially crafted table. But please note in versions >= 5.1 you need to prefix table name with #mysql50#.
Move (use RENAME TO) all good tables to a temporary database, drop&recreate the original one and then move the tables back. See a blog post for details.
in additional I'm loging in with root to do the recover job but failed. then i chown the .frm file to meet the owner of mysql service and succeed.
For anyone still facing this problem, I have just followed the following steps to solve it, which (to me at least) seem far less daunting than other solutions:
Use mysqldump to back up the database with all its data.
Drop and recreate the database.
Reload the database and all its schema from the file generated in (1).
Because the orphaned tables are hidden anyway, they don't get backed up, so you end up with a database without them. I had all my procedures/functions scripted out anyway, so was able to restore them easily - if you don't, make sure you use the --routines parameter to dump those too.
My dump file was around 1.5GB for the database in question (so it's not small), and the whole thing was completed in a few minutes.
I had the same error. I fixed it by switching the order in which I dropped the tables at the beginning of the file:
DROP TABLE IF EXISTS table_name;
This line is repeated for each table. Tables with foreign keys need to be deleted before the tables with the primary keys to which they point.

MySQL Create Table Error - Table Doesn't Exist

I am new to MySQL, and I am having a problem where if I try to create a Table in my newly created Database "recommend", I get the following error:
ERROR 1146 (42S02): Table 'recommend.Users' doesn't exist
I checked related posts here and on the internet but nothing helped.
If I use the MySQL command line I still get the same error.
SELECT DATABASE() FROM DUAL;
+------------+
| DATABASE() |
+------------+
| recommend |
+------------+
1 row in set (0.00 sec)
but then when i run this command :
mysql> use recommend
Database changed
mysql> CREATE TABLE Users (UserName VARCHAR(20),password VARCHAR(20),PRIMARY KEY(UserName));
ERROR 1146 (42S02): Table 'recommend.Users' doesn't exist
I also tried using Navicat and still get the same error :(
I had the same problem. I actually read this thread before I got lucky with a simple solution. I attempted to drop my table and it worked, in your case your table is User:
DROP TABLE Users;
The table does not exist, so naturally MySQL complains:
Error Code: 1051. Unknown table 'Users'
But then I ran my CREATE TABLE statement again and it worked. Hopefully others can validate this works every time? A lot easier than going to an error log, especially if you are not the greatest DBA/System Admin/Hacker.
This looks like a data dictionary problem. You can get more information by checking the error log. (See the MySQL docs here).
Possibly, you have an orphaned table. If so, the solution is to create a table of the same name in a different database, then copy the .frm file to the current database. Then you can DROP the table, and a subsequent CREATE should then succeed. More details on troubleshooting this sort of problem can be found here
The Problem was...
You have a DB with "InnoDB ENGINE".......
My Solution was...
You must have a "MyISAM ENGINE",
how to change?... find on your environment may be with
# find / -name my.cnf
on LINUX, simply add the following line to
vim /etc/mysql/my.cnf
Add:
default-storage-engine= MyISAM
Then restart mysql:
service mysql restart

MYSQL table existence oddity

The series of events that lead to this oddity are as follows:
mysql> DROP TABLE `mytest`;
ERROR 1051 (42S02): Unknown table 'mytest'
mysql> CREATE TABLE `mytest` (id INT NOT NULL PRIMARY KEY,_modified DATETIME, KEY(_modified));
ERROR 1050 (42S01): Table '`mydb`.`mytest`' already exists
mysql> show tables like '%mytest%';
Empty set (0.01 sec)
I initially thought this might be a file permission issue in the data directory, but I have checked and the files that should be present to represent this table do not exist.
This is mysql version 5.1, myisam tables.
I should probably also mention that this occurs during an automated nightly restore of a large database, which is pulled from backup location as a gzipped tar archive, extracted into the mysql data directory and then myisamchk is run against all MYI files in the new directory.
After 2 hours of investigation I am still lost as to what could be causing this - any assistance would be appreciated.
try this
CREATE TABLE `mytest` IF NOT EXISTS ELSE TRUNCATE `mytest`
Use TRUNCATE to empty the table and reset cardinality instead of deleting the table and recreating it.
try this :
mysql> DROP TABLE '`mydb`.`mytest`'
Seems like broken indexes or definition files.
Try to stop mysql, go to the mysql's data directory, delete the files for the particular table, and restart mysql. Be careful not to delete other files.
If it's InnoDB type, than some command to repair broken tables could help.
IIRC it's CHECK TABLE ... and REPAIR TABLE.