The FEDERATED MySQL storage engine is the coolest thing EVER! Seriously. It’s already saved me from having to do a whole bunch of synchronization coding. I can only imagine how it’ll come in useful in the future.
So, here’s my situation. I have two mysql servers sitting behind a firewall at “location 1”. People at “location 2” need to write some software to connect to both mysql servers at location 1. However, MyODBC gets confused when connecting to the same hostname on two different tcp ports, or so I’m told.
Anyway, since I was basically told that there’s no way to connect to two seperate mysql servers behind one firewall, I got to thinking. So, I set off searching google for method for mirroring data in MySQL and came across the FEDERATED storage engine.
Now, the servers at location 1 are on a VPN with the network at location 3, my location. So, my network (at location 3) can see the network at location 1 without the firewall getting in the way. Since that’s the case here, I can connect to the default mysql port, 3306, on both servers because I can see their LAN IP, where the people at location 2 can’t (no VPN).
So, we’ve got the network flow figured out, now we can go about getting the FEDERATED storage engine in MySQL working. First, you’ll need MySQL 5.x. I chose MySQL 5.0.24 as it’s the latest stable 5.x release.
To enable the FEDERATED storage engine in mysql 5, you must pass the –with-federated-storage-engine option when running configure. That’s pretty much all that’s required to start using the FEDERATED storage engine. Most linux distributions probably have a mysql 5 package that comes with the FEDERATED engine on already, although Slackware does not currently.
After you build mysql to support the FEDERATED storage engine, there’s really very little left to do to start making use of it. First you’ll need the table structure of the table you want to link to. We’ll use a table named “customers” as an example, with 3 fields, custId, fName, lName. The SQL used to build the initial “customers” table is below. The “customers” table is in the “companyData” database on the 192.168.1.248 MySQL server.
CREATE TABLE `customers` ( `custId` int(10) unsigned NOT NULL default '0', `fName` varchar(100) default NULL, `lName` varchar(100) default NULL, PRIMARY KEY (`custId`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
A pretty simple and very basic table setup. Now, the SQL above is the SQL that’s used to generate the original table, the one that is actually storing all the data. To create a FEDERATED table that links to the original customers table, we use basically the same SQL, except we need to change a few things on the last couple lines:
CREATE TABLE `customers` ( `custId` int(10) unsigned NOT NULL default '0', `fName` varchar(100) default NULL, `lName` varchar(100) default NULL, PRIMARY KEY (`custId`) ) ENGINE=FEDERATED DEFAULT CHARSET=latin1 CONNECTION='mysql://mysqluser:[email protected]:3306/companyData/customers';
See the last line there, the one starting with CONNECTION? That’s the real important one. It tells the new FEDERATED table where it needs to link to to get it’s data. The format of the CONNECTION clause is:
Pretty simple. You can read more about the FEDERATED storage engine in the MySQL reference manual. There’s also a support forum dedicated to the FEDERATED storage engine, as well as a nice article over at the MySQL developer zone.