您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么通過(guò)mysql的federated插件實(shí)現(xiàn)dblink功能”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“怎么通過(guò)mysql的federated插件實(shí)現(xiàn)dblink功能”吧!
db1:172.26.99.157 3306(源庫(kù))
db2:172.26.99.157 3310(通過(guò)dblink調(diào)用源庫(kù))
[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
重啟數(shù)據(jù)庫(kù),
# 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中添加參數(shù):
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)
除了直接使用連接串的方法,還可以先創(chuàng)建server,然后建表時(shí)調(diào)用server:
還可以使用server的方式將連接串存儲(chǔ)起來(lái)。
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
遠(yuǎn)程服務(wù)器中包括表定義和數(shù)據(jù),可以是myisam、innodb或其他引擎;
本地服務(wù)器中只包括相同表結(jié)構(gòu)的federated引擎表和遠(yuǎn)程服務(wù)器的連接串信息。增刪改查操作都需要發(fā)到遠(yuǎn)程服務(wù)器中。
數(shù)據(jù)交互如下:
1.存儲(chǔ)引擎查看FEDERATED表?yè)碛械拿恳涣?,并?gòu)造引用遠(yuǎn)程表的適當(dāng)SQL語(yǔ)句
2.語(yǔ)句使用MySQL客戶端API發(fā)送到遠(yuǎn)程服務(wù)器
3.遠(yuǎn)程服務(wù)器處理語(yǔ)句,本地服務(wù)器檢索語(yǔ)句產(chǎn)生的所有結(jié)果(受影響的行數(shù)或結(jié)果集)
4.如果語(yǔ)句生成了結(jié)果集,則每一列都轉(zhuǎn)換為FEDERATED引擎所期望的內(nèi)部存儲(chǔ)引擎格式,并可用于向發(fā)出原始語(yǔ)句的客戶端顯示結(jié)果。
限制:
遠(yuǎn)端服務(wù)器必須是mysql
在調(diào)用前,F(xiàn)EDERATED指向的遠(yuǎn)端表必須存在
可以指向FEDERATED引擎表,注意不要循環(huán)嵌套
FEDERATED引擎無(wú)法使用索引,如果結(jié)果集很大,數(shù)據(jù)存放在內(nèi)存中,可能使用大量swap甚至hang住。
FEDERATED引擎表支持insert、update、delete、select、truncate table操作和索引操作,但不支持alter table操作
能接受insert …… on deplicate key update,但不生效,有重復(fù)值仍然會(huì)報(bào)錯(cuò)
不支持事務(wù)
可以進(jìn)行批量插入操作,但注意大小不要超過(guò)服務(wù)器建能傳輸?shù)淖畲蟀笮?/p>
FEDERATED引擎表無(wú)法獲知遠(yuǎn)程服務(wù)器上表的變化
使用連接串時(shí),密碼不能包括@符號(hào)
INSERT_id和時(shí)間戳選項(xiàng)不會(huì)傳播到data provider
針對(duì)FEDERATED表發(fā)出的任何DROP TABLE語(yǔ)句只刪除本地表,而不刪除遠(yuǎn)程表
不使用查詢緩存
不支持用戶定義的分區(qū)
感謝各位的閱讀,以上就是“怎么通過(guò)mysql的federated插件實(shí)現(xiàn)dblink功能”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)怎么通過(guò)mysql的federated插件實(shí)現(xiàn)dblink功能這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。