Docker容器之MySQL版本升级

1. 概览

  • 系统:KVM(Ubuntu16.04)
  • docker版本:Docker version 20.10.7

2. 版本升级

  • mysql Ver 14.14 Distrib 5.7.24  -->  mysql Ver 8.0.27

3. 适配MySQL

  • 拉取MySQL8.0.27镜像
docker pull mysql:8.0.27
  • 运行MySQL8.0.27镜像(用做镜像定制)
docker run -d --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 33061:3306 mysql:8.0.27

 

 

3.1 MySQL配置

  • MySQL5.7的配置结构与MySQL8.0的配置结构不相同
#MySQL5.7
root@e7a86cb119e7:/etc/mysql# ls -la
total 24
drwxr-xr-x 1 root root 4096 Dec 29  2018 .
drwxr-xr-x 1 root root 4096 Dec  9 15:02 ..
drwxr-xr-x 1 root root 4096 Apr 15  2020 conf.d
lrwxrwxrwx 1 root root   24 Dec 29  2018 my.cnf -> /etc/alternatives/my.cnf
-rw-r--r-- 1 root root  839 Jul 10  2016 my.cnf.fallback
-rw-r--r-- 1 root root  796 Oct  4  2018 mysql.cnf
drwxr-xr-x 1 root root 4096 Feb 19  2021 mysql.conf.d
#MySQL8.0
root@296df8cb7e94:/etc/mysql# ls -la
total 24
drwxr-xr-x 1 root root 4096 Dec 10 19:17 .
drwxr-xr-x 1 root root 4096 Dec 10 19:23 ..
drwxrwxr-x 1 root root 4096 Dec  2 19:22 conf.d
-rw-rw-r-- 1 1000 1000 2514 Dec 10 19:17 my.cnf
-rw-r--r-- 1 root root 1448 Sep 28 21:27 my.cnf.fallback
-rw-r--r-- 1 1000 1000 1080 Dec 10 18:48 my.cnf_bak

所以将MySQL5.7的配置复制到MySQL8.0下并统一更名为my.cnf即可,MySQL8.0的配置如下:

 

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
# * Basic Settings
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
bind-address	= 0.0.0.0
server-id = 1

init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8
skip-name-resolve
back_log = 300

max_connections = 50
max_connect_errors = 500
open_files_limit = 1024
table_open_cache = 128
max_allowed_packet = 100M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 32M

read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 16M

thread_cache_size = 16

#query_cache_type = 1
#query_cache_size = 16M
#query_cache_limit = 2M
#ft_min_word_len = 4

log_bin	  = /var/log/mysql/mysql-bin.log
binlog_format = mixed
expire_logs_days = 7

log_error = /var/log/mysql/error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp
skip-external-locking

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 128M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120

bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 16M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800

3.2 MySQL entrypoint.sh设置

  • 每个容器数据库需要运行时都需要entrypoint.sh,如运行数据库时自动创建指定数据库等等
  • 所以将原本MySQL5.7的entrypoint.sh复制到MySQL8.0下,entrypoint.sh配置如下:
#!/bin/bash
set -eo pipefail
shopt -s nullglob

# if command starts with an option, prepend mysqld
if [ "${1:0:1}" = '-' ]; then
	set -- mysqld "$@"
fi

# skip setup if they want an option that stops mysqld
wantHelp=
for arg; do
	case "$arg" in
		-'?'|--help|--print-defaults|-V|--version)
			wantHelp=1
			break
			;;
	esac
done

# 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
		echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
		exit 1
	fi
	local val="$def"
	if [ "${!var:-}" ]; then
		val="${!var}"
	elif [ "${!fileVar:-}" ]; then
		val="$(< "${!fileVar}")"
	fi
	export "$var"="$val"
	unset "$fileVar"
}

# usage: process_init_file FILENAME MYSQLCOMMAND...
#    ie: process_init_file foo.sh mysql -uroot
# (process a single initializer file, based on its extension. we define this
# function here, so that initializer scripts (*.sh) can use the same logic,
# potentially recursively, or override the logic used in subsequent calls)
process_init_file() {
	local f="$1"; shift
	local mysql=( "$@" )

	case "$f" in
		*.sh)     echo "$0: running $f"; . "$f" ;;
		*.sql)    echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
		*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
		*)        echo "$0: ignoring $f" ;;
	esac
	echo
}

_check_config() {
	toRun=( "$@" --verbose --help )
	if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
		cat >&2 <<-EOM

			ERROR: mysqld failed while attempting to check config
			command was: "${toRun[*]}"

			$errors
		EOM
		exit 1
	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
_get_config() {
	local conf="$1"; shift
	"$@" --verbose --help --log-bin-index="$(mktemp -u)" 2>/dev/null | awk '$1 == "'"$conf"'" { print $2; exit }'
}

# allow the container to be started with `--user`
if [ "$1" = 'mysqld' -a -z "$wantHelp" -a "$(id -u)" = '0' ]; then
	_check_config "$@"
	DATADIR="$(_get_config 'datadir' "$@")"
	mkdir -p "$DATADIR"
	chown -R mysql:mysql "$DATADIR"
	exec gosu mysql "$BASH_SOURCE" "$@"
fi

if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
	# still need to check config, container may have started with --user
	_check_config "$@"
	# Get config
	DATADIR="$(_get_config 'datadir' "$@")"

	if [ ! -d "$DATADIR/mysql" ]; then
		file_env 'MYSQL_ROOT_PASSWORD'
		if [ -z "$MYSQL_ROOT_PASSWORD" -a -z "$MYSQL_ALLOW_EMPTY_PASSWORD" -a -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
			echo >&2 'error: database is uninitialized and password option is not specified '
			echo >&2 '  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD'
			exit 1
		fi

		mkdir -p "$DATADIR"

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

		if command -v mysql_ssl_rsa_setup > /dev/null && [ ! -e "$DATADIR/server-key.pem" ]; then
			# https://github.com/mysql/mysql-server/blob/23032807537d8dd8ee4ec1c4d40f0633cd4e12f9/packaging/deb-in/extra/mysql-systemd-start#L81-L84
			echo 'Initializing certificates'
			mysql_ssl_rsa_setup --datadir="$DATADIR"
			echo 'Certificates initialized'
		fi

		SOCKET="$(_get_config 'socket' "$@")"
		"$@" --skip-networking --socket="${SOCKET}" &
		pid="$!"

		mysql=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )

		for i in {30..0}; do
			if echo 'SELECT 1' | "${mysql[@]}" &> /dev/null; then
				break
			fi
			echo 'MySQL init process in progress...'
			sleep 1
		done
		if [ "$i" = 0 ]; then
			echo >&2 'MySQL init process failed.'
			exit 1
		fi

		if [ -z "$MYSQL_INITDB_SKIP_TZINFO" ]; then
			# sed is for https://bugs.mysql.com/bug.php?id=20545
			mysql_tzinfo_to_sql /usr/share/zoneinfo | sed 's/Local time zone must be set--see zic manual page/FCTY/' | "${mysql[@]}" mysql
		fi

		if [ ! -z "$MYSQL_RANDOM_ROOT_PASSWORD" ]; then
			export MYSQL_ROOT_PASSWORD="$(pwgen -1 32)"
			echo "GENERATED ROOT PASSWORD: $MYSQL_ROOT_PASSWORD"
		fi

		rootCreate=
		# default root to listen for connections from anywhere
		file_env 'MYSQL_ROOT_HOST' '%'
		if [ ! -z "$MYSQL_ROOT_HOST" -a "$MYSQL_ROOT_HOST" != 'localhost' ]; then
			# no, we don't care if read finds a terminating character in this heredoc
			# https://unix.stackexchange.com/questions/265149/why-is-set-o-errexit-breaking-this-read-heredoc-expression/265151#265151
			read -r -d '' rootCreate <<-EOSQL || true
				CREATE USER 'root'@'${MYSQL_ROOT_HOST}' IDENTIFIED BY '${MYSQL_ROOT_PASSWORD}' ;
				GRANT ALL ON *.* TO 'root'@'${MYSQL_ROOT_HOST}' WITH GRANT OPTION ;
			EOSQL
		fi

		"${mysql[@]}" <<-EOSQL
			-- What's done in this file shouldn't be replicated
			--  or products like mysql-fabric won't work
			SET @@SESSION.SQL_LOG_BIN=0;

			SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
			GRANT ALL ON *.* TO 'root'@'localhost' WITH GRANT OPTION ;
			${rootCreate}
			DROP DATABASE IF EXISTS test ;
			FLUSH PRIVILEGES ;
		EOSQL

		if [ ! -z "$MYSQL_ROOT_PASSWORD" ]; then
			mysql+=( -p"${MYSQL_ROOT_PASSWORD}" )
		fi
        
		file_env 'MYSQL_DATABASE'
		if [ "$MYSQL_DATABASE" ]; then
			echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` ;" | "${mysql[@]}"
			mysql+=( "$MYSQL_DATABASE" )
		fi

		file_env 'MYSQL_USER'
		file_env 'MYSQL_PASSWORD'
		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

			echo 'FLUSH PRIVILEGES ;' | "${mysql[@]}"
		fi
        
        #################################
        # Create multiple custom databases
		file_env 'WS_DATABASE'
        mysql_ws=( mysql --protocol=socket -uroot -hlocalhost --socket="${SOCKET}" )
        mysql_ws+=( -p"${MYSQL_ROOT_PASSWORD}" )

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

		if [ "$MYSQL_USER" -a "$MYSQL_PASSWORD" ]; then

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

			echo 'FLUSH PRIVILEGES ;' | "${mysql_ws[@]}"
		fi
        #############################

		echo
		ls /docker-entrypoint-initdb.d/ > /dev/null
		for f in /docker-entrypoint-initdb.d/*; do
			process_init_file "$f" "${mysql[@]}"
		done

		if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then
			"${mysql[@]}" <<-EOSQL
				ALTER USER 'root'@'%' PASSWORD EXPIRE;
			EOSQL
		fi
		if ! kill -s TERM "$pid" || ! wait "$pid"; then
			echo >&2 'MySQL init process failed.'
			exit 1
		fi

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

exec "$@"

3.3 适配完成后重启MySQL8.0.27容器,确保数据库运行正常

docker restart mysql

 

 

4. 应用部署

  • 将适配好的容器进行commit
docker commit mysql 192.168.2.223:5004/lct_demotions/database:v3.3

 

 

  • 新镜像应用到项目
#docker-compose.yml
...
    database:
        build: Database
        restart: always
        #image: 192.168.2.221:5003/database:v3.1
        image: 192.168.2.223:5004/lct_demotions/database:v3.3
        ports:
          - "33060:3306"
        environment:
          WS_DATABASE:
          MYSQL_ROOT_PASSWORD:
          MYSQL_DATABASE:
          MYSQL_USER:
          MYSQL_PASSWORD:
        volumes:
          - ./Database/data:/var/lib/mysql
          - ./Database/backup:/backup
        command: --default-authentication-plugin=mysql_native_password --lower_case_table_names=1 --skip-name-resolve=false --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
...
kyfq@rsync:~/lct_demonstration$ docker ps
CONTAINER ID   IMAGE                                                   COMMAND                  CREATED      STATUS      PORTS                                                    NAMES
296df8cb7e94   192.168.2.223:5004/lct_demotions/database:v3.3          "docker-entrypoint.s…"   2 days ago   Up 2 days   33060/tcp, 0.0.0.0:33060->3306/tcp, :::33060->3306/tcp   lct_demonstration_database_1
  • 测试数据库的数据是否有差别,无差别即升级成功

PS:我测试的项目运行成功,数据无丢失。

 

 

 

版权声明:
作者:lee
链接:https://www.goufusheng.top/archives/734
来源:苟浮生的桃花扇
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录