docker使用mysql自定义镜像启动

Posted _雪辉_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker使用mysql自定义镜像启动相关的知识,希望对你有一定的参考价值。

一、mysql软件

解压mysql软件到镜像目录

/data/image/mysql

二、编辑mysql配置文件

/data/image/my.cnf
[client]
port    = 3306
socket  = /data/mysql/data/mysql.sock

[mysql]
prompt="\\u@mysql \\R:\\m:\\s [\\d]> "
no-auto-rehash

[mysqld]
user=mysql
port=3306
default_authentication_plugin=mysql_native_password
basedir=/usr/local/mysql
datadir=/data/mysql/data
tmpdir =/data/mysql/tmp
socket=/data/mysql/tmp/mysql.sock
innodb_doublewrite=1
pid-file=mysql.pid
character-set-server = utf8mb4
skip_name_resolve = 1
default_time_zone = "+8:00"
open_files_limit= 65535
back_log = 1024
max_connections = 256
max_user_connections = 64
max_connect_errors = 10000
autocommit = 1
table_open_cache = 1024
table_definition_cache = 1024
table_open_cache_instances = 4
thread_stack = 512K
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
innodb_sort_buffer_size = 64M
thread_cache_size = 384
interactive_timeout = 600
wait_timeout = 600
tmp_table_size = 32M
max_heap_table_size = 32M
slow_query_log = 1
log_timestamps = SYSTEM
slow_query_log_file = /data/mysql/logs/slow.log
log-error = /data/mysql/logs/error.log
long_query_time = 0.1
log_queries_not_using_indexes =1
log_throttle_queries_not_using_indexes = 60
min_examined_row_limit = 0
log_slow_admin_statements = 1
log_slow_slave_statements = 1
server-id = 33061
log-bin = /data/mysql/binlog/binlog
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 2G
max_binlog_size = 1G
auto_increment_offset=1
auto_increment_increment=1
expire_logs_days = 7
master_info_repository = TABLE
relay_log_info_repository = TABLE
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates
slave-rows-search-algorithms = 'INDEX_SCAN,HASH_SCAN'
binlog_format = row
relay_log = /data/mysql/binlog/relaylog
relay_log_recovery = 1
relay-log-purge = 1
key_buffer_size = 32M
read_buffer_size = 8M
read_rnd_buffer_size = 4M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
lock_wait_timeout = 3600
explicit_defaults_for_timestamp = 1
innodb_thread_concurrency = 0
innodb_sync_spin_loops = 100
innodb_spin_wait_delay = 30
transaction_isolation = READ-COMMITTED
innodb_buffer_pool_size = 2048M
innodb_buffer_pool_instances = 2
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_log_group_home_dir = /data/mysql/data/
innodb_data_file_path = ibdata1:1G:autoextend
innodb_temp_data_file_path=ibtmp1:500M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 32M
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_max_undo_log_size = 4G
innodb_undo_directory = /data/mysql/data
innodb_undo_tablespaces = 2
innodb_io_capacity = 4000
innodb_io_capacity_max = 8000
innodb_flush_sync = 0
innodb_flush_neighbors = 0
innodb_write_io_threads = 8
innodb_read_io_threads = 8
innodb_purge_threads = 4
innodb_page_cleaners = 4
innodb_open_files = 65535
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lru_scan_depth = 4000
innodb_checksum_algorithm = crc32
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
innodb_online_alter_log_max_size = 4G
innodb_stats_on_metadata = 0
innodb_undo_log_truncate = 1
slave_preserve_commit_order=1

log_error_verbosity = 3
innodb_print_ddl_logs = 1
binlog_expire_logs_seconds = 604800
innodb_status_file = 1
innodb_status_output = 0
innodb_status_output_locks = 0

performance_schema = 1
performance_schema_instrument = '%memory%=on'
performance_schema_instrument = '%lock%=on'

[mysqldump]
quick
max_allowed_packet = 32M

三、修改dockerfile

cd /data/image

echo '''FROM centos:7

COPY mysql /usr/local/mysql
COPY my.cnf /etc/my.cnf
RUN echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
RUN yum install -y libaio* numactl
RUN groupadd mysql && useradd mysql -g mysql && mkdir -p /data/mysql/{data,binlog,logs,tmp} && chown -R mysql.mysql /data/mysql
RUN /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize-insecure
ENTRYPOINT /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
'''> Dockerfile

四、构建镜像

docker build -t mysql8026 .

五、启动

docker run -itd --name mysql8.0.26 -p 33061:3306 mysql8026:latest /bin/bash
8ac398361b3bc429e458eb1da76f5542c77e3d08f1092e6d39c59c2ad22bb447

docker ps
coCONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                     NAMES
8ac398361b3b        mysql8026:latest    "/bin/sh -c '/usr/..."   7 seconds ago       Up 6 seconds        0.0.0.0:33061->3306/tcp   mysql8.0.26

docker exec -it 8ac398361b3b bash
[root@8ac398361b3b /]# /usr/local/mysql/bin/mysql -uroot -p -S /data/mysql/tmp/mysql.sock
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 9
Server version: 8.0.26 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

root@mysql 12:47:  [(none)]>
root@mysql 12:47:  [(none)]> ^DBye

六、导出镜像

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql8026           latest              11f3a1fbd8b1        6 minutes ago       7.75 GB
docker.io/centos    7                   eeb6ee3f44bd        3 weeks ago         204 MB
docker save -o ./mysql8026_image.tar mysql8026:latest

以上是关于docker使用mysql自定义镜像启动的主要内容,如果未能解决你的问题,请参考以下文章

基于官方镜像MySQL做自定义业务扩充镜像

docker自定义镜像

docker基于mysql:5.7构建镜像并初始化脚本

Docker学习之五Docker自定义镜像示例

五十四.自定义镜像及仓库持久化存储 Docker网络架构

44_自定义镜像及仓库持久化存储 Docker网络架构