在多个数据库上执行一个MySQL查询 [英] Execute one MySQL query on multiple databases
问题描述
我有一个简单的问题,使我整夜不眠:(.
I have a simple question that keeps me up all night :( .
假设我们在不同的服务器上有2个数据库.您可以轻松地创建2个连接并处理两个数据库中的数据.
Suppose we have 2 databases on different servers. You can easily create 2 connections and manipulate data in both databases.
如果我要在两个数据库上执行一个查询怎么办?
What if I want to execute one query on both databases?
示例:
INSERT INTO database1.table1
VALUES ( 'one','two','three')
WHERE database1.table1.something LIKE (SELECT something from database2.table2)
对不起,我的代码对SQL代码没有真正的经验.
Sorry for my code, I'm not really experienced with SQL code.
当所有数据库都可以通过同一连接访问时,这样的尝试是可能的.
Such attempts are possible when the databases are all accessible through the same connection.
但是,如果数据库位于单独的服务器上(需要两个连接时),是否有可能获得相同的结果?
But is it possible to achieve the same result if the databases are on separate servers (when you need two connections) ?
如果存在这样的解决方案,对我来说会容易得多:(.
It would be so much easier for me if such a solution would exist :( .
最诚挚的问候
推荐答案
嗯,可以通过一个连接(嗯,从客户端到其中一台服务器的一个连接.从服务器到另一台服务器的连接是可能的. ).只需使用 FEDERATED
存储引擎 .它会按照您想要的方式工作(但是请注意,由于所有添加的解析和网络流量,它可能会非常慢)...
Well, it's possible over one connection (Well, one connection from the client to one of the servers. There'll be connections from the server to the other servers). Just use the FEDERATED
storage engine. It'll work the way you want (but beware it'll likely be quite slow due to all the added parsing and network traffic)...
这篇关于在多个数据库上执行一个MySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!