• ADADADADAD

    MySQL 5.7如何使用GTID方式搭建复制环境[ mysql数据库 ]

    mysql数据库 时间:2024-11-26 22:15:22

    作者:文/会员上传

    简介:

    当使用GTIDs(global transaction identifiers),每个事务在提交时,都会被标记一个独特的事务号,被用于在备库上应用,这样在搭建复制环境时,不用使用日志文件和日志位置的传统方式搭

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    当使用GTIDs(global transaction identifiers),每个事务在提交时,都会被标记一个独特的事务号,被用于在备库上应用,这样在搭建复制环境时,不用使用日志文件和日志位置的传统方式搭建,大大简化了复制环境的搭建流程。可以使用语句级别和行级别的复制格式,建议使用行级别的复制格式。

    GTID的格式如下
    GTID = source_id:transaction_id
    source_id代表源服务端,transaction_id代表事务的顺序号。

    限制:
    因为以GTID为基础的复制时基于事务的,一些特性在复制中会有限制。
    不支持非事务性的表,如MyISAM表等。
    不支持CREATE TABLE ... SELECT语句。CREATE TABLE ... SELECT对于语句级别的复制格式不安全。当使用行级别的复制格式时,这个语句在日志中被记录成两个单独的事件,一个是表的创建,另一个是表的插入操作。当这个语句在一个事务中被执行时,在某些情况下这两个事件会被分配相同的事务号,这样第2个执行插入操作的事务可能会被从库跳过。
    临时表。在事务内部,GTID复制不支持CREATE TEMPORARY TABLE、DROP TEMPORARY TABLE语句。
    GTID复制不支持sql_slave_skip_counter参数。如果需要跳过事务,使用主库上的gtid_executed参数。
    主库gtid_purged参数包含了所有在主库二进制日志中清除的所有事务。

    搭建流程:

    编辑主库的配置文件,并重启主库
    # Log
    server-id = 27100
    log-bin = production-bin
    #log-bin-index = /log/production-bin.index
    binlog_format = row
    log_slave_updates
    gtid-mode = ON
    enforce-gtid-consistency = ON

    编辑从库的配置文件,并重启从库
    # Log
    server-id = 35100
    log-bin = production-bin
    #log-bin-index = /log/production-bin.index
    binlog_format = row
    log_slave_updates
    gtid-mode = ON
    enforce-gtid-consistency = ON

    在主库上导出备份,并传输到从库
    [root@localhost 20160609]# mysqldump -uroot -p'System#2013' -S /var/lib/mysql/mysql.sock -A -R --single-transaction --default-character-set=utf8 > 20160609.sql

    在从库上应用备份
    [root@localhost 20160609]# mysql -uroot -p'System#2013' < 20160609.sql

    在Master数据库上面创建复制专用账户
    mysql> grant replication slave on *.* to 'repl'@'192.168.78.%' identified by 'Mysql#2015';
    Query OK, 0 rows affected, 1 warning (0.17 sec)

    在从库上执行CHANGE MASTER命令
    mysql> change master to
    -> master_host='192.168.78.141',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='Mysql#2015',
    -> master_auto_position = 1;
    Query OK, 0 rows affected, 2 warnings (0.31 sec)

    --启动IO和SQL线程
    mysql> start slave;
    Query OK, 0 rows affected (0.04 sec)

    mysql> show slave status\G
    *************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
    Master_Host: 192.168.78.141
    Master_User: repl
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: production-bin.000002
    Read_Master_Log_Pos: 448
    Relay_Log_File: localhost-relay-bin.000002
    Relay_Log_Pos: 671
    Relay_Master_Log_File: production-bin.000002
    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:
    Last_Errno: 0
    Last_Error:
    Skip_Counter: 0
    Exec_Master_Log_Pos: 448
    Relay_Log_Space: 882
    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: 27100
    Master_UUID: cf291e84-2c89-11e6-b6f0-000c29631605
    Master_Info_File: /var/lib/mysql/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: cf291e84-2c89-11e6-b6f0-000c29631605:1
    Executed_Gtid_Set: cf291e84-2c89-11e6-b6f0-000c29631605:1
    Auto_Position: 1
    Replicate_Rewrite_DB:
    Channel_Name:
    Master_TLS_Version:
    1 row in set (0.00 sec)

    mysql> show processlist;
    +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
    | Id | User| Host | db| Command | Time | State | Info |
    +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
    | 4 | root| localhost | fire | Query|0 | starting| show processlist |
    | 6 | system user || NULL | Connect | 480 | Waiting for master to send event| NULL |
    | 7 | system user || NULL | Connect | 153 | Slave has read all relay log; waiting for more updates | NULL |
    +----+-------------+-----------+------+---------+------+--------------------------------------------------------+------------------+
    3 rows in set (0.02 sec)

    MySQL 5.7如何使用GTID方式搭建复制环境.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: gtidmysql