码迷,mamicode.com
首页 > 数据库 > 详细

auto_install_mariadb_ab.sh

时间:2018-09-17 17:48:41      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:cut   server-id   author   ase   server   system   mysqld   mysq   safe   

#!/bin/bash
#auto make install mariadb ab repliation
#by author efoni
#20180914

MARIADB_SOFT="mariadb mariadb-server mariadb-devel php-mysql"
#MYSQL_SOFT="mysql mysql-server mysql-devel php-mysql"
NUM=rpm -qa | grep -i mariadb | wc -l
CODE=$?

#mariadb install
if [ $NUM -ne 0 ]; then
echo -e "\033[32mthis server had installed mysql .\033[0m"
read -p "please ensure yum remove mariadb server .yes or no?": INPUT
if [ $INPUT == "y" -o $INPUT == "yes" ]; then
yum remove -y $MARIADB_SOFT;rm -rf /usr/lib64/mysql;rm -rf /usr/share/mysql;rm -f /etc/my.cnf
yum install -y $MARIADB_SOFT
else
exit 0
fi
else
yum install -y $MARIADB_SOFT
if [ $CODE -eq 0 ]; then
echo -e "\033[32m mariadb installed successfully .\033[0m"
else
echo -e "\033[32m mariadb installtion failed .\033[0m"
exit 1
fi
fi

systemctl start mariadb
for i in seq 10 ;do sleep 1;done
systemctl stop mariadb
for i in seq 5 ;do sleep 1;done
mv /etc/my.cnf /etc/my.cnf.bak
touch /etc/my.cnf

#master mariadb configuration
cat >/etc/my.cnf<<EOF
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
log-bin=mysql-bin
server-id=1
auto_increment_offset=1
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
replication-do-db=all
EOF

systemctl restart mariadb
#/etc/init.d/mysqld restart
ps -ef | grep mariadb

function MARIADB_CONFIG(){
#master configuration
mysql -e "grant FILE on . to ‘tongbu‘@‘192.168.3.228‘ identified by ‘tongbu‘;"
mysql -e "grant replication slave on . to ‘tongbu‘@‘%‘ identified by ‘tongbu‘;"
mysql -e "flush privileges;"

MASTER_FILE=mysql -e "show master status;"|tail -l|awk ‘{print $1}‘|grep -v ‘File‘
MASTER_POS=mysql -e "show master status;"|tail -l|awk ‘{print $2}‘|grep -v ‘Position‘

MASTER_IPADDR=ifconfig ens32|grep "netmask"|awk ‘{print $2}‘|cut -d: -f2

read -p "please input slave ip address": SLAVE_IPADDR

#slave configuration
ssh -l root $SLAVE_IPADDR "systemctl stop mariadb"
ssh -l root $SLAVE_IPADDR "yum remove -y mariadb mariadb-server mariadb-devel php-mysql"
ssh -l root $SLAVE_IPADDR "rm -rf /var/lib/mysql; rm -f /etc/my.cnf"
ssh -l root $SLAVE_IPADDR "yum install -y mariadb mariadb-server mariadb-devel php-mysql"
ssh -l root $SLAVE_IPADDR "systemctl restart mariadb"
ssh -l root $SLAVE_IPADDR "echo [mysqld] >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo datadir=/var/lib/mysql >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo socket=/var/lib/mysql/mysql.sock >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo user=mysql >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo symbolic-links=0 >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo log-bin=mysql-bin >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo server-id=2 >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo auto_increment_offset=1 >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo auto_increment_increment=2 >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo [mysqld_safe] >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo log-error=/var/log/mariadb/mariadb.log >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo pid-file=/var/run/mariadb/mariadb.pid >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "echo replication-do-db=all >>/etc/my.cnf"
ssh -l root $SLAVE_IPADDR "systemctl restart mariadb"
ssh -l root $SLAVE_IPADDR "mysql -e \"slave stop;\""
ssh -l root $SLAVE_IPADDR "mysql -e \"change master to master_host=‘$MASTER_IPADDR‘,master_user=‘tongbu‘,master_password=‘tongbu‘,master_log_file=‘$MASTER_FILE‘,master_log_pos=$MASTER_POS;\""

ssh -l root $SLAVE_IPADDR "mysql -e \"slave start;\""
ssh -l root $SLAVE_IPADDR "mysql -e \"show slave status\G;\""

}

read -p "please ensure your server is master role ,yes or no ?": INPUT
if [ $INPUT == "y" -o $INPUT == "yes" ]; then
MARIADB_CONFIG
else
exit 0
fi

auto_install_mariadb_ab.sh

标签:cut   server-id   author   ase   server   system   mysqld   mysq   safe   

原文地址:http://blog.51cto.com/efoni/2176230

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!