MySQL集羣高可用

MySQL高可用 Galera Cluster

Galera Cluster

Galera Cluster:集成了Galera插件的MySQL集羣,是一種新型的,數據不共享的,高度冗餘的高可用方案,目前Galera Cluster有兩個版本,分別是Percona Xtradb Cluster及MariaDB Cluster,Galera自己是具備多主特性的,即採用multi-master的集羣架構,是一個既穩健,又在數據一致性、完整性及高性能方面有出色表現的高可用解決方案

在這裏插入圖片描述

Galera Cluster特色

• 多主架構:真正的多點讀寫的集羣,在任什麼時候候讀寫數據,都是最新的

• 同步複製:集羣不一樣節點之間數據同步,沒有延遲,在數據庫掛掉以後,數據不會丟失

• 併發複製:從節點APPLY數據時,支持並行執行,更好的性能

• 故障切換:在出現數據庫故障時,因支持多點寫入,切換容易

• 熱插拔:在服務期間,若是數據庫掛了,只要監控程序發現的夠快,不可服務時間就會很是少。在節點故障期間,節點自己對集羣的影響很是小

• 自動節點克隆:在新增節點,或者停機維護時,增量數據或者基礎數據不須要人工手動備份提供,Galera Cluster會自動拉取在線節點數據,最終集羣會變爲一致

• 對應用透明:集羣的維護,對應用程序是透明的
只支持innodb存儲引擎的表

Galera Cluster 缺點

• 因爲DDL 需全局驗證經過,則集羣性能由集羣中最差性能節點決定(通常集羣節點配置都是同樣的)

• 新節點加入或延後較大的節點從新加入需全量拷貝數據(SST,State Snapshot Transfer),做爲donor( 貢獻者,如: 同步數據時的提供者)的節點在同步過程當中沒法提供讀寫

• 只支持innodb存儲引擎的表

Galera Cluster工做過程

在這裏插入圖片描述

Galera Cluster官方文檔

http://galeracluster.com/documentation-webpages/galera-documentation.pdf
http://galeracluster.com/documentation-webpages/index.html
https://www.percona.com/doc/percona-xtradb-cluster/LATEST/index.html
https://mariadb.com/kb/en/library/getting-started-with-mariadb-galera-cluster/前端

Galera Cluster包括兩個組件

• Galera replication library (galera-3)
• WSREP:MySQL extended with the Write Set Replication

WSREP複製實現

  • PXC:Percona XtraDB Cluster,是Percona對Galera的實現
    參考倉庫:
https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
  • MariaDB Galera Cluster:
    參考倉庫:
https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-5.5.X/yum/centos7-amd64/
注意:二者都須要至少三個節點,不能安裝mysql server 或 mariadb-server

PXC 原理

在這裏插入圖片描述

PXC最常使用以下4個端口號:

• 3306:數據庫對外服務的端口號
• 4444:請求SST的端口號
• 4567:組成員之間進行溝通的端口號
• 4568:用於傳輸IST的端口號

PXC中涉及到的重要概念和核心參數:

  1. 集羣中節點的數量:整個集羣中節點數量應該控制在最少3個、最多8個的範圍內。最少3個節點是爲了防止出現腦裂現象,由於只有在2個節點下才會出現此現象。腦裂現象的標誌就是輸入任何命令,返回的結果都是unknown command。節點在集羣中,會因新節點的加入或故障、同步失效等緣由發生狀態的切換。
  2. 節點狀態的變化階段:
    在這裏插入圖片描述
• open:節點啓動成功,嘗試鏈接到集羣時的狀態
• primary:節點已處於集羣中,在新節點加入並選取donor進行數據同步時的狀態
• joiner:節點處於等待接收同步文件時的狀態
• joined:節點完成數據同步工做,嘗試保持和集羣進度一致時的狀態
• synced:節點正常提供服務時的狀態,表示已經同步完成並和集羣進度保持一致
• donor:節點處於爲新加入的節點提供全量數據時的狀態
備註:donor節點就是數據的貢獻者,若是一個新節點加入集羣,此時又須要大量數據的SST數據傳輸,就有可能所以而拖垮整個集羣的性能,因此在生產環境中,若是數據量較小,還可使用SST全量數據傳輸,但若是數據量很大就不建議使用這種方式,能夠考慮先創建主從關係,而後再加入集羣。
  1. 節點的數據傳輸方式:
• SST:State Snapshot Transfer,全量數據傳輸
• IST:Incremental State Transfer,增量數據傳輸
SST數據傳輸有xtrabackup、mysqldump和rsync三種方式,而增量數據傳輸就只有一種方式xtrabackup,但生產環境中通常數據量較小時,可使用SST全量數據傳輸,但也只使用xtrabackup方法。
  1. GCache模塊:
在PXC中一個特別重要的模塊,它的核心功能就是爲每一個節點緩存當前最新的寫集。若是有新節點加入進來,就能夠把新數據的增量傳遞給新節點,而不須要再使用SST傳輸方式,這樣可讓節點更快地加入集羣中
  1. 涉及以下參數:
• gcache.size:緩存寫集增量信息的大小,它的默認大小是128MB,經過wsrep_provider_options參數設置,建議調整爲2GB~4GB範圍,足夠的空間便於緩存更多的增量信息。
• gcache.mem_size:GCache中內存緩存的大小,適度調大能夠提升整個集羣的性能
• gcache.page_size:若是內存不夠用(GCache不足),就直接將寫集寫入磁盤文件中

實戰案例:Percona XtraDB Cluster(PXC 5.7)

  1. 環境準備node

    四臺主機:mysql

    pxc1:192.168.39.7
    pxc1:192.168.39.27
    pxc1:192.168.39.37
    pxc4:192.168.39.47

       OS版本git

    [root@pxc1 ~]#cat /etc/redhat-release 
    CentOS Linux release 7.6.1810 (Core)
    關閉防火牆和SELinux,保證時間同步
    注意:若是已經安裝MySQL,必須卸載
  2. 安裝 Percona XtraDB Cluster 5.7
  • 配置yum源
[root@pxc1 ~]#vim /etc/yum.repos.d/percona_pxc.repo

[percona]
name=percona_repo
baseurl=https://mirrors.tuna.tsinghua.edu.cn/percona/release/$releasever/RPMS/$basearch
enabled=1
gpgcheck=0
  • 其餘三臺虛擬機也配置yum源
scp /etc/yum.repos.d/percona_pxc.repo 192.168.39.27:/etc/yum.repos.d/
scp /etc/yum.repos.d/percona_pxc.repo 192.168.39.37:/etc/yum.repos.d/
scp /etc/yum.repos.d/percona_pxc.repo 192.168.39.47:/etc/yum.repos.d/
  • 在三個節點都安裝好PXC 5.7
[root@pxc1 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc2 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc3 ~]#yum install Percona-XtraDB-Cluster-57 -y
[root@pxc4 ~]#yum install Percona-XtraDB-Cluster-57 -y
  1. 在各個節點上分別配置mysql及集羣配置文件
/etc/my.cnf爲主配置文件,當前版本中,其他的配置文件都放在/etc/percona-xtradb-cluster.conf.d目錄裏,包括mysqld.cnf,mysqld_safe.cnf,wsrep.cnf 三個文件
#主配置文件不須要修改
[root@pxc1 ~]#cat /etc/my.cnf
#
# The Percona XtraDB Cluster 5.7 configuration file.
#
#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#   Please make any edits and changes to the appropriate sectional files
#   included below.
#
!includedir /etc/my.cnf.d/
!includedir /etc/percona-xtradb-cluster.conf.d/

[root@pxc1 ~]#ls /etc/percona-xtradb-cluster.conf.d/  # 查看一下三個文件
mysqld.cnf  mysqld_safe.cnf  wsrep.cnf

#下面配置文件不須要修改
[root@pxc1 ~]#cat /etc/percona-xtradb-cluster.conf.d/mysqld.cnf 
# Template my.cnf for PXC
# Edit to your requirements.
[client]
socket=/var/lib/mysql/mysql.sock

[mysqld]
server-id=1   # 建議各個節點不一樣
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
log-bin   # 建議啓用,非必須項
log_slave_updates
expire_logs_days=7

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#下面配置文件不須要修改
[root@pxc1 ~]#cat /etc/percona-xtradb-cluster.conf.d/mysqld_safe.cnf 
#
# The Percona Server 5.7 configuration file.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
socket   = /var/lib/mysql/mysql.sock
nice     = 0

#PXC的配置文件必須修改
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://10.0.0.7,10.0.0.17,10.0.0.27 #三個節點的IP
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=10.0.0.7 #各個節點,指定自已的IP
wsrep_cluster_name=pxc-cluster   # 集羣名稱
wsrep_node_name=pxc-cluster-node-1 #各個節點,指定自已節點名稱
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:centos7" #取消本行註釋,能夠改一個本身的密碼

# 根據以上修改信息全部主機都修改
[root@pxc2 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf  
[root@pxc3 ~]#vim /etc/percona-xtradb-cluster.conf.d/wsrep.cnf

[root@pxc1 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf # 經過這個命令查看配置文件
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.39.7,192.168.39.27,192.168.39.37
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.39.7
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-1
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:centos7"
注意:儘管Galera Cluster再也不須要經過binlog的形式進行同步,但仍是建議在配置文件中開啓二進制日誌功能,緣由是後期若是有新節點須要加入,老節點經過SST全量傳輸的方式向新節點傳輸數據,極可能會拖垮集羣性能,因此讓新節點先經過binlog方式完成同步後再加入集羣會是一種更好的選擇
  1. 配置文件各項配置意義
    在這裏插入圖片描述
    在這裏插入圖片描述
[root@pxc1 ~]#ss -ntul
Netid State      Recv-Q Send-Q                                               Local Address:Port                                                              Peer Address:Port              
udp   UNCONN     0      0                                                    192.168.122.1:53                                                                           *:*                  
udp   UNCONN     0      0                                                         *%virbr0:67                                                                           *:*                  
udp   UNCONN     0      0                                                                *:111                                                                          *:*                  
udp   UNCONN     0      0                                                                *:5353                                                                         *:*                  
udp   UNCONN     0      0                                                                *:58153                                                                        *:*                  
udp   UNCONN     0      0                                                                *:812                                                                          *:*                  
udp   UNCONN     0      0                                                               :::111                                                                         :::*                  
udp   UNCONN     0      0                                                               :::812                                                                         :::*                  
tcp   LISTEN     0      128                                                              *:111                                                                          *:*                  
tcp   LISTEN     0      128                                                              *:6000                                                                         *:*                  
tcp   LISTEN     0      5                                                    192.168.122.1:53                                                                           *:*                  
tcp   LISTEN     0      128                                                              *:22                                                                           *:*                  
tcp   LISTEN     0      128                                                      127.0.0.1:631                                                                          *:*                  
tcp   LISTEN     0      128                                                      127.0.0.1:6010                                                                         *:*                  
tcp   LISTEN     0      128                                                             :::111                                                                         :::*                  
tcp   LISTEN     0      128                                                             :::6000                                                                        :::*                  
tcp   LISTEN     0      128                                                             :::22                                                                          :::*                  
tcp   LISTEN     0      128                                                            ::1:631                                                                         :::*                  
tcp   LISTEN     0      128                                                            ::1:6010                                                                        :::*            

#啓動第一人節點(第一個節點啓動和別的節點啓動都不同)屬於建立集羣的服務器如今階段能夠理解爲主服務器,可是集羣作完以後就沒有主從之分了都是主
[root@pxc1 ~]#systemctl start mysql@bootstrap.service
[root@pxc1 ~]#ss -ntul  # 如下兩個端口啓動起來就好了 (330六、4567)
。。。。。。。。。。。。。。。。。。。。。。。
Netid  State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
udp    UNCONN     0      0      192.168.122.1:53                         *:*                  
udp    UNCONN     0      0       *%virbr0:67                         *:*                  
udp    UNCONN     0      0              *:111                        *:*                  
udp    UNCONN     0      0              *:5353                       *:*                  
udp    UNCONN     0      0              *:58153                      *:*                  
udp    UNCONN     0      0              *:812                        *:*                  
udp    UNCONN     0      0             :::111                       :::*                  
udp    UNCONN     0      0             :::812                       :::*                  
tcp    LISTEN     0      128            *:111                        *:*                  
tcp    LISTEN     0      128            *:6000                       *:*                  
tcp    LISTEN     0      5      192.168.122.1:53                         *:*                  
tcp    LISTEN     0      128            *:22                         *:*                  
tcp    LISTEN     0      128            *:4567                       *:*           
tcp    LISTEN     0      128    127.0.0.1:631                        *:*                  
tcp    LISTEN     0      128    127.0.0.1:6010                       *:*                  
tcp    LISTEN     0      80            :::3306                      :::*           
tcp    LISTEN     0      128           :::111                       :::*                  
tcp    LISTEN     0      128           :::6000                      :::*                  
tcp    LISTEN     0      128           :::22                        :::*                  
tcp    LISTEN     0      128          ::1:631                       :::*                  
tcp    LISTEN     0      128          ::1:6010                      :::*
  1. 找到MySQL的臨時密碼,第一次登陸要修改要不執行不了管理操做。
[root@centos7 ~]#grep "password" /var/log/mysqld.log 
2019-12-03T06:13:39.187929Z 1 [Note] A temporary password is generated for root@localhost: a>#;L(Sm4Ln:

[root@centos7 ~]#mysql -uroot -p'a>#;L(Sm4Ln:'   # 須要加引號引發來
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> show databases; # 執行查看庫操做提示必須重置密碼
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. # 在執行此語句以前,必須使用ALTER USER語句重置密碼.

mysql> alter user root@'localhost' identified by 'magedu';  # 修改密碼(其餘節點不用修改加入集羣之後會自動同步密碼)
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@centos7 ~]#mysql -uroot -pmagedu  # 修改之後從新登陸
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql>
  1. 查看變量
# 查看寫集複製相關變量
mysql> SHOW VARIABLES LIKE 'wsrep%'\G
*************************** 1. row ***************************
Variable_name: wsrep_OSU_method
        Value: TOI
*************************** 2. row ***************************
Variable_name: wsrep_RSU_commit_timeout
        Value: 5000
*************************** 3. row ***************************
Variable_name: wsrep_auto_increment_control
        Value: ON
*************************** 4. row ***************************
Variable_name: wsrep_causal_reads
        Value: OFF
*************************** 5. row ***************************
Variable_name: wsrep_certification_rules
        Value: strict
*************************** 6. row ***************************
Variable_name: wsrep_certify_nonPK
        Value: ON
*************************** 7. row ***************************
Variable_name: wsrep_cluster_address
        Value: gcomm://192.168.39.7,192.168.39.27,192.168.39.37
*************************** 8. row ***************************
Variable_name: wsrep_cluster_name
        Value: pxc-cluster
*************************** 9. row ***************************
Variable_name: wsrep_convert_LOCK_to_trx
        Value: OFF
*************************** 10. row ***************************
Variable_name: wsrep_data_home_dir
        Value: /var/lib/mysql/
*************************** 11. row ***************************
Variable_name: wsrep_dbug_option
        Value: 
*************************** 12. row ***************************
Variable_name: wsrep_debug
        Value: OFF
*************************** 13. row ***************************
Variable_name: wsrep_desync
        Value: OFF
*************************** 14. row ***************************
Variable_name: wsrep_dirty_reads
        Value: OFF
*************************** 15. row ***************************
Variable_name: wsrep_drupal_282555_workaround
        Value: OFF
*************************** 16. row ***************************
Variable_name: wsrep_forced_binlog_format
        Value: NONE
*************************** 17. row ***************************
Variable_name: wsrep_load_data_splitting
        Value: ON
*************************** 18. row ***************************
Variable_name: wsrep_log_conflicts
        Value: ON
*************************** 19. row ***************************
Variable_name: wsrep_max_ws_rows
        Value: 0
*************************** 20. row ***************************
Variable_name: wsrep_max_ws_size
        Value: 2147483647
*************************** 21. row ***************************
Variable_name: wsrep_node_address
        Value: 192.168.39.7
*************************** 22. row ***************************
Variable_name: wsrep_node_incoming_address
        Value: AUTO
*************************** 23. row ***************************
Variable_name: wsrep_node_name
        Value: pxc-cluster-node-1
*************************** 24. row ***************************
Variable_name: wsrep_notify_cmd
        Value: 
*************************** 25. row ***************************
Variable_name: wsrep_on
        Value: ON
*************************** 26. row ***************************
Variable_name: wsrep_preordered
        Value: OFF
*************************** 27. row ***************************
Variable_name: wsrep_provider
        Value: /usr/lib64/galera3/libgalera_smm.so
*************************** 28. row ***************************
Variable_name: wsrep_provider_options
        Value: base_dir = /var/lib/mysql/; base_host = 192.168.39.7; base_port = 4567; cert.log_conflicts = no; cert.optimistic_pa = yes; debug = no; evs.auto_evict = 0; evs.causal_keepalive_period = PT1S; evs.debug_log_mask = 0x1; evs.delay_margin = PT1S; evs.delayed_keep_period = PT30S; evs.inactive_check_period = PT0.5S; evs.inactive_timeout = PT15S; evs.info_log_mask = 0; evs.install_timeout = PT7.5S; evs.join_retrans_period = PT1S; evs.keepalive_period = PT1S; evs.max_install_timeouts = 3; evs.send_window = 10; evs.stats_report_period = PT1M; evs.suspect_timeout = PT5S; evs.use_aggregate = true; evs.user_send_window = 4; evs.version = 0; evs.view_forget_timeout = P1D; gcache.dir = /var/lib/mysql/; gcache.freeze_purge_at_seqno = -1; gcache.keep_pages_count = 0; gcache.keep_pages_size = 0; gcache.mem_size = 0; gcache.name = /var/lib/mysql//galera.cache; gcache.page_size = 128M; gcache.recover = no; gcache.size = 128M; gcomm.thread_prio = ; gcs.fc_debug = 0; gcs.fc_factor = 1; gcs.fc_limit = 100; gcs.fc_master_slave = no;
*************************** 29. row ***************************
Variable_name: wsrep_recover
        Value: OFF
*************************** 30. row ***************************
Variable_name: wsrep_reject_queries
        Value: NONE
*************************** 31. row ***************************
Variable_name: wsrep_replicate_myisam
        Value: OFF
*************************** 32. row ***************************
Variable_name: wsrep_restart_slave
        Value: OFF
*************************** 33. row ***************************
Variable_name: wsrep_retry_autocommit
        Value: 1
*************************** 34. row ***************************
Variable_name: wsrep_slave_FK_checks
        Value: ON
*************************** 35. row ***************************
Variable_name: wsrep_slave_UK_checks
        Value: OFF
*************************** 36. row ***************************
Variable_name: wsrep_slave_threads
        Value: 8
*************************** 37. row ***************************
Variable_name: wsrep_sst_auth
        Value: ********
*************************** 38. row ***************************
Variable_name: wsrep_sst_donor
        Value: 
*************************** 39. row ***************************
Variable_name: wsrep_sst_donor_rejects_queries
        Value: OFF
*************************** 40. row ***************************
Variable_name: wsrep_sst_method
        Value: xtrabackup-v2
*************************** 41. row ***************************
Variable_name: wsrep_sst_receive_address
        Value: AUTO
*************************** 42. row ***************************
Variable_name: wsrep_start_position
        Value: 00000000-0000-0000-0000-000000000000:-1
*************************** 43. row ***************************
Variable_name: wsrep_sync_wait
        Value: 0
43 rows in set (0.01 sec)

# 查看相關狀態變量
mysql> SHOW STATUS LIKE 'wsrep%'\G
*************************** 1. row ***************************
Variable_name: wsrep_local_state_uuid
        Value: 10c3398e-1594-11ea-9f84-c2cae1c33555
*************************** 2. row ***************************
Variable_name: wsrep_protocol_version
        Value: 9
*************************** 3. row ***************************
Variable_name: wsrep_last_applied
        Value: 1
*************************** 4. row ***************************
Variable_name: wsrep_last_committed
        Value: 1
*************************** 5. row ***************************
Variable_name: wsrep_replicated
        Value: 1
*************************** 6. row ***************************
Variable_name: wsrep_replicated_bytes
        Value: 232
*************************** 7. row ***************************
Variable_name: wsrep_repl_keys
        Value: 1
*************************** 8. row ***************************
Variable_name: wsrep_repl_keys_bytes
        Value: 32
*************************** 9. row ***************************
Variable_name: wsrep_repl_data_bytes
        Value: 133
*************************** 10. row ***************************
Variable_name: wsrep_repl_other_bytes
        Value: 0
*************************** 11. row ***************************
Variable_name: wsrep_received
        Value: 2
*************************** 12. row ***************************
Variable_name: wsrep_received_bytes
        Value: 154
*************************** 13. row ***************************
Variable_name: wsrep_local_commits
        Value: 0
*************************** 14. row ***************************
Variable_name: wsrep_local_cert_failures
        Value: 0
*************************** 15. row ***************************
Variable_name: wsrep_local_replays
        Value: 0
*************************** 16. row ***************************
Variable_name: wsrep_local_send_queue
        Value: 0
*************************** 17. row ***************************
Variable_name: wsrep_local_send_queue_max
        Value: 1
*************************** 18. row ***************************
Variable_name: wsrep_local_send_queue_min
        Value: 0
*************************** 19. row ***************************
Variable_name: wsrep_local_send_queue_avg
        Value: 0.000000
*************************** 20. row ***************************
Variable_name: wsrep_local_recv_queue
        Value: 0
*************************** 21. row ***************************
Variable_name: wsrep_local_recv_queue_max
        Value: 2
*************************** 22. row ***************************
Variable_name: wsrep_local_recv_queue_min
        Value: 0
*************************** 23. row ***************************
Variable_name: wsrep_local_recv_queue_avg
        Value: 0.500000
*************************** 24. row ***************************
Variable_name: wsrep_local_cached_downto
        Value: 1
*************************** 25. row ***************************
Variable_name: wsrep_flow_control_paused_ns
        Value: 0
*************************** 26. row ***************************
Variable_name: wsrep_flow_control_paused
        Value: 0.000000
*************************** 27. row ***************************
Variable_name: wsrep_flow_control_sent
        Value: 0
*************************** 28. row ***************************
Variable_name: wsrep_flow_control_recv
        Value: 0
*************************** 29. row ***************************
Variable_name: wsrep_flow_control_interval
        Value: [ 100, 100 ]
*************************** 30. row ***************************
Variable_name: wsrep_flow_control_interval_low
        Value: 100
*************************** 31. row ***************************
Variable_name: wsrep_flow_control_interval_high
        Value: 100
*************************** 32. row ***************************
Variable_name: wsrep_flow_control_status
        Value: OFF
*************************** 33. row ***************************
Variable_name: wsrep_cert_deps_distance
        Value: 1.000000
*************************** 34. row ***************************
Variable_name: wsrep_apply_oooe
        Value: 0.000000
*************************** 35. row ***************************
Variable_name: wsrep_apply_oool
        Value: 0.000000
*************************** 36. row ***************************
Variable_name: wsrep_apply_window
        Value: 1.000000
*************************** 37. row ***************************
Variable_name: wsrep_commit_oooe
        Value: 0.000000
*************************** 38. row ***************************
Variable_name: wsrep_commit_oool
        Value: 0.000000
*************************** 39. row ***************************
Variable_name: wsrep_commit_window
        Value: 1.000000
*************************** 40. row ***************************
Variable_name: wsrep_local_state
        Value: 4
*************************** 41. row ***************************
Variable_name: wsrep_local_state_comment
        Value: Synced
*************************** 42. row ***************************
Variable_name: wsrep_cert_index_size
        Value: 1
*************************** 43. row ***************************
Variable_name: wsrep_cert_bucket_count
        Value: 22
*************************** 44. row ***************************
Variable_name: wsrep_gcache_pool_size
        Value: 1592
*************************** 45. row ***************************
Variable_name: wsrep_causal_reads
        Value: 0
*************************** 46. row ***************************
Variable_name: wsrep_cert_interval
        Value: 0.000000
*************************** 47. row ***************************
Variable_name: wsrep_open_transactions
        Value: 0
*************************** 48. row ***************************
Variable_name: wsrep_open_connections
        Value: 0
*************************** 49. row ***************************
Variable_name: wsrep_ist_receive_status
        Value: 
*************************** 50. row ***************************
Variable_name: wsrep_ist_receive_seqno_start
        Value: 0
*************************** 51. row ***************************
Variable_name: wsrep_ist_receive_seqno_current
        Value: 0
*************************** 52. row ***************************
Variable_name: wsrep_ist_receive_seqno_end
        Value: 0
*************************** 53. row ***************************
Variable_name: wsrep_incoming_addresses
        Value: 192.168.39.7:3306
*************************** 54. row ***************************
Variable_name: wsrep_cluster_weight
        Value: 1
*************************** 55. row ***************************
Variable_name: wsrep_desync_count
        Value: 0
*************************** 56. row ***************************
Variable_name: wsrep_evs_delayed
        Value: 
*************************** 57. row ***************************
Variable_name: wsrep_evs_evict_list
        Value: 
*************************** 58. row ***************************
Variable_name: wsrep_evs_repl_latency
        Value: 0/0/0/0/0
*************************** 59. row ***************************
Variable_name: wsrep_evs_state
        Value: OPERATIONAL
*************************** 60. row ***************************
Variable_name: wsrep_gcomm_uuid
        Value: 10c2c674-1594-11ea-83fc-6e0df986585c
*************************** 61. row ***************************
Variable_name: wsrep_cluster_conf_id
        Value: 1
*************************** 62. row ***************************
Variable_name: wsrep_cluster_size
        Value: 1
*************************** 63. row ***************************
Variable_name: wsrep_cluster_state_uuid
        Value: 10c3398e-1594-11ea-9f84-c2cae1c33555
*************************** 64. row ***************************
Variable_name: wsrep_cluster_status
        Value: Primary
*************************** 65. row ***************************
Variable_name: wsrep_connected
        Value: ON
*************************** 66. row ***************************
Variable_name: wsrep_local_bf_aborts
        Value: 0
*************************** 67. row ***************************
Variable_name: wsrep_local_index
        Value: 0
*************************** 68. row ***************************
Variable_name: wsrep_provider_name
        Value: Galera
*************************** 69. row ***************************
Variable_name: wsrep_provider_vendor
        Value: Codership Oy <info@codership.com>
*************************** 70. row ***************************
Variable_name: wsrep_provider_version
        Value: 3.39(rb3295e6)
*************************** 71. row ***************************
Variable_name: wsrep_ready
        Value: ON
71 rows in set (0.00 sec)

#重點內容
mysql> SHOW STATUS like 'wsrep%';
+----------------------------------+--------------------------------------+
| Variable_name                    | Value                                |
+----------------------------------+--------------------------------------+
| wsrep_local_state_uuid           | 10c3398e-1594-11ea-9f84-c2cae1c33555 |
| wsrep_protocol_version           | 9                                    |
| wsrep_last_applied               | 1                                    |
| wsrep_last_committed             | 1                                    |
| wsrep_replicated                 | 1                                    |
| wsrep_replicated_bytes           | 232                                  |
| wsrep_repl_keys                  | 1                                    |
| wsrep_repl_keys_bytes            | 32                                   |
| wsrep_repl_data_bytes            | 133                                  |
| wsrep_repl_other_bytes           | 0                                    |
| wsrep_received                   | 2                                    |
| wsrep_received_bytes             | 154                                  |
| wsrep_local_commits              | 0                                    |
| wsrep_local_cert_failures        | 0                                    |
| wsrep_local_replays              | 0                                    |
| wsrep_local_send_queue           | 0                                    |
| wsrep_local_send_queue_max       | 1                                    |
| wsrep_local_send_queue_min       | 0                                    |
| wsrep_local_send_queue_avg       | 0.000000                             |
| wsrep_local_recv_queue           | 0                                    |
| wsrep_local_recv_queue_max       | 2                                    |
| wsrep_local_recv_queue_min       | 0                                    |
| wsrep_local_recv_queue_avg       | 0.500000                             |
| wsrep_local_cached_downto        | 1                                    |
| wsrep_flow_control_paused_ns     | 0                                    |
| wsrep_flow_control_paused        | 0.000000                             |
| wsrep_flow_control_sent          | 0                                    |
| wsrep_flow_control_recv          | 0                                    |
| wsrep_flow_control_interval      | [ 100, 100 ]                         |
| wsrep_flow_control_interval_low  | 100                                  |
| wsrep_flow_control_interval_high | 100                                  |
| wsrep_flow_control_status        | OFF                                  |
| wsrep_cert_deps_distance         | 1.000000                             |
| wsrep_apply_oooe                 | 0.000000                             |
| wsrep_apply_oool                 | 0.000000                             |
| wsrep_apply_window               | 1.000000                             |
| wsrep_commit_oooe                | 0.000000                             |
| wsrep_commit_oool                | 0.000000                             |
| wsrep_commit_window              | 1.000000                             |
| wsrep_local_state                | 4                                    |
| wsrep_local_state_comment        | Synced                               |
| wsrep_cert_index_size            | 1                                    |
| wsrep_cert_bucket_count          | 22                                   |
| wsrep_gcache_pool_size           | 1592                                 |
| wsrep_causal_reads               | 0                                    |
| wsrep_cert_interval              | 0.000000                             |
| wsrep_open_transactions          | 0                                    |
| wsrep_open_connections           | 0                                    |
| wsrep_ist_receive_status         |                                      |
| wsrep_ist_receive_seqno_start    | 0                                    |
| wsrep_ist_receive_seqno_current  | 0                                    |
| wsrep_ist_receive_seqno_end      | 0                                    |
| wsrep_incoming_addresses         | 192.168.39.7:3306                    |
| wsrep_cluster_weight             | 1                                    |
| wsrep_desync_count               | 0                                    |
| wsrep_evs_delayed                |                                      |
| wsrep_evs_evict_list             |                                      |
| wsrep_evs_repl_latency           | 0/0/0/0/0                            |
| wsrep_evs_state                  | OPERATIONAL                          |
| wsrep_gcomm_uuid                 | 10c2c674-1594-11ea-83fc-6e0df986585c |
| wsrep_cluster_conf_id            | 1                                    |
| wsrep_cluster_size               | 1                                    |
| wsrep_cluster_state_uuid         | 10c3398e-1594-11ea-9f84-c2cae1c33555 |
| wsrep_cluster_status             | Primary                              |
| wsrep_connected                  | ON                                   |
| wsrep_local_bf_aborts            | 0                                    |
| wsrep_local_index                | 0                                    |
| wsrep_provider_name              | Galera                               |
| wsrep_provider_vendor            | Codership Oy <info@codership.com>    |
| wsrep_provider_version           | 3.39(rb3295e6)                       |
| wsrep_ready                      | ON                                   |
+----------------------------------+--------------------------------------+
71 rows in set (0.00 sec)
說明:
wsrep_cluster_size表示,該Galera集羣中只有一個節點
wsrep_local_state_comment 狀態爲Synced(4),表示數據已同步完成(由於是第一個引導節點,無數據須要同步)。 若是狀態是Joiner, 意味着 SST 沒有完成. 只有全部節點狀態是Synced,才能夠加新節點
wsrep_cluster_status爲Primary,且已經徹底鏈接並準備好

啓動PXC集羣中其它全部節點github

  1. 啓動PXC集羣中其它全部節點
# 啓動前在第一個節點上建立一個受權帳戶(要否則可能別的節點沒有受權數據庫會起不來)
mysql> GRANT RELOAD, LOCK TABLES, PROCESS, REPLICATION CLIENT ON *.* TO 'sstuser'@'localhost' identified by 'centos7'; # 這裏受權的密碼必須和配置文件裏設置的密碼對應,全部服務器都要同樣。
Query OK, 0 rows affected, 1 warning (0.00 sec)

[root@pxc2 etc]#ss -ntl  # 啓動前端口狀況
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128               *:111                           *:*                  
LISTEN     0      128               *:6000                          *:*                  
LISTEN     0      5      192.168.122.1:53                            *:*                  
LISTEN     0      128               *:22                            *:*                  
LISTEN     0      128       127.0.0.1:631                           *:*                  
LISTEN     0      128       127.0.0.1:6010                          *:*                  
LISTEN     0      128              :::111                          :::*                  
LISTEN     0      128              :::6000                         :::*                  
LISTEN     0      128              :::22                           :::*                  
LISTEN     0      128             ::1:631                          :::*                  
LISTEN     0      128             ::1:6010                         :::*    

[root@pxc2 etc]#systemctl start mysql # 啓動數據庫
[root@pxc2 etc]#ss -ntl   # 啓動後 330六、4567 端口都出現了
State       Recv-Q Send-Q                                                 Local Address:Port                                                                Peer Address:Port              
LISTEN      0      128                                                                *:111                                                                            *:*                  
LISTEN      0      128                                                                *:6000                                                                           *:*                  
LISTEN      0      5                                                      192.168.122.1:53                                                                             *:*                  
LISTEN      0      128                                                                *:22                                                                             *:*                  
LISTEN      0      128                                                                *:4567                                                                           *:*                  
LISTEN      0      128                                                        127.0.0.1:631                                                                            *:*                  
LISTEN      0      128                                                        127.0.0.1:6010                                                                           *:*                  
LISTEN      0      128                                                        127.0.0.1:6011                                                                           *:*                  
LISTEN      0      80                                                                :::3306                                                                          :::*                  
LISTEN      0      128                                                               :::111                                                                           :::*                  
LISTEN      0      128                                                               :::6000                                                                          :::*                  
LISTEN      0      128                                                               :::22                                                                            :::*                  
LISTEN      0      128                                                              ::1:631                                                                           :::*                  
LISTEN      0      128                                                              ::1:6010                                                                          :::*                  
LISTEN      0      128                                                              ::1:6011                                                                          :::*         
[root@pxc2 etc]#mysql -uroot -pmagedu  # 登陸數據庫
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> 

# 另外一個節點同樣啓動
[root@pxc3 etc]#systemctl start mysql # 啓動數據庫
[root@pxc3 ~]#mysql -uroot -pmagedu
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql>
  1. 查看集羣狀態,驗證集羣是否成功
# 在任意節點,查看集羣狀態
mysql> SHOW VARIABLES LIKE 'wsrep_node_name';
+-----------------+--------------------+
| Variable_name   | Value              |
+-----------------+--------------------+
| wsrep_node_name | pxc-cluster-node-1 |
+-----------------+--------------------+
1 row in set (0.00 sec)

mysql> show variables like 'wsrep_node_address';
+--------------------+--------------+
| Variable_name      | Value        |
+--------------------+--------------+
| wsrep_node_address | 192.168.39.7 |
+--------------------+--------------+
1 row in set (0.01 sec)

mysql> show variables like 'wsrep_on';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_on      | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show status like 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 3     |
+--------------------+-------+
1 row in set (0.00 sec)

# 在任意節點建立數據庫測試(pxc2)
mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+---------------+
| Variable_name      | Value         |
+--------------------+---------------+
| wsrep_node_address | 192.168.39.27 |
+--------------------+---------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database db1;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

# 在pxc1上查看
mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+--------------+
| Variable_name      | Value        |
+--------------------+--------------+
| wsrep_node_address | 192.168.39.7 |
+--------------------+--------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

# 在pxc2上查看  
mysql> SHOW VARIABLES LIKE 'wsrep_node_address';
+--------------------+---------------+
| Variable_name      | Value         |
+--------------------+---------------+
| wsrep_node_address | 192.168.39.37 |
+--------------------+---------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
# 兩個節點均可以看到同步成功
  1. 測試是否有衝突問題
    在這裏插入圖片描述
#利用Xshell軟件,同時在三個節點數據庫,在其中一個節點成功
mysql> create database db2;  # 只有一個節點會成功其餘節點報錯不建立
Query OK, 1 row affected (0.00 sec)

mysql> create database db2;
ERROR 1007 (HY000): Can't create database 'db2'; database exists 

mysql> create database db2;
ERROR 1007 (HY000): Can't create database 'db2'; database exists
  1. PXC缺點(同時寫入數據性能會有影響)
# 使用一個命令循環建立10w條記錄
mysql> create table test (id int auto_increment primary key,name char(10));

delimiter ;
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> delimiter $$
mysql> 
mysql> create procedure  proc_test() 
    -> begin  
    -> declare i int;
    -> set i = 1; 
    -> while i < 100000 
    -> do  insert into test(name) values (concat('wang',i)); 
    -> set i = i +1; 
    -> end while; 
    -> end$$
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> delimiter ;
mysql> call proc_test();
Query OK, 1 row affected (3 min 39.51 sec)   # 用了39s接近40s時間才添加完成

# 使用事務方式添加記錄仍是很快的
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> call proc_test();
Query OK, 1 row affected (2.44 sec)

mysql> commit;
Query OK, 0 rows affected (0.25 sec)
  1. 添加記錄的話每一個節點添加id變化
# 每一個節點添加記錄可重複。(是自動增加的)
mysql> insert t1(name)values('mage');
Query OK, 1 row affected (0.01 sec)

mysql> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | mage  |
|  4 | magea |
|  7 | mageb |
| 10 | mage  |
+----+-------+
4 rows in set (0.00 sec)
# 2 節點添加
mysql> insert t1(name)values('mage');
Query OK, 1 row affected (0.00 sec)

mysql> insert t1(name)values('mage');
Query OK, 1 row affected (0.00 sec)

mysql> insert t1(name)values('mage');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+----+-------+
| id | name  |
+----+-------+
|  1 | mage  |
|  4 | magea |
|  7 | mageb |
| 10 | mage  |
| 11 | mage  |
| 14 | mage  |
| 17 | mage  |
+----+-------+
7 rows in set (0.00 sec)

在PXC集羣中加入節點

一個節點加入到Galera集羣有兩種狀況:新節點加入集羣、暫時離組的成員再次加入集羣
1)新節點加入Galera集羣
新節點加入集羣時,須要從當前集羣中選擇一個Donor節點來同步數據,也就是所謂state_snapshot_tranfer(SST)過程。SST同步數據的方式由選項wsrep_sst_method決定,通常選擇的是xtrabackup。
必須注意,新節點加入Galera時,會刪除新節點上全部已有數據,再經過xtrabackup(假設使用的是該方式)從Donor處完整備份全部數據進行恢復。因此,若是數據量很大,新節點加入過程會很慢。並且,在一個新節點成爲Synced狀態以前,不要同時加入其它新節點,不然很容易將集羣壓垮。若是是這種狀況,能夠考慮使用wsrep_sst_method=rsync來作增量同步,既然是增量同步,最好保證新節點上已經有一部分數據基礎,不然和全量同步沒什麼區別,且這樣會對Donor節點加上全局readonly鎖。
2)舊節點加入Galera集羣
若是舊節點加入Galera集羣,說明這個節點在以前已經在Galera集羣中呆過,有一部分數據基礎,缺乏的只是它離開集羣時的數據。這時加入集羣時,會採用IST(incremental snapshot transfer)傳輸機制,即便用增量傳輸。
# 一樣先配置yum源
# 在安裝數據庫
[root@pxc4 ~]#yum install Percona-XtraDB-Cluster-57 -y
# 修改配置文件
[root@pxc4 ~]#grep -Ev "^#|^$" /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
[mysqld]
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
wsrep_cluster_address=gcomm://192.168.39.7,192.168.39.27,192.168.39.37,192.168.39.47 # 這一行添加上新節點的ip(雖然其餘節點配置文件不添加也能夠,可是最好都加上,好比說服務器依次關機到新添加的節點(新添加的節點留爲最後的引導服務器),可是下次開機別的節點會找不到新添加的服務器,數據庫就起不來了)
binlog_format=ROW
default_storage_engine=InnoDB
wsrep_slave_threads= 8
wsrep_log_conflicts
innodb_autoinc_lock_mode=2
wsrep_node_address=192.168.39.47
wsrep_cluster_name=pxc-cluster
wsrep_node_name=pxc-cluster-node-4
pxc_strict_mode=ENFORCING
wsrep_sst_method=xtrabackup-v2
wsrep_sst_auth="sstuser:centos7"

# 查看鏈接數以變爲4個
mysql> show status like 'wsrep_cluster_size'\G
*************************** 1. row ***************************
Variable_name: wsrep_cluster_size
        Value: 4
1 row in set (0.00 sec)

在PXC集羣中修復故障節點

  1. 在其餘節點中止服務
[root@pxc2 ~]#systemctl stop mysql
[root@pxc2 ~]#systemctl status mysql
● mysql.service - Percona XtraDB Cluster
   Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2019-12-03 16:52:51 CST; 3min 21s ago
  Process: 29259 ExecStopPost=/usr/bin/mysql-systemd stop-post (code=exited, status=0/SUCCESS)
  Process: 29221 ExecStop=/usr/bin/mysql-systemd stop (code=exited, status=0/SUCCESS)
  Process: 26928 ExecStartPost=/usr/bin/mysql-systemd start-post $MAINPID (code=exited, status=0/SUCCESS)
  Process: 26925 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)
  Process: 26853 ExecStartPre=/usr/bin/mysql-systemd start-pre (code=exited, status=0/SUCCESS)
 Main PID: 26925 (code=exited, status=0/SUCCESS)

Dec 03 15:12:54 pxc2 mysqld_safe[26925]: 2019-12-03T07:12:54.344288Z mysqld_safe Assigning 00000000-0000-0000-0000-000000000000:-1 to wsrep_start_position
Dec 03 15:12:56 pxc2 mysql-systemd[26928]: State transfer in progress, setting sleep higher
Dec 03 15:13:16 pxc2 mysql-systemd[26928]: SUCCESS!
Dec 03 15:13:16 pxc2 systemd[1]: Started Percona XtraDB Cluster.
Dec 03 16:52:39 centos7.localdomain systemd[1]: Stopping Percona XtraDB Cluster...
Dec 03 16:52:39 centos7.localdomain mysql-systemd[29221]: SUCCESS! Stopping Percona XtraDB Cluster......
Dec 03 16:52:51 centos7.localdomain mysqld_safe[26925]: 2019-12-03T08:52:51.720383Z mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
Dec 03 16:52:51 centos7.localdomain mysql-systemd[29259]: WARNING: mysql pid file /var/run/mysqld/mysqld.pid empty or not readable
Dec 03 16:52:51 centos7.localdomain mysql-systemd[29259]: WARNING: mysql may be already dead
Dec 03 16:52:51 centos7.localdomain systemd[1]: Stopped Percona XtraDB Cluster.
  1. 在其它任意節點查看wsrep_cluster_size變量
[root@centos7 ~]#mysql -uroot -pmagedu
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> show status like 'wsrep_cluster_size'\G
*************************** 1. row ***************************
Variable_name: wsrep_cluster_size
        Value: 3                          # 少了一個節點
1 row in set (0.00 sec)
  1. 添加一個數據庫查看同步狀況
mysql> create database db4;
Query OK, 1 row affected (0.01 sec)

#在其它任意節點可看到數據已同步
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| db4                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
8 rows in set (0.00 sec)
# 其餘節點正常同步
  1. 恢復服務,數據同步
[root@pxc2 ~]#systemctl start mysql
[root@pxc2 ~]#mysql -uroot -pmagedu
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.27-30-57-log Percona XtraDB Cluster (GPL), Release rel30, Revision 64987d4, WSREP version 31.39, wsrep_31.39

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| db2                |
| db3                |
| db4                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
8 rows in set (0.00 sec)
# 恢復服務以後數據仍是能同步的
mysql> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| wsrep_cluster_size | 4     |
+--------------------+-------+
1 row in set (0.00 sec)

實現MariaDB Galera Cluster

#在三個節點上都實現
[root@centos8 ~]#dnf install mariadb-server-galera -y
[root@centos8 ~]#vim /etc/my.cnf.d/galera.cnf  # 修改配置文件
#wsrep_cluster_address="dummy://"
wsrep_cluster_address="gcomm://192.168.39.7,192.168.39.27,192.168.39.37"
#啓動第一節點
[root@centos8 ~]#galera_new_cluster
#再啓動其它節點
[root@centos8 ~]#systemctl start mariadb
[root@centos8 ~]#ss -ntul
Netid             State              Recv-Q            Send-Q
Local Address:Port                            Peer Address:Port
tcp              LISTEN              0                 128
0.0.0.0:22                                   0.0.0.0:*
tcp              LISTEN              0                 128
0.0.0.0:4567                                 0.0.0.0:*
tcp              LISTEN              0                  80
0.0.0.0:3306                                 0.0.0.0:*
tcp              LISTEN              0                 128
[::]:22                                      [::]:*

[root@centos8 ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.11-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON |
+---------------+-------+
1 row in set (0.001 sec)
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_cluster_size';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.001 sec)
MariaDB [(none)]> SHOW VARIABLES LIKE 'wsrep_%'\G
MariaDB [(none)]> SHOW STATUS LIKE 'wsrep_%';

複製的問題和解決方案

複製的問題和解決方案:
(1) 數據損壞或丟失
    Master: MHA + semi repl
    Slave: 從新複製
(2) 混合使用存儲引擎
    MyISAM:不支持事務
    InnoDB: 支持事務
(3) 不唯一的server id
    從新複製
(4) 複製延遲
    須要額外的監控工具的輔助
    一從多主:mariadb10版後支持
    多線程複製:對多個數據庫複製

性能衡量指標

數據庫服務衡量指標:

Qps:query per second
Tps:transaction per second

壓力測試工具

常見MySQl壓力測試工具

mysqlslap
Sysbench:功能強大
https://github.com/akopytov/sysbench
tpcc-mysql
MySQL Benchmark Suite
MySQL super-smack
MyBench
MYSQL壓力測試

mysqlslap

Mysqlslap:來自於mariadb包,測試的過程默認生成一個mysqlslap的schema,生成測試表t1,查詢和插入測試數據,mysqlslap庫自動生成,若是已經存在則先刪除。用--only-print來打印實際的測試過程,整個測試完成後不會在數據庫中留下痕跡。
  • 使用格式:
mysqlslap [options]

經常使用參數 [options] 說明:

--auto-generate-sql, -a #自動生成測試表和數據,表示用mysqlslap工具本身生成的SQL腳原本測試
併發壓力
--auto-generate-sql-load-type=type #測試語句的類型。表明要測試的環境是讀操做仍是寫操做還 
是二者混合的。取值包括:read,key,write,update和mixed(默認)
--auto-generate-sql-add-auto-increment #表明對生成的表自動添加auto_increment列,從 5.1.18版本開始支持
--number-char-cols=N, -x N #自動生成的測試表中包含多少個字符類型的列,默認1
--number-int-cols=N, -y N #自動生成的測試表中包含多少個數字類型的列,默認1
--number-of-queries=N  #總的測試查詢次數(併發客戶數×每客戶查詢次數)
--query=name,-q   #使用自定義腳本執行測試,例如能夠調用自定義的存儲過程或者sql語句來執行測試
--create-schema  #表明自定義的測試庫名稱,測試的schema
--commint=N #多少條DML後提交一次
--compress, -C #如服務器和客戶端都支持壓縮,則壓縮信息
--concurrency=N, -c N #表示併發量,即模擬多少個客戶端同時執行select。可指定多個值,以逗號 
或者--delimiter參數指定值作爲分隔符,如:--concurrency=100,200,500
--engine=engine_name, -e engine_name #表明要測試的引擎,能夠有多個,用分隔符隔開。例 如:--engines=myisam,innodb
--iterations=N, -i N #測試執行的迭代次數,表明要在不一樣併發環境下,各自運行測試多少次
--only-print #只打印測試語句而不實際執行。
--detach=N #執行N條語句後斷開重連
--debug-info, -T #打印內存和CP

mysqlslap示例

#單線程測試
mysqlslap -a -uroot -pmagedu
#多線程測試。使用--concurrency來模擬併發鏈接
mysqlslap -a -c 100 -uroot -pmagedu
#迭代測試。用於須要屢次執行測試獲得平均值
mysqlslap -a -i 10 -uroot -pmagedu
mysqlslap ---auto-generate-sql-add-autoincrement -a
mysqlslap -a --auto-generate-sql-load-type=read
mysqlslap -a --auto-generate-secondary-indexes=3
mysqlslap -a --auto-generate-sql-write-number=1000
mysqlslap --create-schema world -q "select count(*) from City"
mysqlslap -a -e innodb -uroot -pmagedu
mysqlslap -a --number-of-queries=10 -uroot -pmagedu
#測試同時不一樣的存儲引擎的性能進行對比
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --
engine=myisam,innodb --debug-info -uroot -pmagedu
#執行一次測試,分別50和100個併發,執行1000次總查詢
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --debug-info -uroot -
pmagedu
#50和100個併發分別獲得一次測試結果(Benchmark),併發數越多,執行完全部查詢的時間越長。爲了準確
起見,能夠多迭代測試幾回
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --
debug-info -uroot -pmagedu

生產環境 my.cnf 配置案例

  • 硬件:內存32G
#打開獨立表空間
innodb_file_per_table = 1
#MySQL 服務所容許的同時會話數的上限,常常出現Too Many Connections的錯誤提示,則須要增大此值
max_connections = 8000
#全部線程所打開表的數量
open_files_limit = 10240
#back_log 是操做系統在監聽隊列中所能保持的鏈接數
back_log = 300
#每一個客戶端鏈接最大的錯誤容許數量,當超過該次數,MYSQL服務器將禁止此主機的鏈接請求,直到MYSQL
服務器重啓或經過flush hosts命令清空此主機的相關信息
max_connect_errors = 1000
#每一個鏈接傳輸數據大小.最大1G,須是1024的倍數,通常設爲最大的BLOB的值
max_allowed_packet = 32M
#指定一個請求的最大鏈接時間
wait_timeout = 10
# 排序緩衝被用來處理相似ORDER BY以及GROUP BY隊列所引發的排序
sort_buffer_size = 16M
#不帶索引的全表掃描.使用的buffer的最小值
join_buffer_size = 16M
#查詢緩衝大小
query_cache_size = 128M
#指定單個查詢可以使用的緩衝區大小,缺省爲1M
query_cache_limit = 4M
# 設定默認的事務隔離級別
transaction_isolation = REPEATABLE-READ
# 線程使用的堆大小. 此值限制內存中能處理的存儲過程的遞歸深度和SQL語句複雜性,此容量的內存在每次
鏈接時被預留.
thread_stack = 512K
# 二進制日誌功能
log-bin
#二進制日誌格式
binlog_format=row
#InnoDB使用一個緩衝池來保存索引和原始數據, 可設置這個變量到物理內存大小的80%
innodb_buffer_pool_size = 24G
#用來同步IO操做的IO線程的數量
innodb_file_io_threads = 4
#在InnoDb核心內的容許線程數量,建議的設置是CPU數量加上磁盤數量的兩倍
innodb_thread_concurrency = 16
# 用來緩衝日誌數據的緩衝區的大小
innodb_log_buffer_size = 16M
在日誌組中每一個日誌文件的大小
innodb_log_file_size = 512M
# 在日誌組中的文件總數
innodb_log_files_in_group = 3
# SQL語句在被回滾前,InnoDB事務等待InnoDB行鎖的時間
innodb_lock_wait_timeout = 120
#慢查詢時長
long_query_time = 2
#將沒有使用索引的查詢也記錄下來
log-queries-not-using-indexes

MySQL配置最佳實踐

高併發大數據的互聯網業務,架構設計思路是「解放數據庫CPU,將計算轉移到服務層」,併發量大的狀況下,這些功能極可能將數據庫拖死,業務邏輯放到服務層具有更好的擴展性,可以輕易實現「增機器就加性能」
  • 參考:
    阿里巴巴Java開發手冊
    58到家數據庫30條軍規解讀
    http://zhuanlan.51cto.com/art/201702/531364.htm在這裏插入圖片描述
相關文章
相關標籤/搜索