We have a RAIC (Redundant Array of Inexpensive Computers) that control our entire conveyor system through out the manufacturing plant. This conveyor system is responsible for staging product and placing it behind machine when a particular order has been scheduled to sending final product all the way down to the truck docks. If this system were to go offline it makes getting product through a much more labor intensive process. So we have this system that keeps track of all order, where they are on the conveyors, when they are scheduled etc. The base of this system runs off of MySQL on Linux servers. If we loose this database everything is lost and we would have to hand enter every product and where it is staged on the line. Not something we want to do.
So I did a little reading and setup MySQL replication with a Master-Slave relationship. Really quite an easy process and extremely quick in getting the data out to the slave. Below are the steps I used to complete this task. This was done on RedHat Enterprise 4.4 and MySQL 4.1.20.
Setup The Master
On the master server edit the /etc/my.cnf file. In the [mysqld] section add the following lines:
# database replication
log-bin
binlog-do-db=database-name
server-id=1
Restart MySQL.
/etc/init.d/mysql restart
Log into the MySQL server /etc/init.d/mysql restart and enter the following query:
GRANT REPLICATION SLAVE ON *.* TO ‘replication-user-account‘@’%’ IDENTIFIED BY ‘password‘;
FLUSH PRIVILEGES;
USE database-name;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;
Make a note of the file and position from the query results you will need this later.
QUIT;
Make a dump of the database to copy to the slave machine.
mysqldump -u root -p –opt database-name > /tmp/database-name.sql
Once you have the dump log back into the MySQL server and issue the following commands;
UNLOCK TABLES;
QUIT;
Copy the database dump to the slave machine:
scp /tmp/database-name.sql root@slave.domain.com:/tmp
Setup The Slave
Log into the slave server.
Log into the MySQL server:
mysql -u root -p
Create a new database:
CREATE DATABASE database-name;
QUIT;
Copy sql dump into the database:
mysql -u root -p database-name < /tmp/database-name.sql
On the slave server edit the /etc/my.cnf file. In the [mysqld] section add the following lines:
# database replication
server-id=2
master-host=master.domain.com
master-user=replication-user-account
master-password=password
master-connect-retry=60
replicate-do-db=database-name
Restart the MySQL service.
/etc/init.d/mysqld restart
Log into the MySQL server and issue the following commands:
SLAVE STOP;
CHANGE MASTER TO MASTER_HOST=‘master.domain.com’, MASTER_USER=‘replication-user-account’, MASTER_PASSWORD=‘password’, MASTER_LOG_FILE=’server-bin.000001′, MASTER_LOG_POS=101091;
Be sure to replace MASTER_LOG_FILE and MASTER_LOG_POS with the values you noted when setting up the master server
SLAVE START;
QUIT;