上传文件到opt目录下
Galera能够实现MySQL/MariaDB数据库的主主复制和多主复制等模式,这些复制模式都是同步进行的,同步时间非常短。
每一个节点都可以同时写入和读取,当某一节点发生故障时,可自动从集群中自动剔除。
HAProxy能提供负载均衡和故障判断等功能解决服务器系统存在的单点故障。
Keepalived能提供客户端连接数据库时使用的虚拟IP地址(VIP)。
(1)主节点
IP地址:node-1 192.168.1.74
(2)备份节点
IP地址:node-2 192.168.1.75
编辑每个节点/etc/hosts文件,添加所有节点IP地址和主机名
vi /etc/hosts
192.168.1.74 node-1
192.168.1.75 node-2
关闭selinux
# vi /etc/selinux/config
SELINUX=permissive
关闭防火墙并设置开机不自启
# systemctl stop firewalld
配置yum源文件
# vi /etc/yum.repos.d/mariadb.repo
#yum clean all
#yum list
安装服务(node-1节点和node-2节点)
#yum install MariaDB-server galera
修改配置文件
(1)修改/etc/my.cnf,添加以下内容(node-1节点和node-2节点)
#vi /etc/my.cnf
[mysqld]max_connect_errors=1000
(2)配置主节点node-1
#vi /etc/my.cnf.d/server.cnf
[galera]wsrep_on=ONwsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address=gcomm:// binlog_format=rowdefault_storage_engine=InnoDBinnodb_autoinc_lock_mode=2bind-address=0.0.0.0wsrep_cluster_name="MariaDB_Cluster"wsrep_node_address="192.168.1.74"wsrep_sst_method=rsyn
(3)配置备份节点node-2
#vi /etc/my.cnf.d/server.cnf
[galera]wsrep_on=ONwsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.1.74,192.168.1.75" binlog_format=rowdefault_storage_engine=InnoDBinnodb_autoinc_lock_mode=2bind-address=0.0.0.0wsrep_cluster_name="MariaDB_Cluster"wsrep_node_address="192.168.1.75"wsrep_sst_method=rsync
启动服务
node-1节点:
#/bin/galera_new_cluster
node-2节点:
#systemctl start mariadb
测试集群状态
(1)查询集群状态
#mysql -u root -p
MariaDB [(none)]> show status like 'wsrep_%';
+------------------------------+---------------------------------------+| Variable_name | Value |+------------------------------+---------------------------------------+| wsrep_apply_oooe | 0.000000 || wsrep_apply_oool | 0.000000 || wsrep_apply_window | 0.000000 || wsrep_causal_reads | 0 || wsrep_cert_deps_distance | 0.000000 || wsrep_cert_index_size | 0 || wsrep_cert_interval | 0.000000 || wsrep_cluster_conf_id | 2 || wsrep_cluster_size | 2 || wsrep_cluster_state_uuid | 841486a3-b56a-11e8-a451-d7d8f2309253 || wsrep_cluster_status | Primary || wsrep_commit_oooe | 0.000000 || wsrep_commit_oool | 0.000000 || wsrep_commit_window | 0.000000 || wsrep_connected | ON || 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_flow_control_paused | 0.000000 || wsrep_flow_control_paused_ns | 0 || wsrep_flow_control_recv | 0 || wsrep_flow_control_sent | 0 || wsrep_gcomm_uuid | 8412f980-b56a-11e8-bb08-4fd6339a7674 || wsrep_incoming_addresses | 192.168.100.11:3306,192.168.1.74:3306 || wsrep_last_committed | 0 || wsrep_local_bf_aborts | 0 || wsrep_local_cached_downto | 18446744073709551615 || wsrep_local_cert_failures | 0 || wsrep_local_commits | 0 || wsrep_local_index | 1 || wsrep_local_recv_queue | 0 || wsrep_local_recv_queue_avg | 0.166667 || wsrep_local_recv_queue_max | 2 || wsrep_local_recv_queue_min | 0 || wsrep_local_replays | 0 || wsrep_local_send_queue | 0 || wsrep_local_send_queue_avg | 0.000000 || wsrep_local_send_queue_max | 1 || wsrep_local_send_queue_min | 0 || wsrep_local_state | 4 || wsrep_local_state_comment | Synced || wsrep_local_state_uuid | 841486a3-b56a-11e8-a451-d7d8f2309253 || wsrep_protocol_version | 7 || wsrep_provider_name | Galera || wsrep_provider_vendor | Codership Oy|| wsrep_provider_version | 25.3.22(r3764) || wsrep_ready | ON || wsrep_received | 6 || wsrep_received_bytes | 414 || wsrep_repl_data_bytes | 0 || wsrep_repl_keys | 0 || wsrep_repl_keys_bytes | 0 || wsrep_repl_other_bytes | 0 || wsrep_replicated | 0 || wsrep_replicated_bytes | 0 || wsrep_thread_count | 2 |+------------------------------+---------------------------------------+58 rows in set (0.00 sec)
如果 "wsrep_local_state_comment" is "Synced" 则表明集群创建成功
(3)数据同步
在node-1上新建数据库galera_test
MariaDB [(none)]> create database galera_test;
MariaDB [(none)]> show databases;
在node-2上查询
MariaDB [(none)]> show databases;