1. 程式人生 > >MySQL學習筆記06基於Binary Log的復制

MySQL學習筆記06基於Binary Log的復制

version gtid 事務提交 必備 running hang mil 記錄日誌 .info

1.1.1. 相關概念

(1)Binary Log

當變量log_bin的值為ON時,MySQL將啟用Binary Log,這將在data目錄下產生類似mysql-bin.00001, mysql-bin.00002的二進制日誌文件,這些文件記錄了數據庫中執行的各種操作。

binlog_format變量指定了MySQL的二進制日誌的格式,支持三種類型的格式:

ROW 使用數據表的行記錄來記錄日誌。優點是避免了STATEMENT格式時SQL語句中自增字段的不良影響。缺點時一條更新大量記錄的SQL語句可能產生大量日誌。

STATEMENT 使用執行的SQL語句來記錄日誌。優點和缺點與ROW格式相反。

MIXED 使用混合格式來記錄日誌。

默認情況下,MySQL使用ROW格式。

(2)基於Binary Log的復制。

使用基於Binary Log的復制方式時,master主機上的MySQL需要啟動Binary Log,同時masterslave上都需要設置MySQLserver-id變量。master上的MySQL將數據庫上所做的修改全部記錄到Binary Log文件中。slave上的MySQLmaster接收其Binary Log 事件,然後將必要的Binary Log在本地MySQL上回放,從而使得slave上的數據庫的內容和master上的數據庫的內容保持相同。

1.1.2. 部署主從復制

目標:實現MySQL 基於Binary Log的復制。

Mastermysql101.coe2coe.me

Slavemysql102.coe2coe.memysql103.coe2coe.me

(1)修改mastermysql配置文件。

在配置文件 /etc/mysql/mysql.conf.d/mysqld.cnf中增加如下內容:

[mysqld]

log-bin=mysql-bin

server-id=101

innodb_flush_log_at_trx_commit=1

sync_binlog=1

其中以下配置為必備配置。

log-bin:啟用

Binary Log

Server-id:設置masterserver id

以下配置為了提高對於InnoDB事務的復制操作的可靠性。

Innodb_flush_log_at_trx_commit: 在InnoDB事務提交時刷新日誌。

Sync_binlog:同步Binary Log

(2)master上創建mysql復制用戶。

mysql> create user [email protected]%.coe2coe.me‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.03 sec)

mysql> grant replication slave on *.* to [email protected]%‘;

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

(3)master上查詢復制位置。

在某個mysql連接中執行以下操作。

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

在另外一個mysql連接中執行以下操作:

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000003 | 154 | | | |

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

1 row in set (0.00 sec)

其中,

File字段的值為日誌文件名稱,位於/opt/mysql/data目錄中。

Position字段的值表示日誌的位置。

(4)修改slavemysql配置文件:

102103上修改配置文件:

(a) 修改/etc/mysql/mysql.conf.d/mysqld.cnf

[mysqld]

log-bin=mysql-bin

server-id=102

innodb_flush_log_at_trx_commit=1

sync_binlog=1

read-only=1

replicate-wild-ignore-table=mysql.%

replicate-wild-ignore-table=information_schema.%

replicate-wild-ignore-table=performance_schema.%

replicate-wild-ignore-table=sys.%

其中:

Read-only:設置為1時,普通用戶將無法對數據庫執行寫操作,超級用戶和復制操作除外。

replicate-wild-ignore-table:忽略指定的數據表,這些表將不進行復制,可配置為MySQL系統相關的數據庫中的數據表不進行復制。

使用replicate-wild-ignore-table可以避免跨數據庫操作數據表時導致的slave上的復制產生問題。跨數據庫操作數據表指實際操作的數據表不是use語句指定的數據庫中的數據表,在某些數據庫不需要復制時,MySQL僅根據use語句來判斷當前使用的數據庫。

例子如下:

mysql> use coe2coe;

Database changed

mysql> insert into test.test (name2) values (‘005‘);

Query OK, 1 row affected (0.01 sec)

(b)修改/opt/mysql/data/auto.cnf

[auto]

server-uuid=a2392929-6dfb-11e7-b294-000c29b1c102

目的是為了修改slaveserver-uuid變量,因為全部slave主機上的文件是從master主機上整體復制得到的,需要將slave上的mysqlserver-uuid修改為跟master不同。

mysql> show variables like ‘server%‘;

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

| Variable_name | Value |

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

| server_id | 102 |

| server_id_bits | 32 |

| server_uuid | a2392929-6dfb-11e7-b294-000c29b1c102 |

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

3 rows in set (0.00 sec)

(5)slave上設置復制參數。

102103上執行以下SQL命令:

mysql> CHANGE MASTER TO

-> MASTER_HOST=‘mysql101.coe2coe.me‘,

-> MASTER_USER=‘repl‘,

-> MASTER_PASSWORD=‘123456‘,

-> MASTER_LOG_FILE=‘mysql-bin.000003‘,

-> MASTER_LOG_POS=154;

Query OK, 0 rows affected, 2 warnings (0.03 sec)

(6)slave上啟動復制過程。

兩個方法:

(a)重新啟動slave上的mysql服務。重新啟動mysql後會自動啟動復制過程。

(b)直接運行start slave命令。

1.1.3. 查看復制狀態

(1)查看slave的復制狀態。

可以查看slave的復制狀態:

mysql> show slave status\G

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

Slave_IO_State: Waiting for master to send event

Master_Host: mysql101.coe2coe.me

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000004

Read_Master_Log_Pos: 154

Relay_Log_File: mysql102-relay-bin.000006

Relay_Log_Pos: 367

Relay_Master_Log_File: mysql-bin.000004

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table: mysql.%,information_schema.%,performance_schema.%,sys.%

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 154

Relay_Log_Space: 1086

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: 101

Master_UUID: a2392929-6dfb-11e7-b294-000c29b1c101

Master_Info_File: /opt/mysql/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position: 0

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.00 sec)

(2)查看master的復制狀態。

mysql> show master status;

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

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000004 | 1024 | | | |

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

1 row in set (0.00 sec)

(3)查看master上已連接的slave主機。

mysql> show slave hosts;

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

| Server_id | Host | Port | Master_id | Slave_UUID |

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

| 103 | | 3306 | 101 | a2392929-6dfb-11e7-b294-000c29b1c103 |

| 102 | | 3306 | 101 | a2392929-6dfb-11e7-b294-000c29b1c102 |

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

2 rows in set (0.00 sec)

MySQL學習筆記06基於Binary Log的復制