当前位置:   article > 正文

mysql 几种集群部署模式_mysql集群搭建几种方式

mysql集群搭建几种方式

1.mysql 常见的部署模式: 一主一从  主主复制  一主多从  多主一从 外加haproxy代理

2.主从部署

 2.1单机安装mysql

 链接: 有道云笔记

2.2修改配置文件

master

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=140
  11. [client]
  12. port=3306
  13. [mysqld_safe]
  14. log-error=/usr/local/mysql5.7/log/mysqld.log

slave01

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=141
  11. [client]
  12. port=3306
  13. [mysqld_safe]
  14. log-error=/usr/local/mysql5.7/log/mysqld.log

slave02

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=142
  11. [client]
  12. port=3306
  13. [mysqld_safe]
  14. log-error=/usr/local/mysql5.7/log/mysqld.log

在 140 主节点 master 查看状态

  • SHOW MASTER STATUS;
  1. +------------------+----------+--------------+------------------+-------------------+
  2. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  3. +------------------+----------+--------------+------------------+-------------------+
  4. | mysql-bin.000002 | 1960 | | | |
  5. +------------------+----------+--------------+------------------+-------------------

在slave节点 执行以下指令链接到master

change master to master_host='192.168.174.204',master_user='root',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=0;

在slave

  1. start slave
  2. show slave status\G

接下自行测试了 

3.主主复制

选择两台服务器部署

master01(192.168.174.204)

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=140
  11. binlog_format=row ## cdc
  12. binlog-do-db=*
  13. relay-log = /usr/local/mysql5.7/data/relay-bin
  14. relay-log-info-file = /usr/local/mysql5.7/data/relay-log.info
  15. log-slave-updates
  16. binlog-ignore-db=mysql
  17. auto_increment_increment = 2
  18. auto_increment_offset = 1
  19. [client]
  20. port=3306
  21. [mysqld_safe]
  22. log-error=/usr/local/mysql5.7/log/mysqld.log

master02(192.168.174.206)

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. binlog_format=row ## cdc
  10. binlog-do-db=*
  11. log-bin=mysql-bin
  12. server-id=142
  13. relay-log = /usr/local/mysql5.7/data/relay-bin
  14. relay-log-info-file = /usr/local/mysql5.7/data/relay-log.info
  15. log-bin = mysql-bin
  16. log-slave-updates
  17. auto_increment_increment = 2
  18. auto_increment_offset = 2
  19. binlog-ignore-db=mysql
  20. [client]
  21. port=3306
  22. [mysqld_safe]
  23. log-error=/usr/local/mysql5.7/log/mysqld.log

两台服务器重启

service mysqld restart

两台服务器对彼此赋权 可以连接

grant replication slave,replication client,select on *.* to slave@'192.168.174.%' identified by '123456';

步骤:

  1. flush privileges;
  2. ##锁住库 两台
  3. flush tables with read lock;
  4. show master status;(记住 position file)
  5. unlock tables;
  6. stop slave;
  7. ##master01
  8. change master to master_host='192.168.174.206',master_user='root',master_password='123456',master_log_file='mysql-bin.000010',master_log_pos=640;
  9. master02
  10. change master to master_host='192.168.174.204',master_user='root',master_password='123456',master_log_file='mysql-bin.000008',master_log_pos=640;
  11. start slave;
  12. show slave status\G;
  13. ##Slave_IO_Running: Yes 这两个都是ON 就可以了
  14. ##Slave_SQL_Running: Yes

接下来测试:略

4.MGR 模式

master

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=140
  11. gtid_mode=ON
  12. enforce_gtid_consistency=ON
  13. binlog_checksum=NONE
  14. log_bin=binlog
  15. log_slave_updates=ON
  16. binlog_format=ROW
  17. master_info_repository=TABLE
  18. relay_log_info_repository=TABLE
  19. transaction_write_set_extraction=XXHASH64
  20. loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
  21. loose-group_replication_start_on_boot=off
  22. loose-group_replication_local_address="192.168.174.204:24901"
  23. loose-group_replication_group_seeds="192.168.174.204:24901,192.168.174.205:24901,192.168.174.206:24901"
  24. loose-group_replication_bootstrap_group=off
  25. report_host=192.168.174.204
  26. report_port=3306
  27. [client]
  28. port=3306
  29. [mysqld_safe]
  30. log-error=/usr/local/mysql5.7/log/mysqld.log
  31. pid-file=/usr/local/mysql5.7/data/localhost.localdomain.pid

slave01

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=141
  11. gtid_mode=ON
  12. enforce_gtid_consistency=ON
  13. binlog_checksum=NONE
  14. log_bin=binlog
  15. log_slave_updates=ON
  16. binlog_format=ROW
  17. master_info_repository=TABLE
  18. relay_log_info_repository=TABLE
  19. transaction_write_set_extraction=XXHASH64
  20. loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
  21. loose-group_replication_start_on_boot=off
  22. loose-group_replication_local_address="192.168.174.205:24901"
  23. loose-group_replication_group_seeds="192.168.174.204:24901,192.168.174.205:24901,192.168.174.206:24901"
  24. loose-group_replication_bootstrap_group=off
  25. report_host=192.168.174.205
  26. report_port=3306
  27. [client]
  28. port=3306
  29. [mysqld_safe]
  30. log-error=/usr/local/mysql5.7/log/mysqld.log
  31. pid-file=/usr/local/mysql5.7/data/node02.pid

slave02

  1. [mysqld]
  2. basedir=/usr/local/mysql5.7/
  3. datadir=/usr/local/mysql5.7/data
  4. user=mysql
  5. max_connections=20000
  6. character-set-server=utf8
  7. default-storage-engine=INNODB
  8. max_allowed_packet=16M
  9. log-bin=mysql-bin
  10. server-id=142
  11. gtid_mode=ON
  12. enforce_gtid_consistency=ON
  13. binlog_checksum=NONE
  14. log_bin=binlog
  15. log_slave_updates=ON
  16. binlog_format=ROW
  17. master_info_repository=TABLE
  18. relay_log_info_repository=TABLE
  19. transaction_write_set_extraction=XXHASH64
  20. loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
  21. loose-group_replication_start_on_boot=off
  22. loose-group_replication_local_address="192.168.174.206:24901"
  23. loose-group_replication_group_seeds="192.168.174.204:24901,192.168.174.205:24901,192.168.174.206:24901"
  24. loose-group_replication_bootstrap_group=off
  25. report_host=192.168.174.206
  26. report_port=3306
  27. [client]
  28. port=3306
  29. [mysqld_safe]
  30. log-error=/usr/local/mysql5.7/log/mysqld.log
  31. pid-file=/usr/local/mysql5.7/data/node03.pid

接下来重启mysql服务 三台服务器 执行接下来的步骤

service mysqld restart

  1. INSTALL PLUGIN group_replication SONAME 'group_replication.so';
  2. SET SQL_LOG_BIN=0;
  3. CREATE USER kuber@'%' IDENTIFIED BY '123456';
  4. GRANT REPLICATION SLAVE ON *.* TO kuber@'%';
  5. FLUSH PRIVILEGES;
  6. SET SQL_LOG_BIN=1;
  7. CHANGE MASTER TO MASTER_USER='kuber', MASTER_PASSWORD='123456' FOR CHANNEL 'group_replication_recovery';

单主模式

在主节点上部署

  1. SET GLOBAL group_replication_bootstrap_group=ON;
  2. START GROUP_REPLICATION;
  3. SET GLOBAL group_replication_bootstrap_group=OFF;
  4. SELECT * FROM performance_schema.replication_group_members;
  5. +---------------------------+--------------------------------------+-----------------+-------------+--------------+
  6. | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  7. +---------------------------+--------------------------------------+-----------------+-------------+--------------+
  8. | group_replication_applier | 90b7daa9-405b-11ec-b230-000c294807c0 | 192.168.174.204 | 3306 | ONLINE |
  9. +---------------------------+--------------------------------------+-----------------+-------------+--------------+

在其他的slave节点上部署

  1. START GROUP_REPLICATION;
  2. ##出现报错
  3. ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.
  4. ##解决方案
  5. set global group_replication_allow_local_disjoint_gtids_join=ON;
  6. ##接着继续操作
  7. START GROUP_REPLICATION;
  8. SELECT * FROM performance_schema.replication_group_members;
  9. +---------------------------+--------------------------------------+-----------------+-------------+--------------+
  10. | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
  11. +---------------------------+--------------------------------------+-----------------+-------------+--------------+
  12. | group_replication_applier | 0094f1a7-04a0-11ec-9e4e-000c29fb0b92 | 192.168.174.205 | 3306 | ONLINE |
  13. | group_replication_applier | 747ae5c5-4064-11ec-99d6-000c29b619af | 192.168.174.206 | 3306 | ONLINE |
  14. | group_replication_applier | 90b7daa9-405b-11ec-b230-000c294807c0 | 192.168.174.204 | 3306 | ONLINE |
  15. +---------------------------+--------------------------------------+-----------------+-------------+--------------+
  16. ##注意 MEMBER_PORT 必须是 ONLINE 模式

当出现节点是 RECOVERING状态

  1. #在主节点操作
  2. show global variables like 'gtid_purged';
  3. +---------------+------------------------------------------+
  4. | Variable_name | Value |
  5. +---------------+------------------------------------------+
  6. | gtid_purged | 8769f936-3e51-11e9-acaa-005056ac6820:1-2 |
  7. +---------------+------------------------------------------+
  8. ##如果没有值 那就三台执行以下步奏 如果有,那就其他两台执行以下的步奏
  9. STOP GROUP_REPLICATION;
  10. reset master;
  11. set global gtid_purged = '8769f936-3e51-11e9-acaa-005056ac6820:1-2';
  12. START GROUP_REPLICATION;
  13. ##查看节点状态
  14. SELECT * FROM performance_schema.replication_group_members;

接下测试:略

总结: 这种模式 只能在主节点上进行增删查改等操作,其他的slave 没有办法进行操作的,如果需要其他的slave的节点能进行增删查改的操作,那就需要接下的多主模式

输入接下的指令进行切换到多主模式

报错

  1. ERROR 3094 (HY000): The START GROUP_REPLICATION command failed as the applier module failed to start.
  2. ##在my.ini 中添加
  3. relay-log-recovery=1
  4. ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.
  5. ## 解决方案
  6. set global group_replication_bootstrap_group = ON;
  7. start group_replication;

查看角色(参考仅可)

  1. SELECT
  2. MEMBER_ID,
  3. MEMBER_HOST,
  4. MEMBER_PORT,
  5. MEMBER_STATE,
  6. IF(global_status.VARIABLE_NAME IS NOT NULL,
  7. 'PRIMARY',
  8. 'SECONDARY') AS MEMBER_ROLE
  9. FROM
  10. performance_schema.replication_group_members
  11. LEFT JOIN
  12. performance_schema.global_status ON global_status.VARIABLE_NAME = 'group_replication_primary_member'
  13. AND global_status.VARIABLE_VALUE = replication_group_members.MEMBER_ID;

使用 show global variables like "%read_only%"; 查看 全部为OFF 即可

切换多主模式

  1. ##停止组复制 所有的节点都停止
  2. stop group_replication;
  3. set global group_replication_single_primary_mode=OFF;
  4. set global group_replication_enforce_update_everywhere_checks=ON;
  5. ##随便选择某个MGR
  6. SET GLOBAL group_replication_bootstrap_group=ON;
  7. START GROUP_REPLICATION;
  8. SET GLOBAL group_replication_bootstrap_group=OFF;
  9. ##在其他的两个节点操作
  10. START GROUP_REPLICATION;
  11. SELECT * FROM performance_schema.replication_group_members;
  12. ##所有的MGR节点都是ONLINE 即可

测试验证:略

切换到单主模式

  1. stop group_replication;
  2. set global group_replication_enforce_update_everywhere_checks=OFF;
  3. set global group_replication_single_primary_mode=ON;
  4. ## 选择主节点服务器
  5. SET GLOBAL group_replication_bootstrap_group=ON;
  6. START GROUP_REPLICATION;
  7. SET GLOBAL group_replication_bootstrap_group=OFF;
  8. ## 在其他的节点操作
  9. START GROUP_REPLICATION;
  10. SELECT * FROM performance_schema.replication_group_members;
  11. ##所有的MGR节点都是ONLINE 即可

5. mysql haproxy 主主复制

5.1.首先关闭

  1. firewall-cmd --state
  2. systemctl stop firewalld.service
  3. systemctl disable firewalld.service
  4. ## 修改该文件
  5. vi /etc/selinux/config
  6. SELINUX=disabled
  7. reboot

2.配置数据库双主复制

参考上面的 主主复制

3.配置haproxy

  1. #添加用户组
  2. groupadd haproxy
  3. useradd -g haproxy haproxy
  4. #install haproxy
  5. yum install haproxy
  6. #加入 centos7 服务
  7. chmod +x /etc/init.d/haproxy
  8. chkconfig haproxy on
  9. #开启日志服务
  10. touch /var/log/haproxy.log
  11. chown haproxy:haproxy /var/log/haproxy.log
  12. chmod a+x /var/log/haproxy.log

开启rsyslog记录haproxy日志功能

  1. vi /etc/rsyslog.conf
  2. (打开如下配置项)
  3. # Provides UDP syslog reception
  4. $ModLoad imudp
  5. $UDPServerRun 514
  6. # Provides TCP syslog reception
  7. $ModLoad imtcp
  8. $InputTCPServerRun 514
  9. (在RULES块中添加)
  10. #### RULES ####
  11. # save haproxy.log
  12. local0.* /var/log/haproxy.log

修改 /etc/sysconfig/rsyslog

  1. vi /etc/sysconfig/rsyslog
  2. # Options for rsyslogd
  3. # Syslogd options are deprecated since rsyslog v3.
  4. # If you want to use them, switch to compatibility mode 2 by "-c 2"
  5. # See rsyslogd(8) for more details
  6. SYSLOGD_OPTIONS="-r -m 0 -c 2"

编辑haproxy.cfg

  1. global
  2. # to have these messages end up in /var/log/haproxy.log you will
  3. # need to:
  4. #
  5. # 1) configure syslog to accept network log events. This is done
  6. # by adding the '-r' option to the SYSLOGD_OPTIONS in
  7. # /etc/sysconfig/syslog
  8. #
  9. # 2) configure local2 events to go to the /var/log/haproxy.log
  10. # file. A line like the following can be added to
  11. # /etc/sysconfig/syslog
  12. #
  13. # local2.* /var/log/haproxy.log
  14. #
  15. log 127.0.0.1 local0
  16. chroot /var/lib/haproxy
  17. pidfile /var/run/haproxy.pid
  18. maxconn 4000
  19. user haproxy
  20. group haproxy
  21. daemon
  22. # turn on stats unix socket
  23. stats socket /var/lib/haproxy/stats
  24. #---------------------------------------------------------------------
  25. # common defaults that all the 'listen' and 'backend' sections will
  26. # use if not designated in their block
  27. #---------------------------------------------------------------------
  28. defaults
  29. mode tcp
  30. log global
  31. option httplog
  32. option dontlognull
  33. option http-server-close
  34. option forwardfor except 127.0.0.0/8
  35. option redispatch
  36. retries 3
  37. timeout http-request 10s
  38. timeout queue 1m
  39. timeout connect 10s
  40. timeout client 1m
  41. timeout server 1m
  42. timeout http-keep-alive 10s
  43. timeout check 10s
  44. maxconn 3000
  45. #---------------------------------------------------------------------
  46. # main frontend which proxys to the backends
  47. #---------------------------------------------------------------------
  48. frontend main *:5000
  49. # acl url_static path_beg -i /static /images /javascript /stylesheets
  50. # acl url_static path_end -i .jpg .gif .png .css .js
  51. # use_backend static if url_static
  52. default_backend mysql_backend
  53. #---------------------------------------------------------------------
  54. # static backend for serving up images, stylesheets and such
  55. #---------------------------------------------------------------------
  56. #backend static
  57. # balance roundrobin
  58. # server static 192.168.174.205:3306 check
  59. #---------------------------------------------------------------------
  60. # round robin balancing between the various backends
  61. #--------------------------------------------------------------------
  62. backend mysql_backend
  63. balance leastconn
  64. server mysql1 192.168.174.205:3306 check port 3306 maxconn 300
  65. server mysql2 192.168.174.206:3306 check port 3306 maxconn 300
  66. #server app3 127.0.0.1:5003 check
  67. # server app4 127.0.0.1:5004 check
  68. listen admin_stats
  69. mode http
  70. stats enable
  71. bind *:7777
  72. stats refresh 30s
  73. stats uri /admin
  74. stats realm haproxy
  75. stats auth root:root
  76. stats hide-version

重启rsyslog与Haproxy服务

  1. service rsyslog restart
  2. service haproxy restart

网页访问 192.168.174.204:7777 可以是mysql客户端 访问代理服务器

192.168.174.204:5000

kuberss

123456

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/586156
推荐阅读
相关标签
  

闽ICP备14008679号