张元兵 发表于 2023-4-23 20:11:15

mysql+proxysql+replication-manager的主从半同步复制+高可用+读写分离

环境:
AlmaLinux release 9.1
MySQL Community Server Ver 8.0.33
Replication Manager v2.2.40 for MariaDB 10.x and MySQL 5.7 Series
ProxySQL version 2.5.1-90-gbedaa6c
主机分配情况:

采用hyper-v创建虚拟机的方式进行的,创建1台模板之后另外3台导入虚拟机复制。
1、安装mysql
mysql8的默认加密插件变为了caching_sha2_password需要修改成mysql_native_password,因为proxysql不支持caching_sha2_password
安装完成后在server01、02、03上创建以下用户
rep-manager供replication-manager使用,repl供主从复制使用
mysql> create user 'rep-manager'@'%' identified by 'your password';
Query OK, 0 rows affected (0.01 sec)

mysql> create user 'repl'@'%' identified by 'your password';
Query OK, 0 rows affected (0.02 sec)

mysql> create user 'proxysql'@'%' identified by 'your password';
Query OK, 0 rows affected (0.02 sec)

mysql> grant process,replication slave,replication client on *.* to 'proxysql'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> grant select,replication slave,replication client,reload,super on *.* to 'repl'@'%';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> grant all privileges on *.* to 'rep-manager'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec) 
2、复制虚拟机
3、在server00上安装replication-manager
4、在server00上安装proxysql
5、启动mysql,创建主从半同步复制
半同步需要安装插件,在主库和从库上都安装
mysql> install plugin rpl_semi_sync_source soname 'semisync_source.so';
mysql> install plugin rpl_semi_sync_replica soname 'semisync_replica.so';
mysql> show plugins;<br>+----------------------------------+----------+--------------------+---------------------+---------+<br>| Name                             | Status   | Type               | Library             | License |<br>+----------------------------------+----------+--------------------+---------------------+---------+<br>| binlog                           | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| mysql_native_password            | ACTIVE   | AUTHENTICATION     | NULL                | GPL     |<br>| sha256_password                  | ACTIVE   | AUTHENTICATION     | NULL                | GPL     |<br>| caching_sha2_password            | ACTIVE   | AUTHENTICATION     | NULL                | GPL     |<br>| sha2_cache_cleaner               | ACTIVE   | AUDIT              | NULL                | GPL     |<br>| daemon_keyring_proxy_plugin      | ACTIVE   | DAEMON             | NULL                | GPL     |<br>| CSV                              | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| MEMORY                           | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| InnoDB                           | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| INNODB_TRX                       | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CMP                       | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CMP_RESET                 | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CMPMEM                    | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CMPMEM_RESET              | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CMP_PER_INDEX             | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CMP_PER_INDEX_RESET       | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_BUFFER_PAGE               | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_BUFFER_PAGE_LRU           | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_BUFFER_POOL_STATS         | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_TEMP_TABLE_INFO           | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_METRICS                   | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_FT_DEFAULT_STOPWORD       | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_FT_DELETED                | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_FT_BEING_DELETED          | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_FT_CONFIG                 | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_FT_INDEX_CACHE            | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_FT_INDEX_TABLE            | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_TABLES                    | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_TABLESTATS                | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_INDEXES                   | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_TABLESPACES               | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_COLUMNS                   | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_VIRTUAL                   | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_CACHED_INDEXES            | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| INNODB_SESSION_TEMP_TABLESPACES  | ACTIVE   | INFORMATION SCHEMA | NULL                | GPL     |<br>| MyISAM                           | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| MRG_MYISAM                       | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| PERFORMANCE_SCHEMA               | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| TempTable                        | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| ARCHIVE                          | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| BLACKHOLE                        | ACTIVE   | STORAGE ENGINE     | NULL                | GPL     |<br>| FEDERATED                        | DISABLED | STORAGE ENGINE     | NULL                | GPL     |<br>| ndbcluster                       | DISABLED | STORAGE ENGINE     | NULL                | GPL     |<br>| ndbinfo                          | DISABLED | STORAGE ENGINE     | NULL                | GPL     |<br>| ndb_transid_mysql_connection_map | DISABLED | INFORMATION SCHEMA | NULL                | GPL     |<br>| ngram                            | ACTIVE   | FTPARSER           | NULL                | GPL     |<br>| mysqlx_cache_cleaner             | ACTIVE   | AUDIT              | NULL                | GPL     |<br>| mysqlx                           | ACTIVE   | DAEMON             | NULL                | GPL     |<br>| rpl_semi_sync_source             | ACTIVE   | REPLICATION        | semisync_source.so  | GPL     |<br>| rpl_semi_sync_replica            | ACTIVE   | REPLICATION        | semisync_replica.so | GPL     |<br>+----------------------------------+----------+--------------------+---------------------+---------+<br>50 rows in set (0.00 sec)<br>要想永久启用半同步需将配置写入my.cnf看下面的配置即可。<br>在主库上查看是否启用了半同步<br> mysql> show variables like 'rpl_semi%';<br>+---------------------------------------------+------------+<br>| Variable_name                               | Value      |<br>+---------------------------------------------+------------+<br>| rpl_semi_sync_replica_enabled               | ON        |<br>| rpl_semi_sync_replica_trace_level           | 32         |<br>| rpl_semi_sync_source_enabled                | ON         |<br>| rpl_semi_sync_source_timeout                | 10000      |<br>| rpl_semi_sync_source_trace_level            | 32         |<br>| rpl_semi_sync_source_wait_for_replica_count | 1          |<br>| rpl_semi_sync_source_wait_no_replica        | ON         |<br>| rpl_semi_sync_source_wait_point             | AFTER_SYNC |<br>+---------------------------------------------+------------+<br>从库上<br>mysql> show variables like 'rpl_semi%';<br>+---------------------------------------------+------------+<br>| Variable_name                               | Value      |<br>+---------------------------------------------+------------+<br>| rpl_semi_sync_replica_enabled               | ON         |<br>| rpl_semi_sync_replica_trace_level           | 32         |<br>| rpl_semi_sync_source_enabled                | ON         |<br>| rpl_semi_sync_source_timeout                | 10000      |<br>| rpl_semi_sync_source_trace_level            | 32         |<br>| rpl_semi_sync_source_wait_for_replica_count | 1          |<br>| rpl_semi_sync_source_wait_no_replica        | ON         |<br>| rpl_semi_sync_source_wait_point             | AFTER_SYNC |<br>+---------------------------------------------+------------+<br>8 rows in set (0.01 sec)master主机mysql配置my.cnf如下
# For advice on how to change settings please see<br># http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html<br><br><br>#<br># Remove leading # and set to the amount of RAM for the most important data<br># cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.<br># innodb_buffer_pool_size = 128M<br>#<br># Remove the leading "# " to disable binary logging<br># Binary logging captures changes between backups and is enabled by<br># default. It's default setting is log_bin=binlog<br># disable_log_bin<br>#<br># Remove leading # to set options mainly useful for reporting servers.<br># The server defaults are faster for transactions and fast SELECTs.<br># Adjust sizes as needed, experiment to find the optimal values.<br># join_buffer_size = 128M<br># sort_buffer_size = 2M<br># read_rnd_buffer_size = 2M<br>#<br># Remove leading # to revert to previous value for default_authentication_plugin,<br># this will increase compatibility with older clients. For background, see:<br># https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin<br># default-authentication-plugin=mysql_native_password<br><br>datadir=/var/lib/mysql<br>socket=/var/lib/mysql/mysql.sock<br><br>log-error=/var/log/mysqld.log<br>pid-file=/var/run/mysqld/mysqld.pid<br><br>default-authentication-plugin=mysql_native_password<br><br>######replication settings######<br>server-id=1001<br>log-bin=mysql-bin<br>binlog_format=row<br><br>binlog-ignore-db=mysql<br>binlog-ignore-db=information_schema<br>binlog-ignore-db=performance_schema<br>binlog-ignore-db=sys<br><br>######gtid#######<br>gtid_mode=on<br>enforce_gtid_consistency=on<br>binlog_gtid_simple_recovery=on<br><br>relay_log_recovery = ON<br>relay-log-index=mysql-relay<br>relay-log=mysql-relay<br><br>loose_rpl_semi_sync_source_enabled = ON<br>loose_rpl_semi_sync_replica_enabled = ON<br><br>log_slow_replica_statements = 1slave主机myql的配置如下:
# For advice on how to change settings please see<br># http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html<br><br><br>#<br># Remove leading # and set to the amount of RAM for the most important data<br># cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.<br># innodb_buffer_pool_size = 128M<br>#<br># Remove the leading "# " to disable binary logging<br># Binary logging captures changes between backups and is enabled by<br># default. It's default setting is log_bin=binlog<br># disable_log_bin<br>#<br># Remove leading # to set options mainly useful for reporting servers.<br># The server defaults are faster for transactions and fast SELECTs.<br># Adjust sizes as needed, experiment to find the optimal values.<br># join_buffer_size = 128M<br># sort_buffer_size = 2M<br># read_rnd_buffer_size = 2M<br>#<br># Remove leading # to revert to previous value for default_authentication_plugin,<br># this will increase compatibility with older clients. For background, see:<br># https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin<br># default-authentication-plugin=mysql_native_password<br><br>datadir=/var/lib/mysql<br>socket=/var/lib/mysql/mysql.sock<br><br>log-error=/var/log/mysqld.log<br>pid-file=/var/run/mysqld/mysqld.pid<br><br>default-authentication-plugin=mysql_native_password<br><br>######replication settings######<br>server-id=1002<br>log-bin=mysql-bin<br>binlog_format=row<br>log_slave_updates=on<br>relay_log_recovery=1<br><br>binlog-ignore-db=mysql<br>binlog-ignore-db=information_schema<br>binlog-ignore-db=performance_schema<br>binlog-ignore-db=sys<br><br>######gtid#######<br>gtid_mode=on<br>enforce_gtid_consistency=on<br>binlog_gtid_simple_recovery=on<br><br>relay_log_recovery = ON<br>relay-log-index=mysql-relay<br>relay-log=mysql-relay<br><br>loose_rpl_semi_sync_source_enabled = ON<br>loose_rpl_semi_sync_replica_enabled = ON<br><br>log_slow_replica_statements = 1<br><br>read_only = on<br>启动主从
mysql> CHANGE REPLICATION SOURCE to SOURCE_HOST='server01.mshome.net',SOURCE_USER='repl',SOURCE_PASSWORD='your password',SOURCE_PORT=3306;<br>Query OK, 0 rows affected, 3 warnings (0.03 sec)<br><br>mysql> start replica;<br>Query OK, 0 rows affected (0.02 sec)<br><br>mysql> show replica status\G;<br>*************************** 1. row ***************************<br>             Replica_IO_State: Waiting for source to send event<br>                  Source_Host: server01.mshome.net<br>                  Source_User: repl<br>                  Source_Port: 3306<br>                Connect_Retry: 60<br>              Source_Log_File: mysql-bin.000009<br>          Read_Source_Log_Pos: 157<br>               Relay_Log_File: mysql-relay.000010<br>                Relay_Log_Pos: 373<br>        Relay_Source_Log_File: mysql-bin.000009<br>           Replica_IO_Running: Yes<br>          Replica_SQL_Running: Yes<br>              Replicate_Do_DB: <br>          Replicate_Ignore_DB: <br>           Replicate_Do_Table: <br>       Replicate_Ignore_Table: <br>      Replicate_Wild_Do_Table: <br>  Replicate_Wild_Ignore_Table: <br>                   Last_Errno: 0<br>                   Last_Error: <br>                 Skip_Counter: 0<br>          Exec_Source_Log_Pos: 157<br>              Relay_Log_Space: 795<br>              Until_Condition: None<br>               Until_Log_File: <br>                Until_Log_Pos: 0<br>           Source_SSL_Allowed: No<br>           Source_SSL_CA_File: <br>           Source_SSL_CA_Path: <br>              Source_SSL_Cert: <br>            Source_SSL_Cipher: <br>               Source_SSL_Key: <br>        Seconds_Behind_Source: 0<br>Source_SSL_Verify_Server_Cert: No<br>                Last_IO_Errno: 0<br>                Last_IO_Error: <br>               Last_SQL_Errno: 0<br>               Last_SQL_Error: <br>  Replicate_Ignore_Server_Ids: <br>             Source_Server_Id: 1001<br>                  Source_UUID: 590af332-e04e-11ed-8935-00155d030202<br>             Source_Info_File: mysql.slave_master_info<br>                    SQL_Delay: 0<br>          SQL_Remaining_Delay: NULL<br>    Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates<br>           Source_Retry_Count: 86400<br>                  Source_Bind: <br>      Last_IO_Error_Timestamp: <br>     Last_SQL_Error_Timestamp: <br>               Source_SSL_Crl: <br>           Source_SSL_Crlpath: <br>           Retrieved_Gtid_Set: <br>            Executed_Gtid_Set: <br>                Auto_Position: 0<br>         Replicate_Rewrite_DB: <br>                 Channel_Name: <br>           Source_TLS_Version: <br>       Source_public_key_path: <br>        Get_Source_public_key: 1<br>            Network_Namespace: <br>1 row in set (0.00 sec)<br><br>ERROR: <br>No query specified server00主机上安装了replication-manager和proxysql
replication-manager的配置如下
查看replication-manager配置文件cluster1.toml<br>cat /etc/replication-manager/cluster.d/cluster1.toml
title = "cluster1"
prov-orchestrator = "onpremise"
prov-db-tags = "innodb,noquerycache,slow,pfs,pkg,linux,smallredolog,logtotable"
prov-db-memory = "256"
prov-db-memory-shared-pct = "threads:16,innodb:60,myisam:10,aria:10,rocksdb:1,tokudb:1,s3:1,archive:1,querycache:0"
prov-db-disk-size = "1"
prov-db-cpu-cores = "1"
prov-db-disk-iops = "300"

db-servers-hosts = "server01.mshome.net:3306,server02.mshome.net:3306,server03.mshome.net:3306"
db-servers-prefered-master = "server01.mshome.net:3306"
db-servers-credential = "rep-manager:yourpassword"
db-servers-connect-timeout = 5
replication-credential = "rep-manager:yourpassword"


verbose = false
log-failed-election= true
log-level = 1
log-rotate-max-age = 7
log-rotate-max-backup = 7
log-rotate-max-size = 5
log-sql-in-monitoring   = true
log-sst = true

##############
## TOPOLOGY ##
##############

replication-multi-master = false
replication-multi-tier-slave = false

############
# BACKUPS ##
###########


backup-streaming = false
backup-streaming-aws-access-key-id = "admin"
backup-streaming-aws-access-secret = "xxxx"
backup-streaming-endpoint= "https://s3.signal18.io/"
backup-streaming-region= "fr-1"
backup-streaming-bucket= "repman"<br><br>
#####这里可以设置备份工具restic,工具可以跨服务器,结合minio备份就很爽了,后续再介绍吧
backup-restic = false
backup-restic-aws =false
backup-physical-type = "mariabackup"
backup-logical-type = "mysqldump"
backup-restic-aws-access-secret = "xxxx"
backup-restic-password = "xxxx"
backup-restic-binary-path = "/usr/bin/restic"

monitoring-scheduler = true
scheduler-db-servers-logical-backup= true
scheduler-db-servers-logical-backup-cron= "0 30 23 * * 6"
scheduler-db-servers-logs   =false
scheduler-db-servers-logs-cron = "0 0 * * * *"
scheduler-db-servers-logs-table-keep = 4
scheduler-db-servers-logs-table-rotate= false
scheduler-db-servers-logs-table-rotate-cron = "0 0 0/6 * * *"
scheduler-db-servers-optimize= false
scheduler-db-servers-optimize-cron = "0 0 3 1 * 5"
scheduler-db-servers-physical-backup = true
scheduler-db-servers-physical-backup-cron = "0 30 23 * * *"

##############
## FAILOVER ##
##############

failover-mode = "manual"
failover-pre-script = ""
failover-post-script = ""

## Slaves will re enter with read-only

failover-readonly-state = true
failover-event-scheduler = false
failover-event-status = false

## Failover after N failures detection

failover-falsepositive-ping-counter = 5

## Cancel failover if already N failover
## Cancel failover if last failover was N seconds before
## Cancel failover in semi-sync when one slave is not in sync
## Cancel failover if one slave receive master heartbeat
## Cancel failover when replication delay is more than N seconds

failover-limit = 0
failover-time-limit = 0
failover-at-sync = false
failover-max-slave-delay = 30
failover-restart-unsafe = false

# failover-falsepositive-heartbeat = true
# failover-falsepositive-heartbeat-timeout = 3
# failover-falsepositive-maxscale = false
# failover-falsepositive-maxscale-timeout = 14
# failover-falsepositive-external = false
# failover-falsepositive-external-port = 80

################
## SWITCHOVER ##
################

## In switchover Wait N milliseconds before killing long running transactions
## Cancel switchover if transaction running more than N seconds
## Cancel switchover if write query running more than N seconds
## Cancel switchover if one of the slaves is not synced based on GTID equality

switchover-wait-kill = 5000
switchover-wait-trx = 10
switchover-wait-write-query = 10
switchover-at-equal-gtid = false
switchover-at-sync = false
switchover-max-slave-delay = 30

############
## REJOIN ##
############

autorejoin = true
autorejoin-script = ""
autorejoin-semisync = true
autorejoin-backup-binlog = true
autorejoin-flashback = false
autorejoin-mysqldump = false

####################
## CHECKS & FORCE ##
####################

check-replication-filters = true
check-binlog-filters = true
check-replication-state = true

force-slave-heartbeat= false
force-slave-heartbeat-retry = 5
force-slave-heartbeat-time = 3
force-slave-gtid-mode = false
force-slave-semisync = false
force-slave-failover-readonly-state = false
force-binlog-row = false
force-binlog-annotate = false
force-binlog-slowqueries = false
force-binlog-compress = false
force-binlog-checksum = false
force-inmemory-binlog-cache-size = false
force-disk-relaylog-size-limit = false
force-disk-relaylog-size-limit-size = 1000000000
force-sync-binlog = false
force-sync-innodb = false

##############
## MAXSCALE ##
##############

## for 2 nodes cluster maxscale can be driven by replication manager

maxscale = false
maxscale-binlog = false
maxscale-servers = "192.168.0.201"
maxscale-port = 4003
maxscale-user = "admin"
maxscale-pass = "mariadb"

## When true replication manager drive maxscale server state
## Not required unless multiple maxscale or release does not support detect_stale_slave

maxscale-disable-monitor = false

## maxinfo|maxadmin

maxscale-get-info-method = "maxadmin"
maxscale-maxinfo-port = 4002

maxscale-write-port = 4007
maxscale-read-port = 4008
maxscale-read-write-port = 4006
maxscale-binlog-port = 4000

#############
## HAPROXY ##
#############

## Wrapper mode unless maxscale or proxysql required to be located with replication-manager

haproxy = false
haproxy-binary-path = "/usr/sbin/haproxy"

## Read write traffic
## Read only load balance least connection traffic
haproxy-write-port = 3306
haproxy-read-port = 3307

####################
## SHARDING PROXY ##
####################

mdbshardproxy = false
mdbshardproxy-hosts = "127.0.0.1:3306"
mdbshardproxy-user = "root:mariadb"

#################################
###### proxysql settings ########
#################################
proxysql = true
proxysql-servers = "127.0.0.1"
proxysql-port = "6033"
proxysql-admin-port = "6032"
proxysql-writer-hostgroup = "1000"
proxysql-reader-hostgroup = "1001"
proxysql-user = "admin"
proxysql-password = "admin"
proxysql-bootstrap = false
proxysql-bootstrap-users = false #不从master复制用户到proxysql,因为复制会出问题查看replication-manager配置文件config.toml<br>cat /etc/replication-manager/config.toml

include = "/etc/replication-manager/cluster.d"

monitoring-save-config = false
monitoring-datadir = "/var/lib/replication-manager"
#monitoring-sharedir = "/usr/share/replication-manager"

## Timeout in seconds between consecutive monitoring

monitoring-ticker = 2

#########
## LOG ##
#########

log-file = "/var/log/replication-manager.log"
log-heartbeat = false
log-syslog = false



#################
## ARBITRATION ##
#################

arbitration-external = false
arbitration-external-secret = "13787932529099014144"
arbitration-external-hosts = "88.191.151.84:80"
arbitration-peer-hosts ="127.0.0.1:10002"

## Unique value on each replication-manager

arbitration-external-unique-id = 0

##########
## HTTP ##
##########

http-server = true
http-bind-address = "0.0.0.0"
http-port = "10001"
http-auth = false
http-session-lifetime =   3600
http-bootstrap-button = false
http-refresh-interval = 4000

#########
## API ##
#########

api-credentials = "admin:repman"
api-port = "10005"
api-https-bind = false

api-credentials-acl-allow ="admin:cluster proxy db prov,dba:cluster proxy db,foo:"
api-credentials-acl-discard = false
api-credentials-external = "dba:repman,foo:bar"

############
## ALERTS ##
############

mail-from = "replication-manager@localhost"
mail-smtp-addr = "localhost:25"
mail-to = "replication-manager@signal18.io"
mail-smtp-password=""
mail-smtp-user=""

alert-slack-channel = "#support"
alert-slack-url = ""
alert-slack-user = "svar"

##########
# STATS ##
##########

graphite-metrics = false
graphite-carbon-host = "127.0.0.1"
graphite-carbon-port = 2003
graphite-embedded = false
graphite-carbon-api-port = 10002
graphite-carbon-server-port = 10003
graphite-carbon-link-port = 7002
graphite-carbon-pickle-port = 2004
graphite-carbon-pprof-port = 7007
<br>####这里可以设置逻辑备份和物理备份的程序,结合上面的配置里的restic做备份,很不错,后续有时间了再继续配置吧
backup-mydumper-path = "/usr/local/bin/mydumper"
backup-myloader-path = "/usr/local/bin/myloader"
backup-mysqlbinlog-path = "/usr/local/bin/mysqlbinlog"
backup-mysqldump-path = "/usr/local/bin/mysqldump"


##############
# BENCHMARK ##
##############

sysbench-binary-path = "/usr/bin/sysbench"
sysbench-threads = 4
sysbench-time = 100
sysbench-v1 = true查看proxysql的配置<br>cat /etc/proxysql.cnf#file proxysql.cfg

########################################################################################
# This config file is parsed using libconfig , and its grammar is described in:      
# http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar
# Grammar is also copied at the end of this file                                       
########################################################################################

########################################################################################
# IMPORTANT INFORMATION REGARDING THIS CONFIGURATION FILE:                           
########################################################################################
# On startup, ProxySQL reads its config file (if present) to determine its datadir.
# What happens next depends on if the database file (disk) is present in the defined
# datadir (i.e. "/var/lib/proxysql/proxysql.db").
#
# If the database file is found, ProxySQL initializes its in-memory configuration from
# the persisted on-disk database. So, disk configuration gets loaded into memory and
# then propagated towards the runtime configuration.
#
# If the database file is not found and a config file exists, the config file is parsed
# and its content is loaded into the in-memory database, to then be both saved on-disk
# database and loaded at runtime.
#
# IMPORTANT: If a database file is found, the config file is NOT parsed. In this case
#            ProxySQL initializes its in-memory configuration from the persisted on-disk
#            database ONLY. In other words, the configuration found in the proxysql.cnf
#            file is only used to initial the on-disk database read on the first startup.
#
# In order to FORCE a re-initialise of the on-disk database from the configuration file
# the ProxySQL service should be started with "systemctl start proxysql-initial".
#
########################################################################################

datadir="/var/lib/proxysql"
errorlog="/var/lib/proxysql/proxysql.log"

admin_variables=
{
    admin_credentials="admin:admin"
#    mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
    mysql_ifaces="0.0.0.0:6032"
#    refresh_interval=2000
#    debug=true
}

mysql_variables=
{
    threads=4
    max_connections=2048
    default_query_delay=0
    default_query_timeout=36000000
    have_compress=true
    poll_timeout=2000
#    interfaces="0.0.0.0:6033;/tmp/proxysql.sock"
    interfaces="0.0.0.0:6033"
    default_schema="information_schema"
    stacksize=1048576
    server_version="5.5.30"
    connect_timeout_server=3000
# make sure to configure monitor username and password
# https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_username-mysql-monitor_password
    monitor_username="monitor"
    monitor_password="monitor"
    monitor_history=600000
    monitor_connect_interval=60000
    monitor_ping_interval=10000
    monitor_read_only_interval=1500
    monitor_read_only_timeout=500
    ping_interval_server_msec=120000
    ping_timeout_server=500
    commands_stats=true
    sessions_sort=true
    connect_retries_on_failure=10
}


# defines all the MySQL servers
mysql_servers =
(
#    {
#      address = "127.0.0.1" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
#      port = 3306         # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
#      hostgroup = 0            # no default, required
#      status = "ONLINE"   # default: ONLINE
#      weight = 1            # default: 1
#      compression = 0       # default: 0
#   max_replication_lag = 10# default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
#    },
#    {
#      address = "/var/lib/mysql/mysql.sock"
#      port = 0
#      hostgroup = 0
#    },
#    {
#      address="127.0.0.1"
#      port=21891
#      hostgroup=0
#      max_connections=200
#    },
#    { address="127.0.0.2" , port=3306 , hostgroup=0, max_connections=5 },
#    { address="127.0.0.1" , port=21892 , hostgroup=1 },
#    { address="127.0.0.1" , port=21893 , hostgroup=1 }
#    { address="127.0.0.2" , port=3306 , hostgroup=1 },
#    { address="127.0.0.3" , port=3306 , hostgroup=1 },
#    { address="127.0.0.4" , port=3306 , hostgroup=1 },
#    { address="/var/lib/mysql/mysql.sock" , port=0 , hostgroup=1 }
)


# defines all the MySQL users
mysql_users:
(
#    {
#      username = "username" # no default , required
#      password = "password" # default: ''
#      default_hostgroup = 0 # default: 0
#      active = 1            # default: 1
#    },
#    {
#      username = "root"
#      password = ""
#      default_hostgroup = 0
#      max_connections=1000
#      default_schema="test"
#      active = 1
#    },
#    { username = "user1" , password = "password" , default_hostgroup = 0 , active = 0 }
)



#defines MySQL Query Rules
mysql_query_rules:
(
#    {
#      rule_id=1
#      active=1
#      match_pattern="^SELECT .* FOR UPDATE$"
#      destination_hostgroup=0
#      apply=1
#    },
#    {
#      rule_id=2
#      active=1
#      match_pattern="^SELECT"
#      destination_hostgroup=1
#      apply=1
#    }
)

scheduler=
(
#{
#    id=1
#    active=0
#    interval_ms=10000
#    filename="/var/lib/proxysql/proxysql_galera_checker.sh"
#    arg1="0"
#    arg2="0"
#    arg3="0"
#    arg4="1"
#    arg5="/var/lib/proxysql/proxysql_galera_checker.log"
#}
)


mysql_replication_hostgroups=
(
#      {
#                writer_hostgroup=30
#                reader_hostgroup=40
#                comment="test repl 1"
#       },
#       {
#                writer_hostgroup=50
#                reader_hostgroup=60
#                comment="test repl 2"
#      }
)




# http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar
#
# Below is the BNF grammar for configuration files. Comments and include directives are not part of the grammar, so they are not included here.
#
# configuration = setting-list | empty
#
# setting-list = setting | setting-list setting
#   
# setting = name (":" | "=") value (";" | "," | empty)
#   
# value = scalar-value | array | list | group
#   
# value-list = value | value-list "," value
#   
# scalar-value = boolean | integer | integer64 | hex | hex64 | float
#                | string
#   
# scalar-value-list = scalar-value | scalar-value-list "," scalar-value
#   
# array = "[" (scalar-value-list | empty) "]"
#   
# list = "(" (value-list | empty) ")"
#   
# group = "{" (setting-list | empty) "}"
#   
# empty =其实proxysql配置基本上在启动了之后都是通过数据库来设置的配置文件
proxysql的配置如下,进入proxysql
# mysql -uadmin -p -P6032 -h127.0.0.1 --prompt='proxysql Admin> '
Enter password:
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id is 9037
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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.

proxysql Admin> <br>proxysql Admin> set mysql-monitor_username='proxysql';<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> set mysql-monitor_password='your password';<br>Query OK, 1 row affected (0.00 sec)<br>proxysql Admin> select * from global_variables;<br><br>| mysql-monitor_username                                               | proxysql                                   |<br>| mysql-monitor_password                                               | your password                              |<br>| mysql-monitor_history                                                | 600000                                     |<br>| mysql-monitor_connect_interval                                       | 60000                                      |<br>| mysql-monitor_ping_interval                                          | 10000                                      |<br>| mysql-monitor_read_only_interval                                     | 1500                                       |<br>| mysql-monitor_read_only_timeout                                      | 500                                        |<br>| mysql-ping_interval_server_msec                                      | 120000                                     |<br>| mysql-ping_timeout_server                                            | 500                                        |<br>| mysql-commands_stats                                                 | true                                       |<br>| mysql-sessions_sort                                                  | true                                       |<br>| mysql-connect_retries_on_failure                                     | 10                                         |<br>| mysql-server_capabilities                                            | 569899                                     |<br>+----------------------------------------------------------------------+--------------------------------------------+<br>proxysql Admin> load mysql variables to runtime;<br>Query OK, 0 rows affected (0.00 sec)<br><br>proxysql Admin> save mysql variables to disk;<br>Query OK, 155 rows affected (0.01 sec)<br>proxysql Admin> insert into mysql_replication_hostgroups values(1000,1001,'read_only','读1000写1001分离');<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1000,'server01.mshome.net',3306);<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1001,'server02.mshome.net',3306);<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> insert into mysql_servers(hostgroup_id,hostname,port) values(1001,'server03.mshome.net',3306);<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> load mysql servers to runtime;<br>Query OK, 0 rows affected (0.00 sec)<br><br>proxysql Admin> save mysql servers to disk;<br>Query OK, 0 rows affected (0.05 sec)<br>proxysql Admin> delete from mysql_users;<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> insert into mysql_users(username,password,default_hostgroup) values('appbox','Appbox@123',1000);<br>Query OK, 1 row affected (0.00 sec)<br><br>proxysql Admin> load mysql users to runtime;<br>Query OK, 0 rows affected (0.00 sec)<br><br>proxysql Admin> save mysql users to disk;<br>Query OK, 0 rows affected (0.02 sec)<br><br> 按照以上配置打开replication-manager的web管理端,默认账号admin密码repman


 切换主从后

 

 

切换主从后
 
 在server01的主库上创建appbox用户
mysql> create user appbox@'%' identified by 'Appbox@123';
Query OK, 0 rows affected (0.01 sec)

mysql> create database appboxdb character set utf8mb4;
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on appboxdb.* to appbox@'%';
Query OK, 0 rows affected (0.01 sec)

mysql>  用Navicat连proxysql

在proxysql中插入读写规则
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',100,1);
insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(2,1,'^SELECT',1000,1);<br>load mysql query rules to runtime;<br>save mysql query rules to disk;bash下进行读写测试
for i in {1..100}; do mysql -uappbox -pAppbox@123 -h server00.mshome.net-P6033-e 'select * from mysql.user LIMIT 2;' ; sleep 0.5; done如果有用,欢迎打赏,不限金额

 

来源:https://www.cnblogs.com/24la/p/mysql-proxysql-replication-manager-read-write-split-mha.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: mysql+proxysql+replication-manager的主从半同步复制+高可用+读写分离