02-003 Docker 啟動(dòng) Mysql

Mysql 5.7
docker run -d --restart=always --name mysql -p 3306:3306 \
--net=host \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
-v /opt/mysql/data:/var/lib/mysql \
-v /opt/mysql/conf.d:/etc/mysql/conf.d \
-v /opt/mysql/log:/var/log/mysq \
mysql:5.7 \
--default-authentication-plugin=mysql_native_password \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci \
--explicit_defaults_for_timestamp=true \
--lower_case_table_names=1 \
--max_allowed_packet=128M 
Mysql 8
mkdir -p  /opt/mysql/{data,log,config}
vim /opt/mysql/config/my.cnf

my.cnf

# 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

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

docker run -d --restart=always --name mysql -p 3306:3306 \
--net=host \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=123456 \
-e TZ=Asia/Shanghai \
-v /opt/mysql/data:/var/lib/mysql:rw \
-v /opt/mysql/log:/var/log/mysql:rw \
-v /opt/mysql/config/my.cnf:/etc/mysql/my.cnf:rw \
mysql:8 \
--default-authentication-plugin=mysql_native_password \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci \
--explicit_defaults_for_timestamp=true \
--lower_case_table_names=1 \
--max_allowed_packet=128M 
  1. 修改最大連接數(shù)
docker exec -it mysql /bin/bash
apt-get update && apt-get install -y vim
vim /etc/mysql/mysql.conf.d/mysqld.cnf
max_connections=2048
image.png
show variables like '%max_connections%';
image.png
  1. my.cnf 詳解
# MySql5.7配置文件my.ini設(shè)置
[client]
port = 3306
socket = /tmp/mysql.sock
 
[mysqld]
###############################基礎(chǔ)設(shè)置#####################################
#Mysql服務(wù)的唯一編號(hào) 每個(gè)mysql服務(wù)Id需唯一
server-id = 1
 
#服務(wù)端口號(hào) 默認(rèn)3306
port = 3306
 
#mysql安裝根目錄
basedir = /opt/mysql
 
#mysql數(shù)據(jù)文件所在位置
datadir = /opt/mysql/data
 
#臨時(shí)目錄 比如load data infile會(huì)用到
tmpdir  = /tmp
 
#設(shè)置socke文件所在目錄
socket  = /tmp/mysql.sock
 
#主要用于MyISAM存儲(chǔ)引擎,如果多臺(tái)服務(wù)器連接一個(gè)數(shù)據(jù)庫(kù)則建議注釋下面內(nèi)容
skip-external-locking
 
#只能用IP地址檢查客戶端的登錄,不用主機(jī)名
skip_name_resolve = 1
#數(shù)據(jù)庫(kù)默認(rèn)字符集,主流字符集支持一些特殊表情符號(hào)(特殊表情符占用4個(gè)字節(jié))
character-set-server = utf8mb4
 
#數(shù)據(jù)庫(kù)字符集對(duì)應(yīng)一些排序等規(guī)則,注意要和character-set-server對(duì)應(yīng)
collation-server = utf8mb4_general_ci
 
#設(shè)置client連接mysql時(shí)的字符集,防止亂碼
init_connect='SET NAMES utf8mb4'
 
#是否對(duì)sql語(yǔ)句大小寫敏感,1表示不敏感
lower_case_table_names = 1
 
#最大連接數(shù)
max_connections = 400
#最大錯(cuò)誤連接數(shù)
max_connect_errors = 1000
 
#TIMESTAMP如果沒有顯示聲明NOT NULL,允許NULL值
explicit_defaults_for_timestamp = true
 
#SQL數(shù)據(jù)包發(fā)送的大小,如果有BLOB對(duì)象建議修改成1G
max_allowed_packet = 128M
 
#MySQL連接閑置超過(guò)一定時(shí)間后(單位:秒)將會(huì)被強(qiáng)行關(guān)閉
#MySQL默認(rèn)的wait_timeout  值為8個(gè)小時(shí), interactive_timeout參數(shù)需要同時(shí)配置才能生效
interactive_timeout = 1800
wait_timeout = 1800
 
#內(nèi)部?jī)?nèi)存臨時(shí)表的最大值 ,設(shè)置成128M。
#比如大數(shù)據(jù)量的group by ,order by時(shí)可能用到臨時(shí)表,
#超過(guò)了這個(gè)值將寫入磁盤,系統(tǒng)IO壓力增大
tmp_table_size = 134217728
max_heap_table_size = 134217728


 
##----------------------------用戶進(jìn)程分配到的內(nèi)存設(shè)置BEGIN-----------------------------##
##每個(gè)session將會(huì)分配參數(shù)設(shè)置的內(nèi)存大小
#用于表的順序掃描,讀出的數(shù)據(jù)暫存于read_buffer_size中,當(dāng)buff滿時(shí)或讀完,將數(shù)據(jù)返回上層調(diào)用者
#一般在128kb ~ 256kb,用于MyISAM
#read_buffer_size = 131072
#用于表的隨機(jī)讀取,當(dāng)按照一個(gè)非索引字段排序讀取時(shí)會(huì)用到,
#一般在128kb ~ 256kb,用于MyISAM
#read_rnd_buffer_size = 262144
#order by或group by時(shí)用到
#建議先調(diào)整為2M,后期觀察調(diào)整
sort_buffer_size = 2097152
#一般數(shù)據(jù)庫(kù)中沒什么大的事務(wù),設(shè)成1~2M,默認(rèn)32kb
binlog_cache_size = 524288
 
############################日  志 設(shè)置##########################################
#數(shù)據(jù)庫(kù)錯(cuò)誤日志文件
log_error = error.log
 
#慢查詢sql日志設(shè)置
slow_query_log = 1
slow_query_log_file = slow.log
#檢查未使用到索引的sql
log_queries_not_using_indexes = 1
#針對(duì)log_queries_not_using_indexes開啟后,記錄慢sql的頻次、每分鐘記錄的條數(shù)
log_throttle_queries_not_using_indexes = 5
#作為從庫(kù)時(shí)生效,從庫(kù)復(fù)制中如何有慢sql也將被記錄
log_slow_slave_statements = 1
#慢查詢執(zhí)行的秒數(shù),必須達(dá)到此值可被記錄
long_query_time = 2
#檢索的行數(shù)必須達(dá)到此值才可被記為慢查詢
min_examined_row_limit = 100
 
#mysql binlog日志文件保存的過(guò)期時(shí)間,過(guò)期后自動(dòng)刪除
expire_logs_days = 5
 
############################主從復(fù)制 設(shè)置########################################
#開啟mysql binlog功能
log-bin=mysql-bin
#binlog記錄內(nèi)容的方式,記錄被操作的每一行
binlog_format = ROW

#作為從庫(kù)時(shí)生效,想進(jìn)行級(jí)聯(lián)復(fù)制,則需要此參數(shù)
log_slave_updates
 
#作為從庫(kù)時(shí)生效,中繼日志relay-log可以自我修復(fù)
relay_log_recovery = 1
 
#作為從庫(kù)時(shí)生效,主從復(fù)制時(shí)忽略的錯(cuò)誤
slave_skip_errors = ddl_exist_errors
 
##---redo log和binlog的關(guān)系設(shè)置BEGIN---##
#(步驟1) prepare dml相關(guān)的SQL操作,然后將redo log buff中的緩存持久化到磁盤
#(步驟2)如果前面prepare成功,那么再繼續(xù)將事務(wù)日志持久化到binlog
#(步驟3)如果前面成功,那么在redo log里面寫上一個(gè)commit記錄
#當(dāng)innodb_flush_log_at_trx_commit和sync_binlog都為1時(shí)是最安全的,
#在mysqld服務(wù)崩潰或者服務(wù)器主機(jī)crash的情況下,binary log只有可能丟失最多一個(gè)語(yǔ)句或者一個(gè)事務(wù)。
#但是都設(shè)置為1時(shí)會(huì)導(dǎo)致頻繁的io操作,因此該模式也是最慢的一種方式。
#當(dāng)innodb_flush_log_at_trx_commit設(shè)置為0,mysqld進(jìn)程的崩潰會(huì)導(dǎo)致上一秒鐘所有事務(wù)數(shù)據(jù)的丟失。
#當(dāng)innodb_flush_log_at_trx_commit設(shè)置為2,只有在操作系統(tǒng)崩潰或者系統(tǒng)掉電的情況下,上一秒鐘所有事務(wù)數(shù)據(jù)才可能丟失。
#commit事務(wù)時(shí),控制redo log buff持久化磁盤的模式 默認(rèn)為1
innodb_flush_log_at_trx_commit = 2
#commit事務(wù)時(shí),控制寫入mysql binlog日志的模式 默認(rèn)為 0
#innodb_flush_log_at_trx_commit和sync_binlog都為1時(shí),mysql最為安全但性能上壓力也是最大
sync_binlog = 1
##---redo log 和 binlog的關(guān)系設(shè)置END---##
 
############################Innodb設(shè)置##########################################
#數(shù)據(jù)塊的單位8k,默認(rèn)是16k,16kCPU壓力稍小,8k對(duì)select的吞吐量大
#innodb_page_size的參數(shù)值也影響最大索引長(zhǎng)度,8k比16k的最大索引長(zhǎng)度小
#innodb_page_size = 8192
#一般設(shè)置物理存儲(chǔ)的60% ~ 70%
innodb_buffer_pool_size = 1G
 
#5.7.6之后默認(rèn)16M
#innodb_log_buffer_size = 16777216
#該參數(shù)針對(duì)unix、linux,window上直接注釋該參數(shù).默認(rèn)值為NULL
#O_DIRECT減少操作系統(tǒng)級(jí)別VFS的緩存和Innodb本身的buffer緩存之間的沖突
innodb_flush_method = O_DIRECT
 
#此格式支持壓縮, 5.7.7之后為默認(rèn)值
innodb_file_format = Barracuda
 
#CPU多核處理能力設(shè)置,假設(shè)CPU是2顆4核的,設(shè)置如下
#讀多,寫少可以設(shè)成2:6的比例
innodb_write_io_threads = 4
innodb_read_io_threads = 4
 
#提高刷新臟頁(yè)數(shù)量和合并插入數(shù)量,改善磁盤I/O處理能力
#默認(rèn)值200(單位:頁(yè))
#可根據(jù)磁盤近期的IOPS確定該值
innodb_io_capacity = 500
 
#為了獲取被鎖定的資源最大等待時(shí)間,默認(rèn)50秒,超過(guò)該時(shí)間會(huì)報(bào)如下錯(cuò)誤:
# ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
innodb_lock_wait_timeout = 30
 
#調(diào)整buffer pool中最近使用的頁(yè)讀取并dump的百分比,通過(guò)設(shè)置該參數(shù)可以減少轉(zhuǎn)儲(chǔ)的page數(shù)
innodb_buffer_pool_dump_pct = 40
 
#設(shè)置redoLog文件所在目錄, redoLog記錄事務(wù)具體操作內(nèi)容
innodb_log_group_home_dir = /opt/mysql/redolog/
 
#設(shè)置undoLog文件所在目錄, undoLog用于事務(wù)回滾操作
innodb_undo_directory = /opt/mysql/undolog/
 
#在innodb_log_group_home_dir中的redoLog文件數(shù), redoLog文件內(nèi)容是循環(huán)覆蓋寫入。
innodb_log_files_in_group = 3
 
#MySql5.7官方建議盡量設(shè)置的大些,可以接近innodb_buffer_pool_size的大小
#之前設(shè)置該值較大時(shí)可能導(dǎo)致mysql宕機(jī)恢復(fù)時(shí)間過(guò)長(zhǎng),現(xiàn)在恢復(fù)已經(jīng)加快很多了
#該值減少臟數(shù)據(jù)刷新到磁盤的頻次
#最大值innodb_log_file_size * innodb_log_files_in_group <= 512GB,單文件<=256GB
innodb_log_file_size = 1024M
 
#設(shè)置undoLog文件所占空間可以回收
#5.7之前的MySql的undoLog文件一直增大無(wú)法回收
innodb_undo_log_truncate = 1
innodb_undo_tablespaces = 3
innodb_undo_logs = 128
 
#5.7.7默認(rèn)開啟該參數(shù) 控制單列索引長(zhǎng)度最大達(dá)到3072
#innodb_large_prefix = 1
 
#5.7.8默認(rèn)為4個(gè), Inodb后臺(tái)清理工作的線程數(shù)
#innodb_purge_threads = 4
 
#通過(guò)設(shè)置配置參數(shù)innodb_thread_concurrency來(lái)限制并發(fā)線程的數(shù)量,
#一旦執(zhí)行線程的數(shù)量達(dá)到這個(gè)限制,額外的線程在被放置到對(duì)隊(duì)列中之前,會(huì)睡眠數(shù)微秒,
#可以通過(guò)設(shè)定參數(shù)innodb_thread_sleep_delay來(lái)配置睡眠時(shí)間
#該值默認(rèn)為0,在官方doc上,對(duì)于innodb_thread_concurrency的使用,也給出了一些建議:
#(1)如果一個(gè)工作負(fù)載中,并發(fā)用戶線程的數(shù)量小于64,建議設(shè)置innodb_thread_concurrency=0;
#(2)如果工作負(fù)載一直較為嚴(yán)重甚至偶爾達(dá)到頂峰,建議先設(shè)置innodb_thread_concurrency=128,
###并通過(guò)不斷的降低這個(gè)參數(shù),96, 80, 64等等,直到發(fā)現(xiàn)能夠提供最佳性能的線程數(shù)
#innodb_thread_concurrency = 0
############################其他內(nèi)容 設(shè)置##########################################
[mysqldump]
quick
max_allowed_packet = 128M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 256k
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
#增加每個(gè)進(jìn)程的可打開文件數(shù)量.
open-files-limit = 28192
# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32M - 64M) where MySQL plays
# an important part, or systems up to 128M where MySQL is used together with
# other programs (such as a web server)
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysqld.sock
[mysql]
default-character-set=utf8

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character_set_server=utf8
#bind-address=
port            = 3306
socket          = /tmp/mysqld.sock
tmpdir=/var/lib/docker/overlay2/data/mytmp
datadir=/var/lib/docker/overlay2/data/mysql
log_timestamps=SYSTEM
pid_file=/var/lib/docker/overlay2/data/mysql/mysqld_pid.pid

log_error=/var/lib/docker/overlay2/data/mysql/err_log.log

#skip-grant-tables

#避免Mysql的外部鎖定,減少出錯(cuò)幾率,增強(qiáng)穩(wěn)定性。
skip-external-locking

#禁止MySql對(duì)外部連接進(jìn)行DNS解析
skip-name-resolve

#skip-grant-tables
#innodb_force_recovery = 4

explicit_defaults_for_timestamp=true

#back_log參數(shù)的值指出在MySQL暫時(shí)停止響應(yīng)新請(qǐng)求之前,短時(shí)間內(nèi)的多少個(gè)請(qǐng)求可以被存在對(duì)堆棧中 推薦設(shè)置為小于512的整數(shù)
back_log=256


#最大連接錯(cuò)誤次數(shù),可適當(dāng)加大,防止頻繁連接錯(cuò)誤后,前端host被mysql拒絕掉
max_connect_errors = 100000
#設(shè)置最大并發(fā)連接數(shù)
max_connections=10000
#修改同一時(shí)間在mysqld上所有session中prepared 語(yǔ)句的上限
max_prepared_stmt_count=124000
#設(shè)置慢查詢閥值,建議設(shè)置最小的 1 秒
long_query_time = 2
slow_query_log = 1
slow_query_log_file = /var/lib/docker/overlay2/data/mysql/localhost-slow.log 

#設(shè)置臨時(shí)表最大值,這是每次連接都會(huì)分配,不宜設(shè)置過(guò)大 max_heap_table_size 和 tmp_table_size 要設(shè)置一樣大
max_heap_table_size = 128M
tmp_table_size = 128M

#每個(gè)連接都會(huì)分配的一些排序、連接等緩沖,一般設(shè)置為 2MB 就足夠了
sort_buffer_size = 2M
join_buffer_size = 2M
read_buffer_size = 4M
read_rnd_buffer_size = 4M

#建議關(guān)閉query cache,有些時(shí)候?qū)π阅芊炊且环N損害
query_cache_size = 0
query_cache_type = 0

#如果是以InnoDB引擎為主的DB,專用于MyISAM引擎的 key_buffer_size 可以設(shè)置較小,8MB 已足夠
#如果是以MyISAM引擎為主,可設(shè)置較大,但不能超過(guò)4G
#在這里,強(qiáng)烈建議不使用MyISAM引擎,默認(rèn)都是用InnoDB引擎
key_buffer_size = 1G
myisam_sort_buffer_size = 8M

# MyISAM表發(fā)生變化時(shí)重新排序所需的緩沖
myisam_sort_buffer_size = 512M

#批量插入數(shù)據(jù)緩存大小,可以有效提高插入效率,默認(rèn)為8M
bulk_insert_buffer_size = 64M

# MySQL重建索引時(shí)所允許的最大臨時(shí)文件的大小 (當(dāng) REPAIR, ALTER TABLE 或者 LOAD DATA INFILE).
# 如果文件大小比此值更大,索引會(huì)通過(guò)鍵值緩沖創(chuàng)建(更慢)
myisam_max_sort_file_size = 10G

# 如果一個(gè)表?yè)碛谐^(guò)一個(gè)索引, MyISAM 可以通過(guò)并行排序使用超過(guò)一個(gè)線程去修復(fù)他們.
# 這對(duì)于擁有多個(gè)CPU以及大量?jī)?nèi)存情況的用戶,是一個(gè)很好的選擇.
myisam_repair_threads = 1

table_open_cache = 64000
net_buffer_length = 8K

interactive_timeout=28800
wait_timeout=28800


# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
# 
#skip-networking

# Replication Master Server (default)
# binary logging is required for replication

# binary logging format - mixed recommended
binlog_format=ROW

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted

server-id       = 86

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/docker/overlay2/data
innodb_data_file_path = ibdata1:1G:autoextend
#innodb_log_group_home_dir = /var/lib/docker/overlay2/data/mysql

log-bin = master-bin
log-bin-index = master-bin.index

relay-log = relay-bin
relay-log-index = relay-bin.index


expire_logs_days = 30
binlog_cache_size = 4M
binlog-ignore-db = test
#binlog-ignore-db = mysql 
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema

log-slave-updates
#slave-skip-errors=all
#非同步方式 
sync_binlog=0 


# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high

#InnoDB用于緩存數(shù)據(jù)、索引、鎖、插入緩沖、數(shù)據(jù)字典等
#如果是專用的DB服務(wù)器,且以InnoDB引擎為主的場(chǎng)景,通??稍O(shè)置物理內(nèi)存的50%
#如果是非專用DB服務(wù)器,可以先嘗試設(shè)置成內(nèi)存的1/4,如果有問(wèn)題再調(diào)整
#默認(rèn)值是8M,非??覺,這也是導(dǎo)致很多人覺得InnoDB不如MyISAM好用的緣故
innodb_buffer_pool_size =2G

#innodb_buffer_pool_instances=2

#InnoDB存儲(chǔ)數(shù)據(jù)字典、內(nèi)部數(shù)據(jù)結(jié)構(gòu)的緩沖池,16MB 已經(jīng)足夠大了。
#innodb_additional_mem_pool_size = 16M

#如果不了解本選項(xiàng),建議設(shè)置為1,能較好保護(hù)數(shù)據(jù)可靠性,對(duì)性能有一定影響,但可控
innodb_flush_log_at_trx_commit = 0

# Set .._log_file_size to 25 % of buffer pool size
#innodb_lock_wait_timeout = 50

#InnoDB的log buffer,通常設(shè)置為 64MB 就足夠了
innodb_log_buffer_size = 64M

#InnoDB redo log大小,通常設(shè)置256MB 就足夠了
innodb_log_file_size = 1G

#InnoDB redo log文件組,通常設(shè)置為 2 就足夠了
innodb_log_files_in_group = 2

#啟用InnoDB的獨(dú)立表空間模式,便于管理
innodb_file_per_table = 1

#啟用InnoDB的status file,便于管理員查看以及監(jiān)控等
innodb_status_file = 1

#設(shè)置事務(wù)隔離級(jí)別為 READ-COMMITED,提高事務(wù)效率,通常都滿足事務(wù)一致性要求
transaction_isolation = READ-COMMITTED 

#innodb線程處理數(shù)該參數(shù)取值為服務(wù)器邏輯CPU數(shù)量*2
innodb_thread_concurrency=8

#innodb刷新方式
innodb_flush_method=O_DIRECT


[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 64M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容