作者:马文斌
mysql ocp 认证,postgressql pgca 认证,擅长 mysql、postgresql、dble 等开源数据库相关产品的备份恢复、读写分离、sql 调优、监控运维、高可用架构设计等。目前任职于月亮小屋(中国)有限公司。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
当客户端发出引用 federated 表的 sql 语句时,本地服务器(执行 sql 语句的地方)和远程服务器(实际存储数据的地方)之间的信息流如下:
1) 插入的大小不能超过服务器之间的最大数据包大小。如果插入超过此大小,它将被分成多个数据包,并可能发生回滚问题。
2) 不会进行批量插入处理 insert ... on duplicate key update。
两个 mysql-5.7.26 实例
"配置文件中添加 federated 引擎就可以,两个实例都要添加 vim /etc/my.cnf [mysqld] federated
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 | | performance_schema | yes | performance schema | no | no | no | | memory | yes | hash based, stored in memory, useful for temporary tables | no | no | no | | archive | yes | archive storage engine | no | no | no | | innodb | default | supports transactions, row-level locking, and foreign keys | yes | yes | yes | | federated | yes | federated mysql storage engine | no | no | no | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec) federated | yes 说明已经开启了federated 引擎
create database db1; use db1; create table tb1( id int primary key not null auto_increment )engine=innodb; 插入数据: insert into tb1 select null; insert into tb1 select null; insert into tb1 select null; 查看数据 mysql> select * from tb1; +----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec)
create user 'test'@'192.168.%' identified by '123456'; grant select,update,insert,delete on db1.* to 'test'@'192.168.%'; flush privileges;
create database db2; use db2; create table remote_tb1( id int primary key not null auto_increment )engine=federated connection='mysql://test:123456@192.168.234.204:3306/db1/tb1'; create table tb2( id int primary key not null auto_increment, name varchar(20) )engine=innodb; 插入数据: insert into tb2(name) select 'a'; insert into tb2(name) select 'b'; insert into tb2(name) select 'c'; mysql> select * from db2.tb2; +----+------+ | id | name | +----+------+ | 1 | a | | 2 | b | | 3 | c | +----+------+ 3 rows in set (0.00 sec)
mysql> select * from db2.remote_tb1; +----+ | id | +----+ | 1 | | 2 | | 3 | +----+ 3 rows in set (0.00 sec) 说明是关联上了, 测试下join: select t2.* from db2.remote_tb1 as t1 join db2.tb2 as t2 on t1.id=t2.id where t2.name='c'; mysql> select t2.* from db2.remote_tb1 as t1 join -> db2.tb2 as t2 on t1.id=t2.id -> where t2.name='c'; +----+------+ | id | name | +----+------+ | 3 | c | +----+------+ 1 row in set (0.00 sec) 说明本地表和远程表关联也是可以的。
mysql> delete from db2.remote_tb1 where id =3; query ok, 1 row affected (0.00 sec) mysql> select * from db2.remote_tb1; +----+ | id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec)
set @@session.gtid_next= 'anonymous'/*!*/; # at 40057515 #210415 14:25:53 server id 2342042 end_log_pos 40057586 crc32 0x82abe215 query thread_id=53 exec_time=0 error_code=0 set timestamp=1618467953/*!*/; set @@session.sql_mode=1411383296/*!*/; /*!c utf8 *//*!*/; set @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=45/*!*/; begin /*!*/; # at 40057586 #210415 14:25:53 server id 2342042 end_log_pos 40057638 crc32 0xddbc9dfc table_map: `db2`.`remote_tb1` mapped to number 416 # at 40057638 #210415 14:25:53 server id 2342042 end_log_pos 40057678 crc32 0x5c28d0d0 delete_rows: table id 416 flags: stmt_end_f binlog ' cdx3yboavcmanaaaacy7ywiaakabaaaaaaeaa2rimgakcmvtb3rlx3rimqabawaa/j283q== cdx3yccavcmakaaaae47ywiaakabaaaaaaeaagab//4daaaa0naoxa== '/*!*/; ### delete from `db2`.`remote_tb1` ### where ### @1=3 /* int meta=0 nullable=0 is_null=0 */ # at 40057678 #210415 14:25:53 server id 2342042 end_log_pos 40057750 crc32 0xb37fe7b3 query thread_id=53 exec_time=0 error_code=0 set timestamp=1618467953/*!*/; commit /*!*/; 从 binlog set @@session.gtid_next= 'anonymous'/*!*/; 是可以看出,链接端操作dml是把gtid 事务隐藏了。
.frm 表定义文件 [ federated链接库本地不产生数据文件 ]
[root@eos_db04 db2]# pwd /mysqldata/3310_data/data/db2 [root@eos_db04 db2]# ll total 128 -rw-r----- 1 mysql mysql 67 apr 15 14:11 db.opt -rw-r----- 1 mysql mysql 8556 apr 15 14:11 remote_tb1.frm -rw-r----- 1 mysql mysql 8586 apr 15 14:18 tb2.frm -rw-r----- 1 mysql mysql 98304 apr 15 14:18 tb2.ibd
对比其他的数据同步产品,这种建立 链接 跨 ip 跨库查询会显轻便一些,搭建起来很方便。
剧情片爱的引擎高清在线观看由6080影院整理于网络,并免费提供爱的引擎剧照,爱的引擎hdbd高清版,爱的引擎酷播在线播放等资源,在线播放有酷播,腾讯视频,优酷视频,爱奇艺视频等多种在线播放模式,在播放不流畅的情况下可以尝试切换播放源。观看《爱的引擎》切勿长时间用眼过度,避免用眼疲劳,如果你喜欢这部片子,可以分享给你的亲朋好友一起免费观看。6080影院收集各类经典电影,是电影爱好者不二的网站选择!