Docker 安裝 MySQL5.6


方法一、docker pull mysql
查找Docker Hub上的mysql鏡像

#docker search mysql

這里我們拉取官方的鏡像,標簽為5.6

#docker pull mysql:5.6

 (第一次啟動Docker-MySql主要是查看Docker里面MySQL的默認配置,數據位置,日志位置,配置文件位置)

#docker run -it --entrypoint /bin/bash --rm mysql:5.6 (創建並進入容器里,方便查看容器里面的默認設置,--rm參數表示退出容器會自動刪除當前容器)
#cat /etc/mysql/mysql.cnf(查看默認配置文件)

 

 

(第二次啟動Docker-MySql)

#docker run --detach \
    --publish 13306:3306 \ --restart always \ --volume /data/mysql/conf:/etc/mysql/conf.d \ --volume /data/mysql/mysqldata:/var/lib/mysql \ --volume /data/mysql/backup:/data/backup \ --volume /etc/localtime:/etc/localtime \ --env MYSQL_ROOT_PASSWORD=1234 \ mysql:5.6 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

停止容器,修改配置文件

#docker ps
#docker stop 4b6967498e1b

 增加自定義配置文件,

vim /data/mysql/conf/my.cnf

my.cnf文件內容

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[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

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

[mysqld]
port=3306  # 指定MsSQL偵聽的端口
socket=/var/lib/mysql/mysql3306.sock  # 為MySQL客戶程序與服務器之間的本地通信指定一個套接字文件(Linux下默認是/var/lib/mysql/mysql.sock文件)
server_id=1
pid_file=/var/lib/mysql/mysql3306.pid
local_infile=1                                                     #開啟Load 
tmpdir=/tmp
datadir=/var/lib/mysql
back_log=250                                                       #max_connec外的連接數
#skip-grant-tables
max_connections=1024
#wait_timeout=86400
wait_timeout=300                                                   #非交互鏈接超時時間
#interactive_timeout= 1800
interactive_timeout= 300                                           #交互鏈接超時時間
max_connect_errors=10000
group_concat_max_len=2048                                          #字符串鏈接操作長度限制
open_files_limit=8192
skip_external_locking
skip_name_resolve
#skip_networking   
max_allowed_packet=32M                                             #server接受數據包大小調整
#thread_cache_size=64
thread_cache_size=20
thread_stack=192K
transaction-isolation = REPEATABLE-READ
#tmp_table_size=128M
tmp_table_size=256M                                                  #臨時表空間大小
#max_tmp_tables=100
#max_prepared_stmt_count=1048576
lower_case_table_names=1
log_bin_trust_function_creators=1
########log  
log_error=/var/lib/mysql/mysql3306.err
slow_query_log=1                                                    #開啟慢查詢
long_query_time=1                                                   #單位是秒
slow_query_log_file=/var/lib/mysql/mysql_slow.log
log_bin=mysqlmaster-bin.log
binlog_cache_size=8M
binlog_format=MIXED
max_binlog_cache_size=1024M
max_binlog_size=1024M
sync_binlog=1
expire_logs_days=15

##########內存,優化,查詢
#key_buffer_size=235M
key_buffer_size=512M                                                ##索引緩沖區大小
#read_buffer_size=2M
read_buffer_size=8M                                                 ##讀buffer
read_rnd_buffer_size=16M
#join_buffer_size=2M
join_buffer_size=16M                                                ##join buffer大小
sort_buffer_size=16M                                                ##排序內存大小,基於鏈接,值過大會消耗大量內存,性能下降

max_heap_table_size=123M
#query_cache_min_res_unit=2K
#query_cache_limit=2M
#query_cache_size=410M
#query_cache_type=1                                                 #查詢緩存0是關,1是開,會消耗性能

##############MYISAM
bulk_insert_buffer_size=64M
myisam_sort_buffer_size=128M
myisam_max_sort_file_size=10G
myisam_repair_threads=1
myisam_recover

##########language
init_connect='SET NAMES utf8'
#########innodb
innodb_file_per_table=1
#innodb_open_file=1678
innodb_open_files=65535                                              #innodb打開文件數
innodb_purge_threads=0
innodb_purge_batch_size=20
innodb_io_capacity=200
innodb_adaptive_flushing=on
innodb_change_buffering=all
innodb_stats_on_metadata=off
innodb_spin_wait_delay=6
innodb_buffer_pool_instances=12
#innodb_open_files=1024            #重復,注釋
innodb_additional_mem_pool_size=16M
innodb_buffer_pool_size=24000M                                      #innodb buffer
innodb_sort_buffer_size=4M
innodb_data_file_path=ibdata1:12M:autoextend
innodb_autoextend_increment=8M
innodb_read_io_threads=16
innodb_write_io_threads=16
innodb_thread_concurrency=0
innodb_replication_delay=0
innodb_flush_log_at_trx_commit=2
#innodb_log_buffer_size=256M
innodb_log_buffer_size=32M                                           #inondb日志緩沖大小
innodb_log_file_size=256M
#innodb_log_file_size=1024M                                          #innodb日志大小   修改此值數據庫無法啟動
innodb_log_files_in_group=2
innodb_max_dirty_pages_pct=85
#innodb_log_group_home_dir=
innodb_lock_wait_timeout=300                                         #innodb事物鎖時間
table_open_cache=16384
#table_definiton_cache=16384
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no_auto_rehash
# 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 

#sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,NO_AUTO_VALUE_ON_ZERO
my.cnf

重新啟動容器

#docker start 4b6967498e1b

進入容器,修改默認root用戶的Host為%,否則在容器外面登錄不進去mysql數據庫。

#docker exec -it 4b6967498e1b /bin/bash

重啟MySql服務

#service mysql restart

宿主執行MySQL備份

#docker exec df687bae1933 sh -c '/data/backup/mysqldump.sh'

宿主定時任務

#crontab -e
00 6 * * * /usr/bin/docker exec df687bae1933 sh -c '/data/backup/mysqldump.sh'

 

 1 #!/bin/sh
 2 find /data/backup/mysqldump -mtime +2  -exec rm -rf {} \;
 3 DATE=`date +%Y%m%d_%H%M%S`
 4 TAR_FILE_PATH="/data/backup/mysqldump/$DATE"
 5 if [ ! -f "$TAR_FILE_PATH" ]; then  
 6    mkdir -p  "$TAR_FILE_PATH"  
 7 fi  
 8 
 9 #備份結構
10 /usr/bin/mysqldump -uroot -p1234 -P3306 -d -q program_shopcart 2>>/data/backup/mysql_backup.log | gzip 1>$TAR_FILE_PATH/program_shopcart_d.sql.gz
11 /usr/bin/mysqldump -uroot -p1234 -P3306 -d -q program_website 2>>/data/backup/mysql_backup.log | gzip 1>$TAR_FILE_PATH/program_website_d.sql.gz
12 
13 
14 #備份數據
15 /usr/bin/mysqldump -uroot -p1234 -P3306 --default-character-set=utf8mb4 -R -q --master-data=2 --hex-blob --single-transaction program_shopcart 2>>/data/backup/mysql_backup.log | gzip 1>$TAR_FILE_PATH/program_shopcart.sql.gz
16 /usr/bin/mysqldump -uroot -p1234 -P3306 --default-character-set=utf8mb4 -R -q --master-data=2 --hex-blob --single-transaction program_website 2>>/data/backup/mysql_backup.log | gzip 1>$TAR_FILE_PATH/program_website.sql.gz
mysqldump.sh

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM