1. MySQL插件的安裝與卸載
# 查看插件信息 mysql> show plugins; mysql> select plugin_name,plugin_status,plugin_library,plugin_description from information_schema.plugins; # 查看插件共享庫位置 mysql> show variables like 'plugin_dir'; +-----------------+---------------------------+ | Variable_name | Value | +-----------------+---------------------------+ | plugin_dir |/usr/lib64/mysql/plugin/ | +-----------------+---------------------------+ # 安裝插件 install plugin PLUGIN_NAME soname 'SHARTED_LIBRARY_NAME.so' # 卸載插件 uninstall plugin PLUGIN_NAME # 開啟/關閉插件 $ vim /etc/my.cnf [mysqld] PLUGIN_NAME = ON|OFF
2. 激活FEDERATED插件,並創建垮服務器數據庫表
FEDERATED存儲引擎訪問在遠程數據庫的表中的數據,而不是本地的表。這個特性給某些開發應用帶來了便利,你可以直接在本地構建一個FEDERATED表來連接遠程數據表,配置好了之后本地表的數據可以直接跟遠程數據表同步。實際上這個引擎里面是不真實存放數據的,所需要的數據都是連接到其他MySQL服務器上。
2.1 查看FEDERATED引擎是否開啟
# 方法1:查看插件,ACTIVE為開啟,DISABLED為未開啟 > show plugins; +--------------------------------+----------+--------------------+--------------------+---------+ | Name | Status | Type | Library | License | +--------------------------------+----------+--------------------+--------------------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | rpl_semi_sync_master | ACTIVE | REPLICATION | semisync_master.so | GPL | +--------------------------------+----------+--------------------+--------------------+---------+ # 方法2:查看引擎,YES為開啟,NO為未開啟 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 | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | InnoDB | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | FEDERATED | NO | FederatedX pluggable storage engine | NULL | NULL | NULL | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
2.2 開啟FEDERATED插件
# MySQL配置文件中添加federated[=ON],然后重啟MySQL服務 $ vim /etc/my.cnf [mysqld] federated=ON $ systemctl restart mariadb
2.3 創建一張虛擬表,此表鏈接到另外一台服務器數據庫上的表中
# 創建數據庫 mysql> CREATE DATABASE DB_NAME DEFAULT CHARSET utf8; # 創建表 # USER:數據庫賬號,PASSWD:數據庫密碼,HOST:遠程數據庫連接地址,PORT:端口,DB_NAME:數據庫名,TB_NAME:表名 mysql> USE DB_NAME; mysql> CREATE TABLE TABLE_NAME ( ... ) ENGINE=FEDERATED DEFAULT CHARSET=utf8 CONNECTION='mysql://USER:PASSWD@HOST:PORT/DB_NAME/TABLE_NAME'
2.4 注意事項
1. 對本地虛擬表的結構修改,並不會修改遠程表的結構
2.truncate 命令,會清除遠程表數據
3. drop命令只會刪除虛擬表,並不會刪除遠程表