mysql复制一条record,如何将单个行/记录从一个MySQL实例复制到另一个实例?

I have two MySQL instances running with the same schema. One is in the cloud; one is on my local box. The local-box version needs a couple of test rows in its main table.

Effectively I'd like to do something like a mysqldump or mysqlhotcopy of a single record on the production table, and then "restore" that record into the same table on the local instance. I don't want to copy the whole table. If there are rows on the local table, I want them left alone.

I'm fine with the PK of the copied row changing. I don't care about foreign keys, if any.

The table is big and complex enough that it's nontrivial for me to print the record with a select statement and then format it for an insert.

What are my options?

解决方案

If you're already familiar with mysqldump, I'll remind you that mysqldump has a --where option, so you can get a single row if you write the condition for that:

mysqldump databasename tablename --where "id=12345" --no-create-info --skip-add-locks --host=db1 | \

mysql --host=db2

You could also use SELECT...INTO OUTFILE to dump the result of any query you want into a flat file, even a query of a single row. Then LOAD DATA INFILE to import that file on the other instance.

You could also write a simple script in your choice of language. Connect to both instances. SELECT a single row from the source instance, save it in a variable in the script. Then form an INSERT command to execute against the destination instance. This may not be the most efficient way to move a large amount of data, but for a single row it would be fine.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值