Skip to content

mysql replication

2012.02.26 19:57

WHRIA 조회 수:12359

http://blog.evandavey.com/2008/05/how-to-setup-mysql-replication-a-simple-guide.html

 

The following is a simple guide to help you setup Mysql replication – a fantastic way to ensure you have an up-to-date backup of your production databases.

This guide only describes a single master-slave relationship, however you can use mysql in various modes which include additional slaves as required.

Production Replication Setup

The master (production server) needs to have binary logging turned on to facilitate replication. To turn on binary logging, we need to take the following steps:

  • Using phpMyAdmin, add a new user called replication_user which only has REPLICATION SLAVE access rights
  • Add the following lines to the /etc/my.cnf file:
[mysqld]
log-bin=mysql-bin
server-id=1
  • Restart mysql (as root)
[user@server]# service mysqld restart
  • Stop the production server, and obtain its state information. Most importantly, note the File and Position values:
mysql> FLUSH TABLES WITH READ LOCK;
mysql> SHOW MASTER STATUS;

+---------------+----------+--------------+------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------+----------+--------------+------------------+
| mysql-bin.003 | 240      | test         | manual,mysql     |
+---------------+----------+--------------+------------------+
  • While the database has the READ LOCK applied, export the data using mysqldump, including master data information:
mysqldump --all-databases --master-data > dbdump.db
  • Then unlock the master database:
mysql> UNLOCK TABLES;

Slave Replication Setup

To initialise the slave server, the following steps need to be taken:

  • Add the following to the slave servers config file /etc/my.cnf:
# Replication settings
log-bin
replicate-ignore-db=mysql
relay-log=triton-relay-bin
log-slave-updates
log-warnings
server-id=2
# Slave details
report-host=slave.mydomain.com  # YOUR SLAVE'S HOSTNAME

# Master Host details
master-host=master.mydomain.com  # YOUR MASTER'S HOSTNAME
master-user=replication_user
master-password=mypasswordhere   # YOUR REPLICATION USERS PASSWORD
master-port=3306
  • Transfer the data (dbdump.db) file from Master and load it into the database:
shell> mysql < fulldb.dump
  • Set the file information – this needs to match the values you observed on the master when you locked the tables above. In this example the filename was mysql-bin.003 and the log position was 240:
mysql> change master to
MASTER_LOG_FILE='mysql-bin.003',
MASTER_LOG_POS=240;
  • Start the slave:
mysql> start slave;

You should now have a working mysql replication database! Login to phpMyAdmin or something similar on the slave to confirm that all the databases are there.

Checking Replication is Working

It’s important to check that replication is working once you set it up. Some information can be gathered from the mysql log (usually found in /var/log/mysqld.log), however the following steps are more conslusive:

  • On the production server, issue the following command to see which slaves are connected:
mysql> show slave hosts;
+-----------+------------------------------+------+-------------------+-----------+
| Server_id | Host                         | Port | Rpl_recovery_rank | Master_id |
+-----------+------------------------------+------+-------------------+-----------+
|         2 | slave.mydomain.com           | 3306 |                 0 |         1 |
+-----------+------------------------------+------+-------------------+-----------+
1 row in set (0.02 sec)
  • On the backup server, issue the following command to see the slave status:
mysql> show slave status\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: master.mydomain.com
                Master_User: replication_user
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 525088794
             Relay_Log_File: slave-relay-bin.000004
              Relay_Log_Pos: 250751844
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB:
        Replicate_Ignore_DB: mysql
         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: 525088794
            Relay_Log_Space: 250751844
            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

More Information

More information can be found online at the Mysql documentation Replication Guide.

번호 제목 글쓴이 날짜 조회 수
1701 2만원 WHRIA 2010.02.11 14168
1700 WHRIA WORLD (http://whria.net) 한승석 2002.04.12 14101
1699 서버가 노후되었음 WHRIA 2010.04.11 13883
1698 FODORA 에서 영문 LINUX 상태에서 한글 입력방법 [1] han 2004.10.07 13877
1697 개원 WHRIA 2010.04.03 13250
1696 That's why I was lucky ... WHRIA 2007.08.08 13229
1695 DeltaCopy 퍼미션 문제 WHRIA 2013.01.22 13073
1694 사진 file WHRIA 2010.01.19 13022
1693 볼펜 주문 [1] WHRIA 2009.12.20 12818
1692 예전 홈페이지 찾았다~ han 2005.09.14 12450
» mysql replication [1] WHRIA 2012.02.26 12359
1690 인테리어가 거의 끝나간다. WHRIA 2010.01.03 12170
1689 명함 file WHRIA 2009.12.10 11180
1688 세무관련 블로그 WHRIA 2009.12.22 11012
1687 SSD 참 좋군 WHRIA 2012.12.19 10572

Powered by Xpress Engine / Designed by Sketchbook

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소