Mariadb Galera Cluster 群集 安装部署

时间:2022-05-31 06:50:46

#Mariadb Galera Cluster 群集 安装部署

openstack pike 部署  目录汇总 http://www.cnblogs.com/elvi/p/7613861.html

#Mariadb Galera Cluster群集

# ###########################
# ##基本设置

# #配置hosts
# echo '#Ha
# 192.168.58.21   controller1
# 192.168.58.22   controller2
# 192.168.58.23   controller3
# '>>/etc/hosts

# #免密验

# #controller1 ssh免密验证
# ssh-keygen -t rsa -P '' -f ~/.ssh/id_dsa
# ssh-copy-id -i ~/.ssh/id_dsa.pub root@controller2
# ssh-copy-id -i ~/.ssh/id_dsa.pub root@controller2
# #其它节点一样
# #shell批量配置http://www.cnblogs.com/elvi/p/7681779.html

# ###########################

# #每个节点执行

# #配置内核
# echo '
# * soft nofile 65536  
# * hard nofile 65536 
# '>>/etc/security/limits.conf
# #
# echo '
# fs.file-max=655350  
# net.ipv4.ip_local_port_range = 1025 65000  
# net.ipv4.tcp_tw_recycle = 1 
# '>>/etc/sysctl.conf
# sysctl -p
# ###########################

#MariaDB Galera 安装
yum install mariadb mariadb-server mariadb-galera-server -y
yum install expect -y

#SQL root密码
DBPass=open2017

#配置数据库
echo "#
[mysqld]
bind-address = 0.0.0.0
default-storage-engine = innodb
innodb_file_per_table
max_connections = 4096
collation-server = utf8_general_ci
character-set-server = utf8
#">/etc/my.cnf.d/openstack.cnf

#启动数据库服务
#systemctl enable mariadb.service
systemctl start mariadb.service

#mysql_secure_installation #初始化设置密码,自动交互
/usr/bin/expect << EOF
set timeout 30
spawn mysql_secure_installation
expect {
    "enter for none" { send "\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    "password:" { send "$DBPass\r"; exp_continue}
    "new password:" { send "$DBPass\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    eof { exit }
}
EOF

#测试
mysql -u root -p$DBPass -e "show databases;"
[ $? = 0 ] || { echo "mariadb初始化失败";exit; }
#############

#galera配置
/usr/bin/cp /etc/my.cnf.d/galera.cnf{,.bak}
egrep -v "#|^$" /etc/my.cnf.d/galera.cnf.bak >/etc/my.cnf.d/galera.cnf
sed -i 's/wsrep_on=1/wsrep_on=ON/' /etc/my.cnf.d/galera.cnf
sed -i 's/wsrep_sst_auth=root:/wsrep_sst_auth=root:'$DBPass'/' /etc/my.cnf.d/galera.cnf

###########
#controller1节点
sed -i 's/bind-address = 0.0.0.0/bind-address = controller1/' /etc/my.cnf.d/openstack.cnf
echo '
wsrep_cluster_address="gcomm://controller1,controller2,controller3"
wsrep_node_name=controller1
wsrep_node_address=controller1
'>>/etc/my.cnf.d/galera.cnf
cat /etc/my.cnf.d/galera.cnf

systemctl daemon-reload
systemctl stop mariadb.service
galera_new_cluster #启动第一个节点

#############
#controller2节点
sed -i 's/bind-address = 0.0.0.0/bind-address = controller2/' /etc/my.cnf.d/openstack.cnf
echo '
wsrep_cluster_address="gcomm://controller1,controller2,controller3"
wsrep_node_name=controller2
wsrep_node_address=controller2
'>>/etc/my.cnf.d/galera.cnf
systemctl restart mariadb.service

#controller3节点
sed -i 's/bind-address = 0.0.0.0/bind-address = controller3/' /etc/my.cnf.d/openstack.cnf
echo '
wsrep_cluster_address="gcomm://controller1,controller2,controller3"
wsrep_node_name=controller3
wsrep_node_address=controller3
'>>/etc/my.cnf.d/galera.cnf
systemctl restart mariadb.service

#其它节点启动后,重启第一个节点 controller1
systemctl restart mariadb.service

#检测
netstat -antp|grep mysqld
mysql -u root -p$DBPass -e "show status like 'wsrep_cluster_size';"
mysql -u root -p$DBPass -e "show status like 'wsrep_incoming_addresses';"

#创建用于监控的mariadb 用户haproxy (haproxy代理,监控使用)
mysql -u root -p$DBPass -e "create user 'haproxy'@'%';flush privileges;"

#root远程访问设置
mysql -u root -p$DBPass -e "
grant all privileges on *.* to 'root'@'%' identified by '$DBPass' with grant option; 
flush privileges;
select user,host,password from mysql.user;"

##########################################

##部署成功,检测结果如下      
# [root@controller1 ~]# mysql -u root -p$DBPass -e "show status like 'wsrep_cluster_size';"
# +--------------------+-------+
# | Variable_name      | Value |
# +--------------------+-------+
# | wsrep_cluster_size | 3     |
# +--------------------+-------+
# [root@controller1 ~]# mysql -u root -p$DBPass -e "show status like 'wsrep_incoming_addresses';"
# +--------------------------+----------------------------------------------------+
# | Variable_name            | Value                                              |
# +--------------------------+----------------------------------------------------+
# | wsrep_incoming_addresses | controller1:3306,controller2:3306,controller3:3306 |
# +--------------------------+----------------------------------------------------+

# #排错
# #关闭selinux、防火墙
# tail /var/log/mariadb/mariadb.log
# rm -f /var/lib/mysql//galera.cache
# #关闭群集后,重启
# #在其中一台
# sed -i 's#seqno: -1#seqno: 1#' /var/lib/mysql/grastate.dat
# galera_new_cluster #启动第一个节点
# #其它节点
# systemctl start mariadb

 
 

#test
#/usr/libexec/mysqld --wsrep-new-cluster --user=root &
#systemctl start mariadb.service
#设置开机启动,设置为第一个节点
# systemctl disable mariadb.service
# echo '#作为第一个节点启动SQL
# /usr/bin/galera_new_cluster
# sleep 5
# ssh controller2 "systemctl start mariadb.service"
# ssh controller3 "systemctl start mariadb.service"
# sleep 3
# systemctl restart mariadb.service
# '>>/etc/rc.local



########################################## #以下为openstack相关的配置 #每个节点都设置 # haproxy高可用代理mariadb
echo '#mariadb_cluster listen mariadb_cluster mode tcp bind controller:3306 balance leastconn option mysql-check user haproxy server controller1 controller1:3306 weight 1 check inter 2000 rise 2 fall 5 server controller2 controller2:3306 weight 1 check inter 2000 rise 2 fall 5 server controller3 controller3:3306 weight 1 check inter 2000 rise 2 fall 5 '>>/etc/haproxy/haproxy.cfg systemctl restart haproxy.service #测试 DBPass=open2017 mysql -h controller -u root -p$DBPass -e "show status like 'wsrep_cluster_size';" ############# #以下在controller其中一节点执行即可 #创建openstack相关数据库、用户授权 #mysql -u root -p mysql -u root -p$DBPass -e " create database keystone; grant all privileges on keystone.* to 'keystone'@'localhost' identified by 'keystone'; grant all privileges on keystone.* to 'keystone'@'%' identified by 'keystone'; create database glance; grant all privileges on glance.* to 'glance'@'localhost' identified by 'glance'; grant all privileges on glance.* to 'glance'@'%' identified by 'glance'; create database nova; grant all privileges on nova.* to 'nova'@'localhost' identified by 'nova'; grant all privileges on nova.* to 'nova'@'%' identified by 'nova'; create database nova_api; grant all privileges on nova_api.* to 'nova'@'localhost' identified by 'nova'; grant all privileges on nova_api.* to 'nova'@'%' identified by 'nova'; create database nova_cell0; grant all privileges on nova_cell0.* to 'nova'@'localhost' identified by 'nova'; grant all privileges on nova_cell0.* to 'nova'@'%' identified by 'nova'; create database neutron; grant all privileges on neutron.* to 'neutron'@'localhost' identified by 'neutron'; grant all privileges on neutron.* to 'neutron'@'%' identified by 'neutron'; flush privileges; select user,host from mysql.user; show databases; " mysql -u root -p$DBPass -e " show databases; "