其他分享
首页 > 其他分享> > 主从复制与GTID

主从复制与GTID

作者:互联网

目录


1.主从形式

主从形式分为五种:

​ 1.一主多从

​ 2.主主复制

​ 3.一主多从

​ 4.多主一从

​ 5.联级复制zhu

2.主从复制原理

主库将所有的记录写入到binlog的到中生成一个log dump线程,然后将binlog日志传给从库的i/o线程,从库还会产生两个线程,一个I/O线程,一个sqk线程

I/O线程会请求读取主库的binlog日志,并将读取到的所有记录下入到relay log(中继日志)日志中

sql线程会读取relay log文件中的日志解析成具体操作然后实现主从操作的一致,达成数据一致性。

3.主从复制配置

1.确保从数据库与主数据库里的数据一致

2.在主数据库里创建一个同步账号授权给数据库使用

3.配置主数据库(修改配置文件)

4.配置从数据库(修改配置文件)

需求:搭建两台mysql服务器,一主一从,主进行写操作,从进行读操作

3.1mysql二进制安装

参考《mysql进阶》此处步骤忽略。

3.2mysql主从配置

3.2.1 确保从数据库与主数据库里面的数据完成一致
master
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuyang            |
| liuyang123         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

slave
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@master ~]# mysqldump -uroot -pliuyang123! --all-databases > /opt/all-20220802.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# scp /opt/all-20220802.sql root@192.168.124.129:/opt/
The authenticity of host '192.168.124.129 (192.168.124.129)' can't be established.
ECDSA key fingerprint is SHA256:gpQekwQwQvWT0VhHm4D0jUyHrgl0SOQIgerJhIyzOvs.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.124.129' (ECDSA) to the list of known hosts.
root@192.168.124.129's password: 
all-20220802.sql                          100%  860KB  57.0MB/s   00:00
//在从库上查看数据库确认主库的数据已经迁移过来
[root@slave ~]# mysql -uroot -pliuyang123! < /opt/all-20220802.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave ~]# mysql -uroot -pliuyang123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| liuyang            |
| liuyang123         |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

在主数据库中创建一个同步用户授权给从数据库使用

mysql> create user 'repl'@'192.168.124.129' identified by 'liuyang123!';
Query OK, 0 rows affected (0.00 sec)

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

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

3.2.3 配置主数据库

[root@master ~]# cat /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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

log-bin=mysql-bin
server-id=10
symbolic-links=0
log-error=/var/log/mysqld.log
[root@master ~]# systemctl  restart mysqld

//查看主库的状态
[root@master ~]# mysql -uroot -pliuyang123! -e 'show master status\G;'
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 

3.2.4配置从数据库

[root@slave ~]# cat /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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

server-id=15
relay-log=mysql-relay-bin
symbolic-links=0
log-error=/var/log/mysqld.log
//重启mysql
[root@slave ~]# systemctl  restart mysqld.service 
//配置并启动主从复制
mysql> change master to master_host='192.168.124.131', master_user='repl',master_password='liuyang123!', master_log_file='mysql-bin.000001', master_loog_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

//查看从服务器状态
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.124.131
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

3.2.5测试验证

主
mysql> insert liu values(3,'l'),(4,'y');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0
mysql> select * from liu;
+----+------+
| id | name |
+----+------+
|  1 | liu  |
|  2 | yang |
|  3 | l    |
|  4 | y    |
+----+------+
4 rows in set (0.00 sec)
//在从数据库中查看是否同步
mysql> select * from liu;
+----+------+
| id | name |
+----+------+
|  1 | liu  |
|  2 | yang |
|  3 | l    |
|  4 | y    |
+----+------+
4 rows in set (0.00 sec)

GTID主从配置

主库配置

[root@master ~]# cat /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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

log-bin=mysql_bin
server-id=10
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
symbolic-links=0
log-error=/var/log/mysqld.log

从库配置

[root@slave ~]# cat /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
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

server-id=15
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
symbolic-links=0
log-error=/var/log/mysqld.log

主库授权复制用户

mysql> grant replication slave on *.* to 'repl'@'%' identified by 'liuyang123!';
Query OK, 0 rows affected, 1 warning (0.01 sec)

从库设置要同步的主库信息,并开启同步

mysql> change master to master_host='192.168.124.128',
    -> master_port=3306,master_user='repl',master_password='liuyang123!',
    -> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.124.128
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000003
          Read_Master_Log_Pos: 737
               Relay_Log_File: myrelay.000004
                Relay_Log_Pos: 950
        Relay_Master_Log_File: mysql_bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
查看是否主从同步完成
//主
mysql> insert liu(id,name)values(1,'l'),(2,'y');
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from liu;
+----+------+
| id | name |
+----+------+
|  1 | l    |
|  2 | y    |
+----+------+
2 rows in set (0.00 sec)
//从
mysql> use liuyang;
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 liu;
+----+------+
| id | name |
+----+------+
|  1 | l    |
|  2 | y    |
+----+------+
2 rows in set (0.00 sec)

mysql> 

GTID与传统主从复制的区别是什么

普通主从复制主要是基于二进制日志文件位置的复制,因此主必须启动二进制日志记录并建立唯一的服务器ID,复制组中的每个服务器都必须配置唯一的服务器ID

全局事务标识符(GTID)是创建的唯一标识符,并与在源(主)服务器上提交的每个事务相关联。此标识符不但是唯一的,而且在给定复制设置中的所有服务器上都是唯一的。所有交易和所有GTID之间都有一对一的映射关系 。它由服务器ID以及事务ID组合而成。这个全局事务ID不仅仅在原始服务器上唯一,在所有存在主从关系 的mysql服务器上也是唯一的。正是因为这样一个特性使得mysql的主从复制变得更加简单,以及数据库一致性更可靠。一个GTID在一个服务器上只执行一次,避免重复执行导致数据混乱或者主从不一致。

标签:主从复制,log,NO,数据库,master,mysql,sec,GTID
来源: https://www.cnblogs.com/TQingS/p/16543374.html