In this blog post I will show you how to setup a replication from MySQL Cluster (ndbcluster) to a regular MySQL Server (InnoDB). If you want to understand the concepts, check out part 7 of our free MySQL Cluster training.
First of all we start with a MySQL Cluster looking like this, and what we want to do is to setup replication server to the Reporting Server (InnoDB slave).
Replication Layer Configuration
In the my.cnf of the replication server you should have the following:
[mysqld] ... #REPLICATION SPECIFIC - GENERAL #server-id must be unique across all mysql servers participating in replication. server-id=101 #REPLICATION SPECIFIC - MASTER log-bin=binlog binlog-do-db=user_db_1 binlog-do-db=user_db_2 binlog-do-db=mysql expire-logs-days=5 ...You may want to skip the binlog-do-db=.., if you want to replicate all databases, but, if you want to replicate a particular database, make sure you also replicate the mysql database in order to get some very important data on the slave.
Restart the replication server for the settings to have effect.
Grant access to the Slave:
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'ip/hostname of mysqld m' IDENTIFIED BY 'repl';
InnoDB Slave Configuration
The first requirement on the InnoDb slave is that it must use the mysqld binary that comes from the MySQL Cluster package. If you already have a MySQL 5.5 installed that is not clustered, you need to upgrade it to the Cluster version of it. E.g, by doing:
sudo rpm -Uvh MySQL-Cluster-server-gpl-7.2.7-1.el6.x86_64.rpm sudo rpm -Uvh MySQL-Cluster-client-gpl-7.2.7-1.el6.x86_64.rpm
The InnoDB slave should have the following:
[mysqld] ... binlog-format=mixed log-bin=binlog log-slave-updates=1 slave-exec-mode=IDEMPOTENT expire-logs-days=5 ...
If you want the InnoDb to further replicate to a set of slaves, then you should set log-slave-updates=1 otherwise you can set it to 0 (log-slave-updates=0). Thatt is all, restart the slave.
You must also create the following table on the Innodb Slave:
CREATE TABLE `ndb_apply_status` ( `server_id` int(10) unsigned NOT NULL, `epoch` bigint(20) unsigned NOT NULL, `log_name` varchar(255) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL, `start_pos` bigint(20) unsigned NOT NULL, `end_pos` bigint(20) unsigned NOT NULL, PRIMARY KEY (`server_id`) USING HASH) ENGINE=INNODB DEFAULT CHARSET=latin1;
Then do CHANGE MASTER:
CHANGE MASTER TO MASTER_HOST='ip/hostname of the replication server', MASTER_USER='repl', MASTER_PASSWORD='repl';
Now you need to stage the InnoDB slave with data. What you need to do is to disable traffic to NDBCLUSTER in order to get a consistent snapshot of the data. As there are no clusterwide table locks in NDBCLUSTER you have two options:
- Block the Loadbalancer from sending any transactions to MySQL Cluster
- Make all SQL nodes READ ONLY, by locking all tables on ALL MySQL servers (if you use NDBAPI applications, then option 1) or shutting down the applications is the the only option): FLUSH TABLES WITH READ LOCK;
FLUSH TABLES WITH READ LOCK;
mysql> show master status; +---------------+-----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------+-----------+--------------+------------------+ | binlog.000008 | 859092065 | | | +---------------+-----------+--------------+------------------+ 1 row in set (0.00 sec)
Run the SHOW MASTER STATUS; a couple of times until you see the Position not changing any more
mysql> RESET MASTER;
- one dump with the schema
- another dump with the data
mysqldump --no-data --routines --trigggers > schema.sql
mysqldump --no-create-info --master-data=1 > data.sql
Of course you can dump out only the databases you are interested in.
Point is that you can enable traffic to NDBCLUSTER again.
Now, change the ENGINE=ndbcluster to ENGINE=innodb in schema.sql:
sed -i.bak 's#ndbcluster#innodb#g' schema.sql
Finally you can start replication, on the InnoDB slave you can now do:
SHOW SLAVE STATUS \G
And hopefully all will be fine :)