mysql复制表 链接服务器,将具有数据的表从一个MySQL服务器复制到另一个

I have a MySQL DB on a computer, and the same MySQL DB on a different server. I need them to be exactly the same in term of structure and contained data and I've come to the point where the only way I can do that is by truncating one table and then inserting into it all the rows of the other (exactly the same) table.

I want this to happen through a MySQL query and not by making backups and then importing it, not by database migrations or such, but by a query, because I plan using this query in a VB project and use it whenever there is a change in any of the two tables.

I know that if the tables where on the same server the query would have been as follows:

INSERT INTO db.table1 SELECT * FROM db.table2

But I don't know how to write the SELECT clause and how to tell it that .table2 is on another server.

I think it should be something like this

INSERT INTO db.table1 SELECT * FROM (ServerName/IP).db.table2

But can't quite figure it out by myself, any ideas?

解决方案

You can setup federated tables, which is basically linking a table on one server to a table on another. Then use the federation to do your data transfers.

First, you must have a table on the remote server that you want to access by using a FEDERATED table. Suppose that the remote table is in the federated database and is defined like this:

CREATE TABLE test_table (

id INT(20) NOT NULL AUTO_INCREMENT,

name VARCHAR(32) NOT NULL DEFAULT '',

other INT(20) NOT NULL DEFAULT '0',

PRIMARY KEY (id),

INDEX name (name),

INDEX other_key (other)

)

ENGINE=MyISAM

DEFAULT CHARSET=latin1;

Next, create a FEDERATED table on the local server for accessing the remote table:

CREATE TABLE federated_table (

id INT(20) NOT NULL AUTO_INCREMENT,

name VARCHAR(32) NOT NULL DEFAULT '',

other INT(20) NOT NULL DEFAULT '0',

PRIMARY KEY (id),

INDEX name (name),

INDEX other_key (other)

)

ENGINE=FEDERATED

DEFAULT CHARSET=latin1

CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

Then you can query it like any other table.

There are however a decent number of limitations you should read about including the remote password being stored in plain text. If this was a temporary setup purely for a once off copy, and the server isn't available to the public you have already minimised most of the risk associated with it though.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过MySQL的replication来将数据库的数据传送到另一个服务器数据库。MySQL的replication是一种异步的主从复制机制,可以将一个MySQL服务器上的数据同步到另一个MySQL服务器上,并保持数据的一致性。 以下是实现的步骤: 1. 在源MySQL服务器上,开启二进制日志功能,并设置server-id参数。可以通过修改my.cnf文件来配置: ``` [mysqld] log-bin=mysql-bin server-id=1 ``` 2. 在目标MySQL服务器上,设置server-id参数,并配置replication连接信息。可以通过修改my.cnf文件来配置: ``` [mysqld] server-id=2 ``` ``` CHANGE MASTER TO MASTER_HOST='source_mysql_host', MASTER_USER='replication_user', MASTER_PASSWORD='replication_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1234; ``` 其中,`source_mysql_host` 是源MySQL服务器的主机名或IP地址,`replication_user` 和 `replication_password` 是用于replication连接的用户名和密码,`mysql-bin.000001` 和 `1234` 是源MySQL服务器上二进制日志文件和位置的信息,需要根据实际情况进行配置。 3. 开始replication。在目标MySQL服务器上执行以下命令: ``` START SLAVE; ``` 执行完毕后,目标MySQL服务器就会开始从源MySQL服务器上同步数据。 需要注意的是,如果在源MySQL服务器上进行数据修改,需要等待replication同步完成后,才能在目标MySQL服务器上看到修改后的数据。此外,在进行replication时,需要确保源MySQL服务器和目标MySQL服务器MySQL版本和存储引擎相同,否则可能会出现数据不一致的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值