签到成功

知道了

CNDBA社区CNDBA社区

TDSQL 集群2个noshard 实例之间搭建主从复制环境(命令行) 操作手册

2021-07-31 11:30 2336 0 原创 TDSQL
作者: dave

1 环境说明

主从复试是MySQL 读写分离和容灾的一个重要方式, 在TDSQL 集群中,对于每个分片(sets)可以指定主从复制的级别。 那么在TDSQL 集群中,不同的noshard之间,也可以搭建主从复制的环境。 这里面同样涉及2个步骤:

  1. 全库数据的同步。
  2. 增量数据的同步。

在之前的博客有介绍相关的技术,如下:

MariaDB 主从复制 搭建手册
https://www.cndba.cn/dave/article/2659

TDSQL 集群 数据迁移工具 操作说明
https://www.cndba.cn/dave/article/4624

本篇我们来看下2个NoShard之间主从复制环境的搭建。

我们这里用set*_18 和 set*_76 来个来搭建,18 为主库,实际上,每个set 又是一个一主两从的主从复制环境。


2 主库配置

2.1 检查配置文件:

TDSQL 集群中DB的配置文件是和端口对应的。 在之前的截图中我们我们主库的端口是4002, 备库是4008。

[dave@www.cndba.cn  percona-5.7.17]# ps -ef|grep 4002.cnf
tdsql     8165     1  0 Jul29 ?        00:00:01 /bin/sh ./bin/mysqld_safe --defaults-file=/data/tdsql_run/4002/percona-5.7.17/etc/my_4002.cnf --user=tdsql
tdsql    27176  8165  3 Jul29 ?        00:40:23 ./bin/mysqld --defaults-file=/data/tdsql_run/4002/percona-5.7.17/etc/my_4002.cnf --basedir=. --datadir=/data1/tdengine/data/4002/dbdata_raw/data --plugin-dir=/data/tdsql_run/4002/percona-5.7.17/lib/mysql/plugin --log-error=/data1/tdengine/log/4002/dblogs/mysqld.err --open-files-limit=100000 --pid-file=/data1/tdengine/data/4002/prod/mysql.pid --socket=/data1/tdengine/data/4002/prod/mysql.sock --port=4002
root     44199  7599  0 21:14 pts/0    00:00:00 grep --color=auto 4002.cnf
You have new mail in /var/spool/mail/root
[dave@www.cndba.cn  percona-5.7.17]#

检查配置文件:/data/tdsql_run/4002/percona-5.7.17/etc/my_4002.cnfhttp://www.cndba.cn/dave/article/4638http://www.cndba.cn/dave/article/4638

在主从复制中,我们需要配置配置如下2个参数:

[dave@www.cndba.cn  percona-5.7.17]# grep -E 'server-id|log-bin' /data/tdsql_run/4002/percona-5.7.17/etc/my_4002.cnf
server-id=   524113826
#replicate-same-server-id=1 #in circle master,may be dead loop
log-bin  = /data1/tdengine/log/4002/dblogs/bin/binlog.log
[dave@www.cndba.cn  percona-5.7.17]#

因为我们TDSQL 的主库环境已经是主从复制,所以这2个参数已经配置过了,我们只需要确认一下即可。

如果没有配置,那么修改后需要重启DB, TDSQL 集群重启命令如下:

http://www.cndba.cn/dave/article/4638

[dave@www.cndba.cn  install]# pwd
/data/tdsql_run/4002/percona-5.7.17/install
[dave@www.cndba.cn  install]# ./restartmysql_cgroup.sh 4002

2.2 创建slave 用户

在赤兔平台创建slave 用户,并赋权:replication slave。http://www.cndba.cn/dave/article/4638

TDSQL 集群的用户创建和赋权需要在赤兔平台进行,具体说明参考:

TDSQL集群 数据库 命令行 创建用户失败 说明
https://www.cndba.cn/dave/article/4635

2.3 检查主数据库的master状态:

[dave@www.cndba.cn  percona-5.7.17]# mysql -htdsql1 -udave -pdave -P4002
Welcome to the MariaDB monitor.  Commands end with ; or /g.
Your MySQL connection id is 197964
Server version: 5.7.17-11-V2.0R540D002-20191226-1152-log Source distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.

MySQL [(none)]> show master status;
+---------------+----------+--------------+------------------+-----------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                             |
+---------------+----------+--------------+------------------+-----------------------------------------------+
| binlog.000002 | 38968413 |              |                  | 3422532c-eee2-11eb-8a1b-000c29be8288:1-197251 |
+---------------+----------+--------------+------------------+-----------------------------------------------+
1 row in set (0.00 sec)

MySQL [(none)]>

3 备份主库到从库

数据迁移的详细说明参考之前的博客,如下:

TDSQL 集群 数据迁移工具 操作说明
https://www.cndba.cn/dave/article/4624

我们这里使用mydumper和myloader 工具来执行,工具在如下目录:

http://www.cndba.cn/dave/article/4638

/data/home/tdsql/tdsqlinstall/mysqlagent/bin

3.1 mydumper导出主库

[dave@www.cndba.cn  bin]# rm -rf /data/mydumper/
[dave@www.cndba.cn  bin]# cd /data/home/tdsql/tdsqlinstall/mysqlagent/bin

[dave@www.cndba.cn  bin]# ./mydumper --host=192.168.31.61 --port=4002 --user=dave --password=dave --events --routines --triggers --less-locking --ignore-sysdb=1 --chunk-filesize=1024 --complete-insert --outputdir=/data/mydumper

3.2 showmyloader导入备库

[dave@www.cndba.cn  mydumper]# mv metadata.partial metadata
[dave@www.cndba.cn  mydumper]# rm -rf sys.*

[dave@www.cndba.cn  bin]# ./myloader --host=192.168.31.61 --port=4008 --user=dave --password=dave --directory=/data/mydumper/ --enable-binlog --overwrite-tables

4 从库配置

我们这里的从库实际上是另一个sets的主库,我们先设置。

4.1 检查配置文件

这里需要配置的三个参数如下,实际上我们这里已经配置过了:http://www.cndba.cn/dave/article/4638

[dave@www.cndba.cn  percona-5.7.17]# grep -E 'server-id|log-bin|relay-log' /data/tdsql_run/4008/percona-5.7.17/etc/my_4008.cnf
server-id=   524110504
#replicate-same-server-id=1 #in circle master,may be dead loop
log-bin  = /data1/tdengine/log/4008/dblogs/bin/binlog.log
relay-log = /data1/tdengine/log/4008/dblogs/relay/relay.log
You have new mail in /var/spool/mail/root
[dave@www.cndba.cn  percona-5.7.17]#

如果没有配置,那么修改后需要重启DB, TDSQL 集群重启命令如下:

[dave@www.cndba.cn  install]# pwd
/data/tdsql_run/4008/percona-5.7.17/install
[dave@www.cndba.cn  install]# ./restartmysql_cgroup.sh 4008

4.2 赤兔设置一切免切

这里必须现在赤兔平台对从库设置一切免切,并且将同步改成异步。http://www.cndba.cn/dave/article/4638

然后重启从库:

[dave@www.cndba.cn  install]# pwd
/data/tdsql_run/4008/percona-5.7.17/install
You have new mail in /var/spool/mail/root
[dave@www.cndba.cn  install]#

[dave@www.cndba.cn  install]# ./restartmysql_cgroup.sh 4008

4.3 命令行配置从库

确认binlog 和对应的位置:

[dave@www.cndba.cn  mydumper]# pwd
/data/mydumper
[dave@www.cndba.cn  mydumper]# ls
cndba.cndba-schema.sql   dave.dave-schema.sql    dump_progress.info  sys-schema-create.sql
cndba.cndba.sql          dave.dave.sql           load_progress.info  sys-schema-post.sql
cndba-schema-create.sql  dave-schema-create.sql  metadata            test-schema-create.sql

[dave@www.cndba.cn  mydumper]# cat metadata
Started dump at: 2021-07-30 23:51:11
SHOW MASTER STATUS:
        Log: binlog.000002
        Pos: 42999710
        GTID:3422532c-eee2-11eb-8a1b-000c29be8288:1-208534

[dave@www.cndba.cn  mydumper]#


mysql> show master status;
+---------------+----------+--------------+------------------+-----------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                             |
+---------------+----------+--------------+------------------+-----------------------------------------------+
| binlog.000002 | 44231066 |              |                  | 3422532c-eee2-11eb-8a1b-000c29be8288:1-211978 |
+---------------+----------+--------------+------------------+-----------------------------------------------+
1 row in set (0.00 sec)

注意这里需要使用jmysql.sh 来执行,否则会报如下错误:

TDSQL 集群 SUPER privilege(s) for this operation 错误解决方法
https://www.cndba.cn/dave/article/4636

http://www.cndba.cn/dave/article/4638

在备库设置slave:

[dave@www.cndba.cn  install]# ./jmysql.sh 4008
cmd: e
/data1/tdengine/data/4008/prod/mysql.sock
/data/tdsql_run/4008/percona-5.7.17
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 1589
Server version: 5.7.17-11-V2.0R540D002-20191226-1152-log Source distribution

Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.


mysql> change master to master_host='192.168.31.61',master_port=4002,master_user='slave',master_password='slave',master_log_file='binlog.000001',master_log_pos=3414767 FOR CHANNEL "master_1";
Query OK, 0 rows affected, 2 warnings (0.23 sec)

注意这里必须加上 FOR CHANNEL “master_1”; 否则会配置失败,具体参考:http://www.cndba.cn/dave/article/4638

TDSQL 集群 noshard 实例之间搭建 主从复制 The server is not configured as slave 解决方法
https://www.cndba.cn/dave/article/4637

启动slave:

mysql> start slave FOR CHANNEL "master_1";
Query OK, 0 rows affected (0.35 sec)

mysql> show slave status /G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.31.61
                  Master_User: slave
                  Master_Port: 4002
                Connect_Retry: 60
              Master_Log_File: binlog.000001
          Read_Master_Log_Pos: 3436217
               Relay_Log_File: relay-master_1.000002
                Relay_Log_Pos: 21759
        Relay_Master_Log_File: binlog.000001
             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: 3436217
              Relay_Log_Space: 21957
              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: 524113826
                  Master_UUID: 3422532c-eee2-11eb-8a1b-000c29be8288
             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: 3422532c-eee2-11eb-8a1b-000c29be8288:9552-9611
            Executed_Gtid_Set: 3422532c-eee2-11eb-8a1b-000c29be8288:4620-4621:9552-9611,
c71da6a5-eee9-11eb-9a98-000c29be8288:1-9523
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name: master_1
           Master_TLS_Version:
1 row in set (0.00 sec)

mysql>

5 验证同步

5.1 主库:

mysql> use cndba;
Database changed
mysql> show tables;
+-----------------+
| Tables_in_cndba |
+-----------------+
| cndba           |
+-----------------+
1 row in set (0.00 sec)

mysql> create table ustc select * from cndba;
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
mysql> create table ustc as select * from cndba;
ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
mysql> select * from cndba;
+---+------+
| a | b    |
+---+------+
| 1 |    2 |
+---+------+
1 row in set (0.08 sec)

mysql> insert into cndba(a,b) values(2,2);
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> create table ustc(a int key,b int);
Query OK, 0 rows affected (0.07 sec)

mysql> insert into ustc(a,b) values(2,2);
Query OK, 1 row affected (0.03 sec)

mysql> insert into ustc(a,b) values(1,2);
Query OK, 1 row affected (0.04 sec)

5.2 从库:

[dave@www.cndba.cn  install]# ./jmysql.sh 4008
cmd: e
/data1/tdengine/data/4008/prod/mysql.sock
/data/tdsql_run/4008/percona-5.7.17
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 16350
Server version: 5.7.17-11-V2.0R540D002-20191226-1152-log Source distribution

Copyright (c) 2009-2016 Percona LLC and/or its affiliates
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.

mysql> use cndba;
Database changed
mysql> select * from cndba;
+---+------+
| a | b    |
+---+------+
| 1 |    2 |
| 2 |    2 |
+---+------+
2 rows in set (0.00 sec)

mysql>

mysql> select * from ustc;
+---+------+
| a | b    |
+---+------+
| 1 |    2 |
| 2 |    2 |
+---+------+
2 rows in set (0.00 sec)

mysql>

同步成功。 http://www.cndba.cn/dave/article/4638

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2261
    原创
  • 3
    翻译
  • 578
    转载
  • 191
    评论
  • 访问:7970424次
  • 积分:4346
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ