翼度科技»论坛 编程开发 mysql 查看内容

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

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
环境:
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供主从复制使用
  1. mysql> create user 'rep-manager'@'%' identified by 'your password';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> create user 'repl'@'%' identified by 'your password';
  4. Query OK, 0 rows affected (0.02 sec)
  5. mysql> create user 'proxysql'@'%' identified by 'your password';
  6. Query OK, 0 rows affected (0.02 sec)
  7. mysql> grant process,replication slave,replication client on *.* to 'proxysql'@'%';
  8. Query OK, 0 rows affected (0.01 sec)
  9. mysql> grant select,replication slave,replication client,reload,super on *.* to 'repl'@'%';
  10. Query OK, 0 rows affected, 1 warning (0.01 sec)
  11. mysql> grant all privileges on *.* to 'rep-manager'@'%' with grant option;
  12. Query OK, 0 rows affected (0.01 sec)
  13. mysql> flush privileges;
  14. Query OK, 0 rows affected (0.01 sec)
复制代码
 
2、复制虚拟机
3、在server00上安装replication-manager
4、在server00上安装proxysql
5、启动mysql,创建主从半同步复制
半同步需要安装插件,在主库和从库上都安装
  1. mysql> install plugin rpl_semi_sync_source soname 'semisync_source.so';
  2. mysql> install plugin rpl_semi_sync_replica soname 'semisync_replica.so';
  3. 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如下
  1. # 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>[mysqld]<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 = 1
复制代码
slave主机myql的配置如下:
  1. # 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>[mysqld]<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>
复制代码
启动主从
  1. 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
复制代码
  1.  
复制代码
server00主机上安装了replication-manager和proxysql
replication-manager的配置如下
  1. 查看replication-manager配置文件cluster1.toml<br>cat /etc/replication-manager/cluster.d/cluster1.toml
复制代码
  1. [cluster1]
  2. title = "cluster1"
  3. prov-orchestrator = "onpremise"
  4. prov-db-tags = "innodb,noquerycache,slow,pfs,pkg,linux,smallredolog,logtotable"
  5. prov-db-memory = "256"
  6. prov-db-memory-shared-pct = "threads:16,innodb:60,myisam:10,aria:10,rocksdb:1,tokudb:1,s3:1,archive:1,querycache:0"
  7. prov-db-disk-size = "1"
  8. prov-db-cpu-cores = "1"
  9. prov-db-disk-iops = "300"
  10. db-servers-hosts = "server01.mshome.net:3306,server02.mshome.net:3306,server03.mshome.net:3306"
  11. db-servers-prefered-master = "server01.mshome.net:3306"
  12. db-servers-credential = "rep-manager:yourpassword"
  13. db-servers-connect-timeout = 5
  14. replication-credential = "rep-manager:yourpassword"
  15. verbose = false
  16. log-failed-election  = true
  17. log-level = 1
  18. log-rotate-max-age = 7
  19. log-rotate-max-backup = 7
  20. log-rotate-max-size = 5
  21. log-sql-in-monitoring   = true
  22. log-sst = true
  23. ##############
  24. ## TOPOLOGY ##
  25. ##############
  26. replication-multi-master = false
  27. replication-multi-tier-slave = false
  28. ############
  29. # BACKUPS ##
  30. ###########
  31. backup-streaming = false
  32. backup-streaming-aws-access-key-id = "admin"
  33. backup-streaming-aws-access-secret = "xxxx"
  34. backup-streaming-endpoint= "https://s3.signal18.io/"
  35. backup-streaming-region= "fr-1"
  36. backup-streaming-bucket= "repman"<br><br>
  37. #####这里可以设置备份工具restic,工具可以跨服务器,结合minio备份就很爽了,后续再介绍吧
  38. backup-restic = false
  39. backup-restic-aws =  false
  40. backup-physical-type = "mariabackup"
  41. backup-logical-type = "mysqldump"
  42. backup-restic-aws-access-secret = "xxxx"
  43. backup-restic-password = "xxxx"
  44. backup-restic-binary-path = "/usr/bin/restic"
  45. monitoring-scheduler = true
  46. scheduler-db-servers-logical-backup  = true
  47. scheduler-db-servers-logical-backup-cron= "0 30 23 * * 6"
  48. scheduler-db-servers-logs   =  false
  49. scheduler-db-servers-logs-cron = "0 0 * * * *"
  50. scheduler-db-servers-logs-table-keep = 4
  51. scheduler-db-servers-logs-table-rotate  = false
  52. scheduler-db-servers-logs-table-rotate-cron = "0 0 0/6 * * *"
  53. scheduler-db-servers-optimize  = false
  54. scheduler-db-servers-optimize-cron = "0 0 3 1 * 5"
  55. scheduler-db-servers-physical-backup = true
  56. scheduler-db-servers-physical-backup-cron = "0 30 23 * * *"
  57. ##############
  58. ## FAILOVER ##
  59. ##############
  60. failover-mode = "manual"
  61. failover-pre-script = ""
  62. failover-post-script = ""
  63. ## Slaves will re enter with read-only
  64. failover-readonly-state = true
  65. failover-event-scheduler = false
  66. failover-event-status = false
  67. ## Failover after N failures detection
  68. failover-falsepositive-ping-counter = 5
  69. ## Cancel failover if already N failover
  70. ## Cancel failover if last failover was N seconds before
  71. ## Cancel failover in semi-sync when one slave is not in sync
  72. ## Cancel failover if one slave receive master heartbeat
  73. ## Cancel failover when replication delay is more than N seconds
  74. failover-limit = 0
  75. failover-time-limit = 0
  76. failover-at-sync = false
  77. failover-max-slave-delay = 30
  78. failover-restart-unsafe = false
  79. # failover-falsepositive-heartbeat = true
  80. # failover-falsepositive-heartbeat-timeout = 3
  81. # failover-falsepositive-maxscale = false
  82. # failover-falsepositive-maxscale-timeout = 14
  83. # failover-falsepositive-external = false
  84. # failover-falsepositive-external-port = 80
  85. ################
  86. ## SWITCHOVER ##
  87. ################
  88. ## In switchover Wait N milliseconds before killing long running transactions
  89. ## Cancel switchover if transaction running more than N seconds
  90. ## Cancel switchover if write query running more than N seconds
  91. ## Cancel switchover if one of the slaves is not synced based on GTID equality
  92. switchover-wait-kill = 5000
  93. switchover-wait-trx = 10
  94. switchover-wait-write-query = 10
  95. switchover-at-equal-gtid = false
  96. switchover-at-sync = false
  97. switchover-max-slave-delay = 30
  98. ############
  99. ## REJOIN ##
  100. ############
  101. autorejoin = true
  102. autorejoin-script = ""
  103. autorejoin-semisync = true
  104. autorejoin-backup-binlog = true
  105. autorejoin-flashback = false
  106. autorejoin-mysqldump = false
  107. ####################
  108. ## CHECKS & FORCE ##
  109. ####################
  110. check-replication-filters = true
  111. check-binlog-filters = true
  112. check-replication-state = true
  113. force-slave-heartbeat= false
  114. force-slave-heartbeat-retry = 5
  115. force-slave-heartbeat-time = 3
  116. force-slave-gtid-mode = false
  117. force-slave-semisync = false
  118. force-slave-failover-readonly-state = false
  119. force-binlog-row = false
  120. force-binlog-annotate = false
  121. force-binlog-slowqueries = false
  122. force-binlog-compress = false
  123. force-binlog-checksum = false
  124. force-inmemory-binlog-cache-size = false
  125. force-disk-relaylog-size-limit = false
  126. force-disk-relaylog-size-limit-size = 1000000000
  127. force-sync-binlog = false
  128. force-sync-innodb = false
  129. ##############
  130. ## MAXSCALE ##
  131. ##############
  132. ## for 2 nodes cluster maxscale can be driven by replication manager
  133. maxscale = false
  134. maxscale-binlog = false
  135. maxscale-servers = "192.168.0.201"
  136. maxscale-port = 4003
  137. maxscale-user = "admin"
  138. maxscale-pass = "mariadb"
  139. ## When true replication manager drive maxscale server state
  140. ## Not required unless multiple maxscale or release does not support detect_stale_slave
  141. maxscale-disable-monitor = false
  142. ## maxinfo|maxadmin
  143. maxscale-get-info-method = "maxadmin"
  144. maxscale-maxinfo-port = 4002
  145. maxscale-write-port = 4007
  146. maxscale-read-port = 4008
  147. maxscale-read-write-port = 4006
  148. maxscale-binlog-port = 4000
  149. #############
  150. ## HAPROXY ##
  151. #############
  152. ## Wrapper mode unless maxscale or proxysql required to be located with replication-manager
  153. haproxy = false
  154. haproxy-binary-path = "/usr/sbin/haproxy"
  155. ## Read write traffic
  156. ## Read only load balance least connection traffic
  157. haproxy-write-port = 3306
  158. haproxy-read-port = 3307
  159. ####################
  160. ## SHARDING PROXY ##
  161. ####################
  162. mdbshardproxy = false
  163. mdbshardproxy-hosts = "127.0.0.1:3306"
  164. mdbshardproxy-user = "root:mariadb"
  165. #################################
  166. ###### proxysql settings ########
  167. #################################
  168. proxysql = true
  169. proxysql-servers = "127.0.0.1"
  170. proxysql-port = "6033"
  171. proxysql-admin-port = "6032"
  172. proxysql-writer-hostgroup = "1000"
  173. proxysql-reader-hostgroup = "1001"
  174. proxysql-user = "admin"
  175. proxysql-password = "admin"
  176. proxysql-bootstrap = false
  177. proxysql-bootstrap-users = false #不从master复制用户到proxysql,因为复制会出问题
复制代码
  1. 查看replication-manager配置文件config.toml<br>cat /etc/replication-manager/config.toml
复制代码
  1. [Default]
  2. include = "/etc/replication-manager/cluster.d"
  3. monitoring-save-config = false
  4. monitoring-datadir = "/var/lib/replication-manager"
  5. #monitoring-sharedir = "/usr/share/replication-manager"
  6. ## Timeout in seconds between consecutive monitoring
  7. monitoring-ticker = 2
  8. #########
  9. ## LOG ##
  10. #########
  11. log-file = "/var/log/replication-manager.log"
  12. log-heartbeat = false
  13. log-syslog = false
  14. #################
  15. ## ARBITRATION ##
  16. #################
  17. arbitration-external = false
  18. arbitration-external-secret = "13787932529099014144"
  19. arbitration-external-hosts = "88.191.151.84:80"
  20. arbitration-peer-hosts ="127.0.0.1:10002"
  21. ## Unique value on each replication-manager
  22. arbitration-external-unique-id = 0
  23. ##########
  24. ## HTTP ##
  25. ##########
  26. http-server = true
  27. http-bind-address = "0.0.0.0"
  28. http-port = "10001"
  29. http-auth = false
  30. http-session-lifetime =   3600
  31. http-bootstrap-button = false
  32. http-refresh-interval = 4000
  33. #########
  34. ## API ##
  35. #########
  36. api-credentials = "admin:repman"
  37. api-port = "10005"
  38. api-https-bind = false
  39. api-credentials-acl-allow =  "admin:cluster proxy db prov,dba:cluster proxy db,foo:"
  40. api-credentials-acl-discard = false
  41. api-credentials-external = "dba:repman,foo:bar"
  42. ############
  43. ## ALERTS ##
  44. ############
  45. mail-from = "replication-manager@localhost"
  46. mail-smtp-addr = "localhost:25"
  47. mail-to = "replication-manager@signal18.io"
  48. mail-smtp-password=""
  49. mail-smtp-user=""
  50. alert-slack-channel = "#support"
  51. alert-slack-url = ""
  52. alert-slack-user = "svar"
  53. ##########
  54. # STATS ##
  55. ##########
  56. graphite-metrics = false
  57. graphite-carbon-host = "127.0.0.1"
  58. graphite-carbon-port = 2003
  59. graphite-embedded = false
  60. graphite-carbon-api-port = 10002
  61. graphite-carbon-server-port = 10003
  62. graphite-carbon-link-port = 7002
  63. graphite-carbon-pickle-port = 2004
  64. graphite-carbon-pprof-port = 7007
  65. <br>####这里可以设置逻辑备份和物理备份的程序,结合上面的配置里的restic做备份,很不错,后续有时间了再继续配置吧
  66. backup-mydumper-path = "/usr/local/bin/mydumper"
  67. backup-myloader-path = "/usr/local/bin/myloader"
  68. backup-mysqlbinlog-path = "/usr/local/bin/mysqlbinlog"
  69. backup-mysqldump-path = "/usr/local/bin/mysqldump"
  70. ##############
  71. # BENCHMARK ##
  72. ##############
  73. sysbench-binary-path = "/usr/bin/sysbench"
  74. sysbench-threads = 4
  75. sysbench-time = 100
  76. sysbench-v1 = true
复制代码
  1. 查看proxysql的配置<br>cat /etc/proxysql.cnf
复制代码
  1. #file proxysql.cfg
  2. ########################################################################################
  3. # This config file is parsed using libconfig , and its grammar is described in:        
  4. # http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar
  5. # Grammar is also copied at the end of this file                                       
  6. ########################################################################################
  7. ########################################################################################
  8. # IMPORTANT INFORMATION REGARDING THIS CONFIGURATION FILE:                             
  9. ########################################################################################
  10. # On startup, ProxySQL reads its config file (if present) to determine its datadir.
  11. # What happens next depends on if the database file (disk) is present in the defined
  12. # datadir (i.e. "/var/lib/proxysql/proxysql.db").
  13. #
  14. # If the database file is found, ProxySQL initializes its in-memory configuration from
  15. # the persisted on-disk database. So, disk configuration gets loaded into memory and
  16. # then propagated towards the runtime configuration.
  17. #
  18. # If the database file is not found and a config file exists, the config file is parsed
  19. # and its content is loaded into the in-memory database, to then be both saved on-disk
  20. # database and loaded at runtime.
  21. #
  22. # IMPORTANT: If a database file is found, the config file is NOT parsed. In this case
  23. #            ProxySQL initializes its in-memory configuration from the persisted on-disk
  24. #            database ONLY. In other words, the configuration found in the proxysql.cnf
  25. #            file is only used to initial the on-disk database read on the first startup.
  26. #
  27. # In order to FORCE a re-initialise of the on-disk database from the configuration file
  28. # the ProxySQL service should be started with "systemctl start proxysql-initial".
  29. #
  30. ########################################################################################
  31. datadir="/var/lib/proxysql"
  32. errorlog="/var/lib/proxysql/proxysql.log"
  33. admin_variables=
  34. {
  35.     admin_credentials="admin:admin"
  36. #    mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock"
  37.     mysql_ifaces="0.0.0.0:6032"
  38. #    refresh_interval=2000
  39. #    debug=true
  40. }
  41. mysql_variables=
  42. {
  43.     threads=4
  44.     max_connections=2048
  45.     default_query_delay=0
  46.     default_query_timeout=36000000
  47.     have_compress=true
  48.     poll_timeout=2000
  49. #    interfaces="0.0.0.0:6033;/tmp/proxysql.sock"
  50.     interfaces="0.0.0.0:6033"
  51.     default_schema="information_schema"
  52.     stacksize=1048576
  53.     server_version="5.5.30"
  54.     connect_timeout_server=3000
  55. # make sure to configure monitor username and password
  56. # https://github.com/sysown/proxysql/wiki/Global-variables#mysql-monitor_username-mysql-monitor_password
  57.     monitor_username="monitor"
  58.     monitor_password="monitor"
  59.     monitor_history=600000
  60.     monitor_connect_interval=60000
  61.     monitor_ping_interval=10000
  62.     monitor_read_only_interval=1500
  63.     monitor_read_only_timeout=500
  64.     ping_interval_server_msec=120000
  65.     ping_timeout_server=500
  66.     commands_stats=true
  67.     sessions_sort=true
  68.     connect_retries_on_failure=10
  69. }
  70. # defines all the MySQL servers
  71. mysql_servers =
  72. (
  73. #    {
  74. #        address = "127.0.0.1" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
  75. #        port = 3306           # no default, required . If port is 0 , address is interpred as a Unix Socket Domain
  76. #        hostgroup = 0            # no default, required
  77. #        status = "ONLINE"     # default: ONLINE
  78. #        weight = 1            # default: 1
  79. #        compression = 0       # default: 0
  80. #   max_replication_lag = 10  # default 0 . If greater than 0 and replication lag passes such threshold, the server is shunned
  81. #    },
  82. #    {
  83. #        address = "/var/lib/mysql/mysql.sock"
  84. #        port = 0
  85. #        hostgroup = 0
  86. #    },
  87. #    {
  88. #        address="127.0.0.1"
  89. #        port=21891
  90. #        hostgroup=0
  91. #        max_connections=200
  92. #    },
  93. #    { address="127.0.0.2" , port=3306 , hostgroup=0, max_connections=5 },
  94. #    { address="127.0.0.1" , port=21892 , hostgroup=1 },
  95. #    { address="127.0.0.1" , port=21893 , hostgroup=1 }
  96. #    { address="127.0.0.2" , port=3306 , hostgroup=1 },
  97. #    { address="127.0.0.3" , port=3306 , hostgroup=1 },
  98. #    { address="127.0.0.4" , port=3306 , hostgroup=1 },
  99. #    { address="/var/lib/mysql/mysql.sock" , port=0 , hostgroup=1 }
  100. )
  101. # defines all the MySQL users
  102. mysql_users:
  103. (
  104. #    {
  105. #        username = "username" # no default , required
  106. #        password = "password" # default: ''
  107. #        default_hostgroup = 0 # default: 0
  108. #        active = 1            # default: 1
  109. #    },
  110. #    {
  111. #        username = "root"
  112. #        password = ""
  113. #        default_hostgroup = 0
  114. #        max_connections=1000
  115. #        default_schema="test"
  116. #        active = 1
  117. #    },
  118. #    { username = "user1" , password = "password" , default_hostgroup = 0 , active = 0 }
  119. )
  120. #defines MySQL Query Rules
  121. mysql_query_rules:
  122. (
  123. #    {
  124. #        rule_id=1
  125. #        active=1
  126. #        match_pattern="^SELECT .* FOR UPDATE$"
  127. #        destination_hostgroup=0
  128. #        apply=1
  129. #    },
  130. #    {
  131. #        rule_id=2
  132. #        active=1
  133. #        match_pattern="^SELECT"
  134. #        destination_hostgroup=1
  135. #        apply=1
  136. #    }
  137. )
  138. scheduler=
  139. (
  140. #  {
  141. #    id=1
  142. #    active=0
  143. #    interval_ms=10000
  144. #    filename="/var/lib/proxysql/proxysql_galera_checker.sh"
  145. #    arg1="0"
  146. #    arg2="0"
  147. #    arg3="0"
  148. #    arg4="1"
  149. #    arg5="/var/lib/proxysql/proxysql_galera_checker.log"
  150. #  }
  151. )
  152. mysql_replication_hostgroups=
  153. (
  154. #        {
  155. #                writer_hostgroup=30
  156. #                reader_hostgroup=40
  157. #                comment="test repl 1"
  158. #       },
  159. #       {
  160. #                writer_hostgroup=50
  161. #                reader_hostgroup=60
  162. #                comment="test repl 2"
  163. #        }
  164. )
  165. # http://www.hyperrealm.com/libconfig/libconfig_manual.html#Configuration-File-Grammar
  166. #
  167. # Below is the BNF grammar for configuration files. Comments and include directives are not part of the grammar, so they are not included here.
  168. #
  169. # configuration = setting-list | empty
  170. #
  171. # setting-list = setting | setting-list setting
  172. #     
  173. # setting = name (":" | "=") value (";" | "," | empty)
  174. #     
  175. # value = scalar-value | array | list | group
  176. #     
  177. # value-list = value | value-list "," value
  178. #     
  179. # scalar-value = boolean | integer | integer64 | hex | hex64 | float
  180. #                | string
  181. #     
  182. # scalar-value-list = scalar-value | scalar-value-list "," scalar-value
  183. #     
  184. # array = "[" (scalar-value-list | empty) "]"
  185. #     
  186. # list = "(" (value-list | empty) ")"
  187. #     
  188. # group = "{" (setting-list | empty) "}"
  189. #     
  190. # empty =
复制代码
其实proxysql配置基本上在启动了之后都是通过数据库来设置的配置文件
proxysql的配置如下,进入proxysql
  1. [root@server00 ~]# mysql -uadmin -p -P6032 -h127.0.0.1 --prompt='proxysql Admin> '
  2. Enter password:
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 9037
  5. Server version: 5.5.30 (ProxySQL Admin Module)
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. 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用户
  1. mysql> create user appbox@'%' identified by 'Appbox@123';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> create database appboxdb character set utf8mb4;
  4. Query OK, 1 row affected (0.01 sec)
  5. mysql> grant all privileges on appboxdb.* to appbox@'%';
  6. Query OK, 0 rows affected (0.01 sec)
  7. mysql>
复制代码
 用Navicat连proxysql

在proxysql中插入读写规则
  1. insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'^SELECT.*FOR UPDATE$',100,1);
  2. 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下进行读写测试
  1. 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】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具