当前位置:   article > 正文

CentOS7上安装 percona 数据库3 主架构_package mysql-libs is obsoleted by percona-server-

package mysql-libs is obsoleted by percona-server-shared-55, but obsoleting

我没有去下载 Percona-XtraDB-Cluster ,而是通过更新yum源的方式:

rpm -Uvh https://www.percona.com/redir/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm

另外,请注意:pxc集群各节点之间不需要配ssh免密互信。

一 准备工作

1. 准备三台机器:

192.168.158.144

192.168.158.145

192.168.158.146

2. 关闭三台机器上的selinux校验

setenforce 0

3. 在三台机器上分别更新percona-release的yum源:

rpm -Uvh https://www.percona.com/redir/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm

更新过yum源后,会在 /etc/yum.repos.d/ 下生成 percona-release.repo,如图:

此时,

 

二 在3台机器上安装 Percona-XtraDB-Cluster

1 先查看有哪些可用Percona-XtraDB-Cluster包,

yum list | grep percona

 

percona 安装包依赖关系:

Percona-XtraDB-Cluster-57-5.7.23-31.31.1.el7.x86_64.rpm 依赖于:
        Percona-XtraDB-Cluster-server-57 = 5.7.23-31.31.1.el7
        Percona-XtraDB-Cluster-client-57 = 5.7.23-31.31.1.el7 

Percona-XtraDB-Cluster-client-57-5.7.23-31.31.1.el7.x86_64.rpm 依赖于:
        perl-DBI

Percona-XtraDB-Cluster-server-57-5.7.23-31.31.1.el7.x86_64.rpm 依赖于:
        Percona-XtraDB-Cluster-client-57 = 5.7.23-31.31.1.el7
        Percona-XtraDB-Cluster-shared-57 = 5.7.23-31.31.1.el7
        percona-xtrabackup-24 >= 2.4.12
        perl(Data::Dumper)
        perl-DBD-MySQL
        perl-DBI
        qpress
        socat

percona-xtrabackup-80-8.0.6-1.el7.x86_64.rpm 依赖于:
        libev.so.4()(64bit)
        perl(DBD::mysql)
        perl(Digest::MD5)

Percona-XtraDB-Cluster-shared-57-5.7.23-31.31.1.el7.x86_64.rpm  有一个冲突:
error: Failed dependencies:
        mariadb-libs >= 5.5.37 is obsoleted by Percona-XtraDB-Cluster-shared-57-5.7.23-31.31.1.el7.x86_64

 

因此,安装顺序:

移除的依赖:

[root@sjk2 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64

yum -y remove mariadb-libs

移除依赖 mariadb-libs 后,

同时,/etc/ 下也不再有

安装依赖1:

yum -y install socat

安装依赖2:

yum -y install libev

安装依赖3:

yum -y install perl-Digest-MD5

或者你多安几个也无所谓:

yum -y install perl-Digest-MD5 perl-Digest-SHA perl-Digest-SHA1

 

安装依赖4:

[root@sjk1 ~]# yum -y install qpress

qpress 来自于 percona-release-x86_64 这个yum 库

有可能出现问题:

The GPG keys listed for the "Percona-Release YUM repository - x86_64" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.


 Failing package is: Percona-Server-shared-56-5.6.44-rel86.0.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Percona

解决方案:

yum -y install qpress --nogpgcheck

遇到这种问题,解决方案同此,加 --nogpgcheck 。下同。

 

这个问题往往源于用户仓库中的rpm包有时候是用户自己制作的,或者从第三方获取,这个时候问题就会出现。从字面上理解,是“为“***”源码仓库出示的GPG密钥已经安装,但不正确”。

首先,GPG密钥存在的目的是处于安全和规范考虑,RedHat在发布软件包的时候会根据软件包生成对应密钥,当用户安装软件包的时候会根据密钥校验软件包。

若是用yum安装,当/etc/yum.conf文件有如下配置项目时。

gpgcheck=1
yum安装的时候就会校验软件包是否是官方发布的。当然可以给yum添加--nogpgcheck来强制安装。
sudo yum install *** --nogpgcheck

 

安装 percona 组件:

安装percona组件1    Percona-XtraDB-Cluster-shared-57

yum -y install Percona-XtraDB-Cluster-shared-57 --nogpgcheck

 

安装percona组2    Percona-XtraDB-Cluster-shared-compat-57

yum -y install Percona-XtraDB-Cluster-shared-compat-57 --nogpgcheck

 

安装percona组件3    percona-xtrabackup-24

yum -y install percona-xtrabackup-24 --nogpgcheck

 

 

安装percona组件4    Percona-XtraDB-Cluster-client-57

yum -y install Percona-XtraDB-Cluster-client-57 --nogpgcheck

 

安装percona组件5    Percona-XtraDB-Cluster-server-57

yum -y install Percona-XtraDB-Cluster-server-57 --nogpgcheck

 

yum 安装时会提示UDFs功能,根据需要可以在mysql启动后执行以下语句:

Percona XtraDB Cluster is distributed with several useful UDFs from Percona Toolkit.
Run the following commands to create these functions:
mysql -e "CREATE FUNCTION fnv1a_64 RETURNS INTEGER SONAME 'libfnv1a_udf.so'"
mysql -e "CREATE FUNCTION fnv_64 RETURNS INTEGER SONAME 'libfnv_udf.so'"
mysql -e "CREATE FUNCTION murmur_hash RETURNS INTEGER SONAME 'libmurmur_udf.so'"
 

 

 

安装 Percona-XtraDB-Cluster-server-57 后,会删除旧的 /etc/my.cnf(如果有的话),并生成新的

 和  

新的 my.cnf 内容如下:

 

安装percona组件6    Percona-XtraDB-Cluster-57

yum -y install Percona-XtraDB-Cluster-57 --nogpgcheck

 

yum 安装 Percona组件 顺序汇总:

三 配置mysql及集群配置文件

默认主配置文件 /etc/my.cnf 文件内容如下:

  1. [root@sjk1 ~]# more /etc/my.cnf
  2. #
  3. # The Percona XtraDB Cluster 5.7 configuration file.
  4. #
  5. #
  6. # * IMPORTANT: Additional settings that can override those from this file!
  7. # The files must end with '.cnf', otherwise they'll be ignored.
  8. # Please make any edits and changes to the appropriate sectional files
  9. # included below.
  10. #
  11. !includedir /etc/my.cnf.d/
  12. !includedir /etc/percona-xtradb-cluster.conf.d/

而 /etc/my.cnf.d/ 下为空的,/etc/percona-xtradb-cluster.conf.d/下有3个配置文件:

  1. [root@sjk1 ~]# ls /etc/my.cnf.d
  2. [root@sjk1 ~]# ls /etc/percona-xtradb-cluster.conf.d/
  3. mysqld.cnf mysqld_safe.cnf wsrep.cnf

所以,mysql及集群的配置文件修改,主要包括3个配置文件内容的修改:mysqld.cnf、mysqld_safe.cnf、wsrep.cnf

下面我将采用直接改动主配置文件my.cnf,而不去改动mysqld.cnf、mysqld_safe.cnf、wsrep.cnf

其中 mysqld_safe.cnf的默认内容如下:

  1. [root@sjk1 percona-xtradb-cluster.conf.d]# more mysqld_safe.cnf
  2. #
  3. # The Percona Server 5.7 configuration file.
  4. #
  5. # One can use all long options that the program supports.
  6. # Run program with --help to get a list of available options and with
  7. # --print-defaults to see which it would actually understand and use.
  8. #
  9. # For explanations see
  10. # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
  11. [mysqld_safe]
  12. pid-file = /var/run/mysqld/mysqld.pid
  13. socket = /var/lib/mysql/mysql.sock
  14. nice = 0

 

 

以第一个节点的 my.cnf 举例。内容更改如下:

  1. [root@sjk1 etc]# cat my.cnf
  2. [mysqld]
  3. #cluster parameters
  4. #wsrep lib
  5. wsrep_provider=/usr/lib64/libgalera_smm.so
  6. #cluster address,first time using blank,then fill with the real node ips
  7. wsrep_cluster_address=gcomm://192.168.158.144,192.168.158.145,192.168.158.146
  8. #wsrep_cluster_address=gcomm://
  9. #must be row
  10. binlog_format=ROW
  11. #only support innodb
  12. default_storage_engine=InnoDB
  13. #must to 2
  14. innodb_autoinc_lock_mode=2
  15. #this node ip address
  16. wsrep_node_address=192.168.158.144
  17. #our cluster name,must be unique in the whole cluster
  18. wsrep_cluster_name=honor-production
  19. wsrep_node_name=honor_node1
  20. #sst syc method
  21. wsrep_sst_method=xtrabackup-v2
  22. #sst user and password
  23. wsrep_sst_auth="sstuser:s3cret"
  24. #transaction cache for Galera replication,larger size,bigger chance to use ist
  25. wsrep_provider_options="gcache.size=300M;gcache.page_size=300M"
  26. #replication transactions threads for client
  27. wsrep_slave_threads=6
  28. #change it to RSU,when big change like alter table ,change column name, add index happened, otherwise it will infute the whole cluster,
  29. wsrep_OSU_method=TOI
  30. #db parameters
  31. log-slave-updates = 1
  32. server-id=144
  33. log-bin=bin-log
  34. expire_logs_days = 7
  35. skip-name-resolve
  36. skip-host-cache
  37. character-set-server=utf8
  38. #the data files go to
  39. datadir=/mysql
  40. #men cache,up tp 60% of whole physical memory,change it when deploy to production env
  41. innodb_buffer_pool_size=200M
  42. #each log file
  43. innodb_log_file_size=256M
  44. #each table in a seprate storage file
  45. innodb_file_per_table=1
  46. #log buffer
  47. innodb_flush_log_at_trx_commit=2
  48. #too small will cause commit error
  49. max_allowed_packet=20M
  50. #it will first read cache,then go to open table
  51. table_open_cache=1024
  52. #increase sort by
  53. sort_buffer_size=4M
  54. join_buffer_size=8M
  55. #increase table sequence scan
  56. read_buffer_size=10M
  57. #1g->8
  58. thread_cache_size=320
  59. tmp_table_size=512M
  60. wait_timeout=108000
  61. max_connections = 5000
  62. query_cache_type=1
  63. query_cache_limit=2M
  64. query_cache_size=512M
  65. slow_query_log=ON
  66. general_log=ON
  67. long_query_time=2

my.cnf简化后的通用配置如下(上面的配置不通用,硬件配置低的话会出问题):

  1. #
  2. # The Percona XtraDB Cluster 5.7 configuration file.
  3. #
  4. #
  5. # * IMPORTANT: Additional settings that can override those from this file!
  6. # The files must end with '.cnf', otherwise they'll be ignored.
  7. # Please make any edits and changes to the appropriate sectional files
  8. # included below.
  9. #
  10. [mysqld]
  11. datadir=/mysql
  12. port=3306
  13. #socket=/mysql/mysql-node1.sock #socket不要指定,配了反而会出问题,让它自己去生成 /var/lib/mysql/mysql.sock
  14. ### 可以不指定pid-file路径,默认生成路径为 $datadir/$hostname.pid ####
  15. pid-file=/mysql/mysql-node1.pid
  16. ### 建议指定log-error路径,不指定log-error的话,也会生成一个 $datadir/$hostname.err,但该文件中是没有临时密码的,导致无法登录数据库 ####
  17. log-error=/mysql/mysql-node1.err
  18. default_storage_engine=InnoDB
  19. character-set-server=utf8
  20. log_timestamps=SYSTEM #默认是UTC,需要我们指定为SYSTEM
  21. max_connections=5000
  22. #server-id 服务器唯一ID,默认是1,一般取IP最后一段
  23. server-id=144
  24. #启用二进制日志
  25. log-bin=bin-log
  26. binlog_format=ROW
  27. #主键自增长不锁表,只能设置为2,设置为0或1时会无法正确处理死锁问题
  28. innodb_autoinc_lock_mode=2
  29. expire_logs_days=7
  30. log-slave-updates=1
  31. #跳过DNS解析
  32. #跳过DNS主机名查找,提高响应速度
  33. skip-name-resolve
  34. #跳过主机名缓存
  35. skip-host-cache
  36. wsrep_provider=/usr/lib64/libgalera_smm.so
  37. wsrep_cluster_name=pxc-cluster
  38. wsrep_node_name=node1
  39. # Galera不指定端口的话,默认 4567
  40. wsrep_cluster_address=gcomm://192.168.158.144:5020,192.168.158.145:5020,192.168.158.146:5020
  41. wsrep_provider_options="base_port=5020;"
  42. wsrep_node_address=192.168.158.144:5020
  43. wsrep_slave_threads=6
  44. #sst syc method
  45. wsrep_sst_method=xtrabackup-v2
  46. #wsrep_sst_method=rsync
  47. #sst user and password
  48. wsrep_sst_auth="sstuser:s3cret"
  49. #开启普通的sql日志(如果是集群,只开启一个节点即可,保证能有日志查就行了,其他节点可开可不开)
  50. general_log=on #默认为off 关闭 ,开启后会生成 $datadir/$hostname.log 文件,记录执行的每一条sql,就连commit这种sql语句都会记录下来
  51. #添加慢查询日志
  52. #log_output=file #不写的话默认是file.如果想将mysql慢查询日志写入表,则必须明确指定 log_output=table
  53. slow_query_log=on #开启慢查询 #默认是不开启 off
  54. #slow_query_log_file=/mysql/mysql-slow.log #不指定的话,默认值是 $datadir/$hostname-slow.log #指定的话,路径必须指定在 $datadir 目录下,否则设置slow_query_log=on无效
  55. long_query_time=2 # 所有超过2s的sql都被记录下来
  56. ########如果需要还可以开启查询缓存query_cache,默认是不开启##########

 

四 启动PXC集群

1 启动第一个节点

第一个节点启动必须用下面这种方式:

systemctl start mysql@bootstrap.service

[root@sjk1 ~]# systemctl start mysql@bootstrap.service

注意:启动第一个节点前要先 setenforce 0

官网解释:

The SELinux security module can constrain access to data for Percona XtraDB cluster. The best solution is to change the mode from enforcing to permissive by running to following command:

setenforce 0

This only changes the mode at runtime. To run SELinux in permissive mode after a reboot, set SELINUX=permissive in the /etc/selunux/config configuration file.

sed -i "s/^SELINUX\=enforcing/SELINUX\=disabled/g" /etc/selinux/config

 

启动第一个节点后,

在$datadir 目录下生成如下文件和文价夹:

查看mysql进程信息如下:

 

从进程信息,可知

错误日志文件是mysql-node1.err 

定位错误日志文件位置:

从错误日志文件 xxxx.err 中找到临时登录密码:

怎么找到这个临时密码呢?

使用命令:

grep 'temporary password' /mysql/mysql-node1.err

即可查询到类似于如下的一条日志记录:

  1. [root@sjk1 ~]# grep 'temporary password' /mysql/mysql-node1.err
  2. 2019-08-16T09:56:01.517698Z 1 [Note] A temporary password is generated for root@localhost: #+UC&*K7pHrE

#+UC&*K7pHrE即为登录密码。使用这个随机密码登录进去,然后修改密码,使用命令:

   mysql -uroot -p

  1. [root@sjk2 ~]# mysql -uroot -p
  2. Enter password: # 在这里输入密码
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 9
  5. Server version: 5.7.25-28-57-log
  6. ......
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. mysql>

更改root本地密码,否则不能执行其他sql语句:比如创建sstuser用户

更改本地root密码:

  1. mysql> alter user root@localhost identified by 'sdbrk';
  2. Query OK, 0 rows affected (0.10 sec)

创建用于节点直接同步数据的sstuser用户并赋权(用户名和密码要和my.cnf中配置的一致:wsrep_sst_auth="sstuser:s3cret",该账户只需在节点1创建即可,且账户的host为localhost):

  1. mysql> create user 'sstuser'@'localhost' identified by 's3cret';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> grant reload,lock tables,process,replication client on *.* to sstuser@localhost;
  4. Query OK, 0 rows affected (0.01 sec)
  5. mysql> flush privileges;
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> quit
  8. Bye

查看pxc集群当前状态

To make sure that the cluster has been initialized,run the following:

  1. mysql> show status like 'wsrep%';
  2. +----------------------------------+--------------------------------------+
  3. | Variable_name | Value |
  4. +----------------------------------+--------------------------------------+
  5. | wsrep_local_state_uuid | 6cb10f99-c3c2-11e9-a912-e6a1e0e11c9f |
  6. | wsrep_protocol_version | 9 |
  7. | wsrep_last_applied | 4 |
  8. | wsrep_last_committed | 4 |
  9. | wsrep_replicated | 4 |
  10. ......
  11. | wsrep_local_state | 4 |
  12. | wsrep_local_state_comment | Synced |
  13. ......
  14. | wsrep_incoming_addresses | 192.168.158.144:3306 |
  15. | wsrep_cluster_weight | 1 |
  16. | wsrep_desync_count | 0 |
  17. | wsrep_evs_delayed | |
  18. | wsrep_evs_evict_list | |
  19. | wsrep_evs_repl_latency | 0/0/0/0/0 |
  20. | wsrep_evs_state | OPERATIONAL |
  21. | wsrep_gcomm_uuid | 6caede01-c3c2-11e9-8eac-f32a75f0b367 |
  22. | wsrep_cluster_conf_id | 1 |
  23. | wsrep_cluster_size | 1 |
  24. | wsrep_cluster_state_uuid | 6cb10f99-c3c2-11e9-a912-e6a1e0e11c9f |
  25. | wsrep_cluster_status | Primary |
  26. | wsrep_connected | ON |
  27. | wsrep_local_bf_aborts | 0 |
  28. | wsrep_local_index | 0 |
  29. | wsrep_provider_name | Galera |
  30. | wsrep_provider_vendor | Codership Oy <info@codership.com> |
  31. | wsrep_provider_version | 3.35(rddf9876) |
  32. | wsrep_ready | ON |
  33. +----------------------------------+--------------------------------------+
  34. 71 rows in set (0.00 sec)

参数 wsrep_cluster_size 的值为1,显示当前pxc集群只有一个节点。在初始化集群过程中,要注意第一个节点的服务的启动方式。下一步就是启动其他节点将其他节点加入集群。

2 启动其他节点

启动第二、三个节点:

其余两个节点按照正常启动mysql方式即可,不需要按照节点1的bootstrap启动方式:

systemctl start mysqld

启动后,将自动创建/mysql/目录并生成相应mysql文件,密码自动和第一个节点的密码同步,因此第二、第三个节点可以直接用 root/sdbrk 进行登录。

第一个节点$datadir目录下将多出一个文件 innobackup.backup.log

第二、三个节点自动创建$datadir目录,不过相对第一个节点略有不同,少个mysql_safe.pid文件,多出了如下红线圈起的文件

三个节点的进程状态:

After the server starts, it should receive SST automatically. To check the status of the second node, run the following:

mysql> show status like 'wsrep%';

节点都启动完后,若在一个节点上进行sql操作,其他节点也会同步更新。比如在任一个节点更改本地root密码,其他节点也自动更新自己的root密码。

三个节点都启动后,在任一个节点上再次查看集群状态:

  1. mysql> show status like 'wsrep%';
  2. +----------------------------------+----------------------------------------------------------------+
  3. | Variable_name | Value |
  4. +----------------------------------+----------------------------------------------------------------+
  5. | wsrep_local_state_uuid | 6cb10f99-c3c2-11e9-a912-e6a1e0e11c9f |
  6. | wsrep_protocol_version | 9 |
  7. | wsrep_last_applied | 4 |
  8. | wsrep_last_committed | 4 |
  9. | wsrep_replicated | 4 |
  10. ......
  11. | wsrep_local_state | 4 |
  12. | wsrep_local_state_comment | Synced |
  13. ......
  14. | wsrep_incoming_addresses | 192.168.158.145:3306,192.168.158.144:3306,192.168.158.146:3306 |
  15. | wsrep_cluster_weight | 3 |
  16. | wsrep_desync_count | 0 |
  17. | wsrep_evs_delayed | |
  18. | wsrep_evs_evict_list | |
  19. | wsrep_evs_repl_latency | 0/0/0/0/0 |
  20. | wsrep_evs_state | OPERATIONAL |
  21. | wsrep_gcomm_uuid | 6caede01-c3c2-11e9-8eac-f32a75f0b367 |
  22. | wsrep_cluster_conf_id | 4 |
  23. | wsrep_cluster_size | 3 |
  24. | wsrep_cluster_state_uuid | 6cb10f99-c3c2-11e9-a912-e6a1e0e11c9f |
  25. | wsrep_cluster_status | Primary |
  26. | wsrep_connected | ON |
  27. | wsrep_local_bf_aborts | 0 |
  28. | wsrep_local_index | 1 |
  29. | wsrep_provider_name | Galera |
  30. | wsrep_provider_vendor | Codership Oy <info@codership.com> |
  31. | wsrep_provider_version | 3.35(rddf9876) |
  32. | wsrep_ready | ON |
  33. +----------------------------------+----------------------------------------------------------------+
  34. 71 rows in set (0.00 sec)

其中

wsrep_incoming_addresses的值为 192.168.158.145:3306,192.168.158.144:3306,192.168.158.146:3306 

wsrep_cluster_size 的值为 3  ,说明此时的集群节点数为3

如果在这个过程中,节点2或者节点3的mysql服务无法启动,可以从以下几个方向着手:
(1)配置文件错误
(2)防火墙没有开放相应的端口,比如默认的4567端口(我这里指定的5020)
(3)忘记进行授权账号
(4)xtrabackup没有安装或者安装有问题。

两个节点按正常方式启动mysql后,会按照配置文件自动加入PXC集群。当所有节点添加完毕后,将节点1的mysql服务关闭,然后再以正常方式启动mysql服务:

  1. systemctl stop mysql@bootstrap.service
  2. systemctl start mysqld
  3. systemctl status mysqld

执行命令的相关截图如下:

 

再次在1节点上查看集群状态。集群状态仍为3

  1. mysql> show status like 'wsrep_clu%';
  2. +--------------------------+--------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+--------------------------------------+
  5. | wsrep_cluster_weight | 3 |
  6. | wsrep_cluster_conf_id | 11 |
  7. | wsrep_cluster_size | 3 |
  8. | wsrep_cluster_state_uuid | 6cb10f99-c3c2-11e9-a912-e6a1e0e11c9f |
  9. | wsrep_cluster_status | Primary |
  10. +--------------------------+--------------------------------------+
  11. 5 rows in set (0.01 sec)

五 同步测试

首先在节点上创建数据库,并写入数据:

  1. [root@sjk1 ~]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 10
  5. Server version: 5.7.25-28-57-log Percona XtraDB Cluster (GPL), Release rel28, Revision a2ef85f, WSREP version 31.35, wsrep_31.35
  6. Copyright (c) 2009-2019 Percona LLC and/or its affiliates
  7. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> create database percona;
  13. Query OK, 1 row affected (0.10 sec)
  14. mysql> use percona;
  15. Database changed
  16. mysql> create table example (node_id int primary key,node_name varchar(30));
  17. Query OK, 0 rows affected (0.02 sec)
  18. mysql> insert into percona.example values (1,'percona1');
  19. Query OK, 1 row affected (0.34 sec)
  20. mysql> select * from percona.example;
  21. +---------+-----------+
  22. | node_id | node_name |
  23. +---------+-----------+
  24. | 1 | percona1 |
  25. +---------+-----------+
  26. 1 row in set (0.00 sec)
  27. mysql> quit
  28. Bye
  29. [root@sjk1 ~]#

在节点2和节点3上验证数据是否同步:

  1. # 节点2
  2. [root@sjk2 mysql]# mysql -uroot -p
  3. Enter password:
  4. Welcome to the MySQL monitor. Commands end with ; or \g.
  5. ......
  6. mysql> select * from percona.example;
  7. +---------+-----------+
  8. | node_id | node_name |
  9. +---------+-----------+
  10. | 1 | percona1 |
  11. +---------+-----------+
  12. 1 row in set (0.00 sec)
  13. # 节点3
  14. [root@sjk3 mysql]# mysql -uroot -p
  15. Enter password:
  16. Welcome to the MySQL monitor. Commands end with ; or \g.
  17. ......
  18. mysql> select * from percona.example;
  19. +---------+-----------+
  20. | node_id | node_name |
  21. +---------+-----------+
  22. | 1 | percona1 |
  23. +---------+-----------+
  24. 1 row in set (0.00 sec)

然后分别在节点2和节点3上写入数据:

  1. # 节点2上写入数据
  2. mysql> insert into percona.example values (2,'percona2');
  3. Query OK, 1 row affected (0.09 sec)
  4. # 节点3上写入数据
  5. mysql> insert into percona.example values (3,'percona3');
  6. Query OK, 1 row affected (0.08 sec)

在节点1上验证数据是否同步:

  1. [root@sjk1 ~]# mysql -uroot -p
  2. Enter password:
  3. mysql> select * from percona.example;
  4. +---------+-----------+
  5. | node_id | node_name |
  6. +---------+-----------+
  7. | 1 | percona1 |
  8. | 2 | percona2 |
  9. | 3 | percona3 |
  10. +---------+-----------+
  11. 3 rows in set (0.00 sec)

PXC集群内的所有节点均可写入并同步复制到其他节点。

此时,如果停止节点1的mysql服务,然后在其他节点写入数据:

  1. [root@sjk1 ~]# systemctl stop mysqld
  2. [root@sjk1 ~]# systemctl status mysqld
  3. ● mysql.service - Percona XtraDB Cluster
  4. Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; vendor preset: disabled)
  5. Active: inactive (dead) since Wed 2019-08-21 16:45:52 CST; 8s ago
  6. Process: 43546 ExecStopPost=/usr/bin/mysql-systemd stop-post (code=exited, status=0/SUCCESS)
  7. Process: 43502 ExecStop=/usr/bin/mysql-systemd stop (code=exited, status=0/SUCCESS)
  8. Process: 41169 ExecStartPost=/usr/bin/mysql-systemd start-post $MAINPID (code=exited, status=0/SUCCESS)
  9. Process: 41168 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=0/SUCCESS)
  10. Process: 41128 ExecStartPre=/usr/bin/mysql-systemd start-pre (code=exited, status=0/SUCCESS)
  11. Main PID: 41168 (code=exited, status=0/SUCCESS)

在节点2上查看集群状态,并在节点2写入数据:

  1. mysql> show status like 'wsrep_cluster%';
  2. +--------------------------+--------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+--------------------------------------+
  5. | wsrep_cluster_weight | 2 |
  6. | wsrep_cluster_conf_id | 14 |
  7. | wsrep_cluster_size | 2 |
  8. | wsrep_cluster_state_uuid | 6cb10f99-c3c2-11e9-a912-e6a1e0e11c9f |
  9. | wsrep_cluster_status | Primary |
  10. +--------------------------+--------------------------------------+
  11. 5 rows in set (0.01 sec)
  12. mysql> insert into percona.example values (4,'percona4');
  13. Query OK, 1 row affected (0.01 sec)

wsrep_cluster_size 显示此时的集群节点数为2。当再次启动节点1的mysql服务,在节点1上检查节点2最近写入的数据是否完成同步:

  1. [root@sjk1 ~]# systemctl start mysqld.service
  2. [root@sjk1 ~]# mysql -uroot -psdbrk
  3. mysql> select * from percona.example;
  4. +---------+-----------+
  5. | node_id | node_name |
  6. +---------+-----------+
  7. | 1 | percona1 |
  8. | 2 | percona2 |
  9. | 3 | percona3 |
  10. | 4 | percona4 |
  11. +---------+-----------+
  12. 4 rows in set (0.01 sec)

由此可见,当集群内的某个节点掉线后,其他节点仍可以正常的工作,新写入的数据会在该节点重新上线后完成同步(即使该节点停掉后对数据目录进行过清空),以实现PXC的高可用。
针对重新上线的节点,PXC有两种方式完成数据传输以保证数据同步:State Snapshot Transfer (SST)和Incremental State Transfer (IST)。
SST通常用在当有新的节点加入PXC集群同时从已存在节点复制全部数据时采用,在PXC中有三种可用的方式完成SST过程:

– mysqldump  
– rsync  
– xtrabackup 

mysqldumprsync的缺点是在数据传输过程中法规,PXC集群将会变成只读模式,SST将会对数据库施加只读锁(FLUSH TABLES WITH READ LOCK)。而使用xtrabackup则不需要再数据同步过程中施加读锁,仅仅是同步.frm文件,类似于常规的备份。
IST用于只将数据的增量变化从一个节点复制到另一个节点。
尽管,如果SST使用xtrabackup不需要施加读锁,SST仍可能扰乱了服务的正常运行。而IST则不会。如果一个节点掉线时间较短,当再次上线,它将只会从其他节点获取掉线期间的数据变化部分。IST是在节点上使用缓存机制实现的。每个节点包含一个缓存,且环形缓冲区(大小是可配置的)存储最后N个变化,并且节点能够传输该缓存的一部分。显然,只有当传输所需的更改量小于N时,才可以执行IST,如果超过N,则加入的节点必须执行SST。
 



如果3个节点中挂了两个呢?恢复方式同上。

停掉2,3节点的mysql服务

systemctl stop mysql.service

在1节点上插入数据

  1. mysql> insert into percona.example values (5,'percona5');
  2. Query OK, 1 row affected (0.00 sec)

重启2,3节点,并在2节点上查看数据有没有同步

  1. # 重启2节点
  2. [root@sjk2 ~]# systemctl start mysqld
  3. # 重启3节点
  4. [root@sjk3 mysql]# systemctl start mysql
  5. # 重启2节点后,登上去,查看数据有没有同步(结果是肯定同步啦)
  6. [root@sjk2 mysql]# mysql -uroot -p
  7. Enter password:
  8. Welcome to the MySQL monitor. Commands end with ; or \g.
  9. Your MySQL connection id is 9
  10. Server version: 5.7.25-28-57-log Percona XtraDB Cluster (GPL), Release rel28, Revision a2ef85f, WSREP version 31.35, wsrep_31.35
  11. Copyright (c) 2009-2019 Percona LLC and/or its affiliates
  12. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  13. Oracle is a registered trademark of Oracle Corporation and/or its
  14. affiliates. Other names may be trademarks of their respective
  15. owners.
  16. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  17. mysql> select * from percona.example;
  18. +---------+-----------+
  19. | node_id | node_name |
  20. +---------+-----------+
  21. | 1 | percona1 |
  22. | 2 | percona2 |
  23. | 3 | percona3 |
  24. | 4 | percona4 |
  25. | 5 | percona5 |
  26. +---------+-----------+
  27. 5 rows in set (0.00 sec)


如果三个节点全挂?此时恢复方式不太一样

mysql HA集群在断网过久或者所有节点都down了之后该怎么恢复呢?

等三台机器恢复网络通讯后,因为此时的mysql已经异常无法加入集群,因此需要先保证所有的mysql都是down的(如果个别节点没死透,存在僵尸进程,3306,4567两个端口没全挂,那么手动杀掉该僵死进程)。然后删除第一个节点的 grastate.dat、galera.cache 两个文件(其他节点不用删)

[root@sjk1 mysql]# rm -rf grastate.dat galera.cache

然后启动第一个节点(以初始化集群的方式)

[root@sjk1 ~]# systemctl start mysql@bootstrap.service

启动后,查看集群状态

  1. mysql> show status like 'wsrep_clus%';
  2. +--------------------------+--------------------------------------+
  3. | Variable_name | Value |
  4. +--------------------------+--------------------------------------+
  5. | wsrep_cluster_weight | 1 |
  6. | wsrep_cluster_conf_id | 1 |
  7. | wsrep_cluster_size | 1 |
  8. | wsrep_cluster_state_uuid | 47a30425-c434-11e9-9a09-56429b062ebd |
  9. | wsrep_cluster_status | Primary |
  10. +--------------------------+--------------------------------------+
  11. 5 rows in set (0.01 sec)

然后在其他两个节点上执行启动命令 systemctl start mysql , 必须等一台成功了,再其另一台

  1. # 节点2
  2. [root@sjk2 ~]# systemctl start mysql
  3. # 节点3
  4. [root@sjk3 mysql]# systemctl start mysql

最后在mysql中执行 show status like 'wsrep%';

  1. mysql> show status like 'wsrep%';
  2. +----------------------------------+----------------------------------------------------------------+
  3. | Variable_name | Value |
  4. +----------------------------------+----------------------------------------------------------------+
  5. | wsrep_local_state_uuid | 47a30425-c434-11e9-9a09-56429b062ebd |
  6. ......
  7. | wsrep_local_state_comment | Synced |
  8. ......
  9. | wsrep_incoming_addresses | 192.168.158.144:3306,192.168.158.145:3306,192.168.158.146:3306 |
  10. | wsrep_cluster_weight | 3 |
  11. ......
  12. | wsrep_cluster_size | 3 |
  13. | wsrep_cluster_state_uuid | 47a30425-c434-11e9-9a09-56429b062ebd |
  14. | wsrep_cluster_status | Primary |
  15. | wsrep_connected | ON |
  16. | wsrep_local_bf_aborts | 0 |
  17. | wsrep_local_index | 0 |
  18. | wsrep_provider_name | Galera |
  19. | wsrep_provider_vendor | Codership Oy <info@codership.com> |
  20. | wsrep_provider_version | 3.35(rddf9876) |
  21. | wsrep_ready | ON |
  22. +----------------------------------+----------------------------------------------------------------+
  23. 71 rows in set (0.01 sec)

我们必须保证 wsrep_local_state_comment 为 Synced,wsrep_incoming_address 为三个mysql服务的ip:port,wsrep_cluster_size 为3(集群有几个节点就要保证为几)

查看数据有没有丢,如下所示:数据没丢:

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | percona |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.51 sec)
  12. mysql> select * from percona.example;
  13. +---------+-----------+
  14. | node_id | node_name |
  15. +---------+-----------+
  16. | 1 | percona1 |
  17. | 2 | percona2 |
  18. | 3 | percona3 |
  19. | 4 | percona4 |
  20. | 5 | percona5 |
  21. +---------+-----------+
  22. 5 rows in set (0.75 sec)

最后,停掉一节点,以普通方式启动。



接下来我们讲讲pxc数据库全备

xtrabackup全量备份:

innobackupex --user=root --password=sdbrk --socket=/var/lib/mysql/mysql.sock /home/backup/full

执行该全备命令后,将会自动在/home/目录下生成 backup/full 子目录

执行全备命令后,数据库 $datadir 目录结构不会发生变化,比如不会新增一些文件或文件夹。

备份后 在备份目录/home/bakcup/full 下创建一个以时间命名的目录,这里面就是相关的备份文件,同样也可以看到我们创建的percona库

 

接下来再讲讲如何使用备份数据库进行还原



使用全备进行还原

1. 破环数据库

[root@sjk1 mysql]# rm -rf /mysql/*

2. 恢复数据库

使用全备进行恢复,恢复之前需要保证数据目录是空的状态,否则会报错:

停掉服务,清空数据目录,进行数据恢复,修复属主属组关系,重启服务

  1. [root@sjk1 ~]# systemctl stop mysql #停掉服务
  2. [root@sjk1 ~]# innobackupex --apply-log --redo-only /home/backup/full/2019-08-22_09-56-24 # 对完全备份进行整理
  3. ......
  4. 190822 12:55:36 completed OK!
  5. [root@sjk1 ~]# rm -rf /mysql/* #清空数据目录
  6. [root@sjk1 ~]# innobackupex --defaults-file=/etc/my.cnf --copy-back /home/backup/full/2019-08-22_09-56-24 #全备恢复--还原数据库,还原前需确认数据库数据目录为空
  7. ......
  8. 190822 13:06:41 [01] Copying ./mysql/plugin.frm to /mysql/mysql/plugin.frm
  9. 190822 13:06:41 [01] ...done
  10. 190822 13:06:41 [01] Copying ./mysql/servers.frm to /mysql/mysql/servers.frm
  11. 190822 13:06:41 [01] ...done
  12. 190822 13:06:41 [01] Copying ./mysql/tables_priv.frm to /mysql/mysql/tables_priv.frm
  13. 190822 13:06:41 [01] ...done
  14. 190822 13:06:41 [01] Copying ./mysql/tables_priv.MYI to /mysql/mysql/tables_priv.MYI
  15. 190822 13:06:41 [01] ...done
  16. 190822 13:06:41 [01] Copying ./mysql/tables_priv.MYD to /mysql/mysql/tables_priv.MYD
  17. ......
  18. 190822 13:06:42 [01] Copying ./performance_schema/session_status.frm to /mysql/performance_schema/session_status.frm
  19. 190822 13:06:42 [01] ...done
  20. 190822 13:06:42 [01] Copying ./ib_buffer_pool to /mysql/ib_buffer_pool
  21. 190822 13:06:42 [01] ...done
  22. 190822 13:06:42 [01] Copying ./xtrabackup_info to /mysql/xtrabackup_info
  23. 190822 13:06:42 [01] ...done
  24. 190822 13:06:42 [01] Copying ./xtrabackup_binlog_pos_innodb to /mysql/xtrabackup_binlog_pos_innodb
  25. 190822 13:06:42 [01] ...done
  26. 190822 13:06:42 [01] Copying ./xtrabackup_master_key_id to /mysql/xtrabackup_master_key_id
  27. 190822 13:06:42 [01] ...done
  28. 190822 13:06:42 [01] Copying ./xtrabackup_galera_info to /mysql/xtrabackup_galera_info
  29. 190822 13:06:42 [01] ...done
  30. 190822 13:06:42 completed OK!
  31. [root@sjk1 ~]# chown -R mysql:mysql /mysql/ # 修复属主属组
  32. [root@sjk1 ~]# systemctl start mysql # 启动服务

登录检查恢复情况

  1. [root@sjk1 ~]# mysql -uroot -p # 登录检查恢复情况
  2. Enter password:
  3. ......
  4. mysql> show databases;
  5. +--------------------+
  6. | Database |
  7. +--------------------+
  8. | information_schema |
  9. | mysql |
  10. | percona |
  11. | performance_schema |
  12. | sys |
  13. +--------------------+
  14. 5 rows in set (0.00 sec)
  15. mysql> select * from percona.example;
  16. +---------+-----------+
  17. | node_id | node_name |
  18. +---------+-----------+
  19. | 1 | percona1 |
  20. | 2 | percona2 |
  21. | 3 | percona3 |
  22. | 4 | percona4 |
  23. | 5 | percona5 |
  24. +---------+-----------+
  25. 5 rows in set (0.25 sec)
  26. mysql>

 

注意:如果数据节点全挂,数据全丢的情况下如何恢复?步骤如下(6步):

1.停掉所有服务
2.清空有节点数据目录下的内容

3.在第一节点上:

  1. innobackupex --apply-log --redo-only /root/full/2019-08-21_01-02-47
  2. innobackupex --defaults-file=/etc/my.cnf --copy-back /home/backup/full/2019-08-22_09-56-24  #全备恢复--还原数据库,还原前需确认数据库数据目录为空
  3. chown -R mysql:mysql /mysql/  # 修复属主属组

4.启动第一个节点
systemctl start mysql@bootstrap.service

5.启动其他节点(必须一个启动完再启动下一个)
systemctl start mysql

6.所有节点都启动完后,停掉第一个节点,然后以正常方式启动

  1. [root@sjk1 mysql]# systemctl stop mysql@bootstrap.service
  2. [root@sjk1 mysql]# systemctl start mysql

 

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

闽ICP备14008679号