15.8 The FEDERATED Storage Engine

The 'FEDERATED' storage engine lets you access data from a remote MySQL database without using replication or cluster technology. Querying a local 'FEDERATED' table automatically pulls the data from the remote (federated) tables. No data is stored on the local tables.

To include the 'FEDERATED' storage engine if you build MySQL from source, invoke 'CMake' with the '-DWITH_FEDERATED_STORAGE_ENGINE' option.

The 'FEDERATED' storage engine is not enabled by default in the running server; to enable 'FEDERATED', you must start the MySQL server binary using the '--federated' option.

To examine the source for the 'FEDERATED' engine, look in the 'storage/federated' directory of a MySQL source distribution.

 File: manual.info.tmp, Node: federated-description, Next: federated-create, Prev: federated-storage-engine, Up: federated-storage-engine

15.8.1 FEDERATED Storage Engine Overview

When you create a table using one of the standard storage engines (such as 'MyISAM', 'CSV' or 'InnoDB'), the table consists of the table definition and the associated data. When you create a 'FEDERATED' table, the table definition is the same, but the physical storage of the data is handled on a remote server.

A 'FEDERATED' table consists of two elements:

When executing queries and statements on a 'FEDERATED' table on the local server, the operations that would normally insert, update or delete information from a local data file are instead sent to the remote server for execution, where they update the data file on the remote server or return matching rows from the remote server.

The basic structure of a 'FEDERATED' table setup is shown in *note figure-se-federated-structure::.

FIGURE GOES HERE: FEDERATED Table Structure

When a client issues an SQL statement that refers to a 'FEDERATED' table, the flow of information between the local server (where the SQL statement is executed) and the remote server (where the data is physically stored) is as follows:

  1. The storage engine looks through each column that the 'FEDERATED' table has and constructs an appropriate SQL statement that refers to the remote table.

  2. The statement is sent to the remote server using the MySQL client API.

  3. The remote server processes the statement and the local server retrieves any result that the statement produces (an affected-rows count or a result set).

  4. If the statement produces a result set, each column is converted to internal storage engine format that the 'FEDERATED' engine expects and can use to display the result to the client that issued the original statement.

The local server communicates with the remote server using MySQL client C API functions. It invokes 'mysql_real_query()' (https://dev.mysql.com/doc/c-api/5.7/en/mysql-real-query.html) to send the statement. To read a result set, it uses 'mysql_store_result()' (https://dev.mysql.com/doc/c-api/5.7/en/mysql-store-result.html) and fetches rows one at a time using 'mysql_fetch_row()' (https://dev.mysql.com/doc/c-api/5.7/en/mysql-fetch-row.html).

 File: manual.info.tmp, Node: federated-create, Next: federated-usagenotes, Prev: federated-description, Up: federated-storage-engine

15.8.2 How to Create FEDERATED Tables

To create a 'FEDERATED' table you should follow these steps:

  1. Create the table on the remote server. Alternatively, make a note of the table definition of an existing table, perhaps using the *note 'SHOW CREATE TABLE': show-create-table. statement.

  2. Create the table on the local server with an identical table definition, but adding the connection information that links the local table to the remote table.

For example, you could create the following table on the remote server:

 CREATE TABLE test_table (
     id     INT(20) NOT NULL AUTO_INCREMENT,
     name   VARCHAR(32) NOT NULL DEFAULT '',
     other  INT(20) NOT NULL DEFAULT '0',
     PRIMARY KEY  (id),
     INDEX name (name),
     INDEX other_key (other)
 )
 ENGINE=MyISAM
 DEFAULT CHARSET=latin1;

For creating the local table to be federated to the remote table, there are two options available. You can either create the local table and specify the connection string (containing the server name, login, password) to be used to connect to the remote table using the 'CONNECTION', or you can use an existing connection that you have previously created using the *note 'CREATE SERVER': create-server. statement.

Important:

When you create the local table it must have an identical field definition to the remote table.

Note:

You can improve the performance of a 'FEDERATED' table by adding indexes to the table on the host. The optimization occurs because the query sent to the remote server includes the contents of the 'WHERE' clause, and is sent to the remote server and subsequently executed locally. This reduces the network traffic that would otherwise request the entire table from the server for local processing.

 File: manual.info.tmp, Node: federated-create-connection, Next: federated-create-server, Prev: federated-create, Up: federated-create

15.8.2.1 Creating a FEDERATED Table Using CONNECTION ....................................................

To use the first method, you must specify the 'CONNECTION' string after the engine type in a *note 'CREATE TABLE': create-table. statement. For example:

 CREATE TABLE federated_table (
     id     INT(20) NOT NULL AUTO_INCREMENT,
     name   VARCHAR(32) NOT NULL DEFAULT '',
     other  INT(20) NOT NULL DEFAULT '0',
     PRIMARY KEY  (id),
     INDEX name (name),
     INDEX other_key (other)
 )
 ENGINE=FEDERATED
 DEFAULT CHARSET=latin1
 CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

Note:

'CONNECTION' replaces the 'COMMENT' used in some previous versions of MySQL.

The 'CONNECTION' string contains the information required to connect to the remote server containing the table used for physical storage of the data. The connection string specifies the server name, login credentials, port number and database/table information. In the example, the remote table is on the server 'remote_host', using port 9306. The name and port number should match the host name (or IP address) and port number of the remote MySQL server instance you want to use as your remote table.

The format of the connection string is as follows:

 SCHEME://USER_NAME[:PASSWORD]@HOST_NAME[:PORT_NUM]/DB_NAME/TBL_NAME

Where:

Sample connection strings:

 CONNECTION='mysql://username:password@hostname:port/database/tablename'
 CONNECTION='mysql://username@hostname/database/tablename'
 CONNECTION='mysql://username:password@hostname/database/tablename'

 File: manual.info.tmp, Node: federated-create-server, Prev: federated-create-connection, Up: federated-create

15.8.2.2 Creating a FEDERATED Table Using CREATE SERVER .......................................................

If you are creating a number of 'FEDERATED' tables on the same server, or if you want to simplify the process of creating 'FEDERATED' tables, you can use the *note 'CREATE SERVER': create-server. statement to define the server connection parameters, just as you would with the 'CONNECTION' string.

The format of the *note 'CREATE SERVER': create-server. statement is:

 CREATE SERVER
 SERVER_NAME
 FOREIGN DATA WRAPPER WRAPPER_NAME
 OPTIONS (OPTION [, OPTION] ...)

The SERVER_NAME is used in the connection string when creating a new 'FEDERATED' table.

For example, to create a server connection identical to the 'CONNECTION' string:

 CONNECTION='mysql://fed_user@remote_host:9306/federated/test_table';

You would use the following statement:

 CREATE SERVER fedlink
 FOREIGN DATA WRAPPER mysql
 OPTIONS (USER 'fed_user', HOST 'remote_host', PORT 9306, DATABASE 'federated');

To create a 'FEDERATED' table that uses this connection, you still use the 'CONNECTION' keyword, but specify the name you used in the *note 'CREATE SERVER': create-server. statement.

 CREATE TABLE test_table (
     id     INT(20) NOT NULL AUTO_INCREMENT,
     name   VARCHAR(32) NOT NULL DEFAULT '',
     other  INT(20) NOT NULL DEFAULT '0',
     PRIMARY KEY  (id),
     INDEX name (name),
     INDEX other_key (other)
 )
 ENGINE=FEDERATED
 DEFAULT CHARSET=latin1
 CONNECTION='fedlink/test_table';

The connection name in this example contains the name of the connection ('fedlink') and the name of the table ('test_table') to link to, separated by a slash. If you specify only the connection name without a table name, the table name of the local table is used instead.

For more information on note 'CREATE SERVER': create-server, see note create-server::.

The note 'CREATE SERVER': create-server. statement accepts the same arguments as the 'CONNECTION' string. The note 'CREATE SERVER': create-server. statement updates the rows in the 'mysql.servers' table. See the following table for information on the correspondence between parameters in a connection string, options in the *note 'CREATE SERVER': create-server. statement, and the columns in the 'mysql.servers' table. For reference, the format of the 'CONNECTION' string is as follows:

 SCHEME://USER_NAME[:PASSWORD]@HOST_NAME[:PORT_NUM]/DB_NAME/TBL_NAME

Description 'CONNECTION' *note 'CREATE SERVER': create-server.'mysql.servers' string option column

Connection scheme

SCHEME 'wrapper_name' 'Wrapper'

Remote user

USER_NAME 'USER' 'Username'

Remote password

PASSWORD 'PASSWORD' 'Password'

Remote host

HOST_NAME 'HOST' 'Host'

Remote port

PORT_NUM 'PORT' 'Port'

Remote database

DB_NAME 'DATABASE' 'Db'

 File: manual.info.tmp, Node: federated-usagenotes, Next: federated-storage-engine-resources, Prev: federated-create, Up: federated-storage-engine

15.8.3 FEDERATED Storage Engine Notes and Tips

You should be aware of the following points when using the 'FEDERATED' storage engine:

The following items indicate features that the 'FEDERATED' storage engine does and does not support:

 File: manual.info.tmp, Node: federated-storage-engine-resources, Prev: federated-usagenotes, Up: federated-storage-engine

15.8.4 FEDERATED Storage Engine Resources

The following additional resources are available for the 'FEDERATED' storage engine:

 File: manual.info.tmp, Node: example-storage-engine, Next: storage-engines-other, Prev: federated-storage-engine, Up: storage-engines