mysql使用federated实现dblink远程表访问

  1. source 端创建测试表
create table s select * from mysql.user ;

2. source 端查看测试表的建表语句
show create table s ;

<<EOF
CREATE TABLE `s` (
`Host` char(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
`ssl_cipher` blob NOT NULL,
`x509_issuer` blob NOT NULL,
`x509_subject` blob NOT NULL,
`max_questions` int(11) unsigned NOT NULL DEFAULT '0',
`max_updates` int(11) unsigned NOT NULL DEFAULT '0',
`max_connections` int(11) unsigned NOT NULL DEFAULT '0',
`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
`plugin` char(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '',
`authentication_string` text CHARACTER SET utf8 COLLATE utf8_bin,
`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'
) ENGINE=InnoDB DEFAULT CHARSET=latin1

EOF

3. source端创建用户并且授权

GRANT ALL PRIVILEGES ON sources.s TO dex IDENTIFIED BY 'xiaojun';
GRANT ALL PRIVILEGES ON sources.s TO dex@192.168.100.42 IDENTIFIED BY 'xiaojun';
SHOW GRANTS FOR dex;

mysql> GRANT ALL PRIVILEGES ON sources.s TO dex IDENTIFIED BY 'xiaojun';
Query OK, 0 rows affected (0.03 sec)

mysql> GRANT ALL PRIVILEGES ON sources.s TO dex@192.168.100.42 IDENTIFIED BY 'xiaojun';
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW GRANTS FOR dex;
+----------------------------------------------------------------------------------------------------+
| Grants for dex@% |
+----------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'dex'@'%' IDENTIFIED BY PASSWORD '*8FDE30312222738F1CD8AC8AF0EE515A9DB8180E' |
| GRANT ALL PRIVILEGES ON `sources`.`s` TO 'dex'@'%' |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

4. 查看target端是否安装了FEDERATED存储引擎

mysql> mysql> show engines ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

看到没有安装federated存储引擎(目标端可以是任何的存储引擎比如说myisam或者innodb)

4.1 先来安装federated存储引擎

install plugin federated soname 'ha_federated.so';

mysql> install plugin federated soname 'ha_federated.so';
ERROR 1125 (HY000): Function 'federated' already exists

已经安装好了,只是没有启用

测试一下是否可以

[root@rhel6Mysql02 ~]# mysqld_safe --federated &
[1] 2194
[root@rhel6Mysql02 ~]# 130620 18:12:28 mysqld_safe Logging to '/var/lib/mysql/rhel6Mysql02.err'.
130620 18:12:28 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql

mysql> show engines ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

好的修改一下my.conf文件

federated

重启一下mysql server
service mysql restart

恩,已经支持了。
mysql> show engines ;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

5. 在target端定义基于federated存储引擎的表links

CREATE TABLE `links` (
`Host` char(60) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`User` char(16) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '',
`Password` char(41) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL DEFAULT '',
`Select_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Insert_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Update_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Delete_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Drop_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Reload_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Shutdown_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Process_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`File_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Grant_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`References_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Index_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_db_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Super_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tmp_table_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Lock_tables_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Execute_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_slave_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Repl_client_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Show_view_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Alter_routine_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_user_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Event_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Trigger_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`Create_tablespace_priv` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
`ssl_type` enum('','ANY','X509','SPECIFIED') CHARACTER SET utf8 NOT NULL DEFAULT '',
`ssl_cipher` blob NOT NULL,
`x509_issuer` blob NOT NULL,
`x509_subject` blob NOT NULL,
`max_questions` int(11) unsigned NOT NULL DEFAULT '0',
`max_updates` int(11) unsigned NOT NULL DEFAULT '0',
`max_connections` int(11) unsigned NOT NULL DEFAULT '0',
`max_user_connections` int(11) unsigned NOT NULL DEFAULT '0',
`plugin` char(64) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT '',
`authentication_string` text CHARACTER SET utf8 COLLATE utf8_bin,
`password_expired` enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N'
)
ENGINE=FEDERATED
DEFAULT CHARSET=latin1
CONNECTION='mysql://dex:xiaojun@192.168.100.41:3306/sources/s';

<!--
dex=username
xiaojun=password
192.168.100.41=source ip
3306=source mysql server listener port
sources=source database name
s=source table name
-->

Query OK, 0 rows affected (1.10 sec)

mysql> select count(*) from links;
+----------+
| count(*) |
+----------+
| 12 |
+----------+
1 row in set (0.00 sec)

时间: 2025-01-01 11:06:04

mysql使用federated实现dblink远程表访问的相关文章

【MySQL】FEDERATED存储引擎

FEDERATED存储引擎的描述 mysql 提供了一个类似Oracle中的数据库链接(DBLINK)功能的存储引擎--FEDERATED.当我们创建一个以FEDERATED为存储引擎的表时,服务器在数据库目录只创建一个表定义文件.文件由表的名字开始,并有一个frm扩展名.无其它文件被创建,因为实际的数据在一个远程数据库上.这不同于为本地表工作的存储引擎的方式. 如何使用FEDERATED存储引擎 CREATE TABLE federated_table (     id     int(20)

Mysql 开启Federated引擎的方法_Mysql

MySQL中针对不同的功能需求提供了不同的存储引擎.所谓的存储引擎也就是MySQL下特定接口的具体实现. FEDERATED是其中一个专门针对远程数据库的实现.一般情况下在本地数据库中建表会在数据库目录中生成相应的表定义文件,并同时生成相应的数据文件. 但通过FEDERATED引擎创建的表只是在本地有表定义文件,数据文件则存在于远程数据库中(这一点很重要). 通过这个引擎可以实现类似Oracle 下DBLINK的远程数据访问功能. 使用show engines 命令查看数据库是否已支持FEDER

mysql root丢失权限而用户不能访问本地数据库

继mysql数据库不能访问之后,通过前面的方法解决问题,但是却出现了两个不相关的数据库在phpmyadmin里面,而mysql等数据库却没有出现,也没有创建新数据库等权限,什么权限都没有啦. 多方查找资料,发现通过mysqld_safe --skip-grant-tables --skip-networking &命令,可以跳过权限扫描,对所有数据库都可以操作,于是这样登陆,然后进入mysql数据库,并对user表进行操作,将值为%的的记录中的Host的值,改为localhost.问题解决. 步

MySQL中的行级锁,表级锁,页级锁

在计算机科学中,锁是在执行多线程时用于强行限制资源访问的同步机制,即用于在并发控制中保证对互斥要求的满足. 在数据库的DBMS中,可以按照锁的粒度把数据库锁分为行级锁(INNODB引擎).表级锁(MYISAM引擎)和页级锁(BDB引擎 ). 行级锁 行级锁是Mysql中锁定粒度最细的一种锁,表示只针对当前操作的行进行加锁.行级锁能大大减少数据库操作的冲突.其加锁粒度最小,但加锁的开销也最大.行级锁分为共享锁和排它锁(MySQL中的共享锁与排他锁) 特点 开销大,加锁慢:会出现死锁:锁定粒度最小,

远程数据访问的解决方案

访问|解决|数据 远程数据访问的解决方案 问题ADO 可让应用程序直接访问并修改数据源(有时称为双层系统).例如,如果要连接到包含所需数据的数据源,则该连接即是在双层系统中的直接连接. 然而,也需要通过象 Microsoft Internet Information Server (IIS) 这样的媒介间接地访问数据源.这种方法称为三层系统.IIS 采用客户端/服务器系统,该系统可帮助本地(或客户端)的应用程序通过 Internet 或 Intranet 高效地调用远程(或服务器)程序.服务器程

MySQL中的创建库、表以及查询的基础语句

MySQL中的创建库.表以及查询语句对我们以后很好的应用数据库是很大有帮助的,本文中是对这些基础语 句的总结,希望会对大家有些帮助 1.创建与删除数据库 创建数据库 mysql> create database testdb; mysql> create database if not exists testdb; mysql> create schema if not exists student character set 'gbk' collate 'gbk_chinese_ci'

如何使用Oracle的外部表访问跟踪文件

前面说过Oracle的外部表可以用来访问警告日志文件,其实Oracle的外部表可以非常灵活的被使用. 通过Create Directory命令创建相应的Directory之后,我们可以将目录的访问权限授予其他用户,这样其他用户就能通过外部表访问很多主机上的文件. 我们看一下使用外部表访问跟踪文件的例子(我的例子是用SYS用户来完成的). 首先创建一个指向跟踪文件的Directory: [oracle@jumper oracle]$ sqlplus "/ as sysdba" SQL*P

Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

  Sql Server中的表访问方式Table Scan, Index Scan, Index Seek 0.参考文献 oracle表访问方式 Index Seek和Index Scan的区别以及适用情况 1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过rowid扫描(table access by rowid),如果知道数据的rowid,那么直接通

MYSQL 中怎样把同一张表中相同字段的内容合并为一条记录?

问题描述 MYSQL 中怎样把同一张表中相同字段的内容合并为一条记录? 我的表现在是这样的: 客户id 购买产品id 1 123 1 124 1 125 1 126 2 168 2 169 想让表转变成: 客户id 购买产品id 1 123,124,125,126 2 168,169 也就是说把一个客户购买的产品合并显示在一条记录中. 解决方案 使用group_concat 解决方案二: 用行转列实现,今天上午看到一个和你的问题完全符合 http://blog.csdn.net/ning1093