標籤:etc packages ant 資料分布 從伺服器 軟體 1.7 跟蹤 ret
## 1 概述 ##
MySQL內建的複製功能是構建大型,高效能應用程式的基礎。將Mysql的資料分布到多個系統上去,這種分布的機制,是通過將Mysql的某一台主機的資料複製到其它主機(slaves)上,並重新執行一遍來實現的。複製過程中一個伺服器充當主伺服器,而一個或多個其它伺服器充當從伺服器。主伺服器將更新寫入二進位記錄檔,並維護檔案的一個索引以追蹤記錄檔迴圈。這些日誌可以記錄發送到從伺服器的更新。當一個從伺服器串連主伺服器時,它通知主伺服器從伺服器在日誌中讀取的最後一次成功更新的位置。從伺服器接收從那時起發生的任何更新,然後封鎖並等待主伺服器通知新的更新。
請注意當你進行複製時,所有對複製中的表的更新必須在主伺服器上進行。否則,你必須要小心,以避免使用者對主伺服器上的表進行的更新與對從伺服器上的表所進行的更新之間的衝突。
## 2 複製解決的問題 ##
MySQL複製技術有以下一些特點:
(1) 資料分布 (Data distribution )
(2) Server Load Balancer(load balancing)
(3) 備份(Backups)
(4) 高可用性和容錯行 High availability and failover
## 3 複製的原理 ##
(1) master將改變記錄到二進位日誌(binary log)中(這些記錄叫做二進位日誌事件,binary log events);
(2) slave將master的binary log events拷貝到它的中繼日誌(relay log);
(3) slave重做中繼日誌中的事件,將更改應用到自己的資料上。
該過程的第一部分就是master記錄二進位日誌。在每個事務更新資料完成之前,master在二日誌記錄這些改變。MySQL將事務串列的寫入二進位日誌,即使事務中的語句都是交叉執行的。在事件寫入二進位日誌完成後,master通知儲存引擎提交事務。
下一步就是slave將master的binary log拷貝到它自己的中繼日誌。首先,slave開始一個背景工作執行緒——I/O線程。I/O線程在master上開啟一個普通的串連,然後開始binlog dump process。Binlog dump process從master的二進位日誌中讀取事件,如果已經跟上master,它會睡眠並等待master產生新的事件。I/O線程將這些事件寫入中繼日誌。
SQL slave thread(SQL從線程)處理該過程的最後一步。SQL線程從中繼日誌讀取事件,並重放其中的事件而更新slave的資料,使其與master中的資料一致。只要該線程與I/O線程保持一致,中繼日誌通常會位於OS的緩衝中,所以中繼日誌的開銷很小。
此外,在master中也有一個背景工作執行緒:和其它MySQL的串連一樣,slave在master中開啟一個串連也會使得master開始一個線程。複製過程有一個很重要的限制——複製在slave上是序列化的,也就是說master上的並行更新操作不能在slave上並行操作。
## 4 配置及環境準備 ##
有兩台MySQL資料庫伺服器Master和slave,Master為主伺服器,slave為從伺服器,初始狀態時,Master和slave中的資料資訊相同,當Master中的資料發生變化時,slave也跟著發生相應的變化,使得master和slave的資料資訊同步,達到備份的目的。
環境準備(主從環境要相同)
OS:centos 6.6
Packages:mysql mysql-server
## 5 軟體安裝 ##
兩台都安裝mysql mysql-server
[[email protected] ~]# yum -y install mysql mysql-server
[[email protected] ~]# yum -y install mysql mysql-server
## 6 資料庫配置 ##
修改資料庫設定檔my.cnf
開啟記錄二進位記錄檔log-bin和server-id(兩台機器server-id不能一樣)
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
log-bin=mysql-bin
server-id=2
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
啟動mysql 登入進資料庫
[[email protected] ~]# /etc/init.d/mysqld start
[[email protected] ~]# mysql -uroot -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73-log Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql>
在master上設定資料庫授權
mysql> grant replication slave on *.* to ‘root‘@‘slave-IP‘ identified by ‘password‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 472 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
在slave上做如下操作
mysql> change master to master_host="master-IP",master_user="root",master_password="password",master_log_file="mysql-bin.000003",master_log_pos=472;
Query OK, 0 rows affected (0.02 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: *.*.*.*
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 472
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
註:Slave_IO_Running: yes Slave_SQL_Running: Yes
這個兩個是yes 就代表主從同步是成功的。
MySQL中如何建立主從複製