shellnode
#!/bin/bash # Create by 909511 # version 1.0 # 2014/08/27 # # check out lockfile whether or not exist IsInputParam="" InputParamFile="" install_home=`pwd` mysql_proxy_home=/usr/local/mysql-proxy install() { # 確保腳本不被重複執行 lockfile=/tmp/$(basename $0)_lockfile if [ -f $lockfile ];then pid=$(cat $lockfile) ps -p $pid | grep $pid &> /dev/null if [ $? == 0 ]; then echo -e "\033[32m the script is already running !!! \033[0m" && exit 16 else echo $$ > $lockfile fi else echo $$ > $lockfile fi setupMode=`cat $InputParamFile |grep "setup-mode" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 安裝lua+MySQL proxy,支持任意局主機安裝 if [ "$setupMode" -eq "3" ] ; then heartbeatSetup=`cat $InputParamFile |grep "heartbeatSetup" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` if [ "$heartbeatSetup" -eq "1" ] ; then installheartbeat; fi #echo -e "\033[32m *** install lua \033[0m" #lua_home=$install_home/lua-5.2.3 #cd $install_home # 安裝lua,安裝前先卸載再安裝; #tar -zxvf $install_home/lua-5.2.3.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.1 luac.1 #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.5-linux-el6-x86-64bit.tar.gz -C /usr/local cd /usr/local # 一點點清理工做 killall -9 mysql-proxy rm -rf /usr/local/mysql-proxy mv mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy # 設置MySQL Proxy,根據安裝參數進行配置 max_open_files=`cat $InputParamFile |grep "max-open-files" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` event_threads=`cat $InputParamFile |grep "event-threads" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` log_level=`cat $InputParamFile |grep "log-level" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` proxy_address=`cat $InputParamFile |grep "proxy-address" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` proxy_backend_addresses=`cat $InputParamFile |grep "proxy-backend-addresses" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` proxy_read_only_backend_addresses=`cat $InputParamFile |grep "proxy-read-only-backend-addresses" | tail -n 1 | 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:4041。 #admin-address=172.20.100.36:4401 # 數據庫用戶名(主從上都需創建相同用戶) #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 0660 $mysql_proxy_home/mysql-proxy.cnf echo "export PATH=$PATH:/usr/local/mysql-proxy/bin/" >> /etc/profile source /etc/profile sleep 30 # 啓動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 [ $? == 0 ]; 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.38-1.el6.x86_64.rpm if [ "$?" == 0 ] ; then echo -e "\033[32m *** MySQL-server install success! \033[0m" else echo -e "\033[32m *** MySQL-server is not installed! \033[0m" exit 1 fi echo echo -e "\033[32m *** step 3: install packages MySQL-client \033[0m" echo # 開始安裝MySQL客戶端 rpm -ivh MySQL-client-5.5.38-1.el6.x86_64.rpm if [ "$?" == 0 ] ; then echo -e "\033[32m *** MySQL-client install success! \033[0m" else echo -e "\033[32m *** MySQL-client is not installed! \033[0m" exit 1 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 1 | 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.38/my-huge.cnf /etc/my.cnf character_set_server=`cat $InputParamFile |grep "character_set_server" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` tmp_table_size=`cat $InputParamFile |grep "tmp_table_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` max_heap_table_size=`cat $InputParamFile |grep "max_heap_table_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` lower_case_table_names=`cat $InputParamFile |grep "lower_case_table_names" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` key_buffer_size=`cat $InputParamFile |grep "key_buffer_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` binlog_ignore_db=`cat $InputParamFile |grep "binlog-ignore-db" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_buffer_pool_size=`cat $InputParamFile |grep "innodb_buffer_pool_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_additional_mem_pool_size=`cat $InputParamFile |grep "innodb_additional_mem_pool_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_log_file_size=`cat $InputParamFile |grep "innodb_log_file_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_log_buffer_size=`cat $InputParamFile |grep "innodb_log_buffer_size" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_flush_log_at_trx_commit=`cat $InputParamFile |grep "innodb_flush_log_at_trx_commit" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_lock_wait_timeout=`cat $InputParamFile |grep "innodb_lock_wait_timeout" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_flush_method=`cat $InputParamFile |grep "innodb_flush_method" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_log_files_in_group=`cat $InputParamFile |grep "innodb_log_files_in_group" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` max_connections=`cat $InputParamFile |grep "max_connections" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` max_connect_errors=`cat $InputParamFile |grep "max_connect_errors" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` innodb_max_dirty_pages_pct=`cat $InputParamFile |grep "innodb_max_dirty_pages_pct" | tail -n 1 | 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 1 | 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 [ "$?" != 0 ] ; 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 [ "$?" != 0 ] ; then chkconfig --add mysql fi # 添加自啓項 chkconfig --level 2 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 755 /bin/start_mysql echo "sh `pwd`/stop_mysql.sh" >> /bin/stop_mysql chmod 755 /bin/stop_mysql echo "sh `pwd`/status_mysql.sh" >> /bin/status_mysql chmod 755 /bin/status_mysql echo "sh `pwd`/restart_mysql.sh" >> /bin/restart_mysql chmod 755 /bin/restart_mysql # 設置互備數據庫 # 根據模式進行MySQL數據庫的安裝工做 # setupMode=0 表示只安裝單純的MySQL單機服務 # setupMode=1 表示安裝主MySQL服務 # setupMode=2 表示安裝從MySQL服務 # setupMode=3 表示安裝讀寫分離服務 # setupMode=4 表示安裝主主服務主1 # setupMode=5 表示安裝主主服務主2 # 主主服務中主1和主2區別在於主1須要先安裝,不須要知道主2的信息,主2安裝須要配置主1的信息 if [ "$setupMode" -eq "1" ] ; 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 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫密碼 slaveToMasterPassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n 1 | 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 "2" ] ; then # 先把防火牆關了 /etc/init.d/iptables stop # 獲取主用數據庫IP地址 masterhost=`cat $InputParamFile |grep "master-host" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取主用數據庫帳號 masteruser=`cat $InputParamFile |grep "master-user" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取主用數據庫密碼 masterpassword=`cat $InputParamFile |grep "master-password" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫IP地址 slavehost=`cat $InputParamFile |grep "slaveToMaster-host" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫帳號 slaveuser=`cat $InputParamFile |grep "slaveToMaster-user" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫密碼 slavepassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n 1 | 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 1 | cut -d ":" -f2- | awk '{print $1}') masterlogpos=$(echo $masterStatus | grep "Position" | tail -n 1 | 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 "4" ] ; 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 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫密碼 slaveToMasterPassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n 1 | 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 "5" ] ; 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 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取主用數據庫帳號 masteruser=`cat $InputParamFile |grep "master-master-user" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取主用數據庫密碼 masterpassword=`cat $InputParamFile |grep "master-master-password" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫IP地址 slavehost=`cat $InputParamFile |grep "slaveToMaster-host" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫帳號 slaveuser=`cat $InputParamFile |grep "slaveToMaster-user" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫密碼 slavepassword=`cat $InputParamFile |grep "slaveToMaster-password" | tail -n 1 | 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 1 | cut -d ":" -f2- | awk '{print $1}') masterlogpos=$(echo $masterStatus | grep "Position" | tail -n 1 | 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 1 | cut -d ":" -f2- | awk '{print $1}') slavelogpos=$(echo $slaveStatus | grep "Position" | tail -n 1 | 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 [ $? == 0 ]; 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 [ $? == 0 ]; 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.4/ha.cf /etc/ha.d/ \cp -f /usr/share/doc/heartbeat-3.0.4/haresources /etc/ha.d/ \cp -f /usr/share/doc/heartbeat-3.0.4/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 600 /etc/ha.d/authkeys # 清空文件內容 echo > /etc/ha.d/ha.cf # 獲取主IP和用戶名 masterIP=`cat $InputParamFile |grep "masterIP" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` masterName=`cat $InputParamFile |grep "masterName" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取備主IP和用戶名 backupIP=`cat $InputParamFile |grep "backupIP" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` backupName=`cat $InputParamFile |grep "backupName" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取仲裁ip地址 pingIP=`cat $InputParamFile |grep "pingIP" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 網卡名稱 ethName=`cat $InputParamFile |grep "ethName" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 浮動IP flotIP=`cat $InputParamFile |grep "flotIP" | tail -n 1 | 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=1 while [ 1 -eq 1 ] do # 獲取從數據庫IP地址 slavehost=`cat $InputParamFile |grep "slave-host$i" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫帳號 slaveuser=`cat $InputParamFile |grep "slave-user$i" | tail -n 1 | cut -d "=" -f2- | awk '{print $1}'` # 獲取從數據庫密碼 slavepassword=`cat $InputParamFile |grep "slave-password$i" | tail -n 1 | 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+1)) 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 775 /etc/ha.d/resource.d/mysql_umount chmod 775 /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 35 heartbeat on fi } paramNum=$# if [ "$paramNum" != "4" ];then echo "-n \"no start\"" echo "-f \"Install from file ,need filePath after this param\"" echo "ip \"ip addr\"" exit 1; fi; IsInputParam=$2 if [ "${IsInputParam}d" != "d" -a "$IsInputParam" != "-f" ]; then echo "Second parameter must be -f" exit 1; fi; InputParamFile=$3 if [ ! -e $InputParamFile ]; then echo "The param file doesn't existed." exit 1; fi; install; echo "install end" exit 0;
配置文件mysql
###################################################################### # 該配置文件爲安裝單機MySQL數據庫 # Create by 909511 # 請仔細閱讀每項配置,避免安裝出錯!! ###################################################################### # 指定MySQL數據庫的安裝類型 # 0 表示只安裝單純的MySQL單機服務 # 1 表示安裝主MySQL服務 # 2 表示安裝從MySQL服務 # 3 表示安裝讀寫分離服務 # 4 表示安裝主主服務主1 # 5 表示安裝主主服務主2 # 主主服務中主1和主2區別在於主1須要先安裝,不須要知道主2的信息,主2安裝須要配置主1的信息 setup-mode=0 # 指定Mysql安裝後的root數據庫用戶的登錄密碼(這裏安裝的數據用戶名默認爲root) password=root # 指定MySQL的serverid,通常單機或者主MySQL默認server-id=1便可,若是是從MySQL須要改爲其餘值 server-id=1 # =======MySQL的my.cnf配置,請根據服務器硬件配置決定========= # 數據庫默認編碼 character_set_server=utf8 tmp_table_size=64M max_heap_table_size=64M # 讓MySQL不區分大小寫 lower_case_table_names=1 # 建議:使用5.5.x以上版本 # 修改/etc/my.cnf(主備服務器都修改),在mysqld節點下增長以下配置: # MYISAM--若是使用混合引擎咱們須要對key_buffer_size=1/8 內存 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 = 2 innodb_lock_wait_timeout = 50 # 如下爲須要新添加配置項## innodb_flush_method = O_DIRECT innodb_log_files_in_group = 3 # 在實際商用環境用不到1000個鏈接,鏈接太多了對系統性能&CPU消耗對會有影響! max_connections=1000 max_connect_errors=10000000 innodb_max_dirty_pages_pct=90 # 在商用環境,建議使用AB模式!