這篇文章主要講解了“怎么通過MySQL的federated插件實現(xiàn)dblink功能”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么通過mysql的federated插件實現(xiàn)dblink功能”吧!
創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),岫巖企業(yè)網(wǎng)站建設(shè),岫巖品牌網(wǎng)站建設(shè),網(wǎng)站定制,岫巖網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,岫巖網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
db1:172.26.99.157 3306(源庫)
db2:172.26.99.157 3310(通過dblink調(diào)用源庫)
[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ù)庫,
# 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,然后建表時調(diào)用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
遠程服務(wù)器中包括表定義和數(shù)據(jù),可以是myisam、innodb或其他引擎;
本地服務(wù)器中只包括相同表結(jié)構(gòu)的federated引擎表和遠程服務(wù)器的連接串信息。增刪改查操作都需要發(fā)到遠程服務(wù)器中。
數(shù)據(jù)交互如下:
1.存儲引擎查看FEDERATED表擁有的每一列,并構(gòu)造引用遠程表的適當(dāng)SQL語句
2.語句使用MySQL客戶端API發(fā)送到遠程服務(wù)器
3.遠程服務(wù)器處理語句,本地服務(wù)器檢索語句產(chǎn)生的所有結(jié)果(受影響的行數(shù)或結(jié)果集)
4.如果語句生成了結(jié)果集,則每一列都轉(zhuǎn)換為FEDERATED引擎所期望的內(nèi)部存儲引擎格式,并可用于向發(fā)出原始語句的客戶端顯示結(jié)果。
限制:
遠端服務(wù)器必須是mysql
在調(diào)用前,F(xiàn)EDERATED指向的遠端表必須存在
可以指向FEDERATED引擎表,注意不要循環(huán)嵌套
FEDERATED引擎無法使用索引,如果結(jié)果集很大,數(shù)據(jù)存放在內(nèi)存中,可能使用大量swap甚至hang住。
FEDERATED引擎表支持insert、update、delete、select、truncate table操作和索引操作,但不支持alter table操作
能接受insert …… on deplicate key update,但不生效,有重復(fù)值仍然會報錯
不支持事務(wù)
可以進行批量插入操作,但注意大小不要超過服務(wù)器建能傳輸?shù)淖畲蟀笮?/p>
FEDERATED引擎表無法獲知遠程服務(wù)器上表的變化
使用連接串時,密碼不能包括@符號
INSERT_id和時間戳選項不會傳播到data provider
針對FEDERATED表發(fā)出的任何DROP TABLE語句只刪除本地表,而不刪除遠程表
不使用查詢緩存
不支持用戶定義的分區(qū)
感謝各位的閱讀,以上就是“怎么通過mysql的federated插件實現(xiàn)dblink功能”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對怎么通過mysql的federated插件實現(xiàn)dblink功能這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!