1-mysql主主安裝腳本

 

由於公司某個項目須要用到mysql主主結構,所以開發了一鍵安裝mysql的腳本,僅供參考:node

有幾個前提:mysql

一、我這裏是yum源安裝的mysqlsql

二、須要提早配置ansible控制mysql兩個節點shell

三、要手動改一下腳本里面的node1和node2的IP,endnode1跟endnode2根據本身要求。bash

四、腳本須要同時在兩Node節點上運行服務器

執行方法(參考):socket

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

 

#!/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的腳本暫時還沒加進去。spa

                  2-由於各個服務器環境不同,所以腳本在刪掉依賴的時候,可能還得手動刪掉一些包code

                  3-slave的帳號權限放得稍爲大了一點,主要是爲了通訊方便,實際上能夠作一下控制。

相關文章
相關標籤/搜索