#!/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 would 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 no 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 had 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 "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 ' @ ' percent ' identified by ' 123456 ';"
Master_file= ' mysql-e ' show master status; | Tail-1|awk ' {print '} '
Master_pos= ' mysql-e ' show master status; | Tail-1|awk ' {print $} '
Master_ipaddr= ' ifconfig eth0|grep "bcast" |awk ' {print $} ' |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 "Ensure your Server is Master?master or Master": INPUT
if [$INPUT = = "Master"-o $INPUT = = "Master"];then
Mysql_config
Else
Exit 0
Fi
MySQL master-slave configuration script