一、復制介紹
MySQL支持單向、異步復制,復制過程中一個服務器充當主服務器,而一個或多個其它服務器充當從服務器。主服務器將更新寫入二進制日志文件,並維護文件的一個索引以跟蹤日志循環。這些日志可以記錄發送到從服務器的更新。當一個從服務器連接主服務器時,它通知主服務器從服務器的日志中讀取的最后一次成功更新的位置。從服務器接收從那時起發生的任何更新,然后封鎖並等待主服務器通知新的更新。
請注意當你進行復制時,所有對復制中的表的更新必須在主服務器上進行。否則,你必須要小心,以避免用戶對主服務器上的表進行的更新與對從服務器上的表所進行的更新之間的沖突。
單向復制有利於健壯性、速度和系統管理:
•主服務器/從服務器設置增加了健壯性。主服務器出現問題時,你可以切換到從服務器作備份。
•通過在主服務器和從服務器之間切分處理客戶查詢的負荷,可以得到更好的客戶響應時間。SELECT查詢可以發送到從服務器以降低主服務器的查詢處理負荷。但修改數據的語句仍然應發送到主服務器,以便主服務器和從服務器保持同步。如果非更新查詢為主,該負載均衡策略很有效,但一般是更新查詢。
•使用復制的另一個好處是可以使用一個從服務器執行備份,而不會干擾主服務器。在備份過程中主服務器可以繼續處理更新。
MySQL提供了數據庫的同步功能,這對我們事先數據庫的容災、備份、恢復、負載均衡等都是有極大幫助的。
二、實驗環境
操作系統:RHEL 5.4 X86
mysql:5.5.22版本
master機器名:node2 IP:192.168.1.152
slave機器名:node1 IP:192.168.1.151
三、MySQL主從模式
注意Mysql數據庫的版本,兩個數據庫版本要相同,或者slave比master版本低!
3.1、通過源碼安裝mysql
此處過程略,可以參考<Mysql 源碼安裝>!
3.2、Master端:
3.2.1、創建目錄
1 [root@node2 ~]# mkdir -p /var/log/mysql 2 [root@node2 ~]# chown -R mysql:mysql /var/log/mysql //創建更新目錄並賦予mysql用戶權限
3.2.2、修改配置文件:
1 [root@node2 mysql]# vi my.cnf //編輯配置文件增加以下內容 2 log-bin=mysql-bin //啟動二進制日志系統 3 binlog-do-db=node1 4 binlog-do-db=node2 //二進制需要同步的數據庫名 如果有多個數據庫,每個數據庫一行 5 server-id = 1 //本機數據庫ID 6 log-bin=/var/log/mysql/updatelog //設定生成log文件名,這里的路徑沒有mysql目錄要手動創建並給於它mysql用戶的權限 7 binlog-ignore-db=mysql //避免同步mysql用戶配置,以免不必要的麻煩
3.2.3、創建用於同步的用戶:
1 mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT,RELOAD,SUPER ON *.* TO 'replication'@'192.168.1.151' IDENTIFIED BY '123456'; 2 Query OK, 0 rows affected (0.00 sec) //給從服務器用戶replication 的同步權限 3 4 mysql> Grant ALL PRIVILEGES ON node1.* TO replication@'%' IDENTIFIED BY '123456'; //創建用戶replication,密碼123456,允許所有用戶訪問數據庫node1,並刷新權限 5 Query OK, 0 rows affected (0.00 sec) 6 7 mysql> Grant ALL PRIVILEGES ON node2.* TO replication@'%' IDENTIFIED BY '123456'; 8 Query OK, 0 rows affected (0.00 sec)
3.2.4、復制數據到從庫:
1 mysql> create database node1; 2 Query OK, 1 row affected (0.00 sec) 3 4 mysql> create database node2; 5 Query OK, 1 row affected (0.00 sec) 6 7 mysql> use node1; 8 Database changed 9 mysql> create table node1 (id char) engine=myisam; 10 Query OK, 0 rows affected (0.01 sec) 11 12 mysql> insert into node1 values(1); 13 Query OK, 1 row affected (0.00 sec) 14 15 mysql> commit; 16 Query OK, 0 rows affected (0.00 sec) 17 18 mysql> exit; 19 Bye 20 [root@node2 app]# service mysql stop; 21 Shutting down MySQL... [ OK ] 22 23 [root@node2 data]# tar -cvf db.tar node1 node2 24 node1/ 25 node1/node1.MYI 26 node1/db.opt 27 node1/node1.frm 28 node1/node1.MYD 29 node2/ 30 node2/db.opt 31 [root@node2 data]# ll 32 total 30140 33 -rw-r--r-- 1 root root 20480 Nov 13 19:29 db.tar 34 35 [root@node2 data]# scp db.tar node1:/app/mysql/data/ 36 db.tar 100% 20KB 20.0KB/s 00:00 37 38 [root@node2 data]# service mysql start 39 Starting MySQL.. [ OK ] 40 41 [root@node2 data]# mysql 42 Welcome to the MySQL monitor. Commands end with ; or \g. 43 Your MySQL connection id is 1 44 Server version: 5.5.22-log Source distribution 45 46 Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. 47 48 Oracle is a registered trademark of Oracle Corporation and/or its 49 affiliates. Other names may be trademarks of their respective 50 owners. 51 52 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 53 54 mysql> show master status; 55 +------------------+----------+--------------+------------------+ 56 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | 57 +------------------+----------+--------------+------------------+ 58 | updatelog.000001 | 107 | node1,node2 | mysql | 59 +------------------+----------+--------------+------------------+ 60 1 row in set (0.00 sec)
3.3、Slave端:
3.3.1、修改配置文件:
1 [root@node1 data]# vi ../my.cnf 2 server-id = 2 //從服務器ID號 3 #master_host=192.168.1.152 //主服務器地址 4 #master_user=replication //指定在主服務器上可以進行同步的用戶名 5 #master_password=123456 //密碼 6 #master-port=3306 //端口號 7 #master-connect-retry=60 //斷點重連時間 8 replicate-ignore-db=mysql //屏蔽對mysql庫的同步,以免有麻煩 9 replicate-do-db=node1 //同步數據庫名稱 10 replicate-do-db=node2 11 ----由於5.3以后不支持master_host參數等,因此下面采用change master to的方式
3.3.2、裝載主服務器數據庫:
[root@node1 data]# tar -xvf db.tar node1/ node1/node1.MYI node1/db.opt node1/node1.frm node1/node1.MYD node2/ node2/db.opt [root@node1 data]# chown -R mysql:mysql node1 [root@node1 data]# chown -R mysql:mysql node2 [root@node1 data]# service mysql restart Starting MySQL [ OK ]
3.3.3、同步數據:
mysql> slave stop -> ; Query OK, 0 rows affected (0.01 sec) mysql> change master to master_host='192.168.1.152', master_user='replication', master_password='123456'; Query OK, 0 rows affected (0.01 sec) mysql> slave start; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.152 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: updatelog.000001 Read_Master_Log_Pos: 107 Relay_Log_File: node1-relay-bin.000002 Relay_Log_Pos: 253 Relay_Master_Log_File: updatelog.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: node1,node2 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: 107 Relay_Log_Space: 409 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 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) ERROR: No query specified mysql> use node1; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------+ | Tables_in_node1 | +-----------------+ | node1 | +-----------------+ 1 row in set (0.00 sec) mysql> select * from node1; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec)
3.4、測試數據
----主庫端操作 mysql> use node1; Database changed mysql> insert into node1 values(2); Query OK, 1 row affected (0.01 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec) mysql> use node2; Database changed mysql> create table node2 (id char); Query OK, 0 rows affected (0.01 sec) ----備庫端查詢 mysql> select * from node1; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec) mysql> use node2; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------+ | Tables_in_node2 | +-----------------+ | node2 | +-----------------+ 1 row in set (0.00 sec)
四、MySQL主主模式:
思路,互為對方的從服務器,每台服務器即是對方的主服務器,又是對方的從服務器。
在這里就省略了!
