Skip to content

mysql replication

2012.02.26 19:57

WHRIA 조회 수:12396

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.

번호 제목 글쓴이 날짜 조회 수
454 easybcd 멀티부팅 관리 프로그램 WHRIA 2012.02.05 7815
453 adobe flash 안써도 웹서핑이 쾌적 WHRIA 2012.02.09 8067
452 공동개원 WHRIA 2012.02.11 3714
451 공동개원2 WHRIA 2012.02.11 5772
450 공동개원3 WHRIA 2012.02.11 5954
449 토렌트 검색 site 만들었음 WHRIA 2012.02.14 6982
» mysql replication [1] WHRIA 2012.02.26 12396
447 우분투에서 NETBIOS 설정 WHRIA 2012.04.02 7533
446 Medicalphoto 1.08 WHRIA 2012.04.04 7954
445 Medicalphoto 홈페이지 개편 WHRIA 2012.04.10 7116
444 simple adblock file WHRIA 2012.04.13 18110
443 차트 php WHRIA 2012.04.24 7880
442 yahoo 데이터 가져오기 WHRIA 2012.04.25 8438
441 deskzoom file WHRIA 2012.05.22 7605
440 피얼룩 지우기 WHRIA 2012.05.26 9971

Powered by Xpress Engine / Designed by Sketchbook

sketchbook5, 스케치북5

sketchbook5, 스케치북5

나눔글꼴 설치 안내


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

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

설치 취소