Rancher部署mysql8

本文详细介绍了如何在Rancher中部署MySQL8,包括环境变量设置、端口映射、用户权限配置、主从集群搭建、解决SQL模式问题以及数据库初始化。还涉及到数据导入优化、SSL加密连接配置等技巧。
摘要由CSDN通过智能技术生成

环境变量设置

      设置root用户的密码

       

端口映射 

        

处理用户远程连接登录异常 

     create user 'taishi'@'%' identified by 'aransfar@123';
     grant all privileges on *.* to 'taishi'@'%' with grant option;
     flush privileges;

    ALTER USER 'taishi'@'%' IDENTIFIED WITH mysql_native_password BY 'aransfar@123';

    set global log_bin_trust_function_creators=1;

        

 处理mysql8在sql查询语句中不区分大小写字符

       

          

          

          docker run --name mysql -p 33306:3306  -v /home/admin/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD="Transfar@123" ab2f358b8612  --lower-case-table-names=1

          --lower-case-table-names=1 相当于作为entrypoint.sh的命令参数 参数的前面需要加--

         

 所有服务正常连接数据库并启动

      数据库传输同步

         

          

版本兼容问题1

       mysql 8.0之后出的异常,MySQL 5.7.5及以上功能依赖检测功能,而使用的mysql是8.0版本.如果启用了ONLY_FULL_GROUP_BY SQL 模式(默认情况下)MySQL将拒绝选择列表,HAVING 条件或ORDER BY列表的查询引用在GROUP BY子句中既未命名的非集合列

        

       解决方法2

          打开mysql配置文件:my.cnf
          直接选择打开mysql配置文件,然后在最底部添加
          sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
          重启mysql

创建只有查询权限的用户

       mysql -uroot -pTransfar@123
       create user 'chaxun'@'%' identified by 'Tranar@123';
       grant select on *.* to 'chaxun'@'%' with grant option;
       flush privileges;
       ALTER USER 'chaxun'@'%' IDENTIFIED WITH mysql_native_password BY 'Trsfar@123';

Rancher搭建mysql主从集群

           

        mysql数据存储

         容器中的/var/lib/mysql 必须设置成有状态存储  这样即使容器重启 也不会使原来数据丢失

           

           mysql容器启动的时候提示

               ENTRYPOINT ["/bin/sh","/start-mysql.sh"]

               /bin/sh  start-mysql.sh  出现  let: not found

          修改Dockerfile

              ENTRYPOINT ["/bin/bash","/start-mysql.sh"]

         主从集群搭建步骤

          1.创建相关文件

           

        2.文件内容

#!/bin/bash
set -eo pipefail
shopt -s nullglob

# logging functions
mysql_log() {
    local type="$1"; shift
    printf '%s [%s] [Entrypoint]: %s\n' "$(date --rfc-3339=seconds)" "$type" "$*"
}
mysql_note() {
    mysql_log Note "$@"
}
mysql_warn() {
    mysql_log Warn "$@" >&2
}
mysql_error() {
    mysql_log ERROR "$@" >&2
    exit 1
}

# usage: file_env VAR [DEFAULT]
#    ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
#  "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
    local var="$1"
    local fileVar="${var}_FILE"
    local def="${2:-}"
    if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
        mysql_error "Both $var and $fileVar are set (but are exclusive)"
    fi
    local val="$def"
    if [ "${!var:-}" ]; then
        val="${!var}"
    elif [ "${!fileVar:-}" ]; then
        val="$(< "${!fileVar}")"
    fi
    export "$var"="$val"
    unset "$fileVar"
}

# check to see if this file is being run or sourced from another script
_is_sourced() {
    # https://unix.stackexchange.com/a/215279
    [ "${#FUNCNAME[@]}" -ge 2 ] \
        && [ "${FUNCNAME[0]}" = '_is_sourced' ] \
        && [ "${FUNCNAME[1]}" = 'source' ]
}

# usage: docker_process_init_files [file [file [...]]]
#    ie: docker_process_init_files /always-initdb.d/*
# process initializer files, based on file extensions
docker_process_init_files() {
    # mysql here for backwards compatibility "${mysql[@]}"
    mysql=( docker_process_sql )

    echo
    local f
    for f; do
        case "$f" in
            *.sh)
                # https://github.com/docker-library/postgres/issues/450#issuecomment-393167936
                # https://github.com/docker-library/postgres/pull/452
                if [ -x "$f" ]; then
                    mysql_note "$0: running $f"
                    "$f"
                else
                    mysql_note "$0: sourcing $f"
                    . "$f"
                fi
                ;;
            *.sql)    mysql_note "$0: running $f"; docker_process_sql < "$f"; echo ;;
            *.sql.gz) mysql_note "$0: running $f"; gunzip -c "$f" | docker_process_sql; echo ;;
            *.sql.xz) mysql_note "$0: running $f"; xzcat "$f" | docker_process_sql; echo ;;
            *)        mysql_warn "$0: ignoring $f" ;;
        esac
        echo
    done
}

mysql_check_config() {
    local toRun=( "$@" --verbose --help ) errors
    if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
        mysql_error $'mysqld failed while attempting to check config\n\tcommand was: '"${toRun[*]}"$'\n\t'"$errors"
    fi
}

# 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
mysql_get_config() {
    local conf="$1"; shift
    "$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null \
        | awk -v conf="$conf" '$1 == conf && /^[^ \t]/ { sub(/^[^ \t]+[ \t]+/, ""); print; exit }'
    # match "datadir      /some/path with/spaces in/it here" but not "--xyz=abc\n     datadir (xyz)"
}

# Do a temporary startup of the MySQL server, for init purposes
docker_temp_server_start() {
    if [ "${MYSQL_MAJOR}" = '5.6' ] || [ "${MYSQL_MAJOR}" = '5.7' ]; then
        "$@" --skip-networking --socket="${SOCKET}" &
        mysql_note "Waiting for server startup"
        local i
        for i in {30..0}; do
            # only use the root password if the database has already been initialized
            # so that it won't try to fill in a password file when it hasn't been set yet
            extraArgs=()
            if [ -z "$DATABASE_ALREADY_EXISTS" ]; then
                extraArgs+=( '--dont-use-mysql-root-password' )
            fi
            if docker_process_sql "${extraArgs[@]}" --database=mysql <<<'SELECT 1' &> /dev/null; then
                break
            fi
            sleep 1
        done
        if [ "$i" = 0 ]; then
            mysql_error "Unable to start server."
        fi
    else
        # For 5.7+ the server is ready for use as soon as startup command unblocks
        if ! "$@" --daemonize --skip-networking --socket="${SOCKET}"; then
            mysql_error "Unable to start server."
        fi
    fi
}

# Stop the server. When using a local socket file mysqladmin will block until
# the shutdown is complete.
docker_temp_server_stop() {
    if ! mysqladmin --defaults-extra-file=<( _mysql_passfile ) shutdown -uroot --socket="${SOCKET}"; then
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值