如何在2个MySQL数据库之间传输数据?

发布于 2024-09-09 04:41:01 字数 118 浏览 5 评论 0原文

我想使用代码而不是使用“MySQL 迁移工具包”之类的工具来做到这一点。我知道的最简单的方法是打开到 DB1 的连接(使用 MySQL 连接器)并读取其数据。打开与 DB2 的连接并将数据写入其中。有更好/最简单的方法吗?

I want to do that using a code and not using a tool like "MySQL Migration Toolkit". The easiest way I know is to open a connection (using MySQL connectors) to DB1 and read its data. Open connection to DB2 and write the data to it. Is there a better/easiest way ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

記柔刀 2024-09-16 04:41:01

首先,我假设您不能只复制数据/目录,因为如果您使用现有的快照/备份/恢复可能就足够了(并测试您的备份/恢复过程) 。

在这种情况下,如果两个表具有相同的结构,通常最快且讽刺的是最简单的方法是在一端使用 SELECT...INTO OUTFILE...,在另一端使用 LOAD DATA INFILE...。

请参阅http://dev.mysql.com/doc/refman/ 5.1/en/load-data.html 和 .../select.html 了解明确的详细信息。

对于简单的表,以下方法将起作用:

SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' 
  FIELDS TERMINATED BY ',' 
  OPTIONALLY ENCLOSED BY '"' 
  ESCAPED BY '\\\\' 
  LINES TERMINATED BY '\\n' ;

LOAD DATA INFILE '/tmp/mytable.csv' INTO TABLE mytable 
  FIELDS TERMINATED BY ',' 
  OPTIONALLY ENCLOSED BY '"' 
  ESCAPED BY '\\\\' 
  LINES TERMINATED BY '\\n' ;

我们还使用了 FIFO 来发挥巨大作用,以避免实际写入磁盘的开销,或者如果我们由于某种原因确实需要写入磁盘,则可以通过 gzip 进行传输。

IE。

mkfifo /tmp/myfifo
gzip -c /tmp/myfifo > /tmp/mytable.csv.gz &
... SEL

ECT... INTO OUTFILE '/tmp/myfifo' .....
wait

gunzip -c /tmp/mytable.csv.gz > /tmp/myfifo &
... LOAD DATA INFILE /tmp/myfifo .....
wait

基本上,只要将表数据定向到 FIFO,您就可以对其进行压缩、整理或通过网络将其传输到您想要的内容。

First I'm going to assume you aren't in a position to just copy the data/ directory, because if you are then using your existing snapshot/backup/restore will probably suffice (and test your backup/restore procedures into the bargain).

In which case, if the two tables have the same structure generally the quickest, and ironically the easiest approach will be to use SELECT...INTO OUTFILE... on one end, and LOAD DATA INFILE... on the other.

See http://dev.mysql.com/doc/refman/5.1/en/load-data.html and .../select.html for definitive details.

For trivial tables the following will work:

SELECT * FROM mytable INTO OUTFILE '/tmp/mytable.csv' 
  FIELDS TERMINATED BY ',' 
  OPTIONALLY ENCLOSED BY '"' 
  ESCAPED BY '\\\\' 
  LINES TERMINATED BY '\\n' ;

LOAD DATA INFILE '/tmp/mytable.csv' INTO TABLE mytable 
  FIELDS TERMINATED BY ',' 
  OPTIONALLY ENCLOSED BY '"' 
  ESCAPED BY '\\\\' 
  LINES TERMINATED BY '\\n' ;

We have also used FIFO's to great effect to avoid the overhead of actually writing to disk, or if we do need to write to disk for some reason, to pipe it through gzip.

ie.

mkfifo /tmp/myfifo
gzip -c /tmp/myfifo > /tmp/mytable.csv.gz &
... SEL

ECT... INTO OUTFILE '/tmp/myfifo' .....
wait

gunzip -c /tmp/mytable.csv.gz > /tmp/myfifo &
... LOAD DATA INFILE /tmp/myfifo .....
wait

Basically, one you direct the table data to a FIFO you can compress it, munge it, or tunnel it across a network to your hearts content.

稀香 2024-09-16 04:41:01

联邦存储引擎?这不是其中最快的一个,但对于一次、偶然或少量数据来说它可以。假设您正在谈论 2 个服务器。如果一台服务器上有 2 个数据库,那么情况就很简单:

INSERT INTO databasename1.tablename SELECT * FROM databasename2.tablename;

The FEDERATED storage engine? Not the fastest one in the bunch, but for one time, incidental, or small amounts of data it'll do. That is assuming you're talking about 2 SERVERS. With 2 databases on one and the same server it'll simply be:

INSERT INTO databasename1.tablename SELECT * FROM databasename2.tablename;
凝望流年 2024-09-16 04:41:01

您可以使用mysqldumpmysql(命令行客户端)。这些是命令行工具,在您编写的问题中您不想使用它们,但仍然使用它们(即使从代码中运行它们)是最简单的方法; mysqldump 解决了很多问题。

您可以从一个数据库中进行选择,然后插入到另一个数据库,这非常简单。但如果您还需要传输数据库模式(创建表等),它会变得有点复杂,这就是我推荐mysqldump的原因。但许多 PHP-MySQL-admin 工具也这样做,因此您可以使用它们或查看它们的代码。

或者您可以使用 MySQL 复制。

You can use mysqldump and mysql (the command line client). These are command line tools and in the question you write you don't want to use them, but still using them (even by running them from your code) is the easiest way; mysqldump solves a lot of problems.

You can make selects from one database and insert to the other, which is pretty easy. But if you need also to transfer the database schema (create tables etc.), it gets little bit more complicated, which is the reason I recommend mysqldump. But lot of PHP-MySQL-admin tools also does this, so you can use them or look at their code.

Or maybe you can use MySQL replication.

我爱人 2024-09-16 04:41:01

来自 http://dev.mysql.com/doc/refman/ 5.0/en/rename-table.html

只要两个数据库位于同一文件系统上,就可以使用 RENAME TABLE 将表从一个数据库移动到另一个数据库:

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;

from http://dev.mysql.com/doc/refman/5.0/en/rename-table.html:

As long as two databases are on the same file system, you can use RENAME TABLE to move a table from one database to another:

RENAME TABLE current_db.tbl_name TO other_db.tbl_name;
离鸿 2024-09-16 04:41:01

如果您在当前服务器上启用了二进制日志记录(并且拥有所有 bin 日志),则可以为第二台服务器设置复制

If you enabled binary logging on your current server (and have all the bin logs) you can setup replication for the second server

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文