3. 安装MySQL-pxc
3.1. 说明
之前为了搭建高可用的MySQL的集群,所以安装MySQL-pxc,具体介绍可以看https://www.percona.com/software/mysql-database/percona-xtradb-cluster
不知道为什么服务器下载都巨慢,所以我都使用离线安装。下载的是主要的包,还有其他一堆依赖需要服务器自己去下载。
ansiblehosts
文件
[mysqlservers]
sz-1 node_name=pxc1
sz-2 node_name=pxc2
3.2. yml脚本
---
- hosts: mysqlservers
remote_user: root
vars_files:
- ../vars.yml
tasks:
- name: Ensure SELinux is set to enforcing mode
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: SELINUX=permissive
- name: Copy rpm
copy:
src: "{{ PLAYBOOK_DIR }}/files/mysql"
dest: /mnt
owner: root
group: root
mode: 0755
- name: install prepare
yum:
name: "/mnt/mysql/percona-release-latest.noarch.rpm"
state: present
- name: install pxc
shell: "yum localinstall -y /mnt/mysql/Percona/*"
- name: firewarld add 3306
firewalld:
port: 3306/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: firewarld add 4444
firewalld:
port: 4444/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: firewarld add 4567
firewalld:
port: 4567/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: firewarld add 4568
firewalld:
port: 4568/tcp
permanent: true
immediate: true
zone: public
state: enabled
- name: Copy config
template:
src: "{{ PLAYBOOK_DIR }}/mysql-pxc/templates/wsrep.cnf.j2"
dest: /etc/percona-xtradb-cluster.conf.d/wsrep.cnf
owner: root
group: root
mode: 0755
- name: set SELinux to permissive
shell: setenforce 0
- name: start mysql@bootstrap
service:
name: mysql@bootstrap
state: started
when: inventory_hostname == groups['mysqlservers'][0]
因为是搭建集群,需要先启动第一个节点,进行配置完后再启动其他节点,各节点会自动建立链接。
3.3. 模板文件
因为搭建的是集群环境,因此都需要配置节点
3.3.1 wsrep.cnf.j2
[mysqld]
# Path to Galera library
wsrep_provider=/usr/lib64/galera3/libgalera_smm.so
# Cluster connection URL contains IPs of nodes
#If no IP is found, this implies that a new cluster needs to be created,
#in order to do that you need to bootstrap this node
wsrep_cluster_address=gcomm://{% set comma = joiner(",") %}
{% for item in groups['mysqlservers'] -%}
{{ comma() }}{{ hostvars[item].inventory_hostname }}
{%- endfor %}
# In order for Galera to work correctly binlog format should be ROW
binlog_format=ROW
# MyISAM storage engine has only experimental support
default_storage_engine=InnoDB
# Slave thread to use
wsrep_slave_threads=8
wsrep_log_conflicts
# This changes how InnoDB autoincrement locks are managed and is a requirement for Galera
innodb_autoinc_lock_mode=2
# Node IP address
wsrep_node_address={{ ansible_default_ipv4.address }}
# Cluster name
wsrep_cluster_name=newtranx-pxc-cluster
#If wsrep_node_name is not specified, then system hostname will be used
wsrep_node_name={{ node_name }}
#pxc_strict_mode allowed values: DISABLED,PERMISSIVE,ENFORCING,MASTER
pxc_strict_mode=ENFORCING
# SST method
wsrep_sst_method=xtrabackup-v2
#Authentication for SST method
wsrep_sst_auth=sstuser:passw0rd
3.4. 运行ansible-playbook
ansible-playbook -i /etc/ansible/hosts mysql/main.yml
3.5. 基本配置
修改密码、创建远程用户、修改权限,这里不做赘述,参考之前写的《Ansible部署系列:(六)安装MySQL》
3.6. 启动其他节点
---
- hosts: mysqlservers
remote_user: root
vars_files:
- ../vars.yml
tasks:
- name: set SELinux to permissive
shell: setenforce 0
- name: start mysql
service:
name: mysqld
state: started
when: inventory_hostname != groups['mysqlservers'][0]
3.7. 查看各节点状态
show status like 'wsrep%';
网友评论