Redhat 6.5下MySQL5.6集群配置方法完整版
1. 环境准备
1.1 安装MySQL
首先需要为每个节点安装MySQL5.6,可以从MySQL官网下载对应的rpm文件进行安装。具体命令如下:
rpm -ivh MySQL-server-5.6.30-1.el6.x86_64.rpm
rpm -ivh MySQL-client-5.6.30-1.el6.x86_64.rpm
1.2 配置MySQL
安装完成后需要进行MySQL的基本配置,打开my.cnf文件,配置bind_address、datadir等参数,确保MySQL可以正常启动。
vi /etc/my.cnf
1.3 安装与配置Pacemaker
Pacemaker是一个用于构建高可用集群的工具,可以通过yum安装。具体命令如下:
yum install pacemaker pcs -y
安装完成后,需要配置pcs服务,并设置hacluster用户的密码。
systemctl start pcsd.service
systemctl enable pcsd.service
passwd hacluster
2. 配置集群
2.1 创建Cluster
在主节点上创建Cluster,类似的,其他节点需先配置pcs后再加入Cluster。具体命令如下:
pcs cluster auth node1 node2 node3
pcs cluster setup --name myCluster node1 node2 node3
2.2 配置IP资源
创建IP资源,并分别设置为Master节点启动,其他节点禁止启动。具体命令如下:
pcs resource create VirtualIP ocf:heartbeat:IPaddr2 ip=192.168.0.100 cidr_netmask=24 op monitor interval=10s
pcs resource master masterIP VirtualIP \
master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=true
pcs resource ban VirtualIP node1 node2 node3
pcs resource ban masterIP node1 node2 node3
pcs resource allow masterIP node1 op monitor interval=10s
2.3 配置MySQL资源
启动MySQL5.6的Master节点,并设置监听端口。
systemctl start mysqld.service
mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;"
mysql -uroot -e "reset master;"
mysql -uroot -e "reset slave all;"
创建MySQL的Master节点资源,并设置监听端口并关联VirtualIP资源。
pcs resource create Master mysqld op monitor interval=30s timeout=30s \
OCF_CHECK_LEVEL=1 OCF_CHECK_INTERVAL=10s \
OCF_CHECK_RETRIES=5 master IP=192.168.0.100 port=3306 \
config="/etc/my.cnf" additional_parameters="--innodb-write-io-threads=16" \
binary="/usr/bin/mysqld_safe" pid="/var/run/mysqld/mysqld.pid" op start timeout=600s \
op stop timeout=600s op promote timeout=300s op demote timeout=300s \
op notify timeout=300s op monitor interval=30s timeout=20s \
OCF_NOT_RUNNING_STOP_OPT="stop" \
ordered=true
pcs constraint order promote VirtualIP then start Master-clone kind=Mandatory
pcs constraint order start VirtualIP then start Master-clone
pcs constraint colocation add Master-clone with VirtualIP same-node
2.4 配置Slave节点
创建2个Slave资源,同时开启GTID复制。
mysql -uroot -e "change master to master_user='repl_user', master_password='repl_password', \
master_host='192.168.0.101', master_port=3306, master_use_gtid=current_pos;"
mysql -uroot -e "start slave;"
mysql -uroot -e "change master to master_user='repl_user', master_password='repl_password', \
master_host='192.168.0.102', master_port=3306, master_use_gtid=current_pos;"
mysql -uroot -e "start slave;"
pcs resource create Slave1Mysql.mysql \
binary="/usr/bin/mysqld_safe" pid="/var/run/mysqld/mysqld.pid" \
config="/etc/my.cnf" datadir="/var/lib/mysql" \
socket="/var/lib/mysql/mysql.sock" port="3306" \
mysql="/usr/bin/mysql" user="mysql" \
group="mysql" \
master_ip="192.168.0.100" \
password="password" \
op start timeout=60s op stop timeout=60s \
op promote timeout=60s op demote timeout=60s \
op monitor interval=10s timeout=30s \
op notify timeout=60s
pcs resource create Slave2Mysql.mysql \
binary="/usr/bin/mysqld_safe" pid="/var/run/mysqld/mysqld.pid" \
config="/etc/my.cnf" datadir="/var/lib/mysql" \
socket="/var/lib/mysql/mysql.sock" port="3306" \
mysql="/usr/bin/mysql" user="mysql" \
group="mysql" \
master_ip="192.168.0.100" \
password="password" \
op start timeout=60s op stop timeout=60s \
op promote timeout=60s op demote timeout=60s \
op monitor interval=10s timeout=30s \
op notify timeout=60s
pcs constraint order promote VirtualIP then start Slave1Mysql-clone kind=Mandatory
pcs constraint order start VirtualIP then start Slave1Mysql-clone
pcs constraint colocation add Slave1Mysql-clone with VirtualIP same-node
pcs constraint order promote VirtualIP then start Slave2Mysql-clone kind=Mandatory
pcs constraint order start VirtualIP then start Slave2Mysql-clone
pcs constraint colocation add Slave2Mysql-clone with VirtualIP same-node
3. 集群管理
3.1 启动集群
已创建好的Cluster资源可以通过以下命令启动:
pcs cluster start --all
3.2 停止节点资源
停止节点资源可以通过以下命令进行:
pcs resource disable node1
3.3 恢复节点资源
恢复节点资源可以通过以下命令进行:
pcs resource enable node1
3.4 查看Cluster状态
查看Cluster的状态,可以通过以下命令进行:
pcs status
4. 示例说明
4.1 查看MySQL节点状态
可以通过以下命令查看MySQL节点状态,以node1为例:
pcs status | grep 'node1' | grep 'Starting\|Running\|Stopped'
如果输出为Starting或Running,则说明节点的MySQL服务正常运行。如果输出为Stopped,则说明节点的MySQL服务已经停止。
4.2 启动Master节点资源
可以通过以下命令启动Master节点资源:
pcs resource enable Master-clone
启动成功后,可以通过以下命令查看节点状态:
pcs status | grep 'Master-clone' | grep 'Started\|Stopped'
如果输出为Started,则表示Master节点资源已经启动。如果输出为Stopped,则表示Master节点资源没有成功启动,可能需要检查是否有其他资源因为依赖关系无法启动。
以上就是在Redhat 6.5下MySQL5.6集群配置方法完整版的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Redhat 6.5下MySQL5.6集群配置方法完整版 - Python技术站