mysql5.7 主从复制简单环境搭建以及开启半同步复制等配置
Posted Y1nn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql5.7 主从复制简单环境搭建以及开启半同步复制等配置相关的知识,希望对你有一定的参考价值。
-
准备两台mysql服务器环境
参考
https://blog.csdn.net/qq_42303467/article/details/122137696
-
配置主库 ip 192.168.95.130
修改/etc/下的my.cnf文件
vim /etc/my.cnf
my.cnf 配置如下
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin=mysql-bin #开启binlog功能
server-id=1 #与从机的id不可相同
sync-binlog=1 #每次执行写入操作与磁盘同步
binlog-ignore-db=performance_schema #指定不同步的从库的系统数据库
binlog-ignore-db=information_schema
binlog-ignore-db=sys
#binlog-do-db=mysql #指定只同步的从库的数据库(可以是任意的) 不指定默认全部
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
-
重启主库mysql
systemctl restart mysqld
-
登录主库
mysql -uroot -p
-
配置主库给从库增加同步授权
mysql>
grant replication slave on *.* to 'root'@'%' identified by 'root';
mysql>
grant all privileges on *.* to 'root'@'%' identified by 'root';
mysql>
flush privileges;
-
查看 主库状态
mysql>
show master status;
+------------------+----------+--------------+-------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------+-------------------+
| mysql-bin.000001 | 869 | | performance_schema,information_schema,sys | |
+------------------+----------+--------------+-------------------------------------------+-------------------+
1 row in set (0.00 sec)
-
配置从库 ip192.168.95.132
vim /etc/my.cnf
从库的binlog可以不需要开启
配置如下
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
server-id=2 #设置serverid
relay_log=mysql-relay-bin #指定中继日志
read_only=1 #设置只读
#
#
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
-
重启从库
systemctl restart mysqld
-
登录从库
mysql -uroot -p
-
指定主库ip 端口 账号 密码 等信息
mysql>
change master to master_host='192.168.95.130',master_port=3306,master_user='root',
master_password='root',master_log_file='mysql-bin.000001',master_log_pos=869;
-
开启从库
mysql>
start slave;
-
查看从机状态
mysql>
show slave status \\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.95.130
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 869
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Connecting
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: 869
Relay_Log_Space: 154
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: NULL
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: 0
Master_UUID:
Master_Info_File: /var/lib/mysql/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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
如果出现 error connecting to master 等问题 记得检查 两台机器的防火墙是否都关闭
-
使用主库 验证是否成功
mysql>
create database example;
mysql>use example;
mysql>create table user( id int primary key, uername varchar(20))engine=innodb charset=utf8;
mysql>insert into user values(1,'zhangsan');
-
使用从库查看对应的数据库 表
mysql>create database example;
+--------------------+
| Database |
+--------------------+
| information_schema |
| example |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>use example;
mysql>show tables;
+-------------------+
| Tables_in_example |
+-------------------+
| user |
+-------------------+
1 row in set (0.00 sec)
mysql>select * from user;
+----+----------+
| id | uername |
+----+----------+
| 1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
增加配置半同步复制
登录主库后
安装主库 rpl_semi_sync_master插件
mysql>install plugin rpl_semi_sync_master soname 'semisync_master.so';
查看主库状态
mysql>show variables like '%semi%';
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
这里的rpl_semi_sync_master_timeout 是毫秒 也就是10秒
mysql>set global rpl_semi_sync_master_enabled=1;
#开启
mysql>set global rpl_semi_sync_master_timeout=1000;
#半同步复制延迟的 时间 改为 1秒
登录从库安装semi插件 开启半同步复制
mysql>install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
开启从库semi
mysql>set global rpl_semi_sync_slave_enabled=1;
#从库只需要开启即可
重启从库slave
mysql> stop slave;
mysql> start slave;
确认是否开启半同步复制
在主库中进行一次sql的执行
mysql>insert into user values('2','wnagwu');
再查看是否已经生效
tail -n10 /var/log/mysqld.log
#查看mysqld.log 最后10行的内容
2021-12-25T07:33:02.823631Z 0 [Note] Starting ack receiver thread
2021-12-25T07:42:15.123509Z 4 [Warning] IP address '192.168.95.132' has been resolved to the host name '192.168.95.132', which resembles IPv4-address itself.
2021-12-25T07:42:15.127760Z 4 [Note] While initializing dump thread for slave with UUID <0903f91a-64cf-11ec-9491-00505639e6c9>, found a zombie dump thread with the same UUID. Master is killing the zombie dump thread(3).
2021-12-25T07:42:15.127922Z 3 [Note] Stop asynchronous binlog_dump to slave (server_id: 2)
2021-12-25T07:42:15.127932Z 4 [Note] Start binlog_dump to master_thread_id(4) slave_server(2), pos(mysql-bin.000004, 991)
2021-12-25T07:42:15.128023Z 4 [Note] Start semi-sync binlog_dump to slave (server_id: 2), pos(mysql-bin.000004, 991)
可以看到 我们这个 semi-sync 是已经开启了
设置 并行复制 组提交
登录主库
mysql>show variables like '%binlog_group%';
+-----------------------------------------+-------+
| Variable_name | Value |
+-----------------------------------------+-------+
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
+-----------------------------------------+-------+
mysql>set global binlog_group_commit_sync_delay=1000;
#组提交的延迟设为 1s
mysql>set global binlog_group_commit_sync_no_delay_count=100;
#每组最大的提交数 设为100
登录从库
mysql>show variables like '%slave_parallel%';
#默认是基于库的 我们要改成基于组的
+------------------------+----------+
| Variable_name | Value |
+------------------------+----------+
| slave_parallel_type | DATABASE |
| slave_parallel_workers | 0 |
+------------------------+----------+
mysql>stop slave;
#先将slave停止 否则会报错
mysql>set global slave_parallel_type='LOGICAL_CLOCK';
mysql>set global slave_parallel_workers=8;
#并行的线程数 最大8
设置relay_log 日志配置
mysql>show variables like '%relay_log%';
+---------------------------+--------------------------------------+
| Variable_name | Value |
+---------------------------+--------------------------------------+
| max_relay_log_size | 0 |
| relay_log | mysql-relay-bin |
| relay_log_basename | /var/lib/mysql/mysql-relay-bin |
| relay_log_index | /var/lib/mysql/mysql-relay-bin.index |
| relay_log_info_file | relay-log.info |
| relay_log_info_repository | FILE |
| relay_log_purge | ON |
| relay_log_recovery | OFF |
| relay_log_space_limit | 0 |
| sync_relay_log | 10000 |
| sync_relay_log_info | 10000 |
+---------------------------+--------------------------------------+
mysql>set global relay_log_recovery=1;
#这个文件是只读的 需要进入配置文件修改
需要 vim /etc/my.cnf
增加 relay_log_recovery=1
以及relay_log_info_repository='TABLE'
参数
设置relay_log_info_repository为 table 性能最大化 开启并行复制后 资源竞争变大
也可以之前 使用set global 配置的参数统一使用配置文件重新配置
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
server-id=2 #设置serverid
relay_log=mysql-relay-bin #指定中继日志
read_only=1 #设置只读
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=8 #并行最大线程数 8
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=1 #开启 recovery
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
重启服务 确认 之前配置的参数是否生效
systemctl restart mysqld
以上是关于mysql5.7 主从复制简单环境搭建以及开启半同步复制等配置的主要内容,如果未能解决你的问题,请参考以下文章