主机 | IP |
---|---|
node1 | 192.168.200.80 |
node2 | 192.168.200.81 |
node3 | 192.168.200.82 |
#!/bin/bash
# 定义节点信息
NODES=("192.168.200.80 node1" "192.168.200.81 node2" "192.168.200.82 node3")
# 定义当前节点的密码(默认集群统一密码)
HOST_PASS="000000"
# 时间同步的目标节点
TIME_SERVER=node1
# 时间同步的地址段
TIME_SERVER_IP=192.160.200.0/24
# 欢迎界面
cat > /etc/motd <<EOF
################################
# Welcome to mycluster #
################################
EOF
#禁用selinux
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#firewalld
systemctl stop firewalld
systemctl disable firewalld >> /dev/null 2>&1
#关闭IPtables,清空规则
yum install iptables-services -y
if [ 0 -ne $? ]; then
echo -e "\033[31mThe installation source configuration errors\033[0m"
exit 1
fi
systemctl restart iptables
iptables -F
iptables -X
iptables -Z
/usr/sbin/iptables-save
systemctl stop iptables
systemctl disable iptables
#禁用NetworkManager
systemctl stop NetworkManager >> /dev/null 2>&1
systemctl disable NetworkManager >> /dev/null 2>&1
yum remove -y NetworkManager firewalld
systemctl restart network
# 优化ssh连接
sed -i -e 's/#UseDNS yes/UseDNS no/g' -e 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
systemctl reload sshd
# 修改主机名
for node in "${NODES[@]}"; do
ip=$(echo "$node" | awk '{print $1}')
hostname=$(echo "$node" | awk '{print $2}')
# 获取当前节点的主机名和 IP
current_ip=$(hostname -I | awk '{print $1}')
current_hostname=$(hostname)
# 检查当前节点与要修改的节点信息是否匹配
if [[ "$current_ip" == "$ip" && "$current_hostname" != "$hostname" ]]; then
echo "Updating hostname to $hostname on $current_ip..."
hostnamectl set-hostname "$hostname"
if [ $? -eq 0 ]; then
echo "Hostname updated successfully."
else
echo "Failed to update hostname."
fi
break
fi
done
# 遍历节点信息并添加到 hosts 文件
for node in "${NODES[@]}"; do
ip=$(echo "$node" | awk '{print $1}')
hostname=$(echo "$node" | awk '{print $2}')
# 检查 hosts 文件中是否已存在相应的解析
if grep -q "$ip $hostname" /etc/hosts; then
echo "Host entry for $hostname already exists in /etc/hosts."
else
# 添加节点的解析条目到 hosts 文件
sudo sh -c "echo '$ip $hostname' >> /etc/hosts"
echo "Added host entry for $hostname in /etc/hosts."
fi
done
if [[ ! -s ~/.ssh/id_rsa.pub ]]; then
ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q -b 2048
fi
# 检查并安装 expect 工具
if ! which expect &> /dev/null; then
echo "expect 工具未安装,正在安装 expect..."
sudo yum install -y expect
fi
# 遍历所有节点
for node in "${NODES[@]}"; do
ip=$(echo "$node" | awk '{print $1}')
hostname=$(echo "$node" | awk '{print $2}')
expect -c "
set timeout -1
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $hostname
expect {
\"*password:*\" { send -- \"$HOST_PASS\r\"; exp_continue }
\"*(yes/no)*\" { send -- \"yes\r\"; exp_continue }
eof { exit 1 }
}
"
done
# 时间同步
if [[ $name == $TIME_SERVER ]]; then
# 配置当前节点为时间同步源
sed -i '3,6s/^/#/g' /etc/chrony.conf
sed -i "7s/^/server $TIME_SERVER iburst/g" /etc/chrony.conf
echo "allow $TIME_SERVER_IP" >> /etc/chrony.conf
echo "local stratum 10" >> /etc/chrony.conf
else
# 配置当前节点同步到目标节点
sed -i '3,6s/^/#/g' /etc/chrony.conf
sed -i "7s/^/server $TIME_SERVER iburst/g" /etc/chrony.conf
fi
# 重启并启用 chrony 服务
systemctl restart chronyd
systemctl enable chronyd
echo "###############################################################"
echo "################# 集群初始化成功 #####################"
echo "###############################################################"
三台节点执行
[root@localhost ~]# bash init.sh
配置yum源
[root@node1 ~]# yum install -y vsftpd
[root@node1 ~]# echo anon_root=/opt >> /etc/vsftpd/vsftpd.conf
[root@node1 ~]# systemctl enable --now vsftpd
[root@node1 ~]# tar -xf mariadb-repo.tar.gz -C /opt/
[root@node1 ~]# mkdir /opt/centos
[root@node1 ~]# mount /dev/sr0 /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
[root@node1 ~]# ls /opt/
centos mariadb-repo
[root@node1 ~]# cat /etc/yum.repos.d/ftp.repo
[db]
name=db
baseurl=file:///opt/mariadb-repo
gpgcheck=0
enabled=1
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
[root@node1 ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos db
Cleaning up list of fastest mirrors
Other repos take up 195 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
db | 2.9 kB 00:00:00
(1/3): db/primary_db | 66 kB 00:00:00
(2/3): centos/group_gz | 153 kB 00:00:00
(3/3): centos/primary_db | 3.3 MB 00:00:00
repo id repo name status
centos centos 4,070
db db 91
repolist: 4,161
[root@node1 ~]#
[root@node2 ~]# rm -rf /etc/yum.repos.d/*
[root@node3 ~]# rm -rf /etc/yum.repos.d/*
[root@node1 ~]# scp /etc/yum.repos.d/* node2:/etc/yum.repos.d/
ftp.repo 100% 67 5.7KB/s 00:00
[root@node1 ~]# scp /etc/yum.repos.d/* node3:/etc/yum.repos.d/
ftp.repo 100% 67 25.6KB/s 00:00
[root@node1 ~]#
修改从节点yum源
[root@node2 ~]# cat /etc/yum.repos.d/ftp.repo
[db]
name=db
baseurl=ftp://node1/mariadb-repo
gpgcheck=0
enabled=1
[centos]
name=centos
baseurl=ftp://node1/centos
gpgcheck=0
enabled=1
[root@node2 ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos db
Cleaning up list of fastest mirrors
Other repos take up 195 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
db | 2.9 kB 00:00:00
(1/3): centos/group_gz | 153 kB 00:00:00
(2/3): db/primary_db | 66 kB 00:00:00
(3/3): centos/primary_db | 3.3 MB 00:00:00
repo id repo name status
centos centos 4,070
db db 91
repolist: 4,161
[root@node2 ~]#
[root@node3 ~]# cat /etc/yum.repos.d/ftp.repo
[db]
name=db
baseurl=ftp://node1/mariadb-repo
gpgcheck=0
enabled=1
[centos]
name=centos
baseurl=ftp://node1/centos
gpgcheck=0
enabled=1
[root@node3 ~]# yum clean all;yum repolist
Loaded plugins: fastestmirror
Cleaning repos: centos db
Cleaning up list of fastest mirrors
Other repos take up 195 M of disk space (use --verbose for details)
Loaded plugins: fastestmirror
Determining fastest mirrors
centos | 3.6 kB 00:00:00
db | 2.9 kB 00:00:00
(1/3): centos/group_gz | 153 kB 00:00:00
(2/3): centos/primary_db | 3.3 MB 00:00:00
(3/3): db/primary_db | 66 kB 00:00:00
repo id repo name status
centos centos 4,070
db db 91
repolist: 4,161
[root@node3 ~]#
三台节点安装数据库
yum install -y mariadb-server
三台节点启动
systemctl start mariadb
三台节点设置密码
mysqladmin -uroot password 123456
三台节点关闭数据库服务
systemctl stop mariadb
三台节点修改配置文件
[root@node3 ~]# cat /etc/my.cnf.d/server.cnf
[server]
[mysqld]
[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_cluster_address="gcomm://node1,node2,node3"
binlog_format=row
default_storage_engine=InnoDB
innodb_autoinc_lock_mode=2
bind-address=0.0.0.0
wsrep_slave_threads=1
innodb_flush_log_at_trx_commit=0
[embedded]
[mariadb]
[mariadb-10.3]
node1节点启动高可用数据库
[root@node1 ~]# galera_new_cluster
从节点启动数据库
[root@node2 ~]# systemctl restart mariadb
[root@node3 ~]# systemctl restart mariadb
验证
[root@node1 ~]# mysql -uroot -p123456
MariaDB [(none)]> show status like "wsrep_ready";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| wsrep_ready | ON |
+---------------+-------+
1 row in set (0.001 sec)
MariaDB [(none)]> show status like "wsrep_cluster_size";
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| wsrep_cluster_size | 3 |
+--------------------+-------+
1 row in set (0.000 sec)
MariaDB [(none)]>