MySQL MariaDB

MySQL Create Server

MySQL provides various storage engines for multiple use cases and applications. One of the supported storage engines in MySQL version 8.0 and above is the FEDERATED Storage Engine.

This type of engine allows you to access data remotely without the use of a replication or cluster implementation. To use the Federated Storage Engine in MySQL, you must create a server definition in the MySQL database.

In this tutorial, we will discuss how to use the CREATE SERVER statement in MySQL to create an entry in the servers table of the MySQL database.

Statement Syntax

The syntax for the server definition is as shown below:

CREATE SERVER server_name
FOREIGN DATA WRAPPER wrapper_name
OPTIONS (option [, option] ...)

option: {
HOST character-literal
| DATABASE character-literal
| USER character-literal
| PASSWORD character-literal
| SOCKET character-literal
| OWNER character-literal
| PORT numeric-literal
}

The server_name specifies the name of the server to create in the table. This should be a unique value that does not exist in the table. Ensure that the name follows the MySQL naming rules. As a convention, the server_name should be specified as a quoted string literal.

The wrapper_name allows you to set the identifier for the server.

Once you run the CREATE SERVER command, the query creates an entry in the mysql.servers table allowing you to use the server to create a FEDERATED table.

Practical Example

The following example shows how to use the CREATE SERVER command to create a server in the mysql.servers table.

create server fd_server
foreign data wrapper mysql
options (user 'root',
host '192.168.0.100',
database 'sakila');

The statement above should create an entry ‘fd_server’ in the servers table. We can confirm this by querying the table as shown:

select * from mysql.servers;

The query above should return the output as:

+-------------+---------------+--------+----------+----------+------+--------+---------+-------+
| Server_name | Host          | Db     | Username | Password | Port | Socket | Wrapper | Owner |
+-------------+---------------+--------+----------+----------+------+--------+---------+-------+
| fd_server   | 192.168.0.100 | sakila | root     |          |    0 |        | mysql   |       |
+-------------+---------------+--------+----------+----------+------+--------+---------+-------+
1 row in set (0.00 sec)

Finally, we can create a federated table using the created server as shown:

mysql> CREATE TABLE sample (col1 INT) ENGINE=FEDERATED CONNECTION='fd_server';

Conclusion

In this post, you learned how to use the CREATE SERVER command to create an entry in the mysql.server table allowing you to create FEDERATED tables.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list