欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL中使用FREDATED引擎实现跨数据库服务器、跨实例访问

程序员文章站 2024-03-02 12:43:34
跨数据库服务器,跨实例访问是比较常见的一种访问方式,在oracle中可以通过db link的方式来实现。对于mysql而言,有一个federated存储引擎与之相对应。同样...

跨数据库服务器,跨实例访问是比较常见的一种访问方式,在oracle中可以通过db link的方式来实现。对于mysql而言,有一个federated存储引擎与之相对应。同样也是通过创建一个链接方式的形式来访问远程服务器上的数据。本文简要描述了federated存储引擎,以及演示了基于federated存储引擎跨实例访问的示例。

1、federated存储引擎的描述

  federated存储引擎允许在不使用复制或集群技术的情况下实现远程访问数据库
  创建基于federated存储引擎表的时候,服务器在数据库目录仅创建一个表定义文件,即以表名开头的.frm文件。

  federated存储引擎表无任何数据存储到本地,即没有.myd文件
  对于远程服务器上表的操作与本地表操作一样,仅仅是数据位于远程服务器
  基本流程如下:   

MySQL中使用FREDATED引擎实现跨数据库服务器、跨实例访问

2、安装与启用federated存储引擎

  源码安装mysql时使用dwith_federated_storage_engine来配置
  rpm安装方式缺省情况下已安装,只需要启用该功能即可

3、准备远程服务器环境

复制代码 代码如下:

-- 此演示中远程服务器与本地服务器为同一服务器上的多版本多实例 
-- 假定远程服务为:5.6.12(实例3406) 
-- 假定本地服务器:5.6.21(实例3306)    
-- 基于实例3306创建federated存储引擎表test.federated_engine以到达访问实例3406数据库tempdb.tb_engine的目的 
 
[root@rhel64a ~]# cat /etc/issue 
red hat enterprise linux server release 6.4 (santiago)  
 
--启动3406的实例 
[root@rhel64a ~]# /u01/app/mysql/bin/mysqld_multi start 3406 
[root@rhel64a ~]# mysql -uroot -pxxx -p3406 --protocol=tcp 
 
root@localhost[(none)]> show variables like 'server_id'; 
+---------------+-------+ 
| variable_name | value | 
+---------------+-------+ 
| server_id     | 3406  | 
+---------------+-------+ 
 
--实例3406的版本号 
root@localhost[tempdb]> show variables like 'version'; 
+---------------+------------+ 
| variable_name | value      | 
+---------------+------------+ 
| version       | 5.6.12-log | 
+---------------+------------+ 
 
--创建数据库 
root@localhost[(none)]> create database tempdb; 
query ok, 1 row affected (0.00 sec) 
 
-- author : leshami 
-- blog   :http://blog.csdn.net/leshami 
 
root@localhost[(none)]> use tempdb 
database changed 
 
--创建用于访问的表 
root@localhost[tempdb]> create table tb_engine as  
    -> select engine,support,comment from information_schema.engines; 
query ok, 9 rows affected (0.10 sec) 
records: 9  duplicates: 0  warnings: 0 
 
--提取表的sql语句用于创建为federated存储引擎表 
root@localhost[tempdb]> show create table tb_engine \g 
*************************** 1. row *************************** 
       table: tb_engine 
create table: create table `tb_engine` ( 
  `engine` varchar(64) not null default '', 
  `support` varchar(8) not null default '', 
  `comment` varchar(80) not null default '' 
) engine=innodb default charset=utf8 
 
--创建用于远程访问的账户 
root@localhost[tempdb]> grant all privileges on tempdb.* to 'remote_user'@'192.168.1.131' identified by 'xxx'; 
query ok, 0 rows affected (0.00 sec) 
 
root@localhost[tempdb]> flush privileges; 
query ok, 0 rows affected (0.00 sec) 

4、演示federated存储引擎跨实例访问

复制代码 代码如下:

[root@rhel64a ~]# mysql -uroot -pxxx 
 
root@localhost[(none)]> show variables like 'version'; 
+---------------+--------+ 
| variable_name | value  | 
+---------------+--------+ 
| version       | 5.6.21 | 
+---------------+--------+ 
 
#查看是否支持federated引擎 
root@localhost[(none)]> select * from information_schema.engines where engine='federated'; 
+-----------+---------+--------------------------------+--------------+------+------------+ 
| engine    | support | comment                        | transactions | xa   | savepoints | 
+-----------+---------+--------------------------------+--------------+------+------------+ 
| federated | no      | federated mysql storage engine | null         | null | null       | 
+-----------+---------+--------------------------------+--------------+------+------------+ 
 
root@localhost[(none)]> exit 
[root@rhel64a ~]# service mysql stop 
shutting down mysql..[  ok  ] 
#配置启用federated引擎 
[root@rhel64a ~]# vi /etc/my.cnf 
[root@rhel64a ~]# tail -7 /etc/my.cnf 
[mysqld] 
socket = /tmp/mysql3306.sock 
port = 3306 
pid-file = /var/lib/mysql/my3306.pid 
user = mysql 
server-id=3306/ 
federated         #添加该选项 
[root@rhel64a ~]# service mysql start 
starting mysql.[  ok  ] 
[root@rhel64a ~]# mysql -uroot -pxxx 
root@localhost[(none)]> select * from information_schema.engines where engine='federated'; 
+-----------+---------+--------------------------------+--------------+------+------------+ 
| engine    | support | comment                        | transactions | xa   | savepoints | 
+-----------+---------+--------------------------------+--------------+------+------------+ 
| federated | yes     | federated mysql storage engine | no           | no   | no         | 
+-----------+---------+--------------------------------+--------------+------+------------+ 
 
root@localhost[(none)]> use test 
 
-- 创建基于federated引擎的表federated_engine 
root@localhost[test]> create table `federated_engine` ( 
    ->   `engine` varchar(64) not null default '', 
    ->   `support` varchar(8) not null default '', 
    ->   `comment` varchar(80) not null default '' 
    -> ) engine=federated default charset=utf8 
    -> connection='mysql://remote_user:xxx@192.168.1.131:3406/tempdb/tb_engine'; 
query ok, 0 rows affected (0.00 sec) 
 
-- 下面是创建后表格式文件 
root@localhost[test]> system ls -hltr /var/lib/mysql/test 
total 12k 
-rw-rw---- 1 mysql mysql 8.5k oct 24 08:22 federated_engine.frm 
 
--查询表federated_engine 
root@localhost[test]> select * from federated_engine limit 2; 
+------------+---------+---------------------------------------+ 
| engine     | support | comment                               | 
+------------+---------+---------------------------------------+ 
| mrg_myisam | yes     | collection of identical myisam tables | 
| csv        | yes     | csv storage engine                    | 
+------------+---------+---------------------------------------+ 
 
--更新表federated_engine 
root@localhost[test]> update federated_engine set support='no' where engine='csv'; 
query ok, 1 row affected (0.03 sec) 
rows matched: 1  changed: 1  warnings: 0 
 
--查看更新后的结果 
root@localhost[test]> select * from federated_engine where engine='csv'; 
+--------+---------+--------------------+ 
| engine | support | comment            | 
+--------+---------+--------------------+ 
| csv    | no      | csv storage engine | 
+--------+---------+--------------------+ 

5、创建federated引擎表的链接方式

复制代码 代码如下:

scheme://user_name[:password]@host_name[:port_num]/db_name/tbl_name
    scheme: a recognized connection protocol. only mysql is supported as the scheme value at this point.
    user_name: the user name for the connection. this user must have been created on the remote server, and must have suitable privileges to perform the required actions (select, insert,update, and so forth) on the remote table.
    password: (optional) the corresponding password for user_name.
    host_name: the host name or ip address of the remote server.
    port_num: (optional) the port number for the remote server. the default is 3306.
    db_name: the name of the database holding the remote table.
    tbl_name: the name of the remote table. the name of the local and the remote table do not have to match.
链接示例样本:
    connection='mysql://username:password@hostname:port/database/tablename'
    connection='mysql://username@hostname/database/tablename'
    connection='mysql://username:password@hostname/database/tablename'