mysqldatabase-backupsmysql-backup

mysqldump produce a corrupted sql file (apparently on tables with stored generated column) (MySQL v8.0.21)


I am using MySQL v8.0.21 and mysqldump v8.0.23.

I have a table with the following schema:

mysql> desc resource_downloads;
+-------------+--------------+------+-----+---------+------------------+
| Field       | Type         | Null | Key | Default | Extra            |
+-------------+--------------+------+-----+---------+------------------+
| compositeId | varchar(250) | NO   | PRI | NULL    | STORED GENERATED |
| resourceId  | int unsigned | NO   | MUL | NULL    |                  |
| userId      | int unsigned | NO   | MUL | NULL    |                  |
| fileId      | int unsigned | YES  | MUL | NULL    |                  |
+-------------+--------------+------+-----+---------+------------------+

and the following values:

mysql> select * from resource_downloads;
+-------------+------------+--------+--------+
| compositeId | resourceId | userId | fileId |
+-------------+------------+--------+--------+
| 24-150-NULL |         24 |    150 |   NULL |
| 37-150-NULL |         37 |    150 |   NULL |
| 56-150-48   |         56 |    150 |     48 |
| 56-150-NULL |         56 |    150 |   NULL |
+-------------+------------+--------+--------+
4 rows in set (0.00 sec)

Here is the SQL statement to create the table:

CREATE TABLE `resource_downloads` (`compositeId` varchar(250) AS (CONCAT_WS('-', resourceId, userId, IFNULL(fileId, 'NULL'))) STORED NOT NULL, `resourceId` int UNSIGNED NOT NULL, `userId` int UNSIGNED NOT NULL, `fileId` int UNSIGNED NULL, PRIMARY KEY (`compositeId`)) ENGINE=InnoDB

When executing the following mysqldump command:

mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME > dump.sql

it does produce the following for the resource_downloads table:

--
-- Dumping data for table `resource_downloads`
--

LOCK TABLES `resource_downloads` WRITE;
/*!40000 ALTER TABLE `resource_downloads` DISABLE KEYS */;
INSERT INTO `resource_downloads` (`resourceId`, `userId`, `fileId`) VALUES ,24,150,NULL),,24,150,NULL),37,150,NULL),,24,150,NULL),37,150,NULL),56,150,48),,24,150,NULL),37,150,NULL),56,150,48),56,150,NULL);
/*!40000 ALTER TABLE `resource_downloads` ENABLE KEYS */;
UNLOCK TABLES;

which is corrupted as you can see, not a valid SQL statement.

I didn't found anything corresponding to this particular situation and I guess this is due to the generated stored column as it only appears after I introduced it in my db a few days ago.

Is there any workaround to this situation for me to be able to generate a usable dump of my database ? I could think of excluding this table from the dump but this is inconvenient...


Solution

  • If you move the generated column to the end, like this:

    CREATE TABLE `resource_downloads` (
       `resourceId` int UNSIGNED NOT NULL, 
       `userId` int UNSIGNED NOT NULL, 
       `fileId` int UNSIGNED NULL, 
       `compositeId` varchar(250) AS (CONCAT_WS('-', resourceId, userId, IFNULL(fileId, 'NULL'))) STORED NOT NULL, 
       PRIMARY KEY (`compositeId`)
    ) ENGINE=InnoDB;
    

    and not like this:

    CREATE TABLE `resource_downloads` (
       `compositeId` varchar(250) AS (CONCAT_WS('-', resourceId, userId, IFNULL(fileId, 'NULL'))) STORED NOT NULL, 
       `resourceId` int UNSIGNED NOT NULL, 
       `userId` int UNSIGNED NOT NULL, 
       `fileId` int UNSIGNED NULL, 
       PRIMARY KEY (`compositeId`)
    ) ENGINE=InnoDB;
    

    The mysqldump seems to go OK. (Tested with mysqldump 8.0.25)