mysqldump处理触发器的异常错误

原文链接:http://www.mysqlperformanceblog.com/2013/02/11/unexpected-problem-with-triggers-and-mysqldump/

Some time ago, I had to convert all tables of a database from MyISAM to InnoDB on a new server. The plan was to take a logical dump on the master, exporting separately the schema and the data, then edit the CREATE TABLE statements to ensure all tables are created with InnoDB, and reload everything on the new server.

不久前我有一个活是转换所有表的引擎到一台新服务器,从MYISAM转到INNODB。我的计划是在master上做一个逻辑备份,分别导出库结构和数据,然后修改create table语句,让所有的表都换成innodb,然后在新服务器上重新导入这些文件。

Quite easy, isn’t it? Of course I wanted to run a test first before performing the actions on a live system.

So let’s play with the sakila database.

mysqldump has options to export schema and data separately, let’s use them:

非常简单,对吧?当然我会在部署到生产环境前做一个测试,我们用sakila数据库试试看。

mysqldump有一个参数去分别导出库结构和数据

# Export schema
$ mysqldump --no-data sakila > schema.sql

# Export data
$ mysqldump --no-create-info sakila > data.sql

Just to check that everything is fine, let’s reimport the data in a new database:

看看一切都如预期一样了没,我们把数据导入到新数据库

$ mysql sakila2 < schema.sql 
$ mysql sakila2 < data.sql 
ERROR 1235 (42000) at line 86: This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'

What????

Let's look around line 86:        出错了。我们看看86行

$ head -90 data.sql | tail -5
/*!50003 CREATE*/ /*!50017 DEFINER=`root`@`localhost`*/ /*!50003 TRIGGER customer_create_date BEFORE INSERT ON customer
    FOR EACH ROW SET NEW.create_date = NOW() */;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;

Ok, so we're trying to create a trigger and it fails. The error message suggests that there is already a trigger on BEFORE INSERT for this table.

That's correct: if we look at the schema.sql file, we can see the same trigger definition.

This means that the --no-create-info option doesn't include the CREATE TABLE statements in the output, but includes CREATE TRIGGER statements. Is it documented? Well, sort of...

If you look at the mysqldump documentation, you will see:

嗯,准备创建一个触发器,但是失败了。error信息是说这个表已经有一个before insert触发器了。这是正确的,如果我们看看schema.sql文件,就能在里面发现触发器定义语句。这个在文档里面有么?稍等,我去瞧瞧。

打开mysqldump文档,有下面的信息

--triggers

Include triggers for each dumped table in the output. This option is enabled by
default; disable it with --skip-triggers.

So the conclusion is that if you are using triggers and if you want to dump data only, you have to use --skip-triggers along with --no-create-info.

The correct way to export data is therefore:

那么结论就是:如果你使用了触发器并且只想备份数据,你需要配合使用--skip-triggers

正确导出数据的方法是这样的:

# Export schema
$ mysqldump --no-data sakila > schema.sql

# Export data
$ mysqldump --no-create-info --skip-triggers sakila > data.sql

I'm quite surprised that such an issue never came up before, it may be an indication that using triggers is far from being a common practice with MySQL.

我很奇怪为什么以前从没遇到这个问题,它可能是说使用触发器和MYSQL通常的用法是不一样的。

原文地址:https://www.cnblogs.com/zuoxingyu/p/2914411.html