GTID主从复制

实验环境

角色 ip
gtid(master) 192.168.118.222
gitd(从) 192.168.118.112

mysql安装编译安装mysql
配置文件(master)

[root@server data]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/111.pid
user = mysql
skip-name-resolve
#GTID
server-id=135          #服务器id
gtid-mode = ON        #开启gtid模式
enforce-gtid-consistency = ON      ##强制gtid一致性,开启后对于特定create table不被支持
#BINLOG
log-slave-updates = 1
log-bin=master-binlog
binlog-format=row              #强烈建议,其他格式可能造成数据不一致
#relay log
skip-slave-start=1

[root@client local]# vim /etc/my.cnf

[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
#gtid
gtid-mode=on
enforce-gtid-consistency=on
server-id=143
#binlog
log-bin=slave-binlog
log-slave-updates=1
binlog_format=row
#relay log
skip_slave_start=1
skip-grant-tables

创建授权用户(master)

mysql> create user haha@'192.168.118.112' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'haha'@'192.168.118.112';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

配置基于gtid的复制(从)

mysql> change master to
    master_host='192.168.118.222',
    master_user='haha',
    master_password='123456',
    master_port=3306,
    master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> flush privileges;
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.118.222
                  Master_User: haha
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-binlog.000012
          Read_Master_Log_Pos: 1059
               Relay_Log_File: client-relay-bin.000004
                Relay_Log_Pos: 1280
        Relay_Master_Log_File: master-binlog.000012
             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: 1059
              Relay_Log_Space: 2569
              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: 135
                  Master_UUID: 1f502a0e-aad5-11e8-af9c-000c2913cc69
             Master_Info_File: /opt/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for moupdates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 1f502a0e-aad5-11e8-af9c-000c2913cc69:1-6
            Executed_Gtid_Set: 1f502a0e-aad5-11e8-af9c-000c2913cc69:1-6,
e0b213d6-e623-11e8-a2b6-000c290cf8d2:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

验证主从复制
在master上创建xsb和xsb2数据库

mysql> create database xsb;
mysql> create database xsb2;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xsb                |
| xsb2               |
+--------------------+
6 rows in set (0.00 sec)

在192.168.118.112上查看数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| xsb                |
| xsb2               |
+--------------------+
6 rows in set (0.00 sec)

你可能感兴趣的:(运维)