mysql slave 触发器_利用MariaDB实现主从同步从库(Slave)触发器(trigger)的自动触发

在binlog_format格式为row或者mixed的情况下,mysql主从同步结构下,从库上的触发器对于来自于主从复制的记录变更不会再次触发触发器。但是,在mariadb中,却可以实现这个功能的,mariadb拥有一个全局参数slave_run_triggers_for_rbr,当设置为1时,即开启从库对triggers的触发。

案例:

业务数据库使用MySQL双主架构,MySQL版本:5.7.15-log,根据业务需要,现需对数据库中部分表添加触发器,将数据变更实时同步到redis中(通过UDF实现),因为UDF需额外编译扩展,不适合放到双主服务器上,所以对其中一台主做一台从,通过主从复制,在第三台数据库服务器上实现数据变更到redis的同步。但是,经过实际测试,发现MySQL主从同步的数据不能够触发触发器(binlog_format格式为statement时,从库可以触发),因为线上使用的是mixed格式,所以不能实现。现使用MariaDB,版本10.3.9(mariadb-10.3.9-linux-systemd-x86_64),开启slave_run_triggers_for_rbr,实现触发。

测试版本

Master:MySQL 5.7.15-log

Slave:MariaDB 10.3.9 (mariadb-10.3.9-linux-systemd-x86_64)

参数差异

在实际操作过程中,发现,部分配置参数仅MySQL支持,MariaDB不支持,部分配置参数仅MariaDB支持,MySQL不支持,这里整理如下:

MariaDB不支持的配置

log_timestamps=SYSTEM

master_info_repository = TABLE

relay_log_info_repository = TABLE

gtid_mode = off

enforce_gtid_consistency = 0

internal_tmp_disk_storage_engine = InnoDB

MySQL不支持的配置

slave_run_triggers_for_rbr = 1

MariaDB替换MySQL

MariaDB的初始化

./mysql_install_db --no-defaults --basedir=/opt/mariadb-10.3.9-linux-systemd-x86_64 --datadir=/data/mysql

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !

To do so, start the server, then issue the following commands:

'/opt/mariadb-10.3.9-linux-systemd-x86_64/bin/mysqladmin' -u root password 'new-password'

'/opt/mariadb-10.3.9-linux-systemd-x86_64/bin/mysqladmin' -u root -h 192.168.1.10 password 'new-password'

Alternatively you can run:

'/opt/mariadb-10.3.9-linux-systemd-x86_64/bin/mysql_secure_installation'

which will also give you the option of removing the test

databases and anonymous user created by default. This is

strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the

MySQL manual for more instructions.

You can start the MariaDB daemon with:

cd '/opt/mariadb-10.3.9-linux-systemd-x86_64' ; /opt/mariadb-10.3.9-linux-systemd-x86_64/bin/mysqld_safe --datadir='/data/mysql'

You can test the MariaDB daemon with mysql-test-run.pl

cd '/opt/mariadb-10.3.9-linux-systemd-x86_64/mysql-test' ; perl mysql-test-run.pl

升级步骤

1、通过xtrabackup工具备份MySQL数据

xtrabackup --defaults-file=/etc/my.cnf --no-timestamp --user=root --password=**** --host=192.168.1.11 --target-dir=/data/backup --backup

2、准备数据

xtrabackup --target-dir=/data/backup --prepare

3、将数据复制到/data/mysql目录

4、启动MariaDB

/opt/mysql_server/mysql2/bin/mysqld_safe --defaults-file=/etc/my.cnf &

5、升级,修复部分表差异

./mysql_upgrade -uroot -p*** -h 192.168.1.10 -P 3306

配置文件

低功耗版本/etc/my.cnf

Apache

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

[client]

port=3306

socket=/data/mysql/mysql.sock

[mysql]

prompt="\u@db \R:\m:\s [\d]> "

no-auto-rehash

[mysqld]

user=mysql

port=3306

server-id=33

basedir=/opt/mysql_server/mysql

datadir=/data/mysql

socket=/data/mysql/mysql.sock

pid-file=/data/mysql/mysqld.pid

##log_timestamps=SYSTEM

character-set-server=utf8

skip_name_resolve=1

open_files_limit=65535

back_log=1024

max_connections=512

max_connect_errors=1000000

table_open_cache=1024

table_definition_cache=1024

table_open_cache_instances=64

thread_stack=512K

external-locking=FALSE

max_allowed_packet=32M

sort_buffer_size=4M

join_buffer_size=4M

thread_cache_size=768

query_cache_size=0

query_cache_type=0

interactive_timeout=600

wait_timeout=600

tmp_table_size=32M

max_heap_table_size=32M

slow_query_log=1

slow_query_log_file=/data/mysql/slow.log

log-error=/data/mysql/error.log

long_query_time=0.5

binlog_format=row

log-bin=/data/mysql/mysql-binlog

sync_binlog=1

binlog_cache_size=4M

max_binlog_cache_size=64M

max_binlog_size=256M

expire_logs_days=3

##master_info_repository = TABLE

##relay_log_info_repository = TABLE

###

##gtid_mode = off

##enforce_gtid_consistency = 0

###only mariadb

slave_run_triggers_for_rbr=1

log_bin_trust_function_creators=1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

log_slave_updates=on

relay_log_recovery=1

relay-log-purge=1

key_buffer_size=32M

read_buffer_size=8M

read_rnd_buffer_size=4M

bulk_insert_buffer_size=32M

lock_wait_timeout=3600

explicit_defaults_for_timestamp=1

innodb_thread_concurrency=0

innodb_sync_spin_loops=100

innodb_spin_wait_delay=30

transaction_isolation=REPEATABLE-READ

innodb_buffer_pool_size=2G

innodb_buffer_pool_instances=8

innodb_buffer_pool_load_at_startup=1

innodb_buffer_pool_dump_at_shutdown=1

innodb_data_file_path=ibdata1:12M;ibdata2:16M:autoextend

innodb_flush_log_at_trx_commit=1

innodb_log_buffer_size=32M

innodb_log_file_size=128M

innodb_log_files_in_group=4

innodb_io_capacity=2000

innodb_io_capacity_max=4000

innodb_flush_neighbors=0

innodb_write_io_threads=8

innodb_read_io_threads=8

innodb_purge_threads=4

innodb_page_cleaners=4

innodb_open_files=65535

innodb_max_dirty_pages_pct=50

innodb_flush_method=O_DIRECT

innodb_lru_scan_depth=4000

innodb_checksum_algorithm=crc32

innodb_lock_wait_timeout=10

innodb_rollback_on_timeout=1

innodb_print_all_deadlocks=1

innodb_file_per_table=1

innodb_online_alter_log_max_size=512M

##internal_tmp_disk_storage_engine = InnoDB

innodb_stats_on_metadata=0

innodb_status_file=1

innodb_status_output=0

innodb_status_output_locks=0

performance_schema=1

performance_schema_instrument='%=on'

[mysqldump]

quick

max_allowed_packet=32M

喜欢 (0)or分享 (0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值