需要输入4个ip地址 前面3个是集群的ip 最后是vip地址 用空格隔开
yum 出现问题 自己配置
我用夸克网盘分享了「mariadb-ha.tar」,点击链接即可保存。打开「夸克APP」,无需下载在线播放视频,畅享原画5倍速,支持电视投屏。
链接:https://pan.quark.cn/s/67bcc60e6165
read -p "输入四个ip:" ip
IPF=" " read -ra IPS <<< "$ip"
tar xf mariadb-ha.tar
mv /etc/yum.repos.d/* /tmp
cat << EOF > /etc/yum.repos.d/local.repo
[a]
baseurl=file:///root/mariadb-ha
gpgcheck=0
cat << EOF > /etc/hosts
${IPS[0]} node1
${IPS[1]} node2
${IPS[2]} node3
EOF
if ! command -v expect &> /dev/null; then
yum install -y expect
fi
filepath=~/.ssh/id_rsa.pub
user=root
password=000000
auto_ssh_copy_id() {
expect -c "set timeout -1;
spawn ssh-copy-id -i $4 $2@$1;
expect {
*(yes/no)* {send -- yes\r;exp_continue;}
*password:* {send -- $3\r;exp_continue;}
eof { exit 0;}
}";
}
# 判断本地的公钥是否存在,如果不存在则需要生成公钥
[ ! -f $filepath ] && ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
hosts=$(grep -v "^#" /etc/hosts)
# 对配置文件中的每一台主机进行免密登录操作
for serverIp in $hosts
do
auto_ssh_copy_id $serverIp $user $password $filepath
done
scp /root/mariadb-ha node2:/root/mariadb-ha
scp /root/mariadb-ha node3:/root/mariadb-ha
ssh node2 "rm -rf /etc/yum.repos.d/*"
ssh node3 "rm -rf /etc/yum.repos.d/*"
scp /etc/yum.repos.d/local.repo node2:/etc/yum.repos.d/
scp /etc/yum.repos.d/local.repo node3:/etc/yum.repos.d/
scp /etc/hosts node2:/etc/hosts
scp /etc/hosts node3:/etc/hosts
setenforce 0
ssh node2 "setenforce 0"
ssh node3 "setenforce 0"
hostnamectl set-hostname node1
ssh node2 "hostnamectl set-hostname node2"
ssh node3 "hostnamectl set-hostname node3"
yum install -y mariadb-server && systemctl restart mariadb && mysqladmin password 000000
ssh node2 "yum install -y mariadb-server && systemctl restart mariadb && mysqladmin password 000000"
ssh node3 "yum install -y mariadb-server && systemctl restart mariadb && mysqladmin password 000000"
sed -i "17a wsrep_on=ON\nwsrep_provider=/usr/lib64/galera/libgalera_smm.so\nwsrep_cluster_address=gcomm://node1,node2,node3\nbinlog_format=row\ndefault_storage_engine=InnoDB\ninnodb_autoinc_lock_mode=2\nbind-address=${IPS[0]}" /etc/my.cnf.d/server.cnf
ssh node2 "sed -i \"17a wsrep_on=ON\nwsrep_provider=/usr/lib64/galera/libgalera_smm.so\nwsrep_cluster_address=gcomm://node1,node2,node3\nbinlog_format=row\ndefault_storage_engine=InnoDB\ninnodb_autoinc_lock_mode=2\nbind-address=${IPS[1]}\" /etc/my.cnf.d/server.cnf"
ssh node3 "sed -i \"17a wsrep_on=ON\nwsrep_provider=/usr/lib64/galera/libgalera_smm.so\nwsrep_cluster_address=gcomm://node1,node2,node3\nbinlog_format=row\ndefault_storage_engine=InnoDB\ninnodb_autoinc_lock_mode=2\nbind-address=${IPS[2]}\" /etc/my.cnf.d/server.cnf"
systemctl stop mariadb
galera_new_cluster
ssh node2 "systemctl restart mariadb"
ssh node3 "systemctl restart mariadb"
mysql -uroot -p000000 -e "grant all privileges on *.* to root@'%' identified by '000000';"
ssh node2 "mysql -uroot -p000000 -e \"grant all privileges on *.* to root@'%' identified by '000000';\""
ssh node3 "mysql -uroot -p000000 -e \"grant all privileges on *.* to root@'%' identified by '000000';\""
mysql -h ${IPS[0]} -uroot -e "SET GLOBAL server_id=11"
mysql -h ${IPS[1]} -uroot -e "SET GLOBAL server_id=12"
mysql -h ${IPS[2]} -uroot -e "SET GLOBAL server_id=13"
cat <<EOF > /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p
scp /etc/sysctl.conf node2:/etc/sysctl.conf
scp /etc/sysctl.conf node3:/etc/sysctl.conf
ssh node2 "sysctl -p"
ssh node3 "sysctl -p"
yum install -y haproxy
ssh node2 "yum install -y haproxy"
ssh node3 "yum install -y haproxy"
cat << EOF > /etc/haproxy/haproxy.cfg
listen stats
bind ${IPS[0]}:9000
mode http
stats enable
stats uri /stats
stats auth admin:admin
stats admin if TRUE
listen mariadb
balance roundrobin
mode tcp
option tcplog
option tcpka
bind ${IPS[3]}:3306
server node1 ${IPS[0]}:3306 check weight 1
server node2 ${IPS[1]}:3306 check weight 2
server node3 ${IPS[2]}:3306 check weight 4
EOF
scp /etc/haproxy/haproxy.cfg node2:/etc/haproxy/haproxy.cfg
scp /etc/haproxy/haproxy.cfg node3:/etc/haproxy/haproxy.cfg
ssh node2 "sed -i \"s/${IPS[0]}:9000/${IPS[1]}:9000/g\" /etc/haproxy/haproxy.cfg"
ssh node3 "sed -i \"s/${IPS[0]}:9000/${IPS[2]}:9000/g\" /etc/haproxy/haproxy.cfg"
systemctl restart haproxy
ssh node2 "systemctl restart haproxy"
ssh node3 "systemctl restart haproxy"
yum install keepalived -y
ssh node2 "yum install -y keepalived"
ssh node3 "yum install -y keepalived"
cat << EOF > /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
script "/etc/keepalived/chk.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0
virtual_router_id 55
priority 130
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
${IPS[3]}
}
track_script {
chk_haproxy
}
}
EOF
scp /etc/keepalived/keepalived.conf node2:/etc/keepalived/keepalived.conf
scp /etc/keepalived/keepalived.conf node3:/etc/keepalived/keepalived.conf
ssh node2 "sed -i 's/priority 130/priority 120/g' /etc/keepalived/keepalived.conf"
ssh node3 "sed -i 's/priority 130/priority 110/g' /etc/keepalived/keepalived.conf"
systemctl restart keepalived
ssh node2 "systemctl restart keepalived"
ssh node3 "systemctl restart keepalived"