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

mysql主从配置脚本

时间:2015-11-19 12:59:22      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

#!/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

mysql主从配置脚本

标签:

原文地址:http://www.cnblogs.com/csylinux/p/4977119.html

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