14.7. The FEDERATED Storage Engine

MySQL 5.0

14.7. The FEDERATED Storage Engine

The storage engine is available beginning with MySQL 5.0.3. It is a storage engine that accesses data in tables of remote databases rather than in local tables.

The storage engine is included in MySQL-Max binary distributions. To enable this storage engine if you build MySQL from source, invoke configure with the option.

To examine the source for the engine, look in the directory of a source distribution for MySQL 5.0.3 or newer.

Additional resources

14.7.1. Description of the FEDERATED Storage Engine

When you create a table, the server creates a table format file in the database directory. The file begins with the table name and has an extension. No other files are created, because the actual data is in a remote table. This differs from the way that storage engines for local tables work.

For local database tables, data files are local. For example, if you create a table named , the handler creates a data file named . A handler for local tables reads, inserts, deletes, and updates data in local data files, and rows are stored in a format particular to the handler. To read rows, the handler must parse data into columns. To write rows, column values must be converted to the row format used by the handler and written to the local data file.

With the MySQL storage engine, there are no local data files for a table (for example, there is no file). Instead, a remote database stores the data that normally would be in the table. The local server connects to a remote server, and uses the MySQL client API to read, delete, update, and insert data in the remote table. Data retrieval is initiated via a SQL statement. To read the result, rows are fetched one at a time by using the C API function, and then converting the columns in the result set to the format that the handler expects.

The flow of information is as follows:

  1. SQL calls issued locally

  2. MySQL handler API (data in handler format)

  3. MySQL client API (data converted to SQL calls)

  4. Remote database -> MySQL client API

  5. Convert result sets (if any) to handler format

  6. Handler API -> Result rows or rows-affected count to local

14.7.2. How to use FEDERATED Tables

The procedure for using tables is very simple. Normally, you have two servers running, either both on the same host or on different hosts. (It is possible for a table to use another table that is managed by the same server, although there is little point in doing so.)

First, you must have a table on the remote server that you want to access by using a table. Suppose that the remote table is in the database and is defined like this:

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;

The example uses a table, but the table could use any storage engine.

Next, create a table on the local server for accessing the remote table:

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://root@remote_host:9306/federated/test_table';

(Before MySQL 5.0.13, use rather than .)

The structure of this table must be exactly the same as that of the remote table, except that the table option should be and the table option is a connection string that indicates to the engine how to connect to the remote server.

The engine creates only the file in the database.

The remote host information indicates the remote server to which your local server connects, and the database and table information indicates which remote table to use as the data source. In this example, the remote server is indicated to be running as on port 9306, so there must be a MySQL server running on the remote host and listening to port 9306.

The general form of the connection string in the option is as follows:

://[:]@[:]//

Only is supported as the value at this point; the password and port number are optional.

Here are some example connection strings:

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

The use of for specifying the connection string is non-optimal and is likely to change in future. Keep this in mind for applications that use tables. Such applications are likely to need modification if the format for specifying connection information changes.

Because any password given in the connection string is stored as plain text, it can be seen by any user who can use or for the table, or query the table in the database.

14.7.3. Limitations of the FEDERATED Storage Engine

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

  • In the first version, the remote server must be a MySQL server. Support by for other database engines may be added in the future.

  • The remote table that a table points to must exist before you try to access the table through the table.

  • It is possible for one table to point to another, but you must be careful not to create a loop.

  • There is no support for transactions.

  • There is no way for the engine to know if the remote table has changed. The reason for this is that this table must work like a data file that would never be written to by anything other than the database. The integrity of the data in the local table could be breached if there was any change to the remote database.

  • The storage engine supports , , , , and indexes. It does not support , or any Data Definition Language statements other than . The current implementation does not use Prepared statements.

  • Any statement issued against a FEDERATED table will only drop the local table, not the remote table.

  • The implementation uses , , , and , but not .

  • tables do not work with the query cache.

Some of these limitations may be lifted in future versions of the handler.