我的Mysql DockerCompose配置带my.cnf和自动定时备份

一鼓作气

把K8S拆了后,一鼓作气,MySQL也自建了一个,放放BLOG太实惠,就算本地用TCP不用套接字连,也比腾讯云的速度快太多。

docker-compose.yml

  • 一共三个Service,mysql和mysql-backup以及PhpMyAdmin的管理界面,其中PMA没有做端口映射,需要访问你可以自己用端口或者Traefik之类代理把服务暴露出来。
  • my.cnf的权限:因为MySQL官方的要求,你需要在windows系统下把文件选择只读然后保存,在linux下chmod 444 my.cnf来修改权限防止world writeable的问题。
  • mysql-backup的备份目录权限:因为mysql-backup的代码是以appuser的权限执行,所以你要么让容器以root的权限执行,要么和我一样给/backup目录777就可以了。
  • backup启动时间要有空格,源代码localtime$target_time=$(date --date="${today}${DB_DUMP_BEGIN}" +"%s")兼容性问题。
# 数据库配置 mysql+backup+phpmyadmin
version: '3.4'
services:
  mysql:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=111111
    volumes:
      - ./my.cnf:/etc/mysql/conf.d/my.cnf # 额外的配置文件
      - ./data:/var/lib/mysql # 挂载data文件
      - ./backup:/backup # 挂载备份目录
      - /etc/localtime:/etc/localtime:ro # 传时间过去
    networks:
      - mysql
  mysql-backup:
    image: databack/mysql-backup:latest
    restart: always
    depends_on:
      - mysql
    environment:
      - DB_SERVER=mysql
      - DB_PORT=3306
      - DB_USER=root
      - DB_PASS=111111
      - DB_DUMP_TARGET=/backup
      # 注意时间要有空格
      - DB_DUMP_BEGIN= 0345
      - DB_DUMP_FREQ=1440
      # 分schema备份
      - DB_DUMP_BY_SCHEMA=true
      - COMPRESSION=bzip2
      - NICE=true
    networks:
      - mysql
    volumes:
      - /etc/localtime:/etc/localtime:ro # 传时间过去
      - ./backup:/backup # 挂载备份目录
  phpmyadmin:
    image: phpmyadmin:latest
    restart: always
    depends_on:
      - mysql
    networks:
      - traefik
      - mysql
    environment:
      - MYSQL_ROOT_PASSWORD=111111 # root密码
      - PMA_HOST=mysql
      - UPLOAD_LIMIT=300M # 上传限制
      #- PMA_ARBITRARY=1 # 可以使用任意服务器
networks:
  traefik:
    external: true
  mysql:
    external: true

my.cnf

  • 这个配置差不多是针对2核4G内存服务器配置的一个优化
[client]
default-character-set=utf8mb4

[mysql]
default-character-set=utf8mb4

[mysqld]
datadir=/var/lib/mysql
server-id=1
skip_ssl
auto_increment_increment=1
auto_increment_offset=1
automatic_sp_privileges=ON
back_log=3000
log-bin=mysql-bin
binlog_cache_size=2097152
binlog_checksum=CRC32
binlog_format=ROW
binlog_order_commits=ON
binlog_row_image=FULL
binlog_rows_query_log_events=OFF
binlog_stmt_cache_size=32768
block_encryption_mode=AES-128-ECB
bulk_insert_buffer_size=8388608
character_set_filesystem=BINARY
character_set_server=utf8mb4
collation-server=utf8mb4_general_ci
concurrent_insert=AUTO
connect_timeout=10
default_password_lifetime=0
default_storage_engine=INNODB
default_week_format=0
delay_key_write=ON
delayed_insert_limit=100
delayed_insert_timeout=300
delayed_queue_size=1000
disconnect_on_expired_password=ON
div_precision_increment=4
end_markers_in_json=OFF
eq_range_index_dive_limit=200
event_scheduler=OFF
explicit_defaults_for_timestamp=OFF
flush_time=0
ft_max_word_len=84
ft_min_word_len=4
ft_query_expansion_limit=20
group_concat_max_len=1024
host_cache_size=644
init_connect=
innodb_adaptive_flushing=ON
innodb_adaptive_flushing_lwm=10
innodb_adaptive_hash_index=OFF
innodb_adaptive_max_sleep_delay=150000
innodb_autoextend_increment=64
innodb_autoinc_lock_mode=2
innodb_buffer_pool_dump_at_shutdown=ON
innodb_buffer_pool_dump_pct=25
innodb_buffer_pool_instances=1
innodb_buffer_pool_load_at_startup=ON
innodb_buffer_pool_size=805306368
innodb_change_buffer_max_size=25
innodb_change_buffering=ALL
innodb_checksum_algorithm=CRC32
innodb_cmp_per_index_enabled=OFF
innodb_commit_concurrency=0
innodb_compression_failure_threshold_pct=5
innodb_compression_level=6
innodb_compression_pad_pct_max=50
innodb_concurrency_tickets=5000
innodb_deadlock_detect=ON
innodb_default_row_format=DYNAMIC
innodb_disable_sort_file_cache=OFF
innodb_flush_log_at_trx_commit=1
innodb_flush_method=O_DIRECT
innodb_flush_neighbors=0
innodb_flush_sync=ON
innodb_ft_cache_size=8000000
innodb_ft_enable_diag_print=OFF
innodb_ft_enable_stopword=ON
innodb_ft_max_token_size=84
innodb_ft_min_token_size=3
innodb_ft_num_word_optimize=2000
innodb_ft_result_cache_limit=2000000000
innodb_ft_server_stopword_table=NULL
innodb_ft_sort_pll_degree=2
innodb_ft_total_cache_size=640000000
innodb_ft_user_stopword_table=NULL
innodb_io_capacity=20000
innodb_io_capacity_max=40000
innodb_large_prefix=ON
innodb_lock_wait_timeout=50
innodb_log_checksums=ON
innodb_log_compressed_pages=ON
innodb_lru_scan_depth=1024
innodb_max_dirty_pages_pct=75
innodb_max_dirty_pages_pct_lwm=0
innodb_max_purge_lag=0
innodb_max_purge_lag_delay=0
innodb_max_undo_log_size=1073741824
innodb_monitor_disable=ALL
innodb_monitor_enable=ALL
innodb_old_blocks_pct=37
innodb_old_blocks_time=1000
innodb_online_alter_log_max_size=134217728
innodb_optimize_fulltext_only=OFF
innodb_page_cleaners=4
innodb_print_all_deadlocks=ON
innodb_purge_batch_size=300
innodb_purge_rseg_truncate_frequency=128
innodb_purge_threads=4
innodb_random_read_ahead=OFF
innodb_read_ahead_threshold=56
innodb_read_io_threads=8
innodb_rollback_on_timeout=OFF
innodb_rollback_segments=128
innodb_sort_buffer_size=1048576
innodb_spin_wait_delay=6
innodb_stats_auto_recalc=ON
innodb_stats_method=NULLS_EQUAL
innodb_stats_on_metadata=OFF
innodb_stats_persistent=ON
innodb_stats_persistent_sample_pages=20
innodb_stats_transient_sample_pages=8
innodb_status_output=OFF
innodb_status_output_locks=OFF
innodb_strict_mode=ON
innodb_sync_array_size=1
innodb_sync_spin_loops=30
innodb_table_locks=ON
innodb_thread_concurrency=0
innodb_write_io_threads=8
interactive_timeout=7200
join_buffer_size=262144
key_cache_age_threshold=300
key_cache_block_size=1024
key_cache_division_limit=100
lc_time_names=EN_US
local_infile=OFF
lock_wait_timeout=31536000
log_output=FILE
log_queries_not_using_indexes=OFF
log_slow_admin_statements=OFF
log_throttle_queries_not_using_indexes=0
log_timestamps=SYSTEM
log_error_verbosity=3
long_query_time=1
low_priority_updates=OFF
lower_case_table_names=1
master_verify_checksum=OFF
max_allowed_packet=1073741824
max_connect_errors=999999999
max_connections=1000
max_error_count=64
max_heap_table_size=67108864
max_length_for_sort_data=1024
max_points_in_geometry=65536
max_prepared_stmt_count=16382
max_sort_length=1024
max_sp_recursion_depth=0
max_user_connections=0
min_examined_row_limit=0
myisam_sort_buffer_size=8388608
mysql_native_password_proxy_users=OFF
net_buffer_length=16384
net_read_timeout=30
net_retry_count=10
net_write_timeout=60
ngram_token_size=2
optimizer_prune_level=1
optimizer_search_depth=62
optimizer_switch=INDEX_MERGE=ON,INDEX_MERGE_UNION=ON,INDEX_MERGE_SORT_UNION=ON,INDEX_MERGE_INTERSECTION=ON,ENGINE_CONDITION_PUSHDOWN=ON,INDEX_CONDITION_PUSHDOWN=ON,MRR=ON,MRR_COST_BASED=ON,BLOCK_NESTED_LOOP=ON,BATCHED_KEY_ACCESS=OFF,MATERIALIZATION=ON,SEMIJOIN=ON,LOOSESCAN=ON,FIRSTMATCH=ON,DUPLICATEWEEDOUT=ON,SUBQUERY_MATERIALIZATION_COST_BASED=ON,USE_INDEX_EXTENSIONS=ON,CONDITION_FANOUT_FILTER=ON,DERIVED_MERGE=ON
optimizer_trace_limit=1
optimizer_trace_max_mem_size=16384
optimizer_trace_offset=-1
performance_schema=OFF
preload_buffer_size=32768
query_alloc_block_size=8192
query_cache_limit=1048576
query_cache_min_res_unit=4096
query_cache_size=0
query_cache_type=OFF
query_cache_wlock_invalidate=OFF
query_prealloc_size=8192
range_alloc_block_size=4096
range_optimizer_max_mem_size=8388608
read_buffer_size=262144
read_rnd_buffer_size=524288
session_track_gtids=OFF
session_track_schema=ON
session_track_state_change=OFF
sha256_password_proxy_users=OFF
show_compatibility_56=OFF
slave_net_timeout=120
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=0
slave_rows_search_algorithms=TABLE_SCAN,INDEX_SCAN
slow_launch_time=2
slow_query_log=ON
sort_buffer_size=868352
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
stored_program_cache=256
sync_binlog=1
table_definition_cache=1024
table_open_cache=1024
table_open_cache_instances=4
thread_cache_size=512
thread_handling=one-thread-per-connection
thread_stack=524288
tmp_table_size=209715200
transaction_alloc_block_size=8192
transaction_prealloc_size=4096
updatable_views_with_limit=YES
wait_timeout=3600

已发布

分类

作者: