last day (15 days later) » 

12:40 PM
2
Q: Mysql FATAL error when running ALTER TABLE

RoberI´m still in shock with what happened to me yesterday. I have a mysql database running in production. Yesterday trying to fix a performance issue in our software, I figured out that we were missing a a foreign key in a table. I run this alter table (IN PRODUCTION) using workbench: ALTER TABLE di...

 
Do you have any other tables that have foreign keys on the discounts.id? For example you might have an orders table with a foreign key column discount_id. Is this the case?
 
No. Please check my update.
 
when you say you have the same database replicated in dev, does that include all the same data, same schema, and same global variables? If so, then i'm stumped. It sounds like id INTEGER NOT NULL AUTO_INCREMENT should possibly be id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT to match a foreign key column that exists on another table. OR some other table is empty on dev and is not empty on prod. But if you have a replica, including all the data and settings, then it can't be those :)
If you can be bothered sharing more about your database structure I may be able to help. It would also be interesting to see if the problem persists if you first run SET FOREIGN_KEY_CHECKS=0; and then run the create table query. This would probably just point to another issue but would at least help you troubleshoot.
 
Thanks for your help! If you can be more specific about the database structure information that you need, I will try to provide it. Initially the tables structure in dev and prod is the same. The data is totally different. There is just a bunch of rows in development.
Please read my new comments.
 
If there aren't too many tables then a show create table tbl_name for each table would help. Also, are there any completely empty tables on dev that are not empty on prod?
 
12:42 PM
Hi, first thanks for your help.
There are around 150 tables
I checked this morning that there are not any foreign keys to discount id, but let me double check again and I let you know.
 
OK nevermind that then! Do any of the tables have a column discount_id?
nice
 
Anyway, as I told in the post. It was pretty strange yesterday.
I have the discounts table and found out that I miss a foreign key to my table boats (I don´t get into details of what kind of product in the post), so I run the alter table
and then I got an error of connection lost and the whole discounts table dissapear
I think that probably this should be the first, but I just wonder if restarting the database would fix something?
I suspect there is something corrupted in the session?
 
Possibly. I have had corruption before when adding foreign keys. If it fails half way through it can become quite messy.
It's not still trying to add it is it?
if you run show processlist your query isn't stuck there?
 
don´t see anything strange there
'111','root','localhost','yanpyprod','Sleep','6','',NULL
'257','root','localhost:40059','yanpyprod','Sleep','407','',NULL
'258','root','localhost:40060','yanpyprod','Query','0',NULL,'show processlist'
'317','root','localhost','yanpyprod','Sleep','18','',NULL
'319','root','localhost','yanpyprod','Sleep','4','',NULL
'320','root','localhost','yanpyprod','Sleep','18','',NULL
'321','root','localhost','yanpyprod','Sleep','18','',NULL
'322','root','localhost','yanpyprod','Sleep','15','',NULL
I don´t use to reboot the database often and I don´t remember now. Do you know how to do it?
 
it depends on the server you're using the for MySQL database. are you using some service to host the database?
 
12:57 PM
let me try to restart and I will come back
 
i think it's a different command depending on the operating system of the server. yeah cool
 
I have this daemon process running
/usr/sbin/mysqld
in ubuntu 14
 
the command for ubuntu looks to be sudo /etc/init.d/mysql restart
if you are able to connect to the server and run that. are you not using a service like digital ocean or aws or something?
 
Yes. Thank you.
Ok. These are the news
I restarted the server
As I´m not very skilled with databases, I first did it withoud sudo and got this error: /etc/init.d/mysql: ERROR: The partition with /var/lib/mysql is too full!
but when I did it with sudo everything worked fine.
After that:
The problem is still the same
SET FOREIGN_KEY_CHECKS=0;DROP TABLE boats_discounts;
CREATE TABLE boats_discounts (
id INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
I run that, but still getting Error Code: 1005. Can't create table 'yanpyprod.boats_discounts' (errno: 150)
just for testing: if I run: CREATE TABLE test_boats_discounts (
id INTEGER NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
it works
 
1:14 PM
CREATE TABLE boats_discounts (
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id)
);
i dont suppose this helpls?
 
no
same
 
i'm guessing it probably won't. it seems like there is an empty table in dev that's full in prod. that's all i can think. and the table in prod has boats_discount_id, and the foreign key fails because there is a record in that table and you are creating an empty table so it cannot match
is that possible?
 
I get your point.
but initially there should not be any foreign keys from any table to discounts table, but I will check deeply again
do you know a quick way to do it?
maybe a query that provide all foreign keys in schema
 
1:30 PM
I´m running everything in workbench. Do you know if there is a more verbose log that provide more specific info about the error, if I run directly in the server?
 
266
Q: How to find all tables that have foreign keys that reference particular table.column and have values for those foreign keys?

nebsI have a table whose primary key is referenced in several other tables as a foreign key. For example: CREATE TABLE `X` ( `X_id` int NOT NULL auto_increment, `name` varchar(255) NOT NULL, PRIMARY KEY (`X_id`) ) CREATE TABLE `Y` ( `Y_id` int(11) NOT NULL auto_increment, ...

the top answer for that looks useful
for searching for a foreign key
even if you just run

USE information_schema;
SELECT *
FROM
KEY_COLUMN_USAGE
then check out the columns starting with REFERENCED_
it should help you build a query to find any columns referencing boats_discounts.id
i have to go but will jump back on later
 
I have run:
SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = 'yanpyprod' AND
REFERENCED_TABLE_NAME = 'boats_discounts';
I have tested with other tables and it´s working fine, I see the foreign keys. So, I can confirm that there is not any table with a foreign key to boats_discounts.
It´s becoming harder.
 
2:18 PM
is it possible to be related with disk space?
 
 
2 hours later…
3:59 PM
I have some news. I saw in a post to run this SHOW ENGINE INNODB STATUS;
to get a status, and I got interesting information:
LATEST FOREIGN KEY ERROR
------------------------
200226 15:55:54 Error in foreign key constraint of table yanpyprod/boats_discounts:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT "boats_discounts_ibfk_1" FOREIGN KEY ("discount_type_id") REFERENCES "discount_types" ("id")
but, don´t understand...
 
4:44 PM
please see my updates in the post
thanks!
 

last day (15 days later) »