准备三台服务器

操作系统版本: centos7

数据库版本:5.5.56-MariaDB

主节点:192.168.224.131

节点1:192.168.224.132

节点2:192.168.224.133

一、各节点之间添加解析

vi /etc/hosts

192.168.224.131 k8s-master
192.168.224.132 k8s-node1
192.168.224.133 k8s-node2

二、配置mariadb的yum源

1.查看是否有安装mariadb
rpm -qa | grep MariaDB 
rpm -qa | grep mariadb
2.卸载
rpm -e --nodeps MariaDB-*
yum remove mysql mysql-server mysql-libs compat-mysql51

3.添加源
vim /etc/yum.repos.d/MariaDB.repo


[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

三、每个节点安装mariadb和galera

yum install -y MariaDB-server MariaDB-client galera -y

四、初始化数据库

systemctl restart mariadb
mysql_secure_installation 
回车
y
设置root密码
y
n
y
y

五、关闭数据库,修改/etc/my.cnf.d/server.cnf

[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://192.168.224.131,192.168.224.132,192.168,224.133"  #节点IP地址
wsrep_node_name= k8s-mastr-1        #节点主机名
wsrep_node_address=192.168.224.131   #节点IP
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
innodb_buffer_pool_size=120M
wsrep_sst_method=rsync
wsrep_causal_reads=ON

将此文件复制到mariadb-2、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改成相应节点的 hostname 和 ip。

六、启动集群

主节点启动192.168.224.131启动命令如下
galera_new_cluster 
 
 其他节点启动:
  systemctl restart mariadb
  
  集群服务监听了4567和3306端口(每个节点都监听了4567和3306端口)   
  
  ss -tnl

连接mariadb,查看是否启用galera插件

##显示on表示开启
show status like "wsrep_ready";
##查看集群集群数
 show status like "wsrep_cluster_size";
 ##查看集群集群状态
 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           | 2                                                     |
| wsrep_cert_deps_distance     | 0.000000                                              |
| wsrep_cert_index_size        | 0                                                     |
| wsrep_cert_interval          | 0.000000                                              |
| wsrep_cluster_conf_id        | 14                                                    |
| wsrep_cluster_size           | 3                                                     |
| wsrep_cluster_state_uuid     | 541248ce-56d0-11e9-b41b-3a9775d73ca7                  |
| 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             | 540ee869-56d0-11e9-955e-9b7f30e437a6                  |
| wsrep_incoming_addresses     | 192.168.64.5:3306,192.168.64.4:3306,192.168.64.7: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.100000                                              |
| 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       | 541248ce-56d0-11e9-b41b-3a9775d73ca7                  |
| wsrep_protocol_version       | 8                                                     |
| wsrep_provider_name          | Galera                                                |
| wsrep_provider_vendor        | Codership Oy <info@codership.com>                     |
| wsrep_provider_version       | 25.3.23(r3789)                                        |
| wsrep_ready                  | ON                                                    |
| wsrep_received               | 20                                                    |
| wsrep_received_bytes         | 3651                                                  |
| 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.002 sec)

###查看集群主机IP
 show status like "wsrep_incoming_addresses";

在其中一个节点操作创建数据库,再另外一个节点查看是否同步

posted on 2022-02-28 13:38  凡是过往皆为序章  阅读(1037)  评论(0编辑  收藏  举报