您的位置:首页 > 数据库 > MySQL

mysql主从自动配置脚本

2015-11-18 09:59 525 查看
#!/bin/bash
#auto make install mysql and configure master and slave
#by csy 2015-11-17

#Define variables++++++++++++++
MYL_PACKAGE="mysql mysql-server mysql-devel php-mysql mysql-libs"
JUDEG=`cat /etc/passwd |grep mysql |wc -l`
MYS_DIR=/var/lib/mysql
MYS_CFG=/etc/my.cnf

echo -e "\033[43mTip: execute the script will delete database of original data, please backup data first then execute the script!\033[0m"

#Mysql TO install..............
if [ $JUDEG -eq 0 ];then
echo -e "\033[35mThe Mysql database is not installed, please install...\033[0m"
yum -y install $MYL_PACKAGE
if [ $? -eq 0 ];then
echo "Mysql database install success,please configure..."
else
echo "Mysql database install error...pleace check!!!"
fi
else
read -p "You have installed mysql before, please uninstall, and reinstall the installation!Please enter yes or YES": INPUT
if [ $INPUT == "yes" -o $INPUT == "YES" ];then
yum -y remove $MYL_PACKAGE ; rm -rf $MYS_DIR $MYS_CFG
if [ $? -eq 0 ];then
echo "Mysql database uninstall complete...."
else
echo "Mysql database uninstall error...pleace check!!!"
exit 0
fi
yum -y install $MYL_PACKAGE
if [ $? -eq 0 ];then
echo "Mysql database install complete...."
else
echo "Mysql database install error...pleace check!!!"
exit 0
fi
else
exit 0
fi
fi

#Mysql The configuration and start the database service
#Mysql master server 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/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db =all
EOF
/etc/init.d/mysqld restart
ps -ef |grep mysql

function sLAVE_CONFIG(){
#Mysql slave server 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 = 2
auto_increment_offset=2
auto_increment_increment=2
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
replicate-do-db =all
EOF
/etc/init.d/mysqld restart
ps -ef |grep mysql
}
read -p "Please ensure your Server is slave?yes or no": INPUT
if [ $INPUT == "yes" -o $INPUT == "YES" ];then
SLAVE_CONFIG
else
echo "Configuration is complete, please continue..."
fi

function MYSQL_CONFIG(){
#Master Config Mysql
mysql -e "grant replication slave on *.* to 'csy'@'%' identified by '123456';"
MASTER_FILE=`mysql -e "show master status;"|tail -1|awk '{print $1}'`
MASTER_POS=`mysql -e "show master status;"|tail -1|awk '{print $2}'`
MASTER_IPADDR=`ifconfig eth0|grep "Bcast"|awk '{print $2}'|cut -d: -f2`
read -p "Please Enter INput Slave IP Address": SLAVE_IPADDR

#Slave Config Mysql
#ssh -l root $SLAVE_IPADDR "sed -i 's#server-id = 1#server-id = 2#g' /etc/my.cnf"
#ssh -l root $SLAVE_IPADDR "/etc/init.d/mysqld restart"
ssh -l root $SLAVE_IPADDR "mysql -e \"change master to master_host='$MASTER_IPADDR',master_user='csy',master_password='123456',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?master or MASTER": INPUT
if [ $INPUT == "master" -o $INPUT == "MASTER" ];then
MYSQL_CONFIG
else
exit 0
fi

本文出自 “生是见识,不是活着” 博客,请务必保留此出处http://linuxshengyu.blog.51cto.com/8810066/1713893
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: