您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么通過mysql的federated插件實現dblink功能”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“怎么通過mysql的federated插件實現dblink功能”吧!
db1:172.26.99.157 3306(源庫)
db2:172.26.99.157 3310(通過dblink調用源庫)
[root@node7 lepus]# mm
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 357
Server version: 5.7.29-log MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV 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 |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql> install plugin federated soname 'ha_federated.so';
ERROR 1125 (HY000): Function 'federated' already exists
mysql> exit
Bye
重啟數據庫,
# mysqladmin -uroot -P3310 -p -h 127.0.0.1 shutdown
# mysqld --defaults-file=/mysql/data/my.cnf --user=mysql --datadir=/mysql/data/3310 --basedir=/mysql/app/mysql --pid-file=/mysql/data/mysql3310.pid --socket=/mysql/data/mysql3310.sock --port=3310 &
在my.cnf中添加參數:
federated
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV 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 |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | YES | Federated MySQL storage engine | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
@3306:
# mysql -uroot -P3306 -h227.0.0.1 -p
mysql> create database testdb;
mysql> use testdb;
mysql> CREATE TABLE `options` (
-> `name` varchar(50) DEFAULT NULL,
-> `value` varchar(255) DEFAULT NULL,
-> `description` varchar(100) DEFAULT NULL,
-> KEY `idx_name` (`name`) USING BTREE
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
@33310:
mysql> use tianlei;
mysql> CREATE TABLE `options` (
-> `name` varchar(50) DEFAULT NULL,
-> `value` varchar(255) DEFAULT NULL,
-> `description` varchar(100) DEFAULT NULL,
-> KEY `idx_name` (`name`) USING BTREE
-> ) ENGINE=FEDERATED DEFAULT CHARSET=utf8
-> CONNECTION='mysql://root:root123@172.26.99.157:3306/testdb/options';
Query OK, 0 rows affected (0.01 sec)
mysql> select * from options;
Empty set (0.01 sec)
@3306:
mysql>
mysql> insert into options (name,value ) values ('log','Y');
Query OK, 1 row affected (0.00 sec)
mysql> select * from options;
+------+-------+-------------+
| name | value | description |
+------+-------+-------------+
| log | Y | NULL |
+------+-------+-------------+
1 row in set (0.00 sec)
@3310:
mysql> select * from options;
+------+-------+-------------+
| name | value | description |
+------+-------+-------------+
| log | Y | NULL |
+------+-------+-------------+
1 row in set (0.00 sec)
mysql> insert into options (name,value ) values ('sql_mode','N');
Query OK, 1 row affected (0.01 sec)
mysql> select * from options;
+----------+-------+-------------+
| name | value | description |
+----------+-------+-------------+
| log | Y | NULL |
| sql_mode | N | NULL |
+----------+-------+-------------+
2 rows in set (0.00 sec)
mysql> update options set description='abc' where name='log';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from options where name ='sql_mode';
Query OK, 1 row affected (0.01 sec)
除了直接使用連接串的方法,還可以先創建server,然后建表時調用server:
還可以使用server的方式將連接串存儲起來。
CREATE SERVER dblink
FOREIGN DATA WRAPPER mysql
OPTIONS (USER 'lepus', HOST '172.26.99.157', PORT 3306, DATABASE 'testdb');
CREATE TABLE `options2` (
`name` varchar(50) DEFAULT NULL,
`value` varchar(255) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
KEY `idx_name` (`name`) USING BTREE
) ENGINE=FEDERATED
DEFAULT CHARSET=utf8
CONNECTION='dblink/options';
mysql> select * from options2;
ERROR 1429 (HY000): Unable to connect to foreign data source: Access denied for user 'lepus'@'172.26.99.157' (using password:
mysql> drop server dblink;
Query OK, 1 row affected (0.00 sec)
CREATE SERVER dblink
FOREIGN DATA WRAPPER mysql
OPTIONS (USER 'lepus', HOST '172.26.99.157', PORT 3306, DATABASE 'testdb',PASSWORD 'lepus');
mysql> CREATE SERVER dblink
-> FOREIGN DATA WRAPPER mysql
-> OPTIONS (USER 'lepus', HOST '172.26.99.157', PORT 3306, DATABASE 'testdb',PASSWORD 'lepus');
Query OK, 1 row affected (0.01 sec)
mysql> select * from options2;
+------+-------+-------------+
| name | value | description |
+------+-------+-------------+
| log | Y | abc |
+------+-------+-------------+
1 row in set (0.01 sec)
mysql> show create table options2\G
*************************** 1. row ***************************
Table: options2
Create Table: CREATE TABLE `options2` (
`name` varchar(50) DEFAULT NULL,
`value` varchar(255) DEFAULT NULL,
`description` varchar(100) DEFAULT NULL,
KEY `idx_name` (`name`) USING BTREE
) ENGINE=FEDERATED DEFAULT CHARSET=utf8 CONNECTION='dblink/options'
1 row in set (0.00 sec)
在mysql 5.7官方文檔中有federated引擎的具體介紹,地址為:
https://dev.mysql.com/doc/refman/5.7/en/federated-storage-engine.html
遠程服務器中包括表定義和數據,可以是myisam、innodb或其他引擎;
本地服務器中只包括相同表結構的federated引擎表和遠程服務器的連接串信息。增刪改查操作都需要發到遠程服務器中。
數據交互如下:
1.存儲引擎查看FEDERATED表擁有的每一列,并構造引用遠程表的適當SQL語句
2.語句使用MySQL客戶端API發送到遠程服務器
3.遠程服務器處理語句,本地服務器檢索語句產生的所有結果(受影響的行數或結果集)
4.如果語句生成了結果集,則每一列都轉換為FEDERATED引擎所期望的內部存儲引擎格式,并可用于向發出原始語句的客戶端顯示結果。
限制:
遠端服務器必須是mysql
在調用前,FEDERATED指向的遠端表必須存在
可以指向FEDERATED引擎表,注意不要循環嵌套
FEDERATED引擎無法使用索引,如果結果集很大,數據存放在內存中,可能使用大量swap甚至hang住。
FEDERATED引擎表支持insert、update、delete、select、truncate table操作和索引操作,但不支持alter table操作
能接受insert …… on deplicate key update,但不生效,有重復值仍然會報錯
不支持事務
可以進行批量插入操作,但注意大小不要超過服務器建能傳輸的最大包大小
FEDERATED引擎表無法獲知遠程服務器上表的變化
使用連接串時,密碼不能包括@符號
INSERT_id和時間戳選項不會傳播到data provider
針對FEDERATED表發出的任何DROP TABLE語句只刪除本地表,而不刪除遠程表
不使用查詢緩存
不支持用戶定義的分區
感謝各位的閱讀,以上就是“怎么通過mysql的federated插件實現dblink功能”的內容了,經過本文的學習后,相信大家對怎么通過mysql的federated插件實現dblink功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。