Thursday, October 9, 2008

mysql replication

While working in application deployment you might have come across the term called "failover database". That means if any hardware problem in the DB server we can't access the database at all. So we have one more DB server to manage the application. When DB1 have some problem we can manage using DB2.

So to achieve this what i thought was have to take mysqldump and import to second server periodically. But there is the better facility in mysql 5.x version called "mysql replication". But mysql 5.1.x is stable for mysql replication. What mysql replication doing is simply coping the mysql-bin log from source DB server to detination DB server. whenever we do any transaction that will be logged in mysql-bin log. So coping mysql-bin log from one machine to another is the soluction for failover database problem.

For mysql replication just we need to change some configuration in my.cnf file.

you can find this file using the following command

find . | grep my.cnf

i) Edit the my.cnf file in server1[uncomment the line server-id = 1 by default commented]
my.cnf
======
server-id = 1

ii) Edit the my.cnf file in server2 [Uncomment the following entries and give proper values]
my.cnf
==========
server-id = 2

master-host = 192.168.2.51
master-user = repl
master-password = password
log-bin = mysql-bin

explanation:

master-host : DB server1 IP address.
master-user : this user should have all privileges. While create this user use the follwoing command to grant permission.
GRANT ALL PRIVILEGES ON *.* TO 'user'@'host'
IDENTIFIED BY 'some_pass';
master-password: password of the master-user

iii) Restart the mysql server using the following command

/etc/init.d/mysql restart

Whenever we do data manipulation both server will have same data and in sync. Now failover database is ready. This is the way to do mysql replication.


Thursday, October 2, 2008

Synchronize directories/files between servers

I came across the scenario where, we have to synchronize files and directories between different servers. For example[Refer image1] my application update images in server1 only. But the same image shoule be synchronized in server2 also.







There is tool 'Rsync' for linux to acheive this scenario.
Install Rsync in two servers. We are going to sync server1 with server2. So here destination server is server2.


In server2 add the following configration files.

i) Create rsync.conf file in /etc/rsync.conf
Add the following code given in between two lines in this file.
#############################
motd file = /etc/rsyncd.motd
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock

[thumbnails]
path = /var/www/html/images
comment = Image Server
uid = apache
gid = apache
read only = no
list = yes

#############################
Explanation:
First four lines are log,pid,lock and motd files path

[thumbnails] is the label. This will refered in server1 to send the directories to server2.
path is the place where the directories and files will get stored.

ii) Create rsyncd.motd file in /ect/rsyncd.motd

Add some text. This will shown at the time of sync.
For expample add text like in between two lines.
#############################
File transfering.........
#############################

iii) By default rsync is disabled. So we have to enable Rsync. You can edit the file /etc/xinetd.d/rsync and chage the line as disable = no [by default disable = yes]

iv) Start the rsync server
/etc/init.d/xinetd start
Server2 [Destination server] configuration over.


In server1, only one line command will sync the file or directories to server2.

rsync -rptgou /absolute path/images 192.168.100.50::thumbnails

Explanation:
rsync ==> is the command
-rptgou ===> is the options
/absolute path/images ===> images is the directory we are going to transfer to server2
192.168.100.50::thumbnails ===> this is the server2 ip and thumbnails is the lable we have added in "rsync.conf" configuration file in server2

We can add the above command in crontab so that two servers in the sync.
Enjoy........