鯤鵬服務器docker部署mysql 8.0.19,及自定義Dockerfile
提示
:本文的操作是在基礎環境搭建完成的基礎上進行的,基礎環境搭建請參閱上一篇文章:
鯤鵬服務器快速部署docker+docker-compose環境:
https://bbs.huaweicloud.com/forum/thread-60765-1-1.html
或:
https://bbs.huaweicloud.com/blogs/176180
docker環境搭建完成,可以進行本文的操作。
系統環境:CentOS 7.6 64bit with ARM
構建Mysql 8.0.19鏡像
構建準備
# clone 官方Dockerfile
git clone https://github.com/mysql/mysql-docker.git
# 進入8.0路徑
cd mysql-docker/8.0
# 修改原版Dockerfile,mysql版本號修改為8.0.19,其他不動
# mysql版本號修改為8.0.19是我方項目需要,如果需要其版本,請參考執行
vim 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
# 版本號改為8.0.19
ARG MYSQL_SERVER_PACKAGE=mysql-community-server-minimal-8.0.19
ARG MYSQL_SHELL_PACKAGE=mysql-shell-8.0.19
# 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 mysql80-server-minimal \
&& yum install -y \
$MYSQL_SERVER_PACKAGE \
$MYSQL_SHELL_PACKAGE \
libpwquality \
&& yum clean all \
&& mkdir /docker-entrypoint-initdb.d
# Data Volume
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /entrypoint.sh
COPY healthcheck.sh /healthcheck.sh
ENTRYPOINT ["/entrypoint.sh"]
HEALTHCHECK CMD /healthcheck.sh
EXPOSE 3306 33060
CMD ["mysqld"]
修改完成,保存、退出。
構建鏡像
# 構建鏡像,注意末尾有個空格,還有個.
docker build -t cehc/mysql-arm:8.0.19 .
# 查看鏡像
docker images
# cehc/mysql-arm:8.0.19鏡像大小438M
發布鏡像
docker login
# 輸入在docker-hub注冊的用戶名密碼登錄
docker tag cehc/mysql-arm:8.0.19 cehc/mysql-arm:8.0.19
docker push cehc/mysql-arm:8.0.19
# 推送成功
說明
:cehc/mysql-arm:8.0.19鏡像已經發布為公共鏡像。
測試鏡像
# 測試運行,不掛載數據目錄和自定義配置
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d cehc/mysql-arm:8.0.19
# 查看啟動日志
docker logs -f -t --tail 100 mysql
# 啟動成功后,可進入 mysql 容器
docker exec -it mysql /bin/bash
# 在容器內登錄mysql
mysql -uroot -p
# 查看 lower_case_table_names 參數設置
mysql> show variables like 'lower%';
# 查看mysql當前時間,檢查當前時區
mysql> select curtime();
# 退出mysql
mysql> exit;
# 退出容器
exit
# 停止容器
docker stop mysql
# 刪除容器
docker rm mysql
實測通過
鏡像編排
# 部署前,主機掛載數據盤到/data,準備數據目錄
mkdir -p /data/mysql/conf
cd /data/mysql
mkdir data log run backup mysql-files tmp
chmod -R 777 /data/mysql
chmod -R 644 /data/mysql/conf
# 說明:容器掛載 /data/mysql:/data/mysql 為了方便進行備份/恢復操作
# 根據容器內置my.cnf,編制自定義配置
vim /data/mysql/conf/my.cnf
# 內容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
# Remove leading # to revert to previous value for default_authentication_plugin,
# this will increase compatibility with older clients. For background, see:
# https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
# default-authentication-plugin=mysql_native_password
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
# 自定義配置
# tmpdir=/tmp
port=3306
# 默認時區設置為東8區
default-time_zone='+8:00'
# 默認編碼utf8mb4
character-set-server=utf8mb4
character-set-client-handshake=FALSE
collation-server=utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
secure_file_priv=/var/lib/mysql
default_authentication_plugin=mysql_native_password
default-storage-engine=INNODB
# 設置數據庫大小寫不敏感,根據實際項目需求設置,默認值是0
lower_case_table_names=1
# 最大連接數,默認設置是100
max_connections=1000
# SQL數據包發送的大小,如果有BLOB對象建議修改成1G
max_allowed_packet=128M
# MySQL連接閑置超過一定時間后(單位:秒)將會被強行關閉
# MySQL默認的 wait_timeout 值為8個小時
# interactive_timeout 參數需要同時配置才能生效
interactive_timeout=28800
wait_timeout=28800
[mysql]
no-auto-rehash
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock
[client]
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock
port=3306
[mysqld_safe]
log-error=/logs/mysql.log
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/lib/mysql/mysql.sock
[mysqldump]
quick
socket=/var/lib/mysql/mysql.sock
[mysqladmin]
socket=/var/lib/mysql/mysql.sock
保存、退出。
# 編制docker-compose腳本
vim docker-compose.yml
# 內容如下:
version: '2'
services:
cehc-mysql:
image: cehc/mysql-arm:8.0.19
container_name: cehc-mysql
privileged: true
restart: always
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: yourpassword # 設置 mysql root 密碼
volumes:
- /data/mysql:/data/mysql
- /data/mysql/data:/var/lib/mysql
- /data/mysql/log:/logs
- /data/mysql/conf/mysql/my.cnf:/etc/my.cnf
- /etc/localtime:/etc/localtime:ro
command: --lower_case_table_names=1 # 注:該設置是我方項目需要
保存、退出。
# 后臺啟動鏡像
docker-compose up -d
# 啟動成功后,可進入 mysql 容器
docker exec -it cehc-mysql /bin/bash
# 在容器內登錄mysql
mysql -u root -p
# 輸入自定義密碼
-- 查看數據庫
mysql> show databases;
+--------------------+
| Database ? ? ? ? ? |
+--------------------+
| information_schema |
| mysql ? ? ? ? ? ? ?|
| performance_schema |
| sys ? ? ? ? ? ? ? ?|
+--------------------+
4 rows in set (0.01 sec)
-- 查看 lower_case_table_names 參數設置
mysql> show variables like 'lower%';
+------------------------+-------+
| Variable_name ? ? ? ? ?| Value |
+------------------------+-------+
| lower_case_file_system | OFF ? |
| lower_case_table_names | 1 ? ? |
+------------------------+-------+
2 rows in set (0.00 sec)
-- 查看是否啟用了二進制日志
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin ? ? ? | ON ? ?|
+---------------+-------+
1 row in set (0.01 sec)
-- 查看二進制日志詳細設置
mysql> show variables like '%binlog%';
+------------------------------------------------+----------------------+
| Variable_name ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| Value ? ? ? ? ? ? ? ?|
+------------------------------------------------+----------------------+
| binlog_cache_size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 32768 ? ? ? ? ? ? ? ?|
| binlog_checksum ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| CRC32 ? ? ? ? ? ? ? ?|
| binlog_direct_non_transactional_updates ? ? ? ?| OFF ? ? ? ? ? ? ? ? ?|
| binlog_encryption ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| OFF ? ? ? ? ? ? ? ? ?|
| binlog_error_action ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ABORT_SERVER ? ? ? ? |
| binlog_expire_logs_seconds ? ? ? ? ? ? ? ? ? ? | 2592000 ? ? ? ? ? ? ?|
| binlog_format ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ROW ? ? ? ? ? ? ? ? ?|
| binlog_group_commit_sync_delay ? ? ? ? ? ? ? ? | 0 ? ? ? ? ? ? ? ? ? ?|
| binlog_group_commit_sync_no_delay_count ? ? ? ?| 0 ? ? ? ? ? ? ? ? ? ?|
| binlog_gtid_simple_recovery ? ? ? ? ? ? ? ? ? ?| ON ? ? ? ? ? ? ? ? ? |
| binlog_max_flush_queue_time ? ? ? ? ? ? ? ? ? ?| 0 ? ? ? ? ? ? ? ? ? ?|
| binlog_order_commits ? ? ? ? ? ? ? ? ? ? ? ? ? | ON ? ? ? ? ? ? ? ? ? |
| binlog_rotate_encryption_master_key_at_startup | OFF ? ? ? ? ? ? ? ? ?|
| binlog_row_event_max_size ? ? ? ? ? ? ? ? ? ? ?| 8192 ? ? ? ? ? ? ? ? |
| binlog_row_image ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | FULL ? ? ? ? ? ? ? ? |
| binlog_row_metadata ? ? ? ? ? ? ? ? ? ? ? ? ? ?| MINIMAL ? ? ? ? ? ? ?|
| binlog_row_value_options ? ? ? ? ? ? ? ? ? ? ? | ? ? ? ? ? ? ? ? ? ? ?|
| binlog_rows_query_log_events ? ? ? ? ? ? ? ? ? | OFF ? ? ? ? ? ? ? ? ?|
| binlog_stmt_cache_size ? ? ? ? ? ? ? ? ? ? ? ? | 32768 ? ? ? ? ? ? ? ?|
| binlog_transaction_dependency_history_size ? ? | 25000 ? ? ? ? ? ? ? ?|
| binlog_transaction_dependency_tracking ? ? ? ? | COMMIT_ORDER ? ? ? ? |
| innodb_api_enable_binlog ? ? ? ? ? ? ? ? ? ? ? | OFF ? ? ? ? ? ? ? ? ?|
| log_statements_unsafe_for_binlog ? ? ? ? ? ? ? | ON ? ? ? ? ? ? ? ? ? |
| max_binlog_cache_size ? ? ? ? ? ? ? ? ? ? ? ? ?| 18446744073709547520 |
| max_binlog_size ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 1073741824 ? ? ? ? ? |
| max_binlog_stmt_cache_size ? ? ? ? ? ? ? ? ? ? | 18446744073709547520 |
| sync_binlog ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| 1 ? ? ? ? ? ? ? ? ? ?|
+------------------------------------------------+----------------------+
27 rows in set (0.00 sec)
-- 查看 log_bin 存儲位置
mysql> show variables like '%log_bin%';
+---------------------------------+-----------------------------+
| Variable_name ? ? ? ? ? ? ? ? ? | Value ? ? ? ? ? ? ? ? ? ? ? |
+---------------------------------+-----------------------------+
| log_bin ? ? ? ? ? ? ? ? ? ? ? ? | ON ? ? ? ? ? ? ? ? ? ? ? ? ?|
| log_bin_basename ? ? ? ? ? ? ? ?| /var/lib/mysql/binlog ? ? ? |
| log_bin_index ? ? ? ? ? ? ? ? ? | /var/lib/mysql/binlog.index |
| log_bin_trust_function_creators | OFF ? ? ? ? ? ? ? ? ? ? ? ? |
| log_bin_use_v1_row_events ? ? ? | OFF ? ? ? ? ? ? ? ? ? ? ? ? |
| sql_log_bin ? ? ? ? ? ? ? ? ? ? | ON ? ? ? ? ? ? ? ? ? ? ? ? ?|
+---------------------------------+-----------------------------+
6 rows in set (0.00 sec)
-- 退出
mysql> exit;
實測通過
構建自定義mysql鏡像
在前面操作的基礎上集成自定義配置,根據我方項目開發實際需求,構建自定義mysql鏡像。
# 編制自定義腳本
vim Dockerfile
# 內容如下:
FROM cehc/mysql-arm:8.0.19
MAINTAINER from www.ceht.com.cn by cehc(humengjie@ceht.com.cn)
# 設置編碼,防止中文亂碼
ENV LANG C.UTF-8
# 設置時區
ENV TZ=Asia/Shanghai
# 添加自定義配置
RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
echo -e "\n# 自定義配置\n# tmpdir=/tmp\nport=3306\n# 默認時區設置為東8區\ndefault-time_zone='+8:00'\n\n# 默認編碼utf8mb4\ncharacter-set-server=utf8mb4\ncharacter-set-client-handshake=FALSE\ncollation-server=utf8mb4_general_ci\ninit_connect='SET NAMES utf8mb4'\nsecure_file_priv=/var/lib/mysql\ndefault_authentication_plugin=mysql_native_password\ndefault-storage-engine=INNODB\n# 設置數據庫大小寫不敏感,根據實際項目需求設置,默認值是0\nlower_case_table_names=1\n\n# 最大連接數,默認設置是100\nmax_connections=1000\n# SQL數據包發送的大小,如果有BLOB對象建議修改成1G\nmax_allowed_packet=128M\n# MySQL連接閑置超過一定時間后(單位:秒)將會被強行關閉\n# MySQL默認的 wait_timeout 值為8個小時\n# interactive_timeout 參數需要同時配置才能生效\ninteractive_timeout=28800\nwait_timeout=28800\n\n[mysql]\nno-auto-rehash\ndefault-character-set=utf8mb4\nsocket=/var/lib/mysql/mysql.sock\n\n[client]\ndefault-character-set=utf8mb4\nsocket=/var/lib/mysql/mysql.sock\nport=3306\n\n[mysqld_safe]\nlog-error=/logs/mysql.log\npid-file=/var/run/mysqld/mysqld.pid\nsocket=/var/lib/mysql/mysql.sock\n\n[mysqldump]\nquick\nsocket=/var/lib/mysql/mysql.sock\n\n[mysqladmin]\nsocket=/var/lib/mysql/mysql.sock" >> /etc/my.cnf
# 構建鏡像
docker build -t cehc/cehc-mysql-arm:8.0.19 .
其他操作內容,不再贅述。
以上內容均在北京四區鯤鵬服務器實測通過,供參考。
鯤鵬 軟件開發
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。