• MySql主从同步实践


    一、环境介绍

    使用docker部署mysql实例
    数据库:MySQL 5.7.x 
    操作系统:CentOS 7.x
    容器:Docker version 20.10.17, build 100c701
    镜像:mysql:5.7
    主库:IP=172.168.10.149; PORT=3306; server-id=100; database=test; table=t1
    从库:IP=172.168.50.151; PORT=3306; server-id=110; database=test; table=t1

    注意事项:
    主从库必须保证网络畅通可访问
    主库必须开启binlog日志
    主从库的server-id必须不同

    1. [root@localhost data]# docker images
    2. REPOSITORY   TAG        IMAGE ID       CREATED       SIZE
    3. mysql        5.7        eef0fab001e8   10 days ago   495MB
    4. [root@localhost conf]# docker ps -a
    5. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    6. 15b8f6e717ad mysql:5.7 "docker-entrypoint.s…" 14 hours ago Up 57 minutes 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysql
    7. [root@localhost ~]# docker --version
    8. Docker version 20.10.17, build 100c701

    如果是系统运行了一段时间后才开始做主从同步,那么首先需要将这部分数据复制到从库。我这里使用 xtrabackup 工具来完成这一步工作,当然如果从节点是一个全新的环境,mysql版本一致的话,可以直接复制主库数据文件到从库。

    首先安装xtrabackup 工具,如下所示:

    1. cat <<eof>>/etc/yum.repos.d/percona.repo
    2. [percona]
    3. name = Percona
    4. baseurl = https://mirrors.tuna.tsinghua.edu.cn/percona/release/\$releasever/RPMS/\$basearch
    5. enabled = 1
    6. gpgcheck = 0
    7. [epel]
    8. name=epelrepo
    9. baseurl=https://mirrors.aliyun.com/epel/\$releasever/\$basearch
    10. gpgcheck=0
    11. enable=1
    12. eof
    13. [root@node1 ~]# yum list all| grep xtraback -i
    14. Repository epel is listed more than once in the configuration
    15. holland-xtrabackup.noarch                      1.0.14-3.el6                 epel
    16. percona-xtrabackup.x86_64                      2.3.10-1.el6                 percona
    17. percona-xtrabackup-20.x86_64                   2.0.8-587.rhel6              percona
    18. percona-xtrabackup-20-debuginfo.x86_64         2.0.8-587.rhel6              percona
    19. percona-xtrabackup-20-test.x86_64              2.0.8-587.rhel6              percona
    20. percona-xtrabackup-21.x86_64                   2.1.9-746.rhel6              percona
    21. percona-xtrabackup-21-debuginfo.x86_64         2.1.9-746.rhel6              percona
    22. percona-xtrabackup-22.x86_64                   2.2.13-1.el6                 percona
    23. percona-xtrabackup-22-debuginfo.x86_64         2.2.13-1.el6                 percona
    24. percona-xtrabackup-24.x86_64                   2.4.11-1.el6                 percona
    25. percona-xtrabackup-24-debuginfo.x86_64         2.4.11-1.el6                 percona
    26. percona-xtrabackup-debuginfo.x86_64            2.3.10-1.el6                 percona
    27. percona-xtrabackup-test.x86_64                 2.3.10-1.el6                 percona
    28. percona-xtrabackup-test-21.x86_64              2.1.9-746.rhel6              percona
    29. percona-xtrabackup-test-22.x86_64              2.2.13-1.el6                 percona
    30. percona-xtrabackup-test-24.x86_64              2.4.11-1.el6                 percona
    31. [root@node1 ~]# yum -y install percona-xtrabackup-24

    在主库执行备份

    innobackupex --datadir=/etc/mysql/data --user=root --password=123456 --host=127.0.0.1 --databases=test /tmp/mysql_data/

    --datadir 参数的路径为mysql数据库文件路径,--databases 表示要设备的库,/tmp/mysql_data/ 备份文件路径

    查看备份文件,如下所示: 

    1. [root@localhost mysql_data]# ls /tmp/mysql_data/
    2. 2022-11-15_23-42-40 2022-11-15_23-44-19
    3. [root@localhost mysql_data]# ls /tmp/mysql_data/2022-11-15_23-42-40/
    4. backup-my.cnf ib_buffer_pool ibdata1 test xtrabackup_binlog_info xtrabackup_checkpoints xtrabackup_info xtrabackup_logfile
    5. [root@localhost mysql_data]# ls /tmp/mysql_data/2022-11-15_23-44-19/
    6. backup-my.cnf ib_buffer_pool ibdata1 mysql performance_schema sys test xtrabackup_binlog_info xtrabackup_checkpoints xtrabackup_info xtrabackup_logfile

    在从库执行还原,需要将在主库备份的文件复制到从库

    innobackupex --datadir=/etc/mysql/data --user=root --password=123456 --host=127.0.0.1 --port=3306  --copy-back /tmp/mysql_data/2022-11-15_23-44-19

    详细介绍可以参考如下:
    https://www.cnblogs.com/f-ck-need-u/p/9018716.html#auto_id_1
    https://www.cnblogs.com/zhoujinyi/p/4088866.html
    命令参数介绍
    https://www.percona.com/doc/percona-xtrabackup/2.1/innobackupex/innobackupex_option_reference.html#cmdoption-innobackupex--parallel

    二、环境搭建

    1、安装docker

    1. yum install epel-release
    2. yum -y install docker
    3. systemctl start docker.service

    2、拉取镜像

    docker pull mysql:5.7

    如出现如下问题:

    1. [root@k3sn1 ~]# docker pull mysql:5.7
    2. Error response from daemon: Head "https://registry-1.docker.io/v2/library/mysql/manifests/5.7": read tcp 172.168.50.144:44414->3.216.34.172:443: read: connection reset by peer

    则需要配置加速器

    网易云加速:https://hub-mirror.c.163.com
    百度去加速:https://mirror.baidubce.com

    1. vi /etc/docker/daemon.json
    2. {
    3.   "registry-mirrors": [
    4.     "https://hub-mirror.c.163.com",
    5.     "https://mirror.baidubce.com"
    6.   ]
    7. }
    8. systemctl daemon-reload
    9. systemctl restart docker

    3、创建主从节

    1. docker run -id --privileged=true -p 3306:3306 --name mysql \
    2. -v /etc/mysql/conf:/etc/mysql/conf.d \
    3. -v /etc/mysql/logs:/var/log/mysql \
    4. -v /etc/mysql/data:/var/lib/mysql \
    5. -e MYSQL_ROOT_PASSWORD=123456 \
    6. -d mysql:5.7

    4、创建从节点

    1. docker run -id --privileged=true -p 3306:3306 --name mysql \
    2. -v /etc/mysql/conf:/etc/mysql/conf.d \
    3. -v /etc/mysql/logs:/var/log/mysql \
    4. -v /etc/mysql/data:/var/lib/mysql \
    5. -e MYSQL_ROOT_PASSWORD=123456 \
    6. -d mysql:5.7

    参数说明:
    –name:容器名,此处命名为mysql
    -e:配置信息,此处配置mysql的root用户的登陆密码
    -p:端口映射,此处映射 主机3306端口 到 容器的3306端口
    -d:后台运行容器,保证在退出终端后容器继续运行
    -v:主机和容器的目录映射关系,":"前为主机目录,之后为容器目录

    5、主库添加配置文件

    1. [root@localhost ~]# cat /etc/mysql/conf/master.cnf
    2. [client]
    3. port = 3306
    4. default-character-set = utf8mb4
    5. [mysql]
    6. port = 3306
    7. default-character-set = utf8mb4
    8. [mysqld]
    9. ##########################
    10. # summary
    11. ##########################
    12. #bind-address = 0.0.0.0
    13. #port = 3306
    14. #datadir=/datavol/mysql/data #数据存储目录,根据实际情况而定,在docker中是指定其他目录了,这个目录没用使用,但是若不是docker的话则需要指定这个配置
    15. ##########################
    16. # log bin
    17. ##########################
    18. server-id = 100 #必须唯一
    19. log_bin = mysql-bin #开启及设置二进制日志文件名称
    20. binlog_format = MIXED
    21. sync_binlog = 1
    22. expire_logs_days =7 #二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
    23. #binlog_cache_size = 128m
    24. #max_binlog_cache_size = 512m
    25. #max_binlog_size = 256M
    26. binlog-do-db = test #要同步的数据库
    27. binlog-ignore-db = mysql #不需要同步的数据库
    28. binlog_ignore_db = information_schema
    29. binlog_ignore_db = performation_schema
    30. binlog_ignore_db = sys
    31. ##########################
    32. # character set
    33. ##########################
    34. character-set-server = utf8mb4
    35. collation-server = utf8mb4_unicode_ci

    6、从库添加配置文件

    1. [root@localhost conf]# cat slave.cnf
    2. [client]
    3. port = 3306
    4. default-character-set = utf8mb4
    5. [mysql]
    6. port = 3306
    7. default-character-set = utf8mb4
    8. [mysqld]
    9. ##########################
    10. # summary
    11. ##########################
    12. #bind-address = 0.0.0.0
    13. #port = 3306
    14. #datadir=/datavol/mysql/data #数据存储目录,根据实际情况而定,在docker中是指定其他目录了,这个目录没用使用,但是若不是docker的话则需要指定这个配置
    15. ##########################
    16. # log bin
    17. ##########################
    18. server-id = 110
    19. ##########################
    20. # character set
    21. ##########################
    22. character-set-server = utf8mb4
    23. collation-server = utf8mb4_unicode_ci

    三、主节点操作

    1. # 进入容器内部
    2. [root@localhost conf]# docker exec -it mysql bash
    3. # 登录mysql
    4. bash-4.2# mysql -uroot -p123456
    5. # 创建mysql账号
    6. grant replication slave on *.* to 'slave_user'@'%' identified by '123456';
    7. # 刷新权限
    8. flush privileges;
    9. # 查看server_id,这个不能和从节点重复
    10. mysql> show global variables like 'server_id';
    11. +---------------+-------+
    12. | Variable_name | Value |
    13. +---------------+-------+
    14. | server_id     | 100   |
    15. +---------------+-------+
    16. 1 row in set (0.00 sec)
    17. # 查看是否启用binlog
    18. mysql> show global variables like 'log_bin';
    19. +---------------+-------+
    20. | Variable_name | Value |
    21. +---------------+-------+
    22. | log_bin       | ON    |
    23. +---------------+-------+
    24. 1 row in set (0.00 sec)
    25. # 查看master状态,注意:mysql-bin.000004、708这两个参数从库会用到
    26. mysql> show master status;
    27. +------------------+----------+--------------+--------------------------------------------------+-------------------+
    28. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                                 | Executed_Gtid_Set |
    29. +------------------+----------+--------------+--------------------------------------------------+-------------------+
    30. | mysql-bin.000004 |      708 | test         | mysql,information_schema,performation_schema,sys |                   |
    31. +------------------+----------+--------------+--------------------------------------------------+-------------------+
    32. 1 row in set (0.00 sec)

    四、从节点操作

    1. # 进入容器内部
    2. [root@localhost conf]# docker exec -it mysql bash
    3. # 登录mysql
    4. bash-4.2# mysql -uroot -p123456
    5. # 配置主从同步
    6. change master to master_host='172.168.50.149',master_port=3306,master_user='slave_user',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=708;
    7. # 开启同步
    8. start slave;
    9. # 查看同步状态
    10. show slave status\G;
    11. mysql> show slave status\G;
    12. *************************** 1. row ***************************
    13.                Slave_IO_State: Waiting for master to send event
    14.                   Master_Host: 172.168.50.149
    15.                   Master_User: slave_user
    16.                   Master_Port: 3306
    17.                 Connect_Retry: 60
    18.               Master_Log_File: mysql-bin.000004
    19.           Read_Master_Log_Pos: 708
    20.                Relay_Log_File: 15b8f6e717ad-relay-bin.000003
    21.                 Relay_Log_Pos: 320
    22.         Relay_Master_Log_File: mysql-bin.000004
    23.              Slave_IO_Running: Yes
    24.             Slave_SQL_Running: Yes
    25.               Replicate_Do_DB: 
    26.           Replicate_Ignore_DB: 
    27.            Replicate_Do_Table: 
    28.        Replicate_Ignore_Table: 
    29.       Replicate_Wild_Do_Table: 
    30.   Replicate_Wild_Ignore_Table: 
    31.                    Last_Errno: 0
    32.                    Last_Error: 
    33.                  Skip_Counter: 0
    34.           Exec_Master_Log_Pos: 708
    35.               Relay_Log_Space: 1254
    36.               Until_Condition: None
    37.                Until_Log_File: 
    38.                 Until_Log_Pos: 0
    39.            Master_SSL_Allowed: No
    40.            Master_SSL_CA_File: 
    41.            Master_SSL_CA_Path: 
    42.               Master_SSL_Cert: 
    43.             Master_SSL_Cipher: 
    44.                Master_SSL_Key: 
    45.         Seconds_Behind_Master: 0
    46. Master_SSL_Verify_Server_Cert: No
    47.                 Last_IO_Errno: 0
    48.                 Last_IO_Error: 
    49.                Last_SQL_Errno: 0
    50.                Last_SQL_Error: 
    51.   Replicate_Ignore_Server_Ids: 
    52.              Master_Server_Id: 100
    53.                   Master_UUID: ca5eab4c-651e-11ed-b768-0242ac110002
    54.              Master_Info_File: /var/lib/mysql/master.info
    55.                     SQL_Delay: 0
    56.           SQL_Remaining_Delay: NULL
    57.       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
    58.            Master_Retry_Count: 86400
    59.                   Master_Bind: 
    60.       Last_IO_Error_Timestamp: 
    61.      Last_SQL_Error_Timestamp: 
    62.                Master_SSL_Crl: 
    63.            Master_SSL_Crlpath: 
    64.            Retrieved_Gtid_Set: 
    65.             Executed_Gtid_Set: 
    66.                 Auto_Position: 0
    67.          Replicate_Rewrite_DB: 
    68.                  Channel_Name: 
    69.            Master_TLS_Version: 
    70. 1 row in set (0.00 sec)
    71. ERROR: 
    72. No query specified

    【Slave_IO_Running】和【Slave_SQL_Running】为Yes,则同步正常。

    五、测试数据同步

    1、在主库执行如下操作

    1. mysql> CREATE DATABASE test;
    2. Query OK, 1 row affected (0.00 sec)
    3. mysql> USE test;
    4. Database changed
    5. # 建表
    6. CREATE TABLE t1 (
    7.     id INT NULL
    8. )
    9. ENGINE=InnoDB
    10. DEFAULT CHARSET=utf8mb4
    11. COLLATE=utf8mb4_general_ci;
    12. # 添加数据
    13. insert into t1(id) value(11)
    14. # 查看
    15. mysql> select * from t1;
    16. +------+
    17. | id   |
    18. +------+
    19. |   11 |
    20. +------+

    2、登录从库验证数据是否同步

    1. mysql> show databases;
    2. +--------------------+
    3. | Database           |
    4. +--------------------+
    5. | information_schema |
    6. | mysql              |
    7. | performance_schema |
    8. | sys                |
    9. | test               |
    10. +--------------------+
    11. 5 rows in set (0.00 sec)
    12. mysql> use test;
    13. Database changed
    14. mysql> show tables;
    15. +----------------+
    16. | Tables_in_test |
    17. +----------------+
    18. | t1             |
    19. +----------------+
    20. 1 row in set (0.00 sec)
    21. mysql> select * from t1;
    22. +------+
    23. | id   |
    24. +------+
    25. |   11 |
    26. +------+
    27. 2 rows in set (0.00 sec)

    参考:MySQL5.7主从同步配置 - 哈喽哈喽111111 - 博客园 

  • 相关阅读:
    ES、kibana、JavaClient详细安装及操作
    Tyvj p1013 找啊找啊找GF
    Python基础学习笔记1(AI Studio)
    企业DevOps之路:Jenkins 流水线
    《猎杀:对决》是适合什么样的人玩 Mac电脑怎么玩《猎杀:对决》
    花 30 美金请 AI 画家弄了个 logo,网友:画得非常好,下次别画了!
    Zabbix登录绕过漏洞复现(CVE-2022-23131)
    12 | 腾讯云代码分析快速部署
    静图表情包怎么做成动态图?动图表情包制作教程
    【深度学习】Pytorch基础
  • 原文地址:https://blog.csdn.net/qq_32109957/article/details/127867734