配置mysql複製基本步驟

來源:互聯網
上載者:User

標籤:master   slave   主從複製   主從同步   

mysql主從複製的過稱其實是:將一台伺服器的資料複製到其他伺服器上並重新執行一遍的。複製過程中一台伺服器當作主伺服器(Master),一台或多台伺服器當作從伺服器(SLave).主伺服器將更新寫入到二進位記錄檔,並維護一個索引以追蹤記錄檔迴圈。這些日誌可以記錄發送給從伺服器的更新,當一個從伺服器串連到主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置,從伺服器接收從那時起的任何更新,然後封鎖並等待主伺服器通知新的更新。

整體上分為三個步驟:

1.master將改變記錄到記錄檔(binary log)中

2.slave複製二進位日誌事件(binary log event)到中繼日誌(relay log)中

3.slave重做中繼日誌,將改變轉化成自己的資料

650) this.width=650;" src="http://hi.csdn.net/attachment/201202/28/0_1330439010P7lI.gif" alt="0_1330439010P7lI.gif" />

環境:

系統:centos6.3

Master:10.10.0.224

Slave:10.10.0.226


1.建立備份帳號,只允許從伺服器有許可權

mysql> grant all privileges on *.* to [email protected] identified by ‘123456‘;

mysql> flush privileges;


2.拷貝資料

停掉mysql服務或者鎖表,將master中的資料拷貝到slave中,保證兩邊資料的一致,並且確保資料同步結束之前禁止在master和slave進行寫操作!

mysql> flush tables with read lock;    #鎖表

mysql> unlock tables;    #解鎖

3.配置master

# vim /etc/my.cnf

    #選擇唯一的server-id

    server-id = (0--2^32-1)

    #啟動二進位日誌

    log-bin = mysql-bin

    log-bin-index = mysql-bin.index

# service mysqld restart

# mysql

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000004 |  3318591 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)


4.配置slave

# vim /etc/my.cnf

    server_id = 2

    log-bin = mysql-bin

    log-bin-index = mysql-bin.index

    relay-log = relay-log

    relay-log-index = relay-log.index


5.串連至主伺服器並開始複製資料

mysql> stop slave;

mysql> change master to master_host=‘10.10.0.224‘,master_port=‘3306‘master_user=‘backup‘,

master_password=‘123456‘,master_log_file=‘mysql-bin.000005‘,master_log_pos=360;

mysql> start slave;

mysql> show slave status\G;

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 10.10.0.224

                  Master_User: backup

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 360

               Relay_Log_File: relay-log.000002

                Relay_Log_Pos: 251

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

*************************** 1. row ***************************


show processlist\G;可以查看I/0線程狀態

本文出自 “ngames” 部落格,請務必保留此出處http://ngames.blog.51cto.com/3187187/1655959

配置mysql複製基本步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.