$ mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2835
Server version: 10.4.6-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB> source /usr/share/mysql/install_spider.sql
MariaDB> SHOW ENGINES;
+--------------------+---------+-------------------------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+-------------------------------------------------------------------------------------------------+--------------+------+------------+
| SPIDER | YES | Spider storage engine | YES | YES | 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 |
| Aria | YES | Crash-safe tables with MyISAM heritage. Used for internal temporary tables and privilege tables | NO | NO | NO |
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
| SEQUENCE | YES | Generated tables filled with sequential values | YES | NO | YES |
| InnoDB | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
+--------------------+---------+-------------------------------------------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.001 sec)
$ mysql -u root -S /tmp/mariadb2.sock -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.4.8-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE spidertest;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use spidertest;
Database changed
MariaDB [spidertest]> CREATE TABLE customer(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(200) NOT NULL,
address VARCHAR(255) NOT NULL);
Query OK, 0 rows affected (0.539 sec)
接下来向该表中插入一些测试数据:
MariaDB [spidertest]> INSERT INTO customer VALUES(NULL, 'John Doe', '1 Main Street');
Query OK, 1 row affected (0.309 sec)
MariaDB [spidertest]> INSERT INTO customer VALUES(NULL, 'Bob Smith', '45 Elm Street');
Query OK, 1 row affected (0.092 sec)
MariaDB [spidertest]> INSERT INTO customer VALUES(NULL, 'Jane Jones',
'18 Second Street');
Query OK, 1 row affected (0.094 sec)
MariaDB [spidertest]> CREATE USER 'spider'@'192.168.0.11' IDENTIFIED BY 'spider';
Query OK, 0 rows affected (0.236 sec)
MariaDB [spidertest]> GRANT ALL ON spidertest.* TO 'spider'@'192.168.0.11';
Query OK, 0 rows affected (0.238 sec)
MariaDB [spidertest]> GRANT ALL ON mysql.* TO 'spider'@'192.168.0.11';
Query OK, 0 rows affected (0.238 sec)
$ mysql -u root -S /tmp/mariadb1.sock -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.4.8-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [none]> CREATE SERVER Server2 FOREIGN DATA WRAPPER mysql
OPTIONS(HOST '192.168.0.11', DATABASE 'spidertest', PORT 10482,
USER 'spider', PASSWORD 'spider');
Query OK, 0 rows affected (0.233 sec)
$ mysql -u root -S /tmp/mariadb1.sock -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 33
Server version: 10.4.8-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> DROP DATABASE IF EXISTS spidertest;
Query OK, 0 rows affected, 1 warning (0.000 sec)
MariaDB [(none)]> CREATE DATABASE spidertest;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> use spidertest;
Database changed
MariaDB [spidertest]> CREATE TABLE customer(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(200) NOT NULL) ENGINE=Spider
COMMENT = 'wrapper "mysql", srv "Server2"';
Query OK, 0 rows affected (0.132 sec)
MariaDB [spidertest]> SELECT * FROM customer;
+----+------------+
| id | name |
+----+------------+
| 1 | John Doe |
| 2 | Bob Smith |
| 3 | Jane Jones |
+----+------------+
3 rows in set (0.006 sec)
CREATE OR REPLACE VIEW global_status_homer
AS SElECT 'homer' host, gs.variable_name, gs.variable_value
FROM information_schema.global_status gs;
CREATE OR REPLACE VIEW global_status_moe
AS SElECT 'moe' host, gs.variable_name, gs.variable_value
FROM information_schema.global_status gs;
CREATE OR REPLACE SERVER homer FOREIGN DATA WRAPPER mysql
OPTIONS(HOST '192.168.0.11', DATABASE 'mysql', PORT 10482, USER 'spider',
PASSWORD 'spider');
CREATE OR REPLACE TABLE global_status_homer(host varchar(2048), variable_name VARCHAR(64), variable_value VARCHAR(64)) ENGINE=Spider COMMENT='wrapper "mysql", srv "homer"';
CREATE OR REPLACE VIEW global_status_all AS
SELECT host, variable_name, variable_value
FROM global_status_homer
UNION
SELECT host, variable_name, variable_value
FROM global_status_moe;
CREATE OR REPLACE VIEW global_status_total AS
SELECT variable_name, SUM(variable_value) sum, MAX(variable_value) max,
MIN(variable_value) min
FROM global_status_all
GROUP BY variable_name;
MariaDB [mysql]> SELECT * FROM global_status_total WHERE variable_name LIKE 'open%';嗯,尽管这是一个简单实例,但它却很有用。如果服务器多于两台时,它会更有价值。
+--------------------------+------+------+------+
| variable_name | sum | max | min |
+--------------------------+------+------+------+
| OPENED_FILES | 629 | 477 | 152 |
| OPENED_PLUGIN_LIBRARIES | 1 | 1 | 0 |
| OPENED_TABLES | 112 | 75 | 37 |
| OPENED_TABLE_DEFINITIONS | 125 | 95 | 30 |
| OPENED_VIEWS | 85 | 43 | 42 |
| OPEN_FILES | 132 | 76 | 56 |
| OPEN_STREAMS | 0 | 0 | 0 |
| OPEN_TABLES | 77 | 46 | 31 |
| OPEN_TABLE_DEFINITIONS | 83 | 49 | 34 |
+--------------------------+------+------+------+
9 rows in set (0.029 sec)
CREATE DATABASE IF NOT EXISTS spidertest;
CREATE TABLE spidertest.customer(
id INT NOT NULL PRIMARY KEY,
name VARCHAR(200) NOT NULL,
address VARCHAR(255) NOT NULL);
CREATE OR REPLACE SERVER Server2 FOREIGN DATA WRAPPER mysql
OPTIONS(HOST '192.168.0.11', DATABASE 'spidertest', PORT 10482,
USER 'spider', PASSWORD 'spider');
CREATE OR REPLACE SERVER Server3 FOREIGN DATA WRAPPER mysql
OPTIONS(HOST '192.168.0.11', DATABASE 'spidertest', PORT 10483,
USER 'spider', PASSWORD 'spider');
CREATE TABLE spidertest.customer(id INT NOT NULL PRIMARY KEY,
name VARCHAR(200) NOT NULL,
address VARCHAR(255) NOT NULL) ENGINE=Spider
COMMENT 'wrapper "mysql", table "customer"'
PARTITION BY RANGE(id) (
PARTITION p0 VALUES LESS THAN (1000) COMMENT = 'srv "Server2"',
PARTITION p1 VALUES LESS THAN (2000) COMMENT = 'srv "Server3
INSERT INTO customer VALUES(1, 'Larry', 'Main Street 1');
INSERT INTO customer VALUES(2, 'Ed', 'Main Street 1');
INSERT INTO customer VALUES(3, 'Bob', 'Main Street 1');
INSERT INTO customer VALUES(1001, 'Monty', 'Main Street 1');
INSERT INTO customer VALUES(1002, 'David', 'Main Street 1');
INSERT INTO customer VALUES(1003, 'Allan', 'Main Street 1');
作者:海洋之心
来源:21CTO
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。