基于Docker的MYSQL PXC集群搭建

pxc集群是Percona XtraDB Cluster的缩写,是基于percona数据库和galera中间件一种特殊MYSQL数据库,而且据说percona数据库的性能要比mysql还要好一些,并且是基于mysql,可以使用mysql的jdbc和客户端进行连接的。

pxc集群相比mysql单一数据库或者mysql主从复制数据库的优点如下

1、所有节点均可读可写,可以轻松的起到HA的作用,任意一台节点宕机都不会影响全局服务

2、在任意一节点写入都是事务强一致性的,不会像主从复制那样使用异步复制的方案导致主从数据不一致的情况。

3、某一个节点宕机之后,再重启恢复后会自动的将宕机期间没有写入的数据补充回来

所以pxc集群不仅可以实现传统的读写分离功能,更在高可用和强一致性上提供了超强的解决方案。大大提高了mysql数据库的可靠性,并且部署还非常简单,下面以基于ubuntu:16.04的docker为例介绍一下在docker部署pxc集群的方法。

官网上也有相关的手册和安装包:https://www.percona.com/software/mysql-database/percona-xtradb-cluster

首先我们需要拉取ubuntu:16.04的操作系统

docker pull ubuntu:16.04

然后我们进入这个docker,安装wget和vim,一会会用到

 
  1. C:\Users\alex>docker run -itd ubuntu:16.04 /bin/bash

  2. 58470aed38c6569fc5a450a59b18163cbfc47c661c830057caed40e180957b67

  3.  
  4. C:\Users\alex>docker attach 5

  5. root@58470aed38c6:/# apt-get update

root@58470aed38c6:/# apt-get install wget vim

然后下载安装Percona XtraDB Cluster的apt源,方便下载相关安装包,注意下面的xenial为ubuntu16.04的代号,要根据操作系统不同而修改

 
  1. root@58470aed38c6:/# cd /home

  2. root@58470aed38c6:/home# wget https://repo.percona.com/apt/percona-release_0.1-6.xenial_all.deb

你也可以去https://repo.percona.com/apt/手动选择deb包。

然后我们就可以安装这个deb包,并观察有哪些软件包可以安装了

 
  1. dpkg -i ./percona-release_0.1-6.xenial_all.deb

  2. apt-get update

  3. apt-cache search percona

软件包列表如下

 
  1. galera-3 - Replication framework for transactional applications

  2. percona-galera-3 - Galera replication framework for Percona XtraDB Cluster

  3. percona-galera-3-dbg - debugging symbols for percona-galera-3

  4. percona-galera-arbitrator-3 - Galera arbitrator daemon for Percona XtraDB Cluster

  5. percona-galera-arbitrator-3-dbg - debugging symbols for percona-galera-arbitrator-3

  6. percona-server-5.6-dbg - Debugging package for Percona Server

  7. percona-server-server - Percona Server database server

  8. percona-server-server-5.6 - Percona Server database server binaries

  9. percona-server-source-5.6 - Percona Server 5.6 source

  10. percona-server-test - Percona Servere regression test suite

  11. percona-server-test-5.6 - Percona Server database test suite

  12. percona-xtrabackup-dbg - Debug symbols for Percona XtraBackup

  13. percona-xtrabackup-test - Test suite for Percona XtraBackup

  14. percona-xtradb-cluster-server - Percona XtraDB Cluster database server

  15. percona-xtradb-cluster-server-5.6 - Percona XtraDB Cluster database server binaries

  16. xtrabackup - Transitional package for percona-xtrabackup

  17. libperconaserverclient18 - Percona Server database client library

  18. libperconaserverclient18-dev - Percona Server database development files

  19. libperconaserverclient18.1 - Percona Server database client library

  20. libperconaserverclient18.1-dev - Percona Server database development files

  21. libperconaserverclient20 - Percona Server database client library

  22. libperconaserverclient20-dev - Percona Server database development files

  23. percona-cacti-templates - Percona Monitoring Plugins for Cacti

  24. percona-nagios-plugins - Percona Monitoring Plugins for Nagios

  25. percona-release - Package to install Percona gpg key and APT repo

  26. percona-server-5.5-dbg - Debugging package for Percona Server

  27. percona-server-5.7-dbg - Debugging package for Percona Server

  28. percona-server-client - Percona Server database client

  29. percona-server-client-5.5 - Percona Server database client binaries

  30. percona-server-client-5.6 - Percona Server database client binaries

  31. percona-server-client-5.7 - Percona Server database client binaries

  32. percona-server-common-5.5 - Percona Server database common files

  33. percona-server-common-5.6 - Percona Server database common files (e.g. /etc/mysql/my.cnf)

  34. percona-server-common-5.7 - Percona Server database common files (e.g. /etc/mysql/my.cnf)

  35. percona-server-mongodb - This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.

  36. percona-server-mongodb-32 - This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.

  37. percona-server-mongodb-32-dbg - Debugging package for Percona Server for MongoDB

  38. percona-server-mongodb-32-mongos - This package contains mongos - the Percona Server for MongoDB sharded cluster query router

  39. percona-server-mongodb-32-server - This package contains the Percona Server for MongoDB server software, default configuration files and init.d scripts

  40. percona-server-mongodb-32-shell - This package contains the Percona Server for MongoDB shell

  41. percona-server-mongodb-32-tools - Mongo tools for high-performance MongoDB fork from Percona

  42. percona-server-mongodb-34 - This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.

  43. percona-server-mongodb-34-dbg - Debugging package for Percona Server for MongoDB

  44. percona-server-mongodb-34-mongos - This package contains mongos - the Percona Server for MongoDB sharded cluster query router

  45. percona-server-mongodb-34-server - This package contains the Percona Server for MongoDB server software, default configuration files and init.d scripts

  46. percona-server-mongodb-34-shell - This package contains the Percona Server for MongoDB shell

  47. percona-server-mongodb-34-tools - Mongo tools for high-performance MongoDB fork from Percona

  48. percona-server-mongodb-36 - This metapackage will install the mongo shell, import/export tools, other client utilities, server software, default configuration, and init.d scripts.

  49. percona-server-mongodb-36-dbg - Debugging package for Percona Server for MongoDB

  50. percona-server-mongodb-36-mongos - This package contains mongos - the Percona Server for MongoDB sharded cluster query router

  51. percona-server-mongodb-36-server - This package contains the Percona Server for MongoDB server software, default configuration files and init.d scripts

  52. percona-server-mongodb-36-shell - This package contains the Percona Server for MongoDB shell

  53. percona-server-mongodb-36-tools - Mongo tools for high-performance MongoDB fork from Percona

  54. percona-server-mongodb-dbg - Debugging package for Percona Server for MongoDB

  55. percona-server-mongodb-mongos - This package contains mongos - the Percona Server for MongoDB sharded cluster query router

  56. percona-server-mongodb-server - This package contains the Percona Server for MongoDB server software, default configuration files and init.d scripts

  57. percona-server-mongodb-shell - This package contains the Percona Server for MongoDB shell

  58. percona-server-mongodb-tools - Mongo tools for high-performance MongoDB fork from Percona

  59. percona-server-rocksdb-5.7 - MyRocks storage engine plugin for Percona Server

  60. percona-server-server-5.5 - Percona Server database server binaries

  61. percona-server-server-5.7 - Percona Server database server binaries

  62. percona-server-source-5.5 - Percona Server 5.5 source

  63. percona-server-source-5.7 - Percona Server 5.7 source

  64. percona-server-test-5.5 - Percona Server database test suite

  65. percona-server-test-5.7 - Percona Server database test suite

  66. percona-server-tokudb-5.6 - TokuDB engine plugin for Percona Server

  67. percona-server-tokudb-5.7 - TokuDB engine plugin for Percona Server

  68. percona-toolkit - Advanced MySQL and system command-line tools

  69. percona-xtrabackup - Open source backup tool for InnoDB and XtraDB

  70. percona-xtrabackup-24 - Open source backup tool for InnoDB and XtraDB

  71. percona-xtrabackup-dbg-24 - Debug symbols for Percona XtraBackup

  72. percona-xtrabackup-test-24 - Test suite for Percona XtraBackup

  73. percona-xtradb-cluster-5.6-dbg - Debugging package for Percona XtraDB Cluster

  74. percona-xtradb-cluster-5.7-dbg - Debugging package for Percona XtraDB Cluster

  75. percona-xtradb-cluster-56 - Percona XtraDB Cluster with Galera

  76. percona-xtradb-cluster-57 - Percona XtraDB Cluster with Galera

  77. percona-xtradb-cluster-client-5.6 - Percona XtraDB Cluster database client binaries

  78. percona-xtradb-cluster-client-5.7 - Percona XtraDB Cluster database client binaries

  79. percona-xtradb-cluster-common-5.6 - Percona XtraDB Cluster database common files (e.g. /etc/mysql/my.cnf)

  80. percona-xtradb-cluster-common-5.7 - Percona XtraDB Cluster database common files (e.g. /etc/mysql/my.cnf)

  81. percona-xtradb-cluster-full-56 - Percona XtraDB Cluster with Galera

  82. percona-xtradb-cluster-full-57 - Percona XtraDB Cluster with Galera

  83. percona-xtradb-cluster-galera-3 - Metapackage for latest version of galera3.

  84. percona-xtradb-cluster-galera-3.x - Galera components of Percona XtraDB Cluster

  85. percona-xtradb-cluster-galera-3.x-dbg - Debugging package for Percona XtraDB Cluster Galera 3.

  86. percona-xtradb-cluster-galera3-dbg - Metapackage for latest version of debug packages.

  87. percona-xtradb-cluster-garbd-3 - Metapackage for latest version of garbd3.

  88. percona-xtradb-cluster-garbd-3.x - Garbd components of Percona XtraDB Cluster

  89. percona-xtradb-cluster-garbd-3.x-dbg - Debugging package for Percona XtraDB Cluster Garbd 3.

  90. percona-xtradb-cluster-garbd-5.7 - Garbd components of Percona XtraDB Cluster

  91. percona-xtradb-cluster-garbd-debug-5.7 - Debugging package for Percona XtraDB Cluster Garbd.

  92. percona-xtradb-cluster-server-5.7 - Percona XtraDB Cluster database server binaries

  93. percona-xtradb-cluster-server-debug-5.6 - Percona XtraDB Cluster database server UNIV_DEBUG binaries

  94. percona-xtradb-cluster-server-debug-5.7 - Percona XtraDB Cluster database server UNIV_DEBUG binaries

  95. percona-xtradb-cluster-source-5.6 - Percona XtraDB Cluster 5.6 source

  96. percona-xtradb-cluster-source-5.7 - Percona XtraDB Cluster 5.7 source

  97. percona-xtradb-cluster-test-5.6 - Percona XtraDB Cluster database test suite

  98. percona-xtradb-cluster-test-5.7 - Percona XtraDB Cluster database test suite

  99. percona-zabbix-templates - Percona Monitoring Plugins for Zabbix

  100. pmm-client - Percona Monitoring and Management Client

 

可以发现,有很多软件包可以选择,由于建立多节点读写结构的集群需要用到galera插件,所以我们不如一步到位安装完整版本,这样安装大小虽然会有点大,但是不用费脑筋去研究每个包的功能和依赖了,方法如下

apt-get install percona-xtradb-cluster-full-57

这样也是官方手册上推荐的方案,大概有下面这些软件包需要安装

 
  1. The following additional packages will be installed:

  2. bzip2 debsums ifupdown iproute iproute2 isc-dhcp-client isc-dhcp-common krb5-locales libaio1 libasn1-8-heimdal

  3. libatm1 libboost-program-options1.58.0 libbsd0 libcurl3 libdbd-mysql-perl libdbi-perl libdns-export162 libdpkg-perl

  4. libev4 libffi6 libfile-fcntllock-perl libfile-fnmatch-perl libgdbm3 libgmp10 libgnutls30 libgssapi-krb5-2

  5. libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhogweed4 libhx509-5-heimdal

  6. libisc-export160 libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libmecab2

  7. libmnl0 libmysqlclient20 libnettle6 libnuma1 libp11-kit0 libperl5.22 libpopt0 libroken18-heimdal librtmp1 libsasl2-2

  8. libsasl2-modules libsasl2-modules-db libtasn1-6 libwind0-heimdal libwrap0 libxtables11 lsof mysql-common netbase

  9. netcat-openbsd percona-xtrabackup-24 percona-xtradb-cluster-5.7-dbg percona-xtradb-cluster-client-5.7

  10. percona-xtradb-cluster-common-5.7 percona-xtradb-cluster-garbd-5.7 percona-xtradb-cluster-garbd-debug-5.7

  11. percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-debug-5.7 percona-xtradb-cluster-test-5.7 perl

  12. perl-modules-5.22 psmisc qpress rename rsync socat tcpd ucf xz-utils

  13. Suggested packages:

  14. bzip2-doc ppp rdnssd iproute2-doc resolvconf avahi-autoipd isc-dhcp-client-ddns apparmor libclone-perl libmldbm-perl

  15. libnet-daemon-perl libsql-statement-perl debian-keyring gcc | c-compiler binutils patch gnutls-bin krb5-doc

  16. krb5-user libsasl2-modules-otp libsasl2-modules-ldap libsasl2-modules-sql libsasl2-modules-gssapi-mit

  17. | libsasl2-modules-gssapi-heimdal tinyca pv perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make

  18. openssh-client openssh-server

  19. The following NEW packages will be installed:

  20. bzip2 debsums ifupdown iproute iproute2 isc-dhcp-client isc-dhcp-common krb5-locales libaio1 libasn1-8-heimdal

  21. libatm1 libboost-program-options1.58.0 libbsd0 libcurl3 libdbd-mysql-perl libdbi-perl libdns-export162 libdpkg-perl

  22. libev4 libffi6 libfile-fcntllock-perl libfile-fnmatch-perl libgdbm3 libgmp10 libgnutls30 libgssapi-krb5-2

  23. libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal libhogweed4 libhx509-5-heimdal

  24. libisc-export160 libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 libmecab2

  25. libmnl0 libmysqlclient20 libnettle6 libnuma1 libp11-kit0 libperl5.22 libpopt0 libroken18-heimdal librtmp1 libsasl2-2

  26. libsasl2-modules libsasl2-modules-db libtasn1-6 libwind0-heimdal libwrap0 libxtables11 lsof mysql-common netbase

  27. netcat-openbsd percona-xtrabackup-24 percona-xtradb-cluster-5.7-dbg percona-xtradb-cluster-client-5.7

  28. percona-xtradb-cluster-common-5.7 percona-xtradb-cluster-full-57 percona-xtradb-cluster-garbd-5.7

  29. percona-xtradb-cluster-garbd-debug-5.7 percona-xtradb-cluster-server-5.7 percona-xtradb-cluster-server-debug-5.7

  30. percona-xtradb-cluster-test-5.7 perl perl-modules-5.22 psmisc qpress rename rsync socat tcpd ucf xz-utils

  31. 0 upgraded, 80 newly installed, 0 to remove and 2 not upgraded.

  32. Need to get 262 MB of archives.

  33. After this operation, 960 MB of additional disk space will be used.

如果网速好的话,等待三五分钟就能安装完毕,然后我们只需简单配置一下配置文件一个pxc集群就搭建好了。

在安装快要结束的时候会需要你输入数据库登陆的root密码

 
  1. Configuring percona-xtradb-cluster-server-5.7

  2. ---------------------------------------------

  3.  
  4. Data directory found when no Percona Server package is installed

  5.  
  6. A data directory '/var/lib/mysql' is present on this system when no MySQL server package is currently installed on the

  7. system. The directory may be under control of server package received from third-party vendors. It may also be an

  8. unclaimed data directory from previous removal of mysql packages.

  9.  
  10. It is highly recommended to take data backup. If you have not done so, now would be the time to take backup in another

  11. shell. Once completed, press 'Ok' to continue.

  12.  
  13. Please provide a strong password that will be set for the root account of your MySQL database. Leave it blank to enable

  14. password less login using UNIX socket based authentication.

  15.  
  16. Enter root password:

  17.  
  18. Now that you have selected a password for the root account, please confirm by typing it again. Do not share the

  19. password with anyone.

  20.  
  21. Re-enter root password:

在安装完毕之后,percona数据库会不自觉的开启,所以我们首先要做的就是关闭

service mysql stop

到这里为止,主从节点都是一样的,下面要根据不同节点写不同的配置文件,并使用不同的启动方式

通过deb包安装的percona数据库不要试图修改/etc/mysql/my.cnf,而要把和节点有关的配置写到/etc/mysql/percona-xtradb-cluster.conf.d/wsrep.cnf里面,这个文件已经给了模板,我们只需稍加修改即可

 

 
  1. [mysqld]

  2. # Path to Galera library

  3. wsrep_provider=/usr/lib/galera3/libgalera_smm.so #这里是galera框架的库地址,需要保证你的磁盘上有这个文件

  4.  
  5. # Cluster connection URL contains IPs of nodes

  6. #If no IP is found, this implies that a new cluster needs to be created,

  7. #in order to do that you need to bootstrap this node

  8. wsrep_cluster_address=gcomm://172.17.0.2,172.17.0.3,172.7.0.4 #在这里记录集群里所有节点的信息,但不是所有节点都需要开启

  9.  
  10. # In order for Galera to work correctly binlog format should be ROW

  11. binlog_format=ROW #只能填ROW

  12.  
  13. # MyISAM storage engine has only experimental support

  14. default_storage_engine=InnoDB

  15.  
  16. # Slave thread to use

  17. wsrep_slave_threads= 8

  18.  
  19. wsrep_log_conflicts

  20.  
  21. # This changes how InnoDB autoincrement locks are managed and is a requirement for Galera

  22. innodb_autoinc_lock_mode=2

  23.  
  24. # Node IP address

  25. wsrep_node_address=172.17.0.2 #填写当前节点的IP地址

  26. # Cluster name

  27. wsrep_cluster_name=pxc-cluster #填写集群的名字,系群里所有节点都要填的一样

  28.  
  29. #If wsrep_node_name is not specified, then system hostname will be used

  30. wsrep_node_name=pxc-cluster-node-1 #当前节点的名称,随便填

  31.  
  32. #pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER

  33. pxc_strict_mode=ENFORCING #开启严格强一致模式,没有主键的表将不能操作

  34.  
  35. # SST method

  36. wsrep_sst_method=xtrabackup-v2

  37.  
  38. #Authentication for SST method

  39. wsrep_sst_auth=sstuser:123456 #设置一个用户名和密码,这个一会开启mysql后还需要手动再配置一遍

主节点和从节点基本配置都是一样的,区别就两行

 

  • For the second node:

    wsrep_node_name=pxc2
    wsrep_node_address=172.17.0.3
    
  • For the third node:

    wsrep_node_name=pxc3
    wsrep_node_address=172.17.0.4

 

节点的启动是有讲究的,第一个节点要建立上面配置文件里面写的sstuser用户,并且使用bootstrap启动,其余两个节点都可以简单的启动mysql即可。其中在第一个节点要建立如下用户

 

mysql> CREATE USER 'sstuser'@'localhost' IDENTIFIED BY '123456';
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost';
mysql> FLUSH PRIVILEGES;

然后就可以开启第一个节点了

 

/etc/init.d/mysql bootstrap-pxc

然后可以执行一个命令查看当前节点的工作状态,注意另外两个节点不开启也不妨碍当前节点正常工作,在另外两个节点开启后,已经插入的数据会同步到另外两个节点上去。

 

mysql> show status like 'wsrep%';
+----------------------------+--------------------------------------+
| Variable_name              | Value                                |
+----------------------------+--------------------------------------+
| wsrep_local_state_uuid     | 39e4729d-6345-11e8-8c52-0b4b3546a2be |
| ...                        | ...                                  |
| wsrep_local_state          | 4                                    |
| wsrep_local_state_comment  | Synced                               |
| ...                        | ...                                  |
| wsrep_cluster_size         | 1                                    |
| wsrep_cluster_status       | Primary                              |
| wsrep_connected            | ON                                   |
| ...                        | ...                                  |
| wsrep_ready                | ON                                   |
+----------------------------+--------------------------------------+
40 rows in set (0.01 sec)

 

开启从节点就简单的多了,只需要把配置文件按照主节点写好,然后启动一下就可以了

/etc/init.d/mysql start

然后在从库上就可以查看当前工作状态

 
  1. mysql> show status like 'wsrep%';

  2. +----------------------------------+--------------------------------------+

  3. | Variable_name | Value |

  4. +----------------------------------+--------------------------------------+

  5. | wsrep_local_state_uuid | 39e4729d-6345-11e8-8c52-0b4b3546a2be |

  6. | wsrep_protocol_version | 8 |

  7. | wsrep_last_applied | 3 |

  8. | wsrep_last_committed | 3 |

  9. | wsrep_replicated | 0 |

  10. | wsrep_replicated_bytes | 0 |

  11. | wsrep_repl_keys | 0 |

  12. | wsrep_repl_keys_bytes | 0 |

  13. | wsrep_repl_data_bytes | 0 |

  14. | wsrep_repl_other_bytes | 0 |

  15. | wsrep_received | 3 |

  16. | wsrep_received_bytes | 240 |

  17. | wsrep_local_commits | 0 |

  18. | wsrep_local_cert_failures | 0 |

  19. | wsrep_local_replays | 0 |

  20. | wsrep_local_send_queue | 0 |

  21. | wsrep_local_send_queue_max | 1 |

  22. | wsrep_local_send_queue_min | 0 |

  23. | wsrep_local_send_queue_avg | 0.000000 |

  24. | wsrep_local_recv_queue | 0 |

  25. | wsrep_local_recv_queue_max | 1 |

  26. | wsrep_local_recv_queue_min | 0 |

  27. | wsrep_local_recv_queue_avg | 0.000000 |

  28. | wsrep_local_cached_downto | 0 |

  29. | wsrep_flow_control_paused_ns | 0 |

  30. | wsrep_flow_control_paused | 0.000000 |

  31. | wsrep_flow_control_sent | 0 |

  32. | wsrep_flow_control_recv | 0 |

  33. | wsrep_flow_control_interval | [ 141, 141 ] |

  34. | wsrep_flow_control_interval_low | 141 |

  35. | wsrep_flow_control_interval_high | 141 |

  36. | wsrep_flow_control_status | OFF |

  37. | wsrep_cert_deps_distance | 0.000000 |

  38. | wsrep_apply_oooe | 0.000000 |

  39. | wsrep_apply_oool | 0.000000 |

  40. | wsrep_apply_window | 0.000000 |

  41. | wsrep_commit_oooe | 0.000000 |

  42. | wsrep_commit_oool | 0.000000 |

  43. | wsrep_commit_window | 0.000000 |

  44. | wsrep_local_state | 4 |

  45. | wsrep_local_state_comment | Synced |

  46. | wsrep_cert_index_size | 0 |

  47. | wsrep_cert_bucket_count | 22 |

  48. | wsrep_gcache_pool_size | 1456 |

  49. | wsrep_causal_reads | 0 |

  50. | wsrep_cert_interval | 0.000000 |

  51. | wsrep_ist_receive_status | |

  52. | wsrep_ist_receive_seqno_start | 0 |

  53. | wsrep_ist_receive_seqno_current | 0 |

  54. | wsrep_ist_receive_seqno_end | 0 |

  55. | wsrep_incoming_addresses | 172.17.0.2:3306,172.17.0.3:3306 |

  56. | wsrep_desync_count | 0 |

  57. | wsrep_evs_delayed | |

  58. | wsrep_evs_evict_list | |

  59. | wsrep_evs_repl_latency | 0/0/0/0/0 |

  60. | wsrep_evs_state | OPERATIONAL |

  61. | wsrep_gcomm_uuid | 84fe5351-6346-11e8-9080-ea323a394028 |

  62. | wsrep_cluster_conf_id | 2 |

  63. | wsrep_cluster_size | 2 |

  64. | wsrep_cluster_state_uuid | 39e4729d-6345-11e8-8c52-0b4b3546a2be |

  65. | wsrep_cluster_status | Primary |

  66. | wsrep_connected | ON |

  67. | wsrep_local_bf_aborts | 0 |

  68. | wsrep_local_index | 1 |

  69. | wsrep_provider_name | Galera |

  70. | wsrep_provider_vendor | Codership Oy <info@codership.com> |

  71. | wsrep_provider_version | 3.26(rac090bc) |

  72. | wsrep_ready | ON |

  73. +----------------------------------+--------------------------------------+

  74. 68 rows in set (0.00 sec)

可以从wsrep_incoming_addresses这一项发现当前有几个激活的了的节点

主库和从库的区别就在于启动方式不一样,谁以bootstrap方式启动谁就是主库,一个从库可以前后隶属于不同的主库,比如一开始跟A是集群,然后我把从库停掉,修改从库的配置文件,把从库归到B集群里,那么在启动过程中就会自动的把从库的数据全部替换为B集群的数据,非常方便,不需要手动干预。

此时我们就可以在主库和从库里写东西,看看两边是否同步了。

像第二个一样,开启第三个节点后,可以在第三个节点上观察集群的运行情况

 
  1. mysql> show status like 'wsrep%';

  2. +----------------------------------+-------------------------------------------------+

  3. | Variable_name | Value |

  4. +----------------------------------+-------------------------------------------------+

  5. | wsrep_local_state_uuid | 39e4729d-6345-11e8-8c52-0b4b3546a2be |

  6. | wsrep_protocol_version | 8 |

  7. | wsrep_last_applied | 4 |

  8. | wsrep_last_committed | 4 |

  9. | wsrep_replicated | 0 |

  10. | wsrep_replicated_bytes | 0 |

  11. | wsrep_repl_keys | 0 |

  12. | wsrep_repl_keys_bytes | 0 |

  13. | wsrep_repl_data_bytes | 0 |

  14. | wsrep_repl_other_bytes | 0 |

  15. | wsrep_received | 3 |

  16. | wsrep_received_bytes | 320 |

  17. | wsrep_local_commits | 0 |

  18. | wsrep_local_cert_failures | 0 |

  19. | wsrep_local_replays | 0 |

  20. | wsrep_local_send_queue | 0 |

  21. | wsrep_local_send_queue_max | 1 |

  22. | wsrep_local_send_queue_min | 0 |

  23. | wsrep_local_send_queue_avg | 0.000000 |

  24. | wsrep_local_recv_queue | 0 |

  25. | wsrep_local_recv_queue_max | 1 |

  26. | wsrep_local_recv_queue_min | 0 |

  27. | wsrep_local_recv_queue_avg | 0.000000 |

  28. | wsrep_local_cached_downto | 0 |

  29. | wsrep_flow_control_paused_ns | 0 |

  30. | wsrep_flow_control_paused | 0.000000 |

  31. | wsrep_flow_control_sent | 0 |

  32. | wsrep_flow_control_recv | 0 |

  33. | wsrep_flow_control_interval | [ 173, 173 ] |

  34. | wsrep_flow_control_interval_low | 173 |

  35. | wsrep_flow_control_interval_high | 173 |

  36. | wsrep_flow_control_status | OFF |

  37. | wsrep_cert_deps_distance | 0.000000 |

  38. | wsrep_apply_oooe | 0.000000 |

  39. | wsrep_apply_oool | 0.000000 |

  40. | wsrep_apply_window | 0.000000 |

  41. | wsrep_commit_oooe | 0.000000 |

  42. | wsrep_commit_oool | 0.000000 |

  43. | wsrep_commit_window | 0.000000 |

  44. | wsrep_local_state | 4 |

  45. | wsrep_local_state_comment | Synced |

  46. | wsrep_cert_index_size | 0 |

  47. | wsrep_cert_bucket_count | 22 |

  48. | wsrep_gcache_pool_size | 1456 |

  49. | wsrep_causal_reads | 0 |

  50. | wsrep_cert_interval | 0.000000 |

  51. | wsrep_ist_receive_status | |

  52. | wsrep_ist_receive_seqno_start | 0 |

  53. | wsrep_ist_receive_seqno_current | 0 |

  54. | wsrep_ist_receive_seqno_end | 0 |

  55. | wsrep_incoming_addresses | 172.17.0.4:3306,172.17.0.2:3306,172.17.0.3:3306 |

  56. | wsrep_desync_count | 0 |

  57. | wsrep_evs_delayed | |

  58. | wsrep_evs_evict_list | |

  59. | wsrep_evs_repl_latency | 0/0/0/0/0 |

  60. | wsrep_evs_state | OPERATIONAL |

  61. | wsrep_gcomm_uuid | 0e5b5865-6348-11e8-8a4c-b370847054fd |

  62. | wsrep_cluster_conf_id | 3 |

  63. | wsrep_cluster_size | 3 |

  64. | wsrep_cluster_state_uuid | 39e4729d-6345-11e8-8c52-0b4b3546a2be |

  65. | wsrep_cluster_status | Primary |

  66. | wsrep_connected | ON |

  67. | wsrep_local_bf_aborts | 0 |

  68. | wsrep_local_index | 0 |

  69. | wsrep_provider_name | Galera |

  70. | wsrep_provider_vendor | Codership Oy <info@codership.com> |

  71. | wsrep_provider_version | 3.26(rac090bc) |

  72. | wsrep_ready | ON |

  73. +----------------------------------+-------------------------------------------------+

  74. 68 rows in set (0.01 sec)

同时,第三个节点开启之前前两个节点写入的数据都成功同步到了第三节点。

同时,不光从节点可以自由的切换自己所在的集群,主节点也是一样。可以通过启动上的区别加入别的集群成为一个从节点,或者组织新的集群。

如果想使用数据库中间件实现读写分离,那么可以参考我的另外两篇博客

 

PXC+Replication多主多从MySQL集群搭建

MYCAT轻松实现mysql读写分离

MySQL5.7.18基于事务的主从复制和读写分离

 

 

https://blog.csdn.net/lvshaorong/article/details/80501905

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值