制作Mysql5.7&Keepalived&ssh的Docker镜像

制作Mysql5.7&Keepalived&ssh的Docker镜像

注:该实验采用的docker版本:Docker version 17.09.0-ce

  1. 下载相关dockerfile
    git clone https://github.com/mysql/mysql-docker.git

  2. Mysql5.7的Dockerfile文件

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
FROM oraclelinux:7-slim

ARG MYSQL_SERVER_PACKAGE=mysql-community-server-minimal-5.7.27
ARG MYSQL_SHELL_PACKAGE=mysql-shell-8.0.17

# Install server
RUN yum install -y https://repo.mysql.com/mysql-community-minimal-release-el7.rpm \
      https://repo.mysql.com/mysql-community-release-el7.rpm \
  && yum-config-manager --enable mysql57-server-minimal \
  && yum install -y \
      $MYSQL_SERVER_PACKAGE \
      $MYSQL_SHELL_PACKAGE \
      libpwquality \
      keepalived \
      net-tools \
      ipsvadm \
      iproute \
      openssh-server \
      openssh-clients \
      passwd \
  && yum clean all \
  && mkdir /docker-entrypoint-initdb.d 

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /healthcheck.sh
COPY mysqld.service /usr/lib/systemd/system/mysqld.service
COPY etc/ /
RUN systemctl enable mysqld
ENTRYPOINT ["/entrypoint.sh"]
HEALTHCHECK CMD /healthcheck.sh
EXPOSE 3306 33060
CMD ["mysqld"]
  1. 修改docker-entrypoint.sh文件以支持systemctl
    在文件结尾处添加"exec /usr/sbin/init"
password=healthcheckpass
EOF
  touch /mysql-init-complete
  chown -R mysql:mysql "$DATADIR"
  echo "[Entrypoint] Starting MySQL 5.7.27-1.1.12"
fi

exec /usr/sbin/init
  1. 执行编译
nohup docker build -t oracle/mysql:5.7 . &

编译成功结尾输出如下:
Removing intermediate container 3df6f2c00af1
Successfully built 19b9f95db734
Successfully tagged oracle/mysql:5.7
  1. 相关文件
├── docker-entrypoint.sh
├── Dockerfile
├── etc
│   └── keepalived
│       ├── keepalived.conf.master
│       ├── keepalived.conf.slave
│       └── mysql
│           ├── mybackup.sh
│           ├── mycheck.sh
│           ├── mymaster.sh
│           └── mystop.sh
│           └── .mysqlenv
├── healthcheck.sh
└── mysqld.service

docker-entrypoint.sh

#!/bin/bash
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
set -e

echo "[Entrypoint] MySQL Docker Image 5.7.27-1.1.12"
# Fetch value from server config
# We use mysqld --verbose --help instead of my_print_defaults because the
# latter only show values present in config files, and not server defaults
_get_config() {
  local conf="$1"; shift
  "$@" --verbose --help 2>/dev/null | grep "^$conf" | awk '$1 == "'"$conf"'" { print $2; exit }'
}

# If command starts with an option, prepend mysqld
# This allows users to add command-line options without
# needing to specify the "mysqld" command
if [ "${1:0:1}" = '-' ]; then
  set -- mysqld "$@"
fi

if [ "$1" = 'mysqld' ]; then
  # Test that the server can start. We redirect stdout to /dev/null so
  # only the error messages are left.
  result=0
  output=$("$@" --verbose --help 2>%%VALIDATE_CONFIG%%1 > /dev/null) || result=$?
  if [ ! "$result" = "0" ]; then
    echo >&2 '[Entrypoint] ERROR: Unable to start MySQL. Please check your configuration.'
    echo >&2 "[Entrypoint] $output"
    exit 1
  fi

  # Get config
  DATADIR="$(_get_config 'datadir' "$@")"
  SOCKET="$(_get_config 'socket' "$@")"

  if [ -n "$MYSQL_LOG_CONSOLE" ] || [ -n "" ]; then
    # Don't touch bind-mounted config files
    if ! cat /proc/1/mounts | grep "etc/my.cnf"; then
      sed -i 's/^log-error=/#&/' /etc/my.cnf
    fi
  fi

  if [ ! -d "$DATADIR/mysql" ]; then
    # If the password variable is a filename we use the contents of the file. We
    # read this first to make sure that a proper error is generated for empty files.
    if [ -f "$MYSQL_ROOT_PASSWORD" ]; then
      MYSQL_ROOT_PASSWORD="$(cat $MYSQL_ROOT_PASSWORD)"
      if [ -z "$MYSQL_ROOT_PASSWORD" ]; then
        echo >&2 '[Entrypoint] Empty MYSQL_ROOT_PASSWORD file specified.'
        exit 1
      fi
    fi
    if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
      echo >&2 '[Entrypoint] No password option specified for new database.'
      echo >&2 '[Entrypoint]   A random onetime password will be generated.'
      MYSQL_RANDOM_ROOT_PASSWORD=true
      MYSQL_ONETIME_PASSWORD=true
    fi
    mkdir -p "$DATADIR"
    chown -R mysql:mysql "$DATADIR"

    echo '[Entrypoint] Initializing database'
    "$@" --initialize-insecure
    echo '[Entrypoint] Database initialized'

    "$@" --daemonize --skip-networking --socket="$SOCKET"

    # To avoid using password on commandline, put it in a temporary file.
    # The file is only populated when and if the root password is set.
    PASSFILE=$(mktemp -u /var/lib/mysql-files/XXXXXXXXXX)
    install /dev/null -m0600 -omysql -gmysql "$PASSFILE"
    # Define the client command used throughout the script
    # "SET @@SESSION.SQL_LOG_BIN=0;" is required for products like group replication to work properly
    mysql=( mysql --defaults-extra-file="$PASSFILE" --protocol=socket -uroot -hlocalhost --socket="$SOCKET" --init-command="SET @@SESSION.SQL_LOG_BIN=0;")

    if [ ! -z "" ];
    then
      for i in {30..0}; do
        if mysqladmin --socket="$SOCKET" ping &>/dev/null; then
          break
        fi
        echo '[Entrypoint] Waiting for server...'
        sleep 1
      done
      if [ "$i" = 0 ]; then
        echo >&2 '[Entrypoint] Timeout during MySQL init.'
        exit 1
      fi
    fi

    mysql_tzinfo_to_sql /usr/share/zoneinfo | "${mysql[@]}" mysql
    
    if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
      MYSQL_ROOT_PASSWORD="$(pwmake 128)"
      echo "[Entrypoint] GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
    fi
    if [ -z "$MYSQL_ROOT_HOST" ]; then
      ROOTCREATE="ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}';"
    else
      ROOTCREATE="ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; \
      CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}'; \
      GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ; \
      GRANT PROXY ON ''@'' TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;"
    fi
    "${mysql[@]}" <<-EOSQL
      DELETE FROM mysql.user WHERE user NOT IN ('mysql.infoschema', 'mysql.session', 'mysql.sys', 'root') OR host NOT IN ('localhost');
      CREATE USER 'healthchecker'@'localhost' IDENTIFIED BY 'healthcheckpass';
      ${ROOTCREATE}
      FLUSH PRIVILEGES ;
    EOSQL
    if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
      # Put the password into the temporary config file
      cat >"$PASSFILE" <<EOF
[client]
password="${MYSQL_ROOT_PASSWORD}"
EOF
      #mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
    fi

    if [ "$MYSQL_DATABASE" ]; then
      echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
      mysql+=( "$MYSQL_DATABASE" )
    fi

    if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
      echo "CREATE USER '"$MYSQL_USER"'@'%' IDENTIFIED BY '"$MYSQL_PASSWORD"' ;" | "${mysql[@]}"

      if [ "$MYSQL_DATABASE" ]; then
        echo "GRANT ALL ON \`"$MYSQL_DATABASE"\`.* TO '"$MYSQL_USER"'@'%' ;" | "${mysql[@]}"
      fi

    elif [ "$MYSQL_USER" -a ! "$MYSQL_PASSWORD" -o ! "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then
      echo '[Entrypoint] Not creating mysql user. MYSQL_USER and MYSQL_PASSWORD must be specified to create a mysql user.'
    fi
    echo
    for f in /docker-entrypoint-initdb.d/*; do
      case "$f" in
        *.sh)  echo "[Entrypoint] running $f"; . "$f" ;;
        *.sql) echo "[Entrypoint] running $f"; "${mysql[@]}" < "$f" && echo ;;
        *)     echo "[Entrypoint] ignoring $f" ;;
      esac
      echo
    done

    # When using a local socket, mysqladmin shutdown will only complete when the server is actually down
    mysqladmin --defaults-extra-file="$PASSFILE" shutdown -uroot --socket="$SOCKET"
    rm -f "$PASSFILE"
    unset PASSFILE
    echo "[Entrypoint] Server shut down"

    # This needs to be done outside the normal init, since mysqladmin shutdown will not work after
    if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
      if [ -z "yes" ]; then
        echo "[Entrypoint] User expiration is only supported in MySQL 5.6+"
      else
        echo "[Entrypoint] Setting root user as expired. Password will need to be changed before database can be used."
        SQL=$(mktemp -u /var/lib/mysql-files/XXXXXXXXXX)
        install /dev/null -m0600 -omysql -gmysql "$SQL"
        if [ ! -z "$MYSQL_ROOT_HOST" ]; then
          cat << EOF > "$SQL"
ALTER USER 'root'@'${MYSQL_ROOT_HOST}' PASSWORD EXPIRE;
ALTER USER 'root'@'localhost' PASSWORD EXPIRE;
EOF
        else
          cat << EOF > "$SQL"
ALTER USER 'root'@'localhost' PASSWORD EXPIRE;
EOF
        fi
        set -- "$@" --init-file="$SQL"
        unset SQL
      fi
    fi

    echo
    echo '[Entrypoint] MySQL init process done. Ready for start up.'
    echo
  fi

  # Used by healthcheck to make sure it doesn't mistakenly report container
  # healthy during startup
  # Put the password into the temporary config file
  touch /healthcheck.cnf
  cat >"/healthcheck.cnf" <<EOF
[client]
user=healthchecker
socket=${SOCKET}
password=healthcheckpass
EOF
  touch /mysql-init-complete
  chown -R mysql:mysql "$DATADIR"
  echo "[Entrypoint] Starting MySQL 5.7.27-1.1.12"
fi

exec /usr/sbin/init

Dockerfile

# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
FROM oraclelinux:7-slim

ARG MYSQL_SERVER_PACKAGE=mysql-community-server-minimal-5.7.27
ARG MYSQL_SHELL_PACKAGE=mysql-shell-8.0.17

# Install server
RUN yum install -y https://repo.mysql.com/mysql-community-minimal-release-el7.rpm \
      https://repo.mysql.com/mysql-community-release-el7.rpm \
  && yum-config-manager --enable mysql57-server-minimal \
  && yum install -y \
      $MYSQL_SERVER_PACKAGE \
      $MYSQL_SHELL_PACKAGE \
      libpwquality \
      keepalived \
      net-tools \
      ipsvadm \
      iproute \
      openssh-server \
      openssh-clients \
      passwd \
  && yum clean all \
  && mkdir /docker-entrypoint-initdb.d 

VOLUME /var/lib/mysql

COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /healthcheck.sh
COPY mysqld.service /usr/lib/systemd/system/mysqld.service
COPY etc/ /
RUN systemctl enable mysqld
ENTRYPOINT ["/entrypoint.sh"]
HEALTHCHECK CMD /healthcheck.sh
EXPOSE 3306 33060
CMD ["mysqld"]

keepalived.conf.master

! Configuration File for keepalived
global_defs {
  router_id KeepAlive_Mysql   
  # 标识,主从一致
}
vrrp_script check_run {
  script "/etc/keepalived/mysql/mycheck.sh"  
  # Mysql状态检查脚本
  interval 10
}

vrrp_sync_group VG1 {
  group {
    VI_1
  }
}

vrrp_instance VI_1 {
  state BACKUP
  # 注意,主从两端都配置成了backup,因为使用了nopreempt,即非抢占模式

  interface eth0
  virtual_router_id 51 
  # 分组,主从相同
  priority 100
  # 优先级,这个高一点则先把它作为master
  advert_int 1
  nopreempt 
  # 不主动抢占资源,设置非抢占模式
  authentication { 
  # 主从一致
    auth_type PASS
    auth_pass 123456
  }
  track_script {
    check_run
  }
  notify_master /etc/keepalived/mysql/mymaster.sh
  notify_backup /etc/keepalived/mysql/mybackup.sh
  notify_stop /etc/keepalived/mysql/mystop.sh
  virtual_ipaddress {
    172.18.0.110/24 brd 172.18.0.255 dev eth0 label eth0:0  
    # 虚拟IP地址
  }
}

keepalived.conf.slave

! Configuration File for keepalived
global_defs {
  router_id KeepAlive_Mysql
}
vrrp_script check_run {
  script "/etc/keepalived/mysql/mycheck.sh"
  interval 10
}
vrrp_sync_group VG1 {
  group {
    VI_1
  }
}
vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1
  nopreempt
  authentication {
    auth_type PASS
    auth_pass 123456
  }
  track_script {
    check_run
  }
  notify_master /etc/keepalived/mysql/mymaster.sh
  notify_backup /etc/keepalived/mysql/mybackup.sh
  notify_stop /etc/keepalived/mysql/mystop.sh
  virtual_ipaddress {
    172.18.0.110/24 brd 172.18.0.255 dev eth0 label eth0:0
  }
}

mybackup.sh

#!/bin/sh
##################################################
#File Name  : mybackup.sh
#Date       : 2019-08-07
#Description: Empty the slave configuration, retrieve
#             the remote log file and Pos, and open
#             the synchronization
#Writer     :by L.H.W
##################################################
BASEPATH=/etc/keepalived/mysql
LOGSPATH=$BASEPATH/logs
[[ -d $LOGSPATH ]] || mkdir -p $LOGSPATH
[[ -d $BASEPATH/syncposfile ]] || mkdir -p $BASEPATH/syncposfile
source $BASEPATH/.mysqlenv

$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'repl'@'%' IDENTIFIED BY '123456';flush privileges;"
$mysql -e "set global innodb_support_xa=0;"
$mysql -e "set global sync_binlog=0;"
$mysql -e "set global innodb_flush_log_at_trx_commit=0;"
$mysql -e "flush logs;"
$mysql -e "reset slave all;"

if [ -f $BASEPATH/syncposfile/backup_master.status ];then
  New_ReM_File=`cat $BASEPATH/syncposfile/backup_master.status | grep -v File |awk '{print $1}'` 
  New_ReM_Position=`cat $BASEPATH/syncposfile/backup_master.status | grep -v File |awk '{print $2}'`
  echo "$(date "+%Y-%m-%d %H:%M:%S") This mybackup.sh, New_ReM_File:$New_ReM_File,New_ReM_Position:$New_ReM_Position" >> $LOGSPATH/mysql_switch.log
  $mysql -e "change master to master_host='$REMOTE_IP',master_port=3306,master_user='replication',master_password='replication',master_log_file='$New_ReM_File',master_log_pos=$New_ReM_Position;"
  $mysql -e "start slave;"
  $mysql -e "show slave status\G;" > $LOGSPATH/slave_status_$(date "+%y%m%d-%H%M").txt
  cat $LOGSPATH/slave_status_$(date "+%y%m%d-%H%M").txt >> $LOGSPATH/mysql_switch.log
  rm -f $BASEPATH/syncposfile/backup_master.status
else
  echo "$(date "+%Y-%m-%d %H:%M:%S") The scripts mybackup.sh running error..." > $LOGSPATH/mysql_switch.log
fi

mycheck.sh

#!/bin/sh
##################################################
#File Name  : mycheck.sh
#Date       : 2019-08-07
#Description: mysql is working MYSQL_OK is 1
#             mysql is down MYSQL_OK is 0
#Writer     :by L.H.W
##################################################
BASEPATH=/etc/keepalived/mysql
LOGSPATH=$BASEPATH/logs
[[ -d $LOGSPATH ]] || mkdir -p $LOGSPATH
source $BASEPATH/.mysqlenv
CHECK_TIME=3
MYSQL_OK=1

function check_mysql_helth(){
$mysql -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
  MYSQL_OK=1
else
  MYSQL_OK=0
fi
return $MYSQL_OK
}

while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
  CHECK_TIME=0
  echo "$(date "+%Y-%m-%d %H:%M:%S") The mycheck.sh, mysql is running..." >> $LOGSPATH/mysql_switch.log
  exit 0
fi

if [ $MYSQL_OK -eq 0 ] &&  [ $CHECK_TIME -eq 0 ];then
  echo "$(date "+%Y-%m-%d %H:%M:%S") The mycheck.sh, mysql is down, after switch..." >> $LOGSPATH/mysql_switch.log
  systemctl stop keepalived
  exit 1
fi
sleep 1
done

mymaster.sh

#!/bin/sh
##################################################
#File Name  : mymaster.sh
#Date       : 2019-08-07
#Description: First determine whether synchronous
#             replication is performed, and if no
#             execution is completed, wait for 1
#             minutes. Log logs and POS after
#             switching, and record files synchronously.
#Writer     :by L.H.W
##################################################
BASEPATH=/etc/keepalived/mysql
LOGSPATH=$BASEPATH/logs
[[ -d $LOGSPATH ]] || mkdir -p $LOGSPATH
source $BASEPATH/.mysqlenv

$mysql -e "show slave status\G" > $LOGSPATH/mysqlslave.states
Master_Log_File=`cat $LOGSPATH/mysqlslave.states | grep -w Master_Log_File | awk -F": " '{print $2}'`
Relay_Master_Log_File=`cat $LOGSPATH/mysqlslave.states | grep -w Relay_Master_Log_File | awk -F": " '{print $2}'`
Read_Master_Log_Pos=`cat $LOGSPATH/mysqlslave.states | grep -w Read_Master_Log_Pos | awk -F": " '{print $2}'`
Exec_Master_Log_Pos=`cat $LOGSPATH/mysqlslave.states | grep -w Exec_Master_Log_Pos | awk -F": " '{print $2}'`

i=1
while true
do
  if [ $Master_Log_File = $Relay_Master_Log_File ] && [ $Read_Master_Log_Pos -eq $Exec_Master_Log_Pos ];then
    echo "$(date "+%Y-%m-%d %H:%M:%S") The mymaster.sh, slave sync ok... " >> $LOGSPATH/mysql_switch.log
    break
  else
    sleep 1
    if [ $i -gt 60 ];then
      break
    fi
    continue
    let i++
  fi
done

$mysql -e "stop slave;"
$mysql -e "set global innodb_support_xa=0;"
$mysql -e "set global sync_binlog=0;"
$mysql -e "set global innodb_flush_log_at_trx_commit=0;"
$mysql -e "flush logs;GRANT ALL PRIVILEGES ON *.* TO 'repl'@'%' IDENTIFIED BY '123456';flush privileges;"
$mysql -e "show master status;" > $LOGSPATH/master_status_$(date "+%y%m%d-%H%M").txt
 
# sync pos file
/usr/bin/scp $LOGSPATH/master_status_$(date "+%y%m%d-%H%M").txt root@$REMOTE_IP:$BASEPATH/syncposfile/backup_master.status
echo "$(date "+%Y-%m-%d %H:%M:%S") The mymaster.sh, Sync pos file sucess." >> $LOGSPATH/mysql_switch.log

mystop.sh

#!/bin/sh
##################################################
#File Name  : mystop.sh
#Date       : 2019-08-07
#Description: Set parameters to ensure that the data
#             is not lost, and finally check to see
#             if there are still write operations,
#             the last 1 minutes to exit
#Writer     :by L.H.W
##################################################
BASEPATH=/etc/keepalived/mysql
LOGSPATH=$BASEPATH/logs
[[ -d $LOGSPATH ]] || mkdir -p $LOGSPATH
source $BASEPATH/.mysqlenv

$mysql -e "GRANT ALL PRIVILEGES ON *.* TO 'repl'@'%' IDENTIFIED BY '123456';flush privileges;"
$mysql -e "set global innodb_support_xa=1;"
$mysql -e "set global sync_binlog=1;"
$mysql -e "set global innodb_flush_log_at_trx_commit=1;"

$mysql -e "show master status\G" > $LOGSPATH/mysqlmaster0.states
M_File1=`cat $LOGSPATH/mysqlmaster0.states | awk -F': ' '/File/{print $2}'`
M_Position1=`cat $LOGSPATH/mysqlmaster0.states | awk -F': ' '/Position/{print $2}'`
sleep 2
$mysql -e "show master status\G" > $LOGSPATH/mysqlmaster1.states
M_File2=`cat $LOGSPATH/mysqlmaster1.states | awk -F': ' '/File/{print $2}'`
M_Position2=`cat $LOGSPATH/mysqlmaster1.states | awk -F': ' '/Position/{print $2}'`

i=1
while true
do
  if [ $M_File1 = $M_File2 ] && [ $M_Position1 -eq $M_Position2 ];then
    echo "$(date "+%Y-%m-%d %H:%M:%S") The mystop.sh, master sync ok..." >> $LOGSPATH/mysql_switch.log
    exit 0
  else
    sleep 1
    if [ $i -gt 60 ];then
      break
    fi
    continue
    let i++
  fi
done
echo "$(date "+%Y-%m-%d %H:%M:%S") The mystop.sh, master sync exceed one minutes..." >> $LOGSPATH/mysql_switch.log

healthcheck.sh

#!/bin/bash
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

# The mysql-init-complete file is touched by the entrypoint file before the
# main server process is started
if [ -f /mysql-init-complete ]; # The entrypoint script touches this file
then # Ping server to see if it is ready
  mysqladmin --defaults-extra-file=/healthcheck.cnf ping
else # Initialization still in progress
  exit 1
fi

mysqld.service

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/sbin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

.mysqlenv

MYSQL=/usr/bin/mysql
MYSQL_CMD="-uroot -p123456"
#远端主机的IP地址
REMOTE_IP=172.18.0.3

export mysql="$MYSQL $MYSQL_CMD"

猜你喜欢

转载自www.cnblogs.com/ihowell/p/11325309.html