通过在不同服务器上的两个数据库中连接两个视图来查询数据

时间:2015-12-26 08:55:20

标签: mysql

在不同服务器上的两个不同数据库中有两个视图,我需要加入它们以便进行少量查询。

我有哪些选项?

我该怎么办?

在同一个数据库中加入视图正在运行。

SELECT * FROM view1
UNION ALL
SELECT * FROM view2

感谢

1 个答案:

答案 0 :(得分:4)

您可以在mysql中使用联合功能。

  1. 在您的mysql中启用联合
  2. 使用root(或具有足够权限的其他帐户)登录mysql CLI。

    输入:show engines; 此时您应该看不到FEDERATED引擎,如下所示:

    mysql> show engines;
    +------------+---------+------------------------------------------------------------+---    -----------+------+------------+
    | Engine     | Support | Comment                                                    |  Transactions | XA   | Savepoints |
    +------------+---------+------------------------------------------------------------+--- -----------+------+------------+
    | MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO               | NO   | NO         |
    | CSV        | YES     | CSV storage engine                                         | NO            | NO   | NO         |
    | MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
    | InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
    | MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO            | NO   | NO         |
    +------------+---------+------------------------------------------------------------+--------------+------+------------+
    5 rows in set (0.00 sec)
    

    要启用联盟引擎,请键入以下内容:

    install plugin federated soname 'ha_federated.so';
    

    不要小心,如果你得到ERROR 1125(HY000):功能'联合'已经存在

    您现在可以安全地将“联合”行添加到/etc/my.cnf文件中,如下所示:

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    federated
    
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    

    重启mysqld(服务mysqld restart等等)

    重启后,返回mysql CLI。

    输入'show engines;' 您现在应该看到FEDERATED引擎可用,并且SUPPORT为YES。

    mysql> show engines;
    +------------+---------+------------------------------------------------------------+--------------+------+------------+
    | Engine     | Support | Comment                                                    | Transactions | XA   | Savepoints |
    +------------+---------+------------------------------------------------------------+--------------+------+------------+
    | FEDERATED  | YES     | Federated MySQL storage engine                             | NO           | NO   | NO         |
    | CSV        | YES     | CSV storage engine                                         | NO           | NO   | NO         |
    | MyISAM     | DEFAULT | Default engine as of MySQL 3.23 with great performance     | NO           | NO   | NO         |
    | InnoDB     | YES     | Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
    | MEMORY     | YES     | Hash based, stored in memory, useful for temporary tables  | NO           | NO   | NO         |
    | MRG_MYISAM | YES     | Collection of identical MyISAM tables                      | NO           | NO   | NO         |
    +------------+---------+------------------------------------------------------------+--------------+------+------------+
    6 rows in set (0.00 sec)
    

    重启mysql

    service mysqld status
    service mysqld stop
    service mysqld start 
    
    1. 在db1中创建view1,在db2中创建view2
    2. 在db1中,

      CREATE table federated_table (
      id int not null,
      name   VARCHAR(32) NOT NULL DEFAULT ''
      ) 
      ENGINE=FEDERATED
      
      CONNECTION='mysql://user_name:password@remote_server_name:3306/db2/view2';
      

      请注意,federated_table和view2必须具有相同的列名称并键入

      1. 执行SELECT * FROM db2.federated_table;

      2. 执行select * from db1.view1 union all select * FROM db1.federated_table;

      3. 参考:http://eves4code.blogspot.in/2015/12/querying-data-by-joining-two-views-in.html