1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| #!/bin/bash # Author: Ropon # Blog: https://www.ropon.top
#借助ansible、expect downurl="http://panel.ropon.top/panel/lnmp/" mysqlname="mysql-5.6.43-1.el7.x86_64.rpm" #mysqlname="mysql-5.7.25-1.el7.x86_64.rpm" ip="172.16.7.124" #多个从服务器 declare -A CserverLst #CserverLst=([s1]="172.16.7.125") CserverLst=([s1]="172.16.7.125" [s2]="172.16.7.126") cport="22" cpasswd="ropon.top" ansible_host="/etc/ansible/hosts" mysqlpasswd="West.cn2020" user="westdemo" passwd="west.cn" host="172.16.7.%" tmpsshfile="/tmp/ssh.exp" mysqlmasterfile="/tmp/master.info" cmysqllibfile="/tmp/cmysqllib.sh" cmysqlslavefile="/tmp/slave.sh"
MySql() { yum install -y ncurses ncurses-devel libaio numactl numactl-libs perl-Module-Install if [ $? -eq 0 ]; then wget ${downurl}${mysqlname} rpm -ivh $mysqlname sleep 1 . /etc/profile mysql -uroot -p$mysqlpasswd -e "grant replication slave on *.* to '${user}'@'${host}' identified by '${passwd}';" mysql -uroot -p$mysqlpasswd -e "flush privileges;" mysql -uroot -p$mysqlpasswd -e "show master status;" > $mysqlmasterfile CFirwall fi }
Ansible() { if [[ -f /etc/yum.repos.d/epel*.repo ]]; then mv /etc/yum.repos.d/epel*.repo{,_bak} fi wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum install -y ansible }
Ssh() { yum install -y expect echo '#!/usr/bin/expect spawn ssh-keygen expect { "*.ssh/id_rsa*" {exp_send "\r";exp_continue} "*passphrase*" {exp_send "\r";exp_continue} "*again*" {exp_send "\r"} }' > $tmpsshfile expect $tmpsshfile sleep 1 echo "[db]" >> $ansible_host
for key in ${!CserverLst[*]}; do cat > $tmpsshfile << EOF #!/usr/bin/expect spawn ssh-copy-id ${CserverLst[$key]} -p ${cport} expect { "*yes/no*" {exp_send "yes\r";exp_continue} "*password*" {exp_send "${cpasswd}\r";exp_continue} } EOF expect $tmpsshfile echo "${CserverLst[$key]} ansible_ssh_port=${cport}" >> $ansible_host done ansible db -m ping }
CMySql() { cat > $cmysqllibfile << EOF #!/bin/bash yum install -y ncurses ncurses-devel libaio numactl numactl-libs perl-Module-Install if [ $? -eq 0 ]; then wget $downurl${mysqlname} rpm -ivh $mysqlname sleep 1 sed -i "s@server-id.*@server-id = 2@g" /etc/my.cnf sed -i "s@log_bin.*@#log_bin = mysql-bin@g" /etc/my.cnf service mysqld restart fi EOF ansible db -m script -a "${cmysqllibfile}" sleep 1 mysql_bin_name=$(cat $mysqlmasterfile awk '{if (NR>1){print $1}}') mysql_bin_offset=$(cat $mysqlmasterfile awk '{if (NR>1){print $2}}') cat > $cmysqlslavefile << EOF #!/bin/bash /usr/local/mysql/bin/mysql -uroot -p${mysqlpasswd} -e "change master to master_host='${ip}',master_user='${user}',master_port=3306,master_password='${passwd}',master_log_file='${mysql_bin_name}',master_log_pos=${mysql_bin_offset};" /usr/local/mysql/bin/mysql -uroot -p${mysqlpasswd} -e "start slave;" /usr/local/mysql/bin/mysql -uroot -p${mysqlpasswd} -e "show slave status\G;" service mysqld restart EOF ansible db -m script -a "${cmysqlslavefile}" }
CFirwall() { iptables -L -n grep -w dpt:$cport >/dev/null if [ $? -eq 0 ] ;then iptables -L -n grep -w dpt:3306 >/dev/null if [ $? -ne 0 ] ;then sed -i "/dport ${cport} -j ACCEPT/a\-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT" /etc/sysconfig/iptables service iptables restart
fi fi }
Clean() { rm -rf $tmpsshfile rm -rf $mysqlmasterfile rm -rf $cmysqllibfile rm -rf $cmysqlslavefile }
Main() { MySql Ansible Ssh CMySql Clean }
Main
|