插入到其他服务器上的MySQL数据库中 [英] insert into a MySQL database on a different server

查看:248
本文介绍了插入到其他服务器上的MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mysite1.com上运行的脚本中使用PHP和MySQL,我试图将mysite2.com上一个表的所有行和列复制到mysite1.com上相同的表(已创建)中.首先,我连接到两个数据库(我已经在mysite2.com上建立了远程MySQL连接).

Using PHP and MySQL in a script run on mysite1.com I'm trying to copy all rows and columns from a table on mysite2.com into an identical table (that has already been created) on mysite1.com. First I connect to both the databases (I've already set up remote MySQL connections on mysite2.com).

$con1 = mysql_connect("mysite1.com", "username1", "password1");
if (!$con1) {die('Could not connect: ' . mysql_error());}
mysql_select_db("database1", $con1);

$con2 = mysql_connect("mysite2.com", "username2", "password2");
if (!$con2) {die('Could not connect: ' . mysql_error());}
mysql_select_db("database2", $con2);

我不知道如何格式化"INSERT INTO" sql字符串,因此它将从mysite2.com获取数据并将其放入mysite1.com.有人知道该怎么做吗?

I can't figure out how to format the "INSERT INTO" sql string so it will get the data from mysite2.com and put it in mysite1.com. Does anyone know how to do this?

INSERT INTO my_table1 SELECT * FROM my_table2

推荐答案

无法按照您的想法进行.您可以设置联合表并进行复制从那开始.使用 mysqldump 然后加载它创建的文件,可能会更简单.

Can't be done the way you're thinking. You could set up a federated table and copy from that. Probably simpler just to use mysqldump and then load the file it creates though.

第三个选项是通过php读取数据并生成INSERT语句,但这是三个选项中最慢的一个.

Third option is to read the data through php and generate INSERT statements, but that'l be the slowest of the three options.

这篇关于插入到其他服务器上的MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆