平时集群需要经常部署mariadb数据库服务。mariadb的部署,有很多种方式,可以根据实际需求选择不同的部署架构。
比较常用的包括:
本篇内容主要主要是完成另一个支持这三种部署方式的脚本,在平时使用足够。
脚本在安装了centos7.9的虚拟机环境测试过,偶尔会发生错误,其他时间都能够正常部署。
保存为:my_mariadb_install.sh后,执行该脚本即可
my_mariadb_install.sh s 单点部署
my_mariadb_install.sh ms 主从架构
my_mariadb_install.sh mm 双主架构
脚本仅在我自己部署的虚拟机测试通过,切勿用于生产环境
#!/bin/bash #部署maraidb的脚本 if [ "$1" != "s" ] && [ "$1" != "mm" ] && [ "$1" != "ms" ] then echo "$(basename $0) [s|mm|ms]" exit 0 fi #创建单节点部署脚本 cat << EOF > install_modify_mariadb.sh HOSTNAME=\$1 IP=\$2 PASSWORD=\$3 echo "数据库主机名:\${HOSTNAME},数据库IP:\${IP},数据库密码是:\${PASSWORD}" echo -ne "正在yum安装mariadb..." yum -y install mariadb mariadb-server mariadb-devel mariadb-libs > /dev/null 2>&1 echo "安装完成." echo "配置编码..." SERVER_CONF=( init_connect='SET collation_connection = utf8_unicode_ci' init_connect='SET NAMES utf8' character-set-server=utf8 collation-server=utf8_general_ci skip-character-set-client-handshake max-connections=4096 open-files-limit=4096 innodb_buffer_pool_size=1024M innodb_log_file_size=64M innodb_lock_wait_timeout=900 ) LENTH=\${#SERVER_CONF[*]} for i in \$(seq 0 \$((\${LENTH}-1))) do sed -i "/mariadb-5.5/a\${SERVER_CONF[LENTH-i-1]}" /etc/my.cnf.d/server.cnf done CLIENT_CONF=( default-character-set=utf8 ) for line in \${CLIENT_CONF[*]} do sed -i "/client-mariadb/a\${line}" /etc/my.cnf.d/client.cnf done echo "配置最大连接数..." sed -i '/PrivateTmp/a\LimitNOFILE=10000' /usr/lib/systemd/system/mariadb.service systemctl daemon-reload systemctl enable mariadb -q echo "启动服务..." systemctl start mariadb 2>/dev/null && echo "本地节点:\${IP}数据库启动成功..." echo "初始化数据库..." mysql << HHH > init_mariadb.sh delete from mysql.user where user = " " and host = "localhost"; delete from mysql.user where user = " " and host = "127.0.0.1"; delete from mysql.user where user = " " and host = "\${HOSTNAME}"; delete from mysql.user where user = "root" and host = "\${HOSTNAME}"; delete from mysql.user where user = "root" and host = "::1"; grant all privileges on *.* to 'root'@'localhost' identified by '\${PASSWORD}'; grant all privileges on *.* to 'root'@'127.0.0.1' identified by '\${PASSWORD}'; flush privileges; HHH /bin/bash init_mariadb.sh EOF #创建双节点脚本 cat << EOF > master_conf.sh #写配置文件 m1_conf=( log-bin=mysql-bin server-id=1 auto_increment_increment=2 auto_increment_offset=1 ) for line in \${m1_conf[*]} do sed -i "/mysqld_safe/i\${line}" /etc/my.cnf done systemctl restart mariadb EOF cat << EOF > master_mariadb.sh PASSWORD=\$1 REMOTEIP=\$2 sql="grant replication slave on *.* to 'repl'@'\${REMOTEIP}' identified by '\${PASSWORD}';" mysql -hlocalhost -uroot -p\${PASSWORD} -e "\${sql}" sql="flush privileges;" mysql -hlocalhost -uroot -p\${PASSWORD} -e "\${sql}" sql="show master status" rtn=\$(mysql -hlocalhost -uroot -p\${PASSWORD} -N -e "\${sql}") mysqlbin=\$(echo \${rtn}|awk '{print \$1}') posttion=\$(echo \${rtn}|awk '{print \$2}') echo "\${mysqlbin} \${posttion}" EOF cat << EOF > slave_conf.sh m2_conf=( log-bin=mysql-bin server-id=2 auto_increment_increment=2 auto_increment_offset=2 ) for line in \${m2_conf[*]} do sed -i "/mysqld_safe/i\${line}" /etc/my.cnf done systemctl restart mariadb EOF cat << EOF > slave_mariadb.sh PASSWORD=\$1 MASTER_IP=\$2 mysqlbin=\$3 position=\$4 sql="change master to master_host='\${MASTER_IP}',master_user='repl',master_password='\${PASSWORD}',master_log_file='\${mysqlbin}',master_log_pos=\${position};" mysql -hlocalhost -uroot -p\${PASSWORD} -e "\${sql}" sql="start slave;" mysql -hlocalhost -uroot -p\${PASSWORD} -e "\${sql}" EOF #脚本功能 #单节点模式 if [ "${1}" == "s" ] then echo "mariadbg单节点快速部署--->" read -p "数据库root密码:" PASSWORD if [ "${PASSWORD}" == "" ] then PASSWORD="liwanliang" fi read -p "输入节点的[IP地址]:" IP if [ "${IP}" == "" ] then echo "未输入IP,退出" exit 0 fi read -p "输入节点的(主机名):" HOST_NAME if [ "${HOST_NAME}" == ] then echo "未输入主机名,退出" exit 0 fi /bin/bash install_modify_mariadb.sh ${HOST_NAME} ${IP} ${PASSWORD} #rm -rf install_modify_mariadb.sh #双节点模式(主从) elif [ "${1}" == "ms" ] then echo "mariadb主从架构部署--->" read -p "数据库root密码:" PASSWORD read -p "主节点master的[IP地址]:" IP1 read -p "主节点master的(主机名):" HOSTNAME1 read -p "从节点slave的[IP地址]:" IP2 read -p "从节点slave的(主机名):" HOSTNAME2 #本地安装 /bin/bash install_modify_mariadb.sh ${HOSTNAME1} ${IP1} ${PASSWORD} /bin/bash master_conf.sh #远程安装 scp install_modify_mariadb.sh ${IP2}:/tmp ssh ${IP2} "/bin/bash /tmp/install_modify_mariadb.sh ${HOSTNAME2} ${IP2} ${PASSWORD}" scp slave_conf.sh ${IP2}:/tmp ssh ${IP2} "/bin/bash /tmp/slave_conf.sh" #本地执行主节点脚本 rtn=$(/bin/bash master_mariadb.sh ${PASSWORD} ${IP2}) echo "主节点状态信息:${rtn}" mysqlbin=$(echo ${rtn}|awk '{print $1}') position=$(echo ${rtn}|awk '{print $2}') #远程执行从脚本 scp slave_mariadb.sh ${IP2}:/tmp/ ssh ${IP2} "/bin/bash /tmp/slave_mariadb.sh ${PASSWORD} ${IP1} ${mysqlbin} ${position}" ### 主主架构 elif [ "${1}" == "mm" ] then echo "mariadb主主架构部署--->" read -p "数据库root密码:" PASSWORD read -p "主节点master1的[IP地址]:" IP1 read -p "主节点master1的(主机名):" HOSTNAME1 read -p "主节点master2的[IP地址]:" IP2 read -p "主节点master2的(主机名):" HOSTNAME2 #本地安装 /bin/bash install_modify_mariadb.sh ${HOSTNAME1} ${IP1} ${PASSWORD} /bin/bash master_conf.sh #远程安装 scp install_modify_mariadb.sh ${IP2}:/tmp ssh ${IP2} "/bin/bash /tmp/install_modify_mariadb.sh ${HOSTNAME2} ${IP2} ${PASSWORD}" scp slave_conf.sh ${IP2}:/tmp ssh ${IP2} "/bin/bash /tmp/slave_conf.sh" #本地执行主节点脚本 rtn=$(/bin/bash master_mariadb.sh ${PASSWORD} ${IP2}) echo "主节点master1状态信息:${rtn}" mysqlbin=$(echo ${rtn}|awk '{print $1}') position=$(echo ${rtn}|awk '{print $2}') #远程执行从脚本 scp slave_mariadb.sh ${IP2}:/tmp/ ssh ${IP2} "/bin/bash /tmp/slave_mariadb.sh ${PASSWORD} ${IP1} ${mysqlbin} ${position}" scp master_mariadb.sh ${IP2}:/tmp/ rtn=$(ssh ${IP2} "/bin/bash /tmp/master_mariadb.sh ${PASSWORD} ${IP1}") echo "主节点master2状态信息:${rtn}" mysqlbin=$(echo ${rtn}|awk '{print $1}') position=$(echo ${rtn}|awk '{print $2}') /bin/bash slave_mariadb.sh ${PASSWORD} ${IP2} ${mysqlbin} ${position} fi