Sunday, 29 January 2012

Web site replication

Let's imagine you have a web site based on PHP and MySQL and you need to make a mirror of it, which always be synchronized. Files and folders synchronization is not an issue, rsync over ssh is a solution:
rsync --delete -avr --links --rsh="/usr/bin/ssh -l mirror -i /home/user/mirror.key" 192.168.47.11:/var/www/site /var/www
But what about MySQL? We can use embedded replication feature.
It's quite easy. First edit my.cnf of your master server and add the following:
log-bin = /var/log/mysql/mysql-bin.log
binlog-do-db=database
server-id=1
replicate-do-db = database

Then restart the server, connect to it with MySQL client and enter the following:
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;


Write down the position and file, you will need it later.
Do not exit mysql to keep the tables locked. Via a new session make a dump of the base:
mysqldump --lock-all-tables database > dbdump.db
Then unlock tables: 
UNLOCK TABLES;

Create a user for replication:
CREATE USER slave@slave_ip_address IDENTIFIED BY 'slavepass';
GRANT REPLICATION SLAVE ON database.* TO slave@slave_ip_address;

Transfer dump to slave server.
Edit my.cnf on slave server:
server-id = 2
Login to mysql, enter the following:
CHANGE MASTER TO MASTER_HOST = "master_ip_address", MASTER_USER = "slave", MASTER_PASSWORD = "slavepass", MASTER_LOG_FILE = "mysql-bin.000003 ", MASTER_LOG_POS = 934325;

Of course, change file and log position to those values you have written down.
Then activate replication:
START SLAVE;

To see status of replication:
SHOW SLAVE STATUS;

P.S This is a very short description of the process. Any details are available on official MySQL site.

After configuration is over DO NOT enter the site copy via browser. If you do this, usually site engine make some changes into a slave database and MySQL replication will be destroyed.

Ping does not work

Today I would like to discuss a banal situation: host A is directly connected to host B, ping from host A to host B does not work. What are...