主从复制gtid主从

传统主从与gtid主从的工作原理


文章目录

      • 1.主从复制原理
      • 2.mysql主从配置
        • 2.1从库要和主库的数据一样
        • 2.2在主数据库里创建一个同步账号授权给从数据库使用
        • 2.3配置主数据库
        • 2.4配置从库
        • 2.5测试验证
      • 3.gtid主从复制
        • 3.1gtid工作原理
        • 3.2gtid相关参数
        • 3.3测试主从复制

1.主从复制原理

主从复制步骤:

  • 主库将所有的写操作记录到binlog日志中并生成一个log dump线程,将binlog日志传给从库的I/O线程
  • 从库生成两个线程,一个I/O线程,一个SQL线程
    • I/O线程去请求主库的binlog,并将得到的binlog日志写到relay log(中继日志) 文件中
    • SQL线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的

2.mysql主从配置

2.1从库要和主库的数据一样
//查看主库的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

//查看从库的数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

//全量备份主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致,一般适用于公司
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

[root@hwf ~]# mysqldump -uroot -p123 --all-databases >/opt/beifen/all-$(date '+%Y%m%d%H%M%S').sql		//将主库全备
mysqldump: [Warning] Using a password on the command line interface can be insecure. 
[root@hwf ~]# ls /opt/beifen/
all-20220801202940.sql

[root@hwf ~]# scp /opt/beifen/all-20220801202940.sql 	[email protected]:/opt/beifen/		//将主库全被数据远程传输到从库
[email protected]'s password: 
all-20220801202940.sql                                               100%  856KB  19.5MB/s   00:00    
[root@hwf ~]# 

[root@hhh ~]# ls /opt/beifen/		//查看是否传输成功
all-20220801202940.sql
[root@hhh ~]# mysql -uroot -p123 < /opt/beifen/all-20220801202940.sql 			//将主库的全备恢复从库中
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@hhh ~]# mysql -uroot -p123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
| sys                |
+--------------------+
2.2在主数据库里创建一个同步账号授权给从数据库使用
mysql> grant replication slave on *.* to 'repl'@'192.168.159.100' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.3配置主数据库
[root@hwf ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysqldata
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysqldata/mysql.pid
user = mysql
skip-name-resolve

log-bin=mysql-bin			//启用binlog日志
server-id=10				//数据库服务器唯一标识符,主库的server-id值必须比从库的小


//重启mysql服务
[root@hwf ~]# systemctl restart mysqld.service 
[root@hwf ~]# ss -anlt
State      Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process     
LISTEN     0           128                    0.0.0.0:111                 0.0.0.0:*                    
LISTEN     0           128                    0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0           80                           *:3306                      *:*                    
LISTEN     0           128                       [::]:111                    [::]:*                    
LISTEN     0           128                       [::]:22                     [::]:*                    
[root@hwf ~]# 

//查看主库状态
[root@hwf ~]# mysql -uroot -p123 -e 'show master status;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      773 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

2.4配置从库
[root@hhh ~]# vim /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysqldata
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysqldata/mysql.pid
user = mysql
skip-name-resolve

server-id=20		//设置从库的唯一标识符,从库的server-id值必须大于主库的该值					
relay-log=mysql-relay-bin			//启用中继日志relay-log


//重启mysql服务
[root@hhh ~]# systemctl restart mysqld.service 
s^H[root@hhh ~]# ss -anlt
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*                
LISTEN   0        80                     *:3306                  *:*                
LISTEN   0        128                    *:80                    *:*                
LISTEN   0        128                 [::]:22                 [::]:*                

//配置启动主从复制
mysql> change master to
    -> master_host='192.168.159.100',
    -> master_user='repl',
    -> master_password='123',
    -> master_log_file='mysql-bin.000002',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

//查看从库服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.159.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-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: 154
              Relay_Log_Space: 527
              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: 10
                  Master_UUID: a9996cc5-1186-11ed-beda-000c291ef509
             Master_Info_File: /opt/mysqldata/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

2.5测试验证
mysql> insert hwf(id,name)values(3,'jilao'),(4,'yongshao'),(5,'jiaopi');
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from hwf;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | zhanglang |
|  2 | nigger    |
|  3 | jilao     |
|  4 | yongshao  |
|  5 | jiaopi    |
+----+-----------+
5 rows in set (0.00 sec)

//在从库中查看是否同步
mysql> use runtime;
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> select * from hwf;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | zhanglang |
|  2 | nigger    |
|  3 | jilao     |
|  4 | yongshao  |
|  5 | jiaopi    |
+----+-----------+
5 rows in set (0.00 sec)

3.gtid主从复制

3.1gtid工作原理
3.2gtid相关参数
参数 comment
gtid_executed 执行过的所有GTID
gtid_purged 丢弃掉的GTID
gtid_mode GTID模式
gtid_next session级别的变量,下一个gtid
gtid_owned 正在运行的GTID
enforce_gtid_consistency 保证GTID安全的参数
//修改主数据库的配置文件
[root@hwf ~]# cat /etc/my.cnf 
[mysqld] 
basedir = /usr/local/mysql 
datadir = /opt/mysqldata 
socket = /tmp/mysql.sock 
port = 3306 
pid-file = /opt/mysqldata/mysql.pid 
user = mysql 
skip-name-resolve 

log-bin=mysql_bin
server-id=10
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
//因为修改密码安全等级失败,所以要添加这两行
plugin-load-add=validate_password.so		
validate-password=FORCE_PLUS_PERMANENT

sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION


//配置从库的配置文件
[root@hhh ~]# cat /etc/my.cnf 
[mysqld] 
basedir = /usr/local/mysql 
datadir = /opt/mysqldata 
socket = /tmp/mysql.sock 
port = 3306 
pid-file = /opt/mysqldata/mysql.pid 
user = mysql 

server-id=20
relay-log=myrelay
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
read_only=on
master-info-repository=TABLE
relay-log-info-repository=TABLE
skip-name-resolve 

sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

//主库授权复制用户。
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)


//从库设置要同步的主库信息,并开启同步。
mysql> change master to
    -> master_host='192.168.159.100',
    -> master_user='repl',
    -> master_password='123456',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)


mysql> start slave;
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.159.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000002
          Read_Master_Log_Pos: 589
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 802
        Relay_Master_Log_File: mysql_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: 589
              Relay_Log_Space: 1001
              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: 10
                  Master_UUID: 08a42b48-11a3-11ed-b1cb-000c291ef509
             Master_Info_File: mysql.slave_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: 08a42b48-11a3-11ed-b1cb-000c291ef509:1-2
            Executed_Gtid_Set: 08a42b48-11a3-11ed-b1cb-000c291ef509:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

3.3测试主从复制
//在主库中插入数据
mysql> create table hhh(id int not null,name varchar(50));
Query OK, 0 rows affected (0.03 sec)

mysql> insert hhh(id,name)values(1,'zhang'),(2,'yang');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from hhh;
+----+-------+
| id | name  |
+----+-------+
|  1 | zhang |
|  2 | yang  |
+----+-------+
2 rows in set (0.00 sec)
mysql> show tables;
+-------------------+
| Tables_in_runtime |
+-------------------+
| hhh               |
+-------------------+
1 row in set (0.00 sec)

//从库查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| runtime            |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use runtime;
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_runtime |
+-------------------+
| hhh               |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from hhh;
+----+-------+
| id | name  |
+----+-------+
|  1 | zhang |
|  2 | yang  |
+----+-------+
2 rows in set (0.00 sec)

你可能感兴趣的:(数据库,mysql)