Home » Debian, How-to, Linux, Mysql

MySQL Replication under Debian

19 February 2009 No Comment

Always when we talk about MySQL replication we talk about high-availability (HA). Why we choose Mysql Replication and not Mysql Clustering in a high-availability environment? Because Mysql Cluster have some limitations on his NDB storage system and probably we want to use INNODB (ACID compliant storage system) and foreign keys.
What is Replication ? Is a Database syncronization between two (or more) MySQLnodes.
I will start explain you in this step by step tutorial how to do a master slave configuration between 2 nodes.

Installing Mysql Server and Client on Debian
As root run on both nodes

apt-get install mysql-server-5.0 mysql-client-5.0

This will install you the MySQL 5.0 Server and Client.
Now is time to set up a DNS hostname for both machines. You can do that directly from your internal DNS or directly on your hosts. Open /etc/hosts and set your hostnames. In my case I choose rb-master and rb-slave as hostnames and my hosts files looks, on both servers, like: localhost rb-slave.randombugs.com rb-slave rb-master.randombugs.com rb-master

Setting up the master server

Open /etc/mysql/my.cnf and add/edit the following values:

bind-address =
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log

and look after skip-networking and if you have it enabled comment it please.
bind-address tells to mysql on what interface will listen for incoming connections. You also can use (your IP address) if you don’t need any other connections from other interfaces.
server-id it will help mysql to identify individual servers in a group

If you use INNODB as storage engine the you can add to INNODB section:


Now restart the mysql server

/etc/init.d/mysql restart

Add a replication user

We need this user for the slave server to get all the updates and other configurations from master.

$mysql -u root -p
Query OK, 0 rows affected (0.02 sec)

Setting up the slave server

Open /etc/mysql/my.cnf and add/edit the next values

bind-address =
server-id = 2

master-host = rb-master
master-user = rbuser
master-password = rbpassword

I hope you know what means all master-* configurations values and is not necessarly to explain them 🙂

after that just restart mysql server

/etc/init.d/mysql restart

Now start mysql client and try to load data from the master:

$mysql -u root -p
load data from master;

Now relog on rb-master and run

mysql> show master status;
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
| mysql-bin.000024 | 144591603 |              |                  |
1 row in set (0.00 sec)

Relog on rb-slave and run on mysql:

mysql> show slave status \G;
*************************** 1. row ***************************
                Master_Host: rb-master
                Master_User: rb-slave
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000024
        Read_Master_Log_Pos: 144591603
             Relay_Log_File: rb-slave relay-bin.000027
              Relay_Log_Pos: 631396708
      Relay_Master_Log_File: mysql-bin.000024
           Slave_IO_Running: No
          Slave_SQL_Running: No
                 Last_Errno: 0
               Skip_Counter: 0
        Exec_Master_Log_Pos: 144591603
            Relay_Log_Space: 0
            Until_Condition: None
              Until_Log_Pos: 0
         Master_SSL_Allowed: No
      Seconds_Behind_Master: NULL
1 row in set (0.00 sec

and you will need to have Position (from show master status) ~= Read_Master_Log_Pos (from show master slave).

And now your servers should be ready for sql scripts to be uploaded.

Good luck!

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.