I've provided a link few weeks ago to a great complete guide for MySQL replication. Yet, since then I got many requests to provide a short and east guide for a new MySQL replication setup.
Well, Fasten Your Seat-belts
- Install a MySQL instance that will serve as a Master
- Install a MySQL instance that will serve as a Slave
- Configure the Master my.cnf file (located at /etc/ at CentOS) with the server id and the log file name:
- [mysqld]
- server-id = 1
- log-bin = master-bin.log
- Configure the Slave my.cnf with the server id, reply logs and key configuration databases;
- server-id = 2
- relay-log-index = slave-relay-bin.index
- relay-log = slave-relay-bin
- replicate-wild-ignore-table=mysql.%
- replicate-wild-ignore-table=information_schema.%
- replicate-wild-ignore-table=performance_schema.%
- For MySQL 5.6: Delete the auto.cnf from your MySQL directory slave> rm -rf /var/lib/mysql/auto.cnf
- Restart the MySQL daemons on both servers to apply the my.cnf changes.
- If both servers were just installed there is no need to sync their data files (since they should have the same data files). O/w you should either stop the slave and master and copy the data files using SCP or perform just perform a mysqldump. Notice! Before copying the files, get the Master location describe below.
- Get the master location, so we can sync the slave to it:
- master> FLUSH TABLES WITH READ LOCK;
- master> SHOW MASTER STATUS;
- +-------------------+----------+--------------+------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
- +-------------------+----------+--------------+------------------+
- | master-bin.000013 | 1233453 | | |
- +-------------------+----------+--------------+------------------+
- master> UNLOCK TABLES;
- Provide the slave with replication permissions:
- master> GRANT REPLICATION SLAVE ON *.* to `repl`@`%` IDENTIFIED BY 'slavepass';
- master> FLUSH PRIVILEGES;
- Setup the slave using the chosen user/password and the master location we found before:
- slave> CHANGE MASTER TO
- -> MASTER_HOST='10.17.16.253',
- -> MASTER_PORT=3306,
- -> MASTER_USER='repl',
- -> MASTER_PASSWORD='slavepass',
- -> MASTER_LOG_FILE='master-bin.000013',
- -> MASTER_LOG_POS=1233453;
- Now, start the slave and verify it's running:
- slave> start slave;show slave status\G
- If everything is Okay, verify the replication really works by:
- Creating a table and inserting values to it in the master:
- master> CREATE DATABASE a;
- master> CREATE TABLE a.b (c int);
- master> INSERT INTO a.b (c) VALUES (1);
- Verifying that SLAVE/MASTER> SELECT * FROM a.b; return the same values in the master and slave;
- Dropping the database in the master: master> DROP DATABASE IF EXISTS a;
P.S If you are interested in implementing the replication using SSL, follow these instructions.
Bottom Line
Only few minutes and you got a running MySQL replication. Even these kind of tasks can be so simple and smooth... and leaving you time to drink a cup of coffee!
Keep Performing,