#!/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<
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
转载于:https://blog.51cto.com/efoni/2176230