MySQL自动化安装(双主多从读写分离)

时间:2022-12-19 05:40:53

shell

#!/bin/bash
# Create by
# version 1.0
# //
# # check out lockfile whether or not exist
IsInputParam=""
InputParamFile=""
install_home=`pwd`
mysql_proxy_home=/usr/local/mysql-proxy
install()
{
# 确保脚本不被重复执行
lockfile=/tmp/$(basename $)_lockfile
if [ -f $lockfile ];then
pid=$(cat $lockfile)
ps -p $pid | grep $pid &> /dev/null
if [ $? == ]; then
echo -e "\033[32m the script is already running !!! \033[0m" && exit
else
echo $$ > $lockfile
fi
else
echo $$ > $lockfile
fi setupMode=`cat $InputParamFile |grep "setup-mode" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 安装lua+MySQL proxy,支持任意局主机安装
if [ "$setupMode" -eq "" ] ; then heartbeatSetup=`cat $InputParamFile |grep "heartbeatSetup" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
if [ "$heartbeatSetup" -eq "" ] ; then
installheartbeat;
fi #echo -e "\033[32m *** install lua \033[0m"
#lua_home=$install_home/lua-5.2.
#cd $install_home
# 安装lua,安装前先卸载再安装;
#tar -zxvf $install_home/lua-5.2..tar.gz
#cd $lua_home/src && cd /usr/local/bin && rm -f lua luac
#cd $lua_home/src && cd /usr/local/include && rm -f lua.h luaconf.h lualib.h lauxlib.h lua.hpp
#cd $lua_home/src && cd /usr/local/lib && rm -f liblua.a
#cd $lua_home/doc && cd /usr/local/man/man1 && rm -f lua. luac. #cd $lua_home
#make linux
#make install # 导出环境变量
#export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm" echo -e "\033[32m *** install mysql Proxy \033[0m"
# 安装MySQL Proxy,解压即可使用
tar -zxf $install_home/mysql-proxy-0.8.-linux-el6-x86-64bit.tar.gz -C /usr/local
cd /usr/local # 一点点清理工作
killall - mysql-proxy
rm -rf /usr/local/mysql-proxy mv mysql-proxy-0.8.-linux-el6-x86-64bit mysql-proxy # 设置MySQL Proxy,根据安装参数进行配置
max_open_files=`cat $InputParamFile |grep "max-open-files" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
event_threads=`cat $InputParamFile |grep "event-threads" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
log_level=`cat $InputParamFile |grep "log-level" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
proxy_address=`cat $InputParamFile |grep "proxy-address" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
proxy_backend_addresses=`cat $InputParamFile |grep "proxy-backend-addresses" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
proxy_read_only_backend_addresses=`cat $InputParamFile |grep "proxy-read-only-backend-addresses" | tail -n | cut -d "=" -f2- | awk '{print $1}'` echo "[mysql-proxy]" >> $mysql_proxy_home/mysql-proxy.cnf
# daemon进程运行
echo "daemon=true" >> $mysql_proxy_home/mysql-proxy.cnf
echo "#user=mysql" >> $mysql_proxy_home/mysql-proxy.cnf
echo "proxy-skip-profiling=true" >> $mysql_proxy_home/mysql-proxy.cnf
# 保持连接(启动进程会有2个,一号进程用来监视二号进行,如果二号进程死掉自动重建,此参数在旧版本中无法使用)
echo "keepalive=true" >> $mysql_proxy_home/mysql-proxy.cnf
# 最大文件句柄数
echo "max-open-files=$max_open_files" >> $mysql_proxy_home/mysql-proxy.cnf
# 线程数
echo "event-threads=$event_threads" >> $mysql_proxy_home/mysql-proxy.cnf
# pid文件
echo "pid-file=/var/run/mysql-proxy.pid" >> $mysql_proxy_home/mysql-proxy.cnf
# 日志文件
echo "log-file=/var/log/mysql-proxy.log" >> $mysql_proxy_home/mysql-proxy.cnf
# 日志级别:error|warning|info|message|debug
echo "log-level=$log_level" >> $mysql_proxy_home/mysql-proxy.cnf
# admin-address=主机:端口 - 指定主机名(或IP地址)和端口管理端口。默认为localhost:。
#admin-address=172.20.100.36:
# 数据库用户名(主从上都需建立相同用户)
#admin-username=admin
# 数据库密码
#admin-password=coship
# admin脚本
#admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua
# mysql-proxy的ip和port,默认port是4040
echo "proxy-address=$proxy_address" >> $mysql_proxy_home/mysql-proxy.cnf
# mysql主库(写)地址,多个以逗号分隔
echo "proxy-backend-addresses=$proxy_backend_addresses" >> $mysql_proxy_home/mysql-proxy.cnf
# mysql从库(读)地址,多个以逗号分隔
echo "proxy-read-only-backend-addresses=$proxy_read_only_backend_addresses" >> $mysql_proxy_home/mysql-proxy.cnf
# 读写分离脚本
echo "proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" >> $mysql_proxy_home/mysql-proxy.cnf
# 设置文件权限
chmod $mysql_proxy_home/mysql-proxy.cnf echo "export PATH=$PATH:/usr/local/mysql-proxy/bin/" >> /etc/profile
source /etc/profile
sleep
# 启动mysqlProxy
mysql-proxy --defaults-file=$mysql_proxy_home/mysql-proxy.cnf # 设置MySQL Proxy服务操作脚本
echo "#!/bin/bash" >> $mysql_proxy_home/mysql-proxy.sh
echo ". /root/.bashrc" >> $mysql_proxy_home/mysql-proxy.sh
echo ". /etc/profile" >> $mysql_proxy_home/mysql-proxy.sh
echo 'mode=$1' >> $mysql_proxy_home/mysql-proxy.sh
echo 'if [ -z "$mode" ] ; then' >> $mysql_proxy_home/mysql-proxy.sh
echo 'mode="start"' >> $mysql_proxy_home/mysql-proxy.sh
echo "fi" >> $mysql_proxy_home/mysql-proxy.sh
echo 'case $mode in' >> $mysql_proxy_home/mysql-proxy.sh
echo "start)" >> $mysql_proxy_home/mysql-proxy.sh
echo "mysql-proxy --defaults-file=/usr/local/mysql-proxy/mysql-proxy.cnf>/usr/local/mysql-proxy/cn.log &" >> $mysql_proxy_home/mysql-proxy.sh
echo ";;" >> $mysql_proxy_home/mysql-proxy.sh
echo "stop)" >> $mysql_proxy_home/mysql-proxy.sh
echo "killall -9 mysql-proxy" >> $mysql_proxy_home/mysql-proxy.sh
echo ";;" >> $mysql_proxy_home/mysql-proxy.sh
echo "restart)" >> $mysql_proxy_home/mysql-proxy.sh
echo 'if $0 stop ; then' >> $mysql_proxy_home/mysql-proxy.sh
echo '$0 start' >> $mysql_proxy_home/mysql-proxy.sh
echo "else" >> $mysql_proxy_home/mysql-proxy.sh
echo 'echo "Restart failed!"' >> $mysql_proxy_home/mysql-proxy.sh
echo "exit 1" >> $mysql_proxy_home/mysql-proxy.sh
echo "fi" >> $mysql_proxy_home/mysql-proxy.sh
echo ";;" >> $mysql_proxy_home/mysql-proxy.sh
echo "esac" >> $mysql_proxy_home/mysql-proxy.sh
echo "exit 0" >> $mysql_proxy_home/mysql-proxy.sh chmod a+x $mysql_proxy_home/mysql-proxy.sh else
# 安装MySQL数据库
echo
echo -e "\033[32m *** step 1: check whether mysql is already installed? \033[0m"
echo # 一点点清理工作############################################
# 检查当前系统是否已安装MySQL,如果已经安装就卸载
for i in `rpm -qa|grep -i mysql`
do
rpm -q $i &> /dev/null
if [ $? == ]; then
# 删除mysql
rpm -e $i --nodeps &> /dev/null
echo $i "was uninstalled"
fi
done # 处理CentOS内部集成Mysql的情况
touch ys
echo "yes">ys
yum remove mysql-libs <ys >/dev/null # 删除配置文件
rm -f /etc/my.cnf
rm -rf /var/lib/mysql
############################################################ echo
echo -e "\033[32m *** step 2: install packages MySQL-server \033[0m"
echo # 开始安装MySQL服务
rpm -ivh MySQL-server-5.5.-.el6.x86_64.rpm if [ "$?" == ] ;
then
echo -e "\033[32m *** MySQL-server install success! \033[0m"
else
echo -e "\033[32m *** MySQL-server is not installed! \033[0m"
exit
fi echo
echo -e "\033[32m *** step 3: install packages MySQL-client \033[0m"
echo # 开始安装MySQL客户端
rpm -ivh MySQL-client-5.5.-.el6.x86_64.rpm if [ "$?" == ] ;
then
echo -e "\033[32m *** MySQL-client install success! \033[0m"
else
echo -e "\033[32m *** MySQL-client is not installed! \033[0m"
exit
fi echo
echo -e "\033[32m *** step 4: configure installation variables for mysql \033[0m"
echo echo -e "\033[32m *** service mysql start \033[0m"
service mysql start echo -e "\033[32m *** mysqladmin -u root password \033[0m" echo $InputParamFile # 获取数据库设置密码
password=`cat $InputParamFile |grep "password" | tail -n | cut -d "=" -f2- | awk '{print $1}'` mysqladmin -u root password $password mysql -uroot -p$password -e "grant all on *.* to 'root'@'%' identified by '$password' WITH GRANT OPTION;FLUSH PRIVILEGES;" echo -e "\033[32m *** cretae /etc/my.cnf \033[0m"
cp /usr/share/doc/MySQL-server-5.5./my-huge.cnf /etc/my.cnf character_set_server=`cat $InputParamFile |grep "character_set_server" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
tmp_table_size=`cat $InputParamFile |grep "tmp_table_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
max_heap_table_size=`cat $InputParamFile |grep "max_heap_table_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
lower_case_table_names=`cat $InputParamFile |grep "lower_case_table_names" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
key_buffer_size=`cat $InputParamFile |grep "key_buffer_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
binlog_ignore_db=`cat $InputParamFile |grep "binlog-ignore-db" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_buffer_pool_size=`cat $InputParamFile |grep "innodb_buffer_pool_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_additional_mem_pool_size=`cat $InputParamFile |grep "innodb_additional_mem_pool_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_log_file_size=`cat $InputParamFile |grep "innodb_log_file_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_log_buffer_size=`cat $InputParamFile |grep "innodb_log_buffer_size" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_flush_log_at_trx_commit=`cat $InputParamFile |grep "innodb_flush_log_at_trx_commit" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_lock_wait_timeout=`cat $InputParamFile |grep "innodb_lock_wait_timeout" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_flush_method=`cat $InputParamFile |grep "innodb_flush_method" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_log_files_in_group=`cat $InputParamFile |grep "innodb_log_files_in_group" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
max_connections=`cat $InputParamFile |grep "max_connections" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
max_connect_errors=`cat $InputParamFile |grep "max_connect_errors" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
innodb_max_dirty_pages_pct=`cat $InputParamFile |grep "innodb_max_dirty_pages_pct" | tail -n | cut -d "=" -f2- | awk '{print $1}'` # 默认都加上
sed -i '/\[client\]/a\default-character-set=utf8' /etc/my.cnf # 先统统干掉
sed -i -e "/character_set_server=/d" /etc/my.cnf
sed -i -e "/tmp_table_size=/d" /etc/my.cnf
sed -i -e "/max_heap_table_size=/d" /etc/my.cnf
sed -i -e "/lower_case_table_names=/d" /etc/my.cnf
sed -i -e "/key_buffer_size=/d" /etc/my.cnf
sed -i -e "/binlog-ignore-db=/d" /etc/my.cnf
sed -i -e "/innodb_buffer_pool_size=/d" /etc/my.cnf
sed -i -e "/innodb_additional_mem_pool_size=/d" /etc/my.cnf
sed -i -e "/innodb_log_file_size=/d" /etc/my.cnf
sed -i -e "/innodb_log_buffer_size=/d" /etc/my.cnf
sed -i -e "/innodb_flush_log_at_trx_commit=/d" /etc/my.cnf
sed -i -e "/innodb_lock_wait_timeout=/d" /etc/my.cnf
sed -i -e "/innodb_flush_method=/d" /etc/my.cnf
sed -i -e "/innodb_log_files_in_group=/d" /etc/my.cnf
sed -i -e "/max_connections=/d" /etc/my.cnf
sed -i -e "/max_connect_errors=/d" /etc/my.cnf
sed -i -e "/innodb_max_dirty_pages_pct=/d" /etc/my.cnf # 再加上所有配置
sed -i "/\[mysqld\]/a\character_set_server=$character_set_server" /etc/my.cnf
sed -i "/\[mysqld\]/a\tmp_table_size=$tmp_table_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\max_heap_table_size=$max_heap_table_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\lower_case_table_names=$lower_case_table_names" /etc/my.cnf
sed -i "/\[mysqld\]/a\key_buffer_size=$key_buffer_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\binlog-ignore-db=$binlog_ignore_db" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_buffer_pool_size=$innodb_buffer_pool_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_additional_mem_pool_size=$innodb_additional_mem_pool_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_log_file_size=$innodb_log_file_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_log_buffer_size=$innodb_log_buffer_size" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_flush_log_at_trx_commit=$innodb_flush_log_at_trx_commit" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_lock_wait_timeout=$innodb_lock_wait_timeout" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_flush_method=$innodb_flush_method" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_log_files_in_group=$innodb_log_files_in_group" /etc/my.cnf
sed -i "/\[mysqld\]/a\max_connections=$max_connections" /etc/my.cnf
sed -i "/\[mysqld\]/a\max_connect_errors=$max_connect_errors" /etc/my.cnf
sed -i "/\[mysqld\]/a\innodb_max_dirty_pages_pct=$innodb_max_dirty_pages_pct" /etc/my.cnf sed -i "/\[myisamchk\]/a\key_buffer_size=$key_buffer_size" /etc/my.cnf # 设置数据库id号
serverid=`cat $InputParamFile |grep "server-id" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
sed -i -e '/server-id/d' /etc/my.cnf
sed -i "/\[mysqld\]/a\server-id=$serverid" /etc/my.cnf # 默认去掉该配置项
sed -i '/log-bin=mysql-bin/d' /etc/my.cnf echo -e "\033[32m *** service mysql restart \033[0m"
rm -rf /var/lib/mysql/ib_logfile*
service mysql restart if [ "$?" != ] ;
then rm -rf /var/lib/mysql/ib_logfile*
service mysql restart
fi echo -e "\033[32m *** chkconfig --list mysql \033[0m"
chkconfig --list mysql
if [ "$?" != ] ;
then
chkconfig --add mysql
fi # 添加自启项
chkconfig --level mysql on # 注册启、停、重启、查看状态命令
rm -rf /bin/start_mysql
rm -rf /bin/stop_mysql
rm -rf /bin/status_mysql
rm -rf /bin/restart_mysql echo "sh `pwd`/start_mysql.sh" >> /bin/start_mysql
chmod /bin/start_mysql echo "sh `pwd`/stop_mysql.sh" >> /bin/stop_mysql
chmod /bin/stop_mysql echo "sh `pwd`/status_mysql.sh" >> /bin/status_mysql
chmod /bin/status_mysql echo "sh `pwd`/restart_mysql.sh" >> /bin/restart_mysql
chmod /bin/restart_mysql # 设置互备数据库
# 根据模式进行MySQL数据库的安装工作
# setupMode= 表示只安装单纯的MySQL单机服务
# setupMode= 表示安装主MySQL服务
# setupMode= 表示安装从MySQL服务
# setupMode= 表示安装读写分离服务
# setupMode= 表示安装主主服务主1
# setupMode= 表示安装主主服务主2
# 主主服务中主1和主2区别在于主1需要先安装,不需要知道主2的信息,主2安装需要配置主1的信息
if [ "$setupMode" -eq "" ] ; then # 先把防火墙关了
/etc/init.d/iptables stop # 设置主用mysql
#sed -i "/\[mysqld\]/a\auto_increment_offset=1" /etc/my.cnf
#sed -i "/\[mysqld\]/a\auto_increment_increment=2" /etc/my.cnf
#sed -i "/\[mysqld\]/a\sync_binlog=1" /etc/my.cnf
#sed -i "/\[mysqld\]/a\log-salve-updates" /etc/my.cnf
# 获取从数据库账号
slaveToMasterUser=`cat $InputParamFile |grep "slaveToMaster-user" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库密码
slaveToMasterPassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
sed -i "/\[mysqld\]/a\log-bin=mysql-bin" /etc/my.cnf # 更改配置文件后需要重启MySQL服务
rm -rf /var/lib/mysql/ib_logfile*
service mysql restart
# 用于访问自身数据账号
echo `mysql -uroot -p$password -e "grant all privileges on *.* to '$slaveToMasterUser'@'%' identified by '$slaveToMasterPassword';FLUSH PRIVILEGES;"`
# 从安装
elif [ "$setupMode" -eq "" ] ; then
# 先把防火墙关了
/etc/init.d/iptables stop # 获取主用数据库IP地址
masterhost=`cat $InputParamFile |grep "master-host" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取主用数据库账号
masteruser=`cat $InputParamFile |grep "master-user" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取主用数据库密码
masterpassword=`cat $InputParamFile |grep "master-password" | tail -n | cut -d "=" -f2- | awk '{print $1}'` # 获取从数据库IP地址
slavehost=`cat $InputParamFile |grep "slaveToMaster-host" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库账号
slaveuser=`cat $InputParamFile |grep "slaveToMaster-user" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库密码
slavepassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n | cut -d "=" -f2- | awk '{print $1}'` echo `mysql -uroot -p$password -e "grant all privileges on *.* to '$slaveuser'@'%' identified by '$slavepassword';FLUSH PRIVILEGES;"` # 登陆主MySQL得到Master Status信息
masterStatus=$(echo `mysql -h$masterhost -u$slaveuser -p$slavepassword -e "show master status\G"`) masterlogfile=$(echo $masterStatus | grep "Position" | tail -n | cut -d ":" -f2- | awk '{print $1}')
masterlogpos=$(echo $masterStatus | grep "Position" | tail -n | cut -d ":" -f2- | awk '{print $3}') # 添加主服务
echo `mysql -uroot -p$password -e "change master to master_host='$masterhost', master_user='$masteruser', master_password='$masterpassword',master_log_file='$masterlogfile', master_log_pos=$masterlogpos;start slave;"` #主主安装(主1)
elif [ "$setupMode" -eq "" ] ; then # 先把防火墙关了
/etc/init.d/iptables stop # 设置主用mysql
#sed -i "/\[mysqld\]/a\auto_increment_offset=1" /etc/my.cnf
#sed -i "/\[mysqld\]/a\auto_increment_increment=2" /etc/my.cnf
#sed -i "/\[mysqld\]/a\sync_binlog=1" /etc/my.cnf
#sed -i "/\[mysqld\]/a\log-salve-updates" /etc/my.cnf
# 获取从数据库账号
slaveToMasterUser=`cat $InputParamFile |grep "slaveToMaster-user" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库密码
slaveToMasterPassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
sed -i "/\[mysqld\]/a\log-bin=mysql-bin" /etc/my.cnf
sed -i "/\[mysqld\]/a\auto_increment_offset = 1" /etc/my.cnf
sed -i "/\[mysqld\]/a\auto_increment_increment = 2" /etc/my.cnf
sed -i "/\[mysqld\]/a\log-slave-updates" /etc/my.cnf # 更改配置文件后需要重启MySQL服务
rm -rf /var/lib/mysql/ib_logfile*
service mysql restart
# 用于访问自身数据账号
echo `mysql -uroot -p$password -e "grant all privileges on *.* to '$slaveToMasterUser'@'%' identified by '$slaveToMasterPassword';FLUSH PRIVILEGES;"`
#主主安装(主2...n)
elif [ "$setupMode" -eq "" ] ; then
# 先把防火墙关了
/etc/init.d/iptables stop sed -i "/\[mysqld\]/a\log-bin=mysql-bin" /etc/my.cnf
sed -i "/\[mysqld\]/a\auto_increment_offset = 2" /etc/my.cnf
sed -i "/\[mysqld\]/a\auto_increment_increment = 2" /etc/my.cnf
sed -i "/\[mysqld\]/a\log-slave-updates" /etc/my.cnf # 更改配置文件后需要重启MySQL服务
rm -rf /var/lib/mysql/ib_logfile*
service mysql restart # 获取主用数据库IP地址
masterhost=`cat $InputParamFile |grep "master-master-host" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取主用数据库账号
masteruser=`cat $InputParamFile |grep "master-master-user" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取主用数据库密码
masterpassword=`cat $InputParamFile |grep "master-master-password" | tail -n | cut -d "=" -f2- | awk '{print $1}'` # 获取从数据库IP地址
slavehost=`cat $InputParamFile |grep "slaveToMaster-host" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库账号
slaveuser=`cat $InputParamFile |grep "slaveToMaster-user" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库密码
slavepassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n | cut -d "=" -f2- | awk '{print $1}'` echo `mysql -uroot -p$password -e "grant all privileges on *.* to '$slaveuser'@'%' identified by '$slavepassword';FLUSH PRIVILEGES;"` # 登陆主MySQL得到Master Status信息
masterStatus=$(echo `mysql -h$masterhost -u$slaveuser -p$slavepassword -e "show master status\G"`) masterlogfile=$(echo $masterStatus | grep "Position" | tail -n | cut -d ":" -f2- | awk '{print $1}')
masterlogpos=$(echo $masterStatus | grep "Position" | tail -n | cut -d ":" -f2- | awk '{print $3}') # 添加主服务
echo `mysql -uroot -p$password -e "change master to master_host='$masterhost', master_user='$masteruser', master_password='$masterpassword',master_log_file='$masterlogfile', master_log_pos=$masterlogpos;start slave;"` # 设置同步数据账号
#echo `mysql -h$masterhost -u$slaveuser -p$slavepassword -e "grant replication slave,replication client on *.* to '$slaveuser'@'$slavehost' identified by '$slavepassword';FLUSH PRIVILEGES;"` # 设置对端主主关联
# 登陆主MySQL得到Master Status信息
slaveStatus=$(echo `mysql -u$slaveuser -p$slavepassword -e "show master status\G"`) slavelogfile=$(echo $slaveStatus | grep "Position" | tail -n | cut -d ":" -f2- | awk '{print $1}')
slavelogpos=$(echo $slaveStatus | grep "Position" | tail -n | cut -d ":" -f2- | awk '{print $3}') echo `mysql -h$masterhost -u$slaveuser -p$slavepassword -e "change master to master_host='$slavehost', master_user='$slaveuser', master_password='$slavepassword',master_log_file='$slavelogfile', master_log_pos=$slavelogpos;start slave;"` ######################## else
echo "No setupMode";
fi
fi
} installheartbeat()
{ echo
echo -e "\033[32m *** step 1: check whether heartbeat is already installed? \033[0m"
echo # 检查当前系统是否已安装heartbeat,如果已经安装就卸载
for i in `rpm -qa|grep heartbeat`
do
rpm -q $i &> /dev/null
if [ $? == ]; then
# 删除heartbeat
rpm -e $i --nodeps &> /dev/null
echo $i "was uninstalled"
fi
done # 删除配置文件
rm -rf /usr/lib/heartbeat
rm -rf /etc/ha.d cd $install_home # 解压安装包
tar -xf packages.tar.gz; # 进入到安装包目录
cd packages # 创建用户
groupadd haclient
useradd hacluster -g haclient
useradd haclient -g haclient # 忽略应用进行安装
rpm -ivh *.rpm --force --nodeps # 安装成功,继续配置
if [ $? == ]; then rm -rf /usr/lib/heartbeat
mkdir -p /usr/lib/heartbeat/ \cp -f /usr/lib64/heartbeat/ipfail /usr/lib/heartbeat/ \cp -f /usr/share/doc/heartbeat-3.0./ha.cf /etc/ha.d/
\cp -f /usr/share/doc/heartbeat-3.0./haresources /etc/ha.d/
\cp -f /usr/share/doc/heartbeat-3.0./authkeys /etc/ha.d/ # 配置鉴权模式
sed -i "/\#auth 1/a\auth 3" /etc/ha.d/authkeys
sed -i "/\#3 md5 Hello\!/a\3 md5 Hello\!" /etc/ha.d/authkeys
sed -i -e '/#auth 1/d' /etc/ha.d/authkeys
sed -i -e '/#3 md5 Hello\!/d' /etc/ha.d/authkeys # 修改文件权限
chmod /etc/ha.d/authkeys # 清空文件内容
echo > /etc/ha.d/ha.cf # 获取主IP和用户名
masterIP=`cat $InputParamFile |grep "masterIP" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
masterName=`cat $InputParamFile |grep "masterName" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取备主IP和用户名
backupIP=`cat $InputParamFile |grep "backupIP" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
backupName=`cat $InputParamFile |grep "backupName" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取仲裁ip地址
pingIP=`cat $InputParamFile |grep "pingIP" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 网卡名称
ethName=`cat $InputParamFile |grep "ethName" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 浮动IP
flotIP=`cat $InputParamFile |grep "flotIP" | tail -n | cut -d "=" -f2- | awk '{print $1}'` # 配置用户,先删除再添加
sed -i -e "/$masterName/d" /etc/hosts
sed -i -e "/$backupName/d" /etc/hosts echo "$masterIP $masterName" >> /etc/hosts
echo "$backupIP $backupName" >> /etc/hosts echo "#debugfile /var/log/ha-debug.log" >> /etc/ha.d/ha.cf
echo "logfile /var/log/ha.log" >> /etc/ha.d/ha.cf
echo "#logfacility local0" >> /etc/ha.d/ha.cf
echo "keepalive 2" >> /etc/ha.d/ha.cf
echo "deadtime 10" >> /etc/ha.d/ha.cf
echo "warntime 5" >> /etc/ha.d/ha.cf
echo "initdead 60" >> /etc/ha.d/ha.cf
echo "udpport 694" >> /etc/ha.d/ha.cf
echo "bcast $ethName" >> /etc/ha.d/ha.cf
echo "#mcast eth1 225.0.0.1 694 1 0" >> /etc/ha.d/ha.cf
echo "#ucast eth1 10.0.0.2 " >> /etc/ha.d/ha.cf
echo "auto_failback off" >> /etc/ha.d/ha.cf
echo "#watchdog /dev/watchdog" >> /etc/ha.d/ha.cf
echo "node $masterName" >> /etc/ha.d/ha.cf
echo "node $backupName" >> /etc/ha.d/ha.cf
echo "ping $pingIP" >> /etc/ha.d/ha.cf
echo "#ping_group group1 10.10.99.254 10.10.99.253" >> /etc/ha.d/ha.cf
echo "respawn hacluster /usr/lib/heartbeat/ipfail" >> /etc/ha.d/ha.cf
echo "apiauth ipfail gid=haclient uid=hacluster" >> /etc/ha.d/ha.cf # 添加Mysql配置
echo "$masterName IPaddr::$flotIP/24/$ethName mysql_umount" >> /etc/ha.d/haresources rm -rf /etc/ha.d/resource.d/mysql_umount
rm -rf /etc/ha.d/resource.d/resetslave.properties
rm -rf /etc/ha.d/resource.d/resetslave.sh cp $install_home/resetslave.properties /etc/ha.d/resource.d/
cp $install_home/resetslave.sh /etc/ha.d/resource.d/ masterhost=`cat $InputParamFile |grep "master-host"`
if [ -n "$masterhost" ]; then
# 获取主用数据库IP地址 # 获取主用数据库账号
masteruser=`cat $InputParamFile |grep "master-user"`
# 获取主用数据库密码
masterpassword=`cat $InputParamFile |grep "master-password"` echo "" > /etc/ha.d/resource.d/resetslave.properties
echo "$masterhost" >> /etc/ha.d/resource.d/resetslave.properties
echo "$masteruser" >> /etc/ha.d/resource.d/resetslave.properties
echo "$masterpassword" >> /etc/ha.d/resource.d/resetslave.properties
# 循环遍历所有从服务
i=
while [ -eq ]
do
# 获取从数据库IP地址
slavehost=`cat $InputParamFile |grep "slave-host$i" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库账号
slaveuser=`cat $InputParamFile |grep "slave-user$i" | tail -n | cut -d "=" -f2- | awk '{print $1}'`
# 获取从数据库密码
slavepassword=`cat $InputParamFile |grep "slave-password$i" | tail -n | cut -d "=" -f2- | awk '{print $1}'` # 如果找不到编号了就结束
if [ -n "$slavehost" ];
then echo "$slavehost" >> /etc/ha.d/resource.d/resetslave.properties
echo "$slaveuser" >> /etc/ha.d/resource.d/resetslave.properties
echo "$slavepassword" >> /etc/ha.d/resource.d/resetslave.properties else
break
fi
i=$(($i+))
done
fi echo "#!/bin/sh" >> /etc/ha.d/resource.d/mysql_umount
echo ". /root/.bashrc" >> /etc/ha.d/resource.d/mysql_umount
echo ". /etc/profile" >> /etc/ha.d/resource.d/mysql_umount
echo "unset LC_ALL; export LC_ALL" >> /etc/ha.d/resource.d/mysql_umount
echo "unset LANGUAGE; export LANGUAGE" >> /etc/ha.d/resource.d/mysql_umount
echo "prefix=/usr" >> /etc/ha.d/resource.d/mysql_umount
echo "exec_prefix=/usr" >> /etc/ha.d/resource.d/mysql_umount
echo "sh /etc/ha.d/shellfuncs" >> /etc/ha.d/resource.d/mysql_umount
echo 'case $1 in' >> /etc/ha.d/resource.d/mysql_umount
echo "'start')" >> /etc/ha.d/resource.d/mysql_umount
echo "sh resetslave.sh start" >> /etc/ha.d/resource.d/mysql_umount
echo "mysql-proxy --defaults-file=/usr/local/mysql-proxy/mysql-proxy.cnf>/usr/local/mysql-proxy/cn.log &" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "'pre-start')" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "'post-start')" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "'stop')" >> /etc/ha.d/resource.d/mysql_umount
echo "killall -9 mysql-proxy" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "'pre-stop')" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "'post-stop')" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "*)" >> /etc/ha.d/resource.d/mysql_umount
echo " echo \"Usage: $0 { start | pre-start | post-start | stop | pre-stop | post-stop }\"" >> /etc/ha.d/resource.d/mysql_umount
echo " ;;" >> /etc/ha.d/resource.d/mysql_umount
echo "esac" >> /etc/ha.d/resource.d/mysql_umount
echo "exit 0" >> /etc/ha.d/resource.d/mysql_umount chmod /etc/ha.d/resource.d/mysql_umount
chmod /etc/ha.d/resource.d/resetslave.sh ln -s /etc/init.d/mysql /etc/ha.d/resource.d/mysql service heartbeat start chkconfig --add heartbeat
chkconfig --level heartbeat on
fi
} paramNum=$#
if [ "$paramNum" != "" ];then
echo "-n \"no start\""
echo "-f \"Install from file ,need filePath after this param\""
echo "ip \"ip addr\""
exit ;
fi; IsInputParam=$
if [ "${IsInputParam}d" != "d" -a "$IsInputParam" != "-f" ]; then
echo "Second parameter must be -f"
exit ;
fi;
InputParamFile=$
if [ ! -e $InputParamFile ]; then
echo "The param file doesn't existed."
exit ;
fi; install; echo "install end"
exit ;

配置文件

######################################################################
# 该配置文件为安装单机MySQL数据库
# Create by
# 请仔细阅读每项配置,避免安装出错!!
######################################################################
# 指定MySQL数据库的安装类型
# 表示只安装单纯的MySQL单机服务
# 表示安装主MySQL服务
# 表示安装从MySQL服务
# 表示安装读写分离服务
# 表示安装主主服务主1
# 表示安装主主服务主2
# 主主服务中主1和主2区别在于主1需要先安装,不需要知道主2的信息,主2安装需要配置主1的信息
setup-mode=
# 指定Mysql安装后的root数据库用户的登陆密码(这里安装的数据用户名默认为root)
password=root
# 指定MySQL的serverid,一般单机或者主MySQL默认server-id=1即可,如果是从MySQL需要改成其他值
server-id=
# =======MySQL的my.cnf配置,请根据服务器硬件配置决定=========
# 数据库默认编码
character_set_server=utf8 tmp_table_size=64M
max_heap_table_size=64M
# 让MySQL不区分大小写
lower_case_table_names=
# 建议:使用5..x以上版本
# 修改/etc/my.cnf(主备服务器都修改),在mysqld节点下增加如下配置:
# MYISAM--如果使用混合引擎我们需要对key_buffer_size=/ 内存
key_buffer_size=2G
###关闭global数据库的binlog日志,减少io需求###
binlog-ignore-db = global
# 优化innodb配置。增大innodb数据内存缓冲区,减少io请求;内存的60%
innodb_buffer_pool_size = 8G
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 512M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit =
innodb_lock_wait_timeout =
# 以下为需要新添加配置项##
innodb_flush_method = O_DIRECT
innodb_log_files_in_group =
# 在实际商用环境用不到1000个连接,连接太多了对系统性能&CPU消耗对会有影响!
max_connections=
max_connect_errors=
innodb_max_dirty_pages_pct=
# 在商用环境,建议使用AB模式!