Friday, May 18, 2012

How to create distributed/mirrored dynamic scripting website (Step - 2 Database synchronization)

Step two: Database Syncrhonization
This is the key of the dynamic website synchronization, I will use mysql server 5.x for example, if you are using a different database server, you will need to find out how to configure their replication.

1. Configure Master & Slave Server
Create a mysql user for replication

GRANT REPLICATION SLAVE ON *.* to 'user_rep'@'SLAVE_IP_ADDRESS' IDENTIFIED BY 'password'; 

Edit my.cnf file [mysqld] section
server-id=10
log-bin=mysql-bin
innodb_flush_logs_at_trx_commit=1
innodb_support_xa=1
sync_binlog=1

2. Copy the database:
We need to lock table on master server before we dump the database

FLUSH TABLE WITH READ LOCK;SHOW MASTER STATUS;

Write down the bin log file record & the postion & leave this connection open, then create a new ssh connection to the master server & dump the databse with:

#mysqldump -u root -p --database | gzip -c > all_database.sql.gz

Copy all_database.sql.gz to slave server & extract the files & import into slave database

source all_database.sql

An alternative way is to copy the content of folder /var/lib/mysql/data/* from master to slave.

Now you received exactly the same database on the salve server, then run SQL command on slave server

CHANGE MASTER TO master_host='MASTER_IP', master_user='user_rep', master_password='password',master_log_file='mysql-bin.XXXXXX',master_log_pos=XXXXX;START SLAVE;SHOW SLAVE STATUS; 

If you see Thread IO & Thread SQL are both running, then your database should be synchronized
Now go back to master server mysql session & run

UNLOCK TABLE;

Next: How to create distributed/mirrored dynamic scripting website (Step – 3 Security)

No comments:

Post a Comment