MariaDb Gone Away On Spesefic Row - mysql

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;

Related

Insert only selected columns from MySQL dump file into database

I am having a simple (I think) problem.
I am having a dump of MySQL database before disaster.
I need to import and replace from this dump only three columns from single table (in over 5000 rows, so that's why I am aware of doing it manually).
What should I do to do it and do not destroy anything else in working database?
I am just thinking that there is an option to skip columns during import and replace (UPDATE command I think) only these I need.
I will be thankful for help :(
------------ UPDATE ---------------
Okay, I used PHPMyAdmin and first I used SELECT query to get only three columns from whole table. Then I dumped it and I have SQL file with a dump containing only three columns.
Now, having this dump, can I (I do not know how to name it) edit or change something inside this typical MySQL dump file to make it possible to import these three columns with replace all the existing values?
I mean - to make existing column empty, then use maybe "INSERT INTO" but to whole table?
It is just over 2600 rows and I can not change it manually, so it would be better do use automation.
As far as I know, this is not possible. You can try to use sed in order to extract only the table you want - but specifically 3 columns would be complicated if not impossible.
Can I restore a single table from a full mysql mysqldump file?
The best way would be as #Ali said and just import it to a temp DB and then export the required data/columns to a new dump.
Restore DB to temp db then:
mysql> CREATE TABLE `tempTable` AS SELECT `columnYouWant` from `table`;
$> mysqldump yourDB tempTable > temp.sql
// Since you updated the question:
You want to probably use REPLACE INTO with your dump with the --replace option - though this will delete the row and replace it, not just the individual columns. If you want just the individual columns, the only way I can think of is with UDPATE. To use UPDATE, your options are:
Multi-table update
UPDATE mydb.mytable AS dest JOIN tempdb.mytable AS origin USING (prim_key)
SET dest.col1 = origin.col1,
dest.col2 = origin.col2,
...
Then drop the temp database.
Search/Replace Dump
Take your dump and use the INSERT ... ON DUPLICATE KEY UPDATE option to add it to the end of each insert line (assuming you exported/dumped individual insert commands).

How to move InnoDB table to another drive?

I have MySQL running on SSDs, SSDs that I'm about to run out of space on. My webhost overcharges for SSDs and the majority of the data in MySQL is "archived" data (i.e. data that isn't actively used). I have larger HDDs that can hold this data. As such, I want to be able to move specific InnoDB tables from the SSDs to the HDDs.
One solution I've thought about and researched is moving the individual .ibd files (I have innodb_file_per_table enabled) for the specific tables in question to the HDDs and then symlink. However, researching this, it looks like that is a bad idea for InnoDB.
I've also seen that since 5.6, MySQL supports the DATA DIRECTORY command:
To create a new InnoDB file-per-table tablespace in a specific
location outside the MySQL data directory, use the DATA DIRECTORY =
absolute_path_to_directory clause of the CREATE TABLE statement.
Plan the location in advance, because you cannot use the DATA
DIRECTORY clause with the ALTER TABLE statement. The directory you
specify could be on another storage device with particular performance
or capacity characteristics, such as a fast SSD or a high-capacity
HDD.
The problem is, it looks like this is only supported for new tables. I want to do it for existing tables. Any tips on how? I'm running Percona MySQL, if it helps.
Thanks!
UPDATE: Here is what I tried, but I'm getting a syntax error:
CREATE TABLE abc_2 LIKE abc ENGINE=InnoDB DATA DIRECTORY='/xxx/mysql/archive/'
Apparently CREATE ... LIKE ... DATA DIRECTORY ... is a combination that is not supported.
Do SHOW CREATE TABLE to get the current definition. Edit it to add DATA DIRECTORY and INDEX_DIRECTORY. Then use the edited text to create the new table.
Then INSERT INTO new_tbl SELECT * FROM real_tbl; and shuffle the names: RENAME TABLE real_tbl TO old_tbl, new_tbl TO real_tbl;.
Verify the results and finally DROP old_tbl;
I have dealt with this problem myself, and eventually found a more elegant solution than 'create new - copy - switch': detaching, moving and re-importing the underlying tablespace files. This is much more efficient on large and/or heavily indexed tables as MySQL does not have to redo work it has already done.
In short, it comes down to the following steps:
FLUSH TABLES `table_name` FOR EXPORT;
While keeping the connection open, move the tablespace files in a shell:
$ mv /var/lib/mysql/database_name/table_name.{ibd,cfg} ~
Now back in MySQL release the lock, drop the table, re-create it with the correct DATA DIRECTORY and discard its tablespace:
UNLOCK TABLES;
SHOW CREATE TABLE `table_name`;
DROP TABLE `table_name`;
CREATE TABLE `table_name` /* ... */ DATA DIRECTORY='/path/to/desired/location';
ALTER TABLE `table_name` DISCARD TABLESPACE;
Now copy the moved tablespace files to the desired location:
$ cp -a ~/table_name.{ibd,cfg} /path/to/desired/location
And import them:
ALTER TABLE `table_name` IMPORT TABLESPACE;
More background and motivation for why 'create new - copy - switch' is inefficient can be found in a blogpost I wrote on this topic: https://www.moxio.com/blog/28/moving-individual-mysql-tables-on-disk.

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.

Drop screwed up table in Mysql db

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.