Tuesday, March 3, 2009

MySQL Master-Slave Server Configuration

How To Set Up MySQL Database Replication On Fedora 10

This tutorial describes how to set up database replication in MySQL. MySQL replication allows you to have an exact copy of a database from a master server on another server (slave), and all updates to the database on the master server are immediately replicated to the database on the slave server so that both databases are in sync. This is not a backup policy because an accidentally issued DELETE command will also be carried out on the slave; but replication can help protect against hardware failures though. I will use Fedora 10 for the master and slave in this tutorial.

I want to say first that this is not the only way of setting up such a system. There are many ways of achieving this goal but this is the way I take. I do not issue any guarantee that this will work for you!

 

1 Preliminary Note

I'm using two Fedora 10 servers in this tutorial:

  • server1.example.com (IP 192.168.0.100): master
  • server2.example.com (IP 192.168.0.101): slave

In this tutorial I will show how to replicate the database exampledb from server1.example.com (master) toserver2.example.com (slave).

I'm assuming that MySQL is already installed (e.g. as shown in chapter 10 on http://www.howtoforge.com/perfect-server-fedora-10-p4) and working on both servers. The database exampledb with tables and data is already existing on the master, but not on the slave.

 

2 Configure The Master

server1:

First we create a log directory for the MySQL bin-logs:

mkdir /var/log/mysql
chown mysql:mysql /var/log/mysql

Then we edit /etc/my.cnf; we have to tell MySQL for which database it should write logs (these logs are used by the slave to see what has changed on the master), which log file it should use, and we have to specify that this MySQL server is the master. We want to replicate the database exampledb, so we put the following lines into /etc/my.cnf (in the [mysqld] section!):

vi /etc/my.cnf

[mysqld] [...] log-bin = /var/log/mysql/mysql-bin.log binlog-do-db=exampledb server-id=1 [...]

Then we restart MySQL:

/etc/init.d/mysqld restart

Then we log into the MySQL database as root and create a user with replication privileges:

mysql -u root -p

Enter password:

Now we are on the MySQL shell.

STOP SLAVE;
GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'slave_password';
FLUSH PRIVILEGES;

Next (still on the MySQL shell) do this:

USE exampledb;
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

The last command should show something like this (please write it down, we'll need it later on):

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |       98 | exampledb    |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql>

Now don't leave the MySQL shell, because if you leave it, the database lock will be removed, and this is not what we want right now because we must create a database dump now. While the MySQL shell is still open, we open a secondcommand line window where we create the SQL dump snapshot.sql and transfer it to server2 (using scp):

cd /tmp
mysqldump -u root -pyourrootsqlpassword --opt exampledb > snapshot.sql
scp snapshot.sql root@192.168.0.101:/tmp

Afterwards, you can close the second command line window. On the first command line window, we can now unlock the database and leave the MySQL shell:

UNLOCK TABLES;
quit;

 

3 Configure The Slave

server2:

Now we have to tell MySQL on the slave that it is the slave, that the master is 192.168.0.100, and that the master database to watch is exampledb. Therefore we add the following lines to the [mysqld] section in /etc/my.cnf:

vi /etc/my.cnf

[mysqld] [...] server-id=2 master-host=192.168.0.100 master-user=slave_user master-password=slave_password master-connect-retry=60 replicate-do-db=exampledb [...]

Then we restart MySQL:

/etc/init.d/mysqld restart

Now we create the empty database exampledb on the slave (make sure you run STOP SLAVE; to stop all slave processes if there are any!):

mysql -u root -p

Enter password:

STOP SLAVE;
CREATE DATABASE exampledb;
quit;

We can now import the SQL dump snapshot.sql as follows:

cd /tmp
mysql -u root -pyourrootsqlpassword exampledb <>

Then we connect to MySQL again...

mysql -u root -p

Enter password:

... and run the following command to make server2 a slave of server1 (it is important that you replace the values in the following command with the values you got from the SHOW MASTER STATUS; command that we ran on server1!):

CHANGE MASTER TO MASTER_HOST='192.168.0.100', MASTER_USER='slave_user', MASTER_PASSWORD='slave_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=98;

Finally start the slave:

START SLAVE;

Then check the slave status:

SHOW SLAVE STATUS\G

It is important that both Slave_IO_Running and Slave_SQL_Running have the value Yes in the output (otherwise something went wrong, and you should check your setup again and take a look at /var/log/mysqld.log to find out about any errors):

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.0.100
                Master_User: slave_user
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 98
             Relay_Log_File: mysqld-relay-bin.000002
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB: exampledb
        Replicate_Ignore_DB:
         Replicate_Do_Table:
     Replicate_Ignore_Table:
    Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
                 Last_Errno: 0
                 Last_Error:
               Skip_Counter: 0
        Exec_Master_Log_Pos: 98
            Relay_Log_Space: 235
            Until_Condition: None
             Until_Log_File:
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
         Master_SSL_CA_File:
         Master_SSL_CA_Path:
            Master_SSL_Cert:
          Master_SSL_Cipher:
             Master_SSL_Key:
      Seconds_Behind_Master: 0
1 row in set (0.00 sec)

mysql>

Afterwards, you can leave the MySQL shell on server2:

quit;

IPTABLES -- Block traffic by country name

#Here is a sample script file that is  used to block traffic from Afganistan and China
#!/bin/bash
### Block all traffic from AFGHANISTAN (af) and CHINA (CN). Use ISO code ###
ISO="af cn"

### Set PATH ###
IPT=/sbin/iptables
WGET=/usr/bin/ wget
EGREP=/bin/egrep

### No editing below ###
SPAMLIST="countrydr op"
ZONEROOT="/root/ iptables"
DLROOT="http://www.ipdeny. com/ipblocks/ data/countries"

cleanOldRules( ){
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
}

# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT

# clean old rules
cleanOldRules

# create a new iptables list
$IPT -N $SPAMLIST

for c in $ISO
do
# local zone file
tDB=$ZONEROOT/ $c.zone

# get fresh zone file
$WGET -O $tDB $DLROOT/$c.zone

# country specific log message
SPAMDROPMSG= "$c Country Drop"

# get
BADIPS=$(egrep -v "^#|^$" $tDB)
for ipblock in $BADIPS
do
$IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"
$IPT -A $SPAMLIST -s $ipblock -j DROP
done
done

# Drop everything
$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST

# call your other iptable script
# /path/to/other/ iptables. sh

exit 0

Step-by-Step configurationo f DNS in RHEL 5.0

check your resolve.conf file 
check your hosts file 
after changing this file to restart your network service 

1. install bind all package
2. install caching-nameserver
3. cd /var/named/chroot/etc
4. cp named.caching-nameserver.conf named.conf
5. vim named.conf
after open named.conf file change listen port
allow quary
forwarders 
forward only
6. append named.rfc1912.zones to named.conf

command is 

7. cat named.rfc1912.zones >> named.conf
8. after this file append open named.conf file
9. creat forward zone and reverce zone

//Forward Lookup Zone
zone "example.com" {
      type master;
      file "example.com.zone";
};


##############################################################
//Reverse Lookup Zone
zone "0.168.192.in-addr.arpa" {
      type master;
      file "192.168.0.zone";
};

10. after this step plaese see your iptables 
 
if iptables is show rules so flus this iptables or add rules allow 53 port

10. now create master dns file , forward and reverce
11. cd /var/named/chroot/var/named/
12. copy localdomain.zone file to example.com.zone file  command is
13. cp localdomain.zone example.com.zone
14. after copy this file edit this file as bellow 

$TTL    86400
example.com.            IN SOA  station19.example.com. root.station19.example.com. (
                                        42              ; serial (d. adams)
                                        3H              ; refresh
                                        15M             ; retry
                                        1W              ; expiry
                                        1D )            ; minimum
                IN NS           station19.example.com.
station9.example.com.  IN A            192.168.0.9


15. after this file edit user for command 
16. for i in {1..254}; do echo "station$i.example.com.   IN  A  192.168.0.$i"; done and see this command is ok  now this command append to example.com.zone file , command is 

17. for i in {1..254}; do echo "station$i.example.com.   IN  A  192.168.0.$i"; done >> example.com.zone

18. copy named.ca to 192.168.0.zone file the command is 

19. cp named.ca 192.168.0.zone after this command open 192.168.0.zone file and edit as bellow

20.

$TTL    86400
0.168.192.IN-ADDR.ARPA.       IN      SOA     station19.example.com. root.station19.example.com.  (
                                      1997022700 ; Serial
                                      28800      ; Refresh
                                      14400      ; Retry
                                      3600000    ; Expire
                                      86400 )    ; Minimum
        IN      NS      station19.example.com.
1       IN      PTR     station19.example.com.


after this edit use for command the command is 


21. for i in {1..254}; do echo "$i IN PTR station$i.example.com."; done 
22. afer this command is work is ok then appen this command to 192.168.0.zone file 
23. for i in {1..254}; do echo "$i IN PTR station$i.example.com."; done >> 192.168.0.zone
24. now your named service restart 
25. dig station19.example.com to see forward zone 
26. dig -x 192.168.0.19 to see reverce zone 

if dig command is work then your DNS configration is Ok


Thanks,
Rambilas Varma