1. 程式人生 > >1-mysql主主安裝腳本

1-mysql主主安裝腳本

then sys and -a 要求 node節點 all ice service

因為公司某個項目需要用到mysql主主結構,因此開發了一鍵安裝mysql的腳本,僅供參考:

有幾個前提:

1、我這裏是yum源安裝的mysql

2、需要提前配置ansible控制mysql兩個節點

3、要手動改一下腳本裏面的node1和node2的IP,endnode1跟endnode2根據自己要求。

4、腳本需要同時在兩Node節點上運行

執行方法(參考):

ansible mysql -m script -a "/root/ansible-shell/mysql.sh"

#!/bin/bash
set -e
set -x

#定義全局變量
node1=10.134.224.159
node2
=10.134.224.161 endnode1=159 endnode2=161 slaveuser="slave" slavepass="Slave@123" em=`route -n|grep "UG"|awk {print $NF}|uniq` node=`ifconfig $em|grep netmask|awk {print $2}` #刪掉依賴 #yum remove -y mariadb-libs* #[ $? -eq 0 ] && echo "del ok" || echo "del error" #安裝包 yum -y install mysql-community-client.x86_64 mysql-community-devel.x86_64 mysql-community-server.x86_64 mysql-community-common.x86_64 > /dev/null
2>&1 #[ $? -eq 0 ] && echo "install ok" || echo "install fail" #調配置及初始化 groups mysql [ $? -eq 0 ] && echo "group mysql exsit" || groupadd mysql id mysql [ $? -eq 0 ] && echo "mysql already exsit" || useradd mysql -g mysql -s /sbin/nologin -M [ -d /data/mysql ] || mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql sed -i s/^datadir=.*/datadir=\/data\/mysql/g /etc/my.cnf systemctl start mysqld.service #查找原始密碼 PASS=$(grep "temporary password" /var/log/mysqld.log |tail -n 1|awk {print $NF}) [ $? -eq 0 ] && echo "log pass get ok" || echo "log pass fail" #修改root密碼 mysql -uroot -p$PASS --connect-expired-password -e "alter user ‘root‘@‘localhost‘ identified by ‘Foxconn@123‘;flush privileges;" [ $? -eq 0 ] && echo "change root pass ok" || echo "change root pass fail!!!" mysql -uroot -pFoxconn@123 --connect-expired-password -e "flush privileges;" #調主主配置 systemctl stop mysqld systemctl start mysqld systemctl enable mysqld #配置my.cnf if [ $node == $node1 ]; then mv /etc/my.cnf /etc/my.cnf.bak cat >/etc/my.cnf<<EOF [client] port = 3306 default-character-set = utf8 socket = /var/lib/mysql/mysql.sock user = root password = "Foxconn@123" [mysqld] #normal port = 3306 datadir = /data/mysql socket = /var/lib/mysql/mysql.sock tmpdir = /tmp pid-file = /data/mysql/mysqld.pid log_error = /var/log/mysqld_error.log expire_logs_days = 7 #relay server_id = $endnode1 log-bin = $endnode1-binlog relay_log = $endnode1-relay-bin auto_increment_increment = 2 auto_increment_offset = 1 #binlog and cache binlog-ignore-db = mysql binlog_format = ROW binlog_cache_size = 128m max_binlog_cache_size = 512m table_open_cache = 8000 #skip-grant-tables #skip-networking explicit_defaults_for_timestamp = 1 #connect connect_timeout = 20 wait_timeout = 86400 max_connections = 2000 max_user_connections = 1900 max_connect_errors = 100000 max_allowed_packet = 1G #utf8 character-set-server = utf8 collation-server = utf8_bin #innodb innodb_file_per_table = 1 innodb_log_file_size = 1024m innodb_log_buffer_size = 256m innodb_file_format = Barracuda skip-name-resolve EOF systemctl stop mysqld.service systemctl start mysqld.service #創建slave用戶 mysql -e "grant replication slave,replication client on *.* to "slave"@"$node2" identified by ‘Slave@123‘;" mysql -e "flush privileges;" [ $? -eq 0 ] && echo "slave create ok" && echo "slave create fail" #這裏設置sleep 30是為了讓node1,node2配置了slave用戶 sleep 30 binfile=$(echo $(mysql -h $node2 -uslave -pSlave@123 --connect-expired-password -e "show master status;" 2>/dev/null)|grep binlog|awk {print$(NF-2)}) pos=$(echo $(mysql -uroot -pFoxconn@123 --connect-expired-password -e "show master status;" 2>/dev/null)|grep binlog|awk {print$(NF-1)}) #通過node2的slave賬號去查binlogfile和pos sql=$(echo "change master to master_host=‘$node2‘,master_user=‘slave‘,master_password=‘Slave@123‘,master_port=3306,master_log_file=‘$binfile‘,master_log_pos=$pos;") mysql -uroot -pFoxconn@123 --connect-expired-password -e "$sql" #mysql -uroot -pFoxconn@123 --connect-expired-password -e "change master to master_host="$node2",master_user=‘slave‘,master_password=‘Slave@123‘,master_port=3306,master_log_file="$binfile",master_log_pos="$pos";" [ $? -eq 0 ] && echo "change master ok" || echo "change master fail" #啟動slave mysql -e "start slave;" else mv /etc/my.cnf /etc/my.cnf.bak cat >/etc/my.cnf<<EOF [client] port = 3306 default-character-set = utf8 socket = /var/lib/mysql/mysql.sock user = root password = "Foxconn@123" [mysqld] #normal port = 3306 datadir = /data/mysql socket = /var/lib/mysql/mysql.sock tmpdir = /tmp pid-file = /data/mysql/mysqld.pid log_error = /var/log/mysqld_error.log expire_logs_days = 7 #relay server_id = $endnode2 log-bin = $endnode2-binlog relay_log = $endnode2-relay-bin auto_increment_increment = 2 auto_increment_offset = 2 #binlog and cache binlog-ignore-db = mysql binlog_format = ROW binlog_cache_size = 128m max_binlog_cache_size = 512m table_open_cache = 8000 #skip-grant-tables #skip-networking explicit_defaults_for_timestamp = 1 #connect connect_timeout = 20 wait_timeout = 86400 max_connections = 2000 max_user_connections = 1900 max_connect_errors = 100000 max_allowed_packet = 1G #utf8 character-set-server = utf8 collation-server = utf8_bin #innodb innodb_file_per_table = 1 innodb_log_file_size = 1024m innodb_log_buffer_size = 256m innodb_file_format = Barracuda skip-name-resolve EOF systemctl stop mysqld.service systemctl start mysqld.service #創建slave用戶 mysql -e "grant replication slave,replication client on *.* to ‘slave‘@"$node1" identified by ‘Slave@123‘;" mysql -e "flush privileges;" [ $? -eq 0 ] && echo "slave create ok" && echo "slave create fail" #這裏設置sleep 30是為了讓node1,node2配置了slave用戶 sleep 20 #通過node1的slave賬號去查binlogfile和pos binfile=$(echo $(mysql -h $node1 -uslave -pSlave@123 --connect-expired-password -e "show master status;" 2>/dev/null)|grep binlog|awk {print$(NF-2)}) pos=$(echo $(mysql -h $node1 -uslave -pSlave@123 --connect-expired-password -e "show master status;" 2>/dev/null)|grep binlog|awk {print$(NF-1)}) #change master to sql=$(echo "change master to master_host=‘$node1‘,master_user=‘slave‘,master_password=‘Slave@123‘,master_port=3306,master_log_file=‘$binfile‘,master_log_pos=$pos;") #mysql -e "change master to master_host=$node1,master_user=‘slave‘,master_password=‘Slave@123‘,master_port=3306,master_log_file="$binfile",master_log_pos="$pos";" mysql -e "$sql" [ $? -eq 0 ] && echo "change master ok" || echo "change master fail" #啟動 mysql -e "start slave;" fi

不足之處:1-因為時間關系,keepalive的腳本暫時還沒加進去。

2-因為各個服務器環境不一樣,因此腳本在刪掉依賴的時候,可能還得手動刪掉一些包

3-slave的賬號權限放得稍為大了一點,主要是為了通信方便,實際上可以做一下控制。

1-mysql主主安裝腳本