目录
手动配置.cnf文件,启动时必须先上传配置文件,创建相应挂载的数据文件、日志文件、配置文件

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。
MySQL(5.7.19)安装
最新官方MySQL(5.7.19)的docker镜像在创建时映射的配置文件目录有所不同,在此记录并分享给大家:
官方原文:
The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.
大概意思是说:
MySQL(5.7.19)的默认配置文件是 /etc/mysql/my.cnf 文件。如果想要自定义配置,建议向 /etc/mysql/conf.d 目录中创建 .cnf 文件。新建的文件可以任意起名,只要保证后缀名是 cnf 即可。新建的文件中的配置项可以覆盖 /etc/mysql/my.cnf 中的配置项。
具体操作:
首先需要创建将要映射到容器中的目录以及.cnf文件,然后再创建容器
mkdir -p docker_v/mysql/conf
cd docker_v/mysql/conf
touch my.cnf
docker run -p 3306:3306 --name mysql -v /opt/docker_v/mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d imageID
4ec4f56455ea2d6d7251a05b7f308e314051fdad2c26bf3d0f27a9b0c0a71414
!NOTE
参数说明:
-
-p 3306:3306:**将容器的
3306端口映射到主机的3306端口 -
-v /opt/docker_v/mysql/conf:/etc/mysql/conf.d:**将主机/opt/docker_v/mysql/conf目录挂载到容器的/etc/mysql/conf.d
-
-e MYSQL_ROOT_PASSWORD=123456:**初始化root用户的密码
-
-d imageID: 后台运行容器,imageID: mysql镜像ID
查看容器运行情况
docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
4ec4f56455ea c73c7527c03a "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mysql
MySQL(5.7.19)安装高级写法
下载MySQL5.7的docker镜像:
docker pull mysql:5.7
文件夹创建的过程忽略...
使用如下命令启动MySQL服务:
docker run -p 3306:3306 --name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-d mysql:5.7
--lower-case-table-names=1 \
!NOTE
参数说明:
-p 3306:3306:将容器的
3306端口映射到主机的3306端口-e MYSQL_ROOT_PASSWORD=root: #初始化root用户的密码
-v /mydata/mysql/conf:/etc/mysql: #将配置文件夹挂在到主机
-v /mydata/mysql/log:/var/log/mysql: #将日志文件夹挂载到主机
-v /mydata/mysql/data:/var/lib/mysql/: #将数据文件夹挂载到主机
--lower-case-table-names=1#忽略数据表名大小写
!WARNING
注意:
--lower-case-table-names=1
该属性只有初始化构建时才生效,my.cnf无法覆盖,如果没有设置的话,需要重新配置mysql才可以且记要备份挂载的数据文件,否则会死的很惨
进入运行MySQL的docker容器:
docker exec -it mysql /bin/bash
使用MySQL命令打开客户端:
mysql -uroot -proot --default-character-set=utf8
创建一个账号并授予远程登录权限,使得任何ip都能访问:
CREATE USER 'reader'@'%' IDENTIFIED WITH mysql_native_password BY 'Lzslov123!';
GRANT ALL PRIVILEGES ON *.* TO 'reader'@'%';
flush privileges;
MySql8安装
下载 、简单运行
docker 中下载 mysql
docker pull mysql
查看mysql镜像
docker images mysql
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8 3218b38490ce 22 months ago 516MB
#启动
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=your_pwd -d mysql
无需手动配置.cnf文件,启动时设置相应的参数和挂载文件
docker run -p 3306:3306 --name test_mysql \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=root \
-v /mnt/data2/mysql/data:/var/lib/mysql \
-v /mnt/data2/mysql/log:/var/log/mysql \
-d mysql:8 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci \
--lower-case-table-names=1
!NOTE
参数说明:
--privileged=true#启动时拥有root 相关的特权,注意位置不用放到 mysql:8 的后面
lower-case-table-names#忽略数据表明大小写, 只能首次初始化设置有效,如果你首次没有加此参数,第二次加的话会报错并且 my.cnf无法覆盖,解决方式:需要重新配置mysql,删除原有的数据库挂载目录且要备份挂载的数据文件,否则会死的很惨
-e MYSQL_ROOT_PASSWORD=<root_password> # 初始化root用户的密码
-v /mnt/data2/mysql/data:/var/lib/mysql# 挂载数据目录
-v /mnt/data2/mysql/log:/var/log/mysql# 挂载日志目录
--character-set-server=utf8mb4#设置服务器的字符集
--collation-server=utf8mb4_0900_ai_ci# 设置服务器的排序规则
手动配置.cnf文件,启动时必须先上传配置文件,创建相应挂载的数据文件、日志文件、配置文件
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
官网:配置文件命令索引目录 8.1是对应的版本号,切换不同版本号就可以找对应的命令索引目录
https://dev.mysql.com/doc/refman/8.1/en/server-option-variable-reference.html
Custom config should go here
MySQL Server 配置参数
mysqld
设置端口号,默认为 3306
port = 3306
设置 MySQL 数据文件存储路径,默认值为mysqld安装目录 linux 通常为 `/var/lib/mysql`
datadir = /var/lib/mysql
允许最大连接数 默认值 Default Value=151, Minimum Value = 1, Maximum Value = 100000
max_connections=5000
服务器等待非交互式连接的时间 默认值 28800
Default Value 默认值 28800
Minimum Value 最小值 1
Maximum Value (Windows) 最大值(视窗) 2147483
Maximum Value (Other) 最大值(其他) 31536000
Unit 单位 seconds 秒
wait_timeout=28800
客户端和服务器之间传输的最大数据包大小、默认为 64M, 最大值为1G
max_allowed_packet=64M
是否将表名视为大小写敏感 0:区分、1:不区分、2:表示表名根据操作系统的规则进行比较
默认值
Default Value (macOS) 2
Default Value (Unix) 0
Default Value (Windows) 1
lower_case_table_names=1
服务器的字符集和排序规则 默认值 utf8mb4_0900_ai_ci
collation_server=utf8mb4_0900_ai_ci
服务器的字符集 默认值 utf8mb4
character_set_server=utf8mb4
是一个 MySQL 启动选项,它使得 MySQL 服务器在启动时跳过权限验证机制,允许所有用户以超级管理员的身份(root)登录,无需提供密码 默认值 OFF(关闭)、开启时放开注释即可
适用于Mysql直接安装、docker 安装Mysql 是有初始化密码设置的无需担心
skip-grant-tables
用于禁用二进制日志记录(binary logging)功能。二进制日志记录是 MySQL 的一项重要功能,用于记录数据库中所做的更改操作,以便进行数据恢复、数据复制和高可用性配置等。
默认是开启的,命令如下 开启:log-bin = mysql-bin, 关闭: skip-log-bin/disable-log-bin
见官网 https://dev.mysql.com/doc/refman/8.1/en/replication-options-binary-log.html#sysvar_log_bin
skip-log-bin
用于设置二进制日志文件(log-bin 备份产生的文件)过期的天数。当二进制日志文件的生成时间超过指定的天数时,MySQL 将自动清理(删除)旧的二进制日志文件,以释放磁盘空间。
默认值是 0天 一般配合log-bin使用
expire_logs_days = 2
warning & error log
log-warnings = 1
log-error = /home/mysql/mysql/log/mysql.err
用于设置日志输出的方式,默认为FILE,你可以将它设为TABLE,然后就可以查询mysql架构下的slow_log表了
具体选项值如下:
FILE:将日志输出到文件中。
TABLE:将日志输出到系统日志表中(mysql.general_log 或 mysql.slow_log)。
NONE:不输出日志。
log_output = FILE
是否开启慢查询 默认是关闭的 值说明 1(or ON): 开启, 0(or OFF): 关闭
slow_query_log = 1
慢查询的日志文件 默认文件名:host_name-slow.log,注意:log_output = FILE 才会用到当前选项!
slow_query_log_file = /home/mysql/mysql/log/slow.log
慢查询时间 超过1秒则为慢查询, 默认值 10
long-query-time = 5
用于禁用 MySQL 主机名缓存功能。当你使用该选项启动 MySQL 服务器时,MySQL 将不会将主机名存储在缓存中
默认情况下,MySQL 会缓存主机名,这样可以提高连接性能,减少对 DNS 查询的依赖。但在某些情况下,你可能希望禁用主机名缓存,以实时地获取主机名的最新解析结果
要使用 skip-host-cache 参数,在 my.cnf 配置文件或启动命令中添加以下行:skip-host-cache 或通过在命令行启动 MySQL 服务器时指定该参数:mysqld --skip-host-cache
skip-host-cache
mysql
更多命令配置详见 https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html
MySQL 支持以上选项,这些选项可以在命令行或选项文件的 mysql AND client 组中指定,只需去掉前面的 --
设置mysql客户端默认字符集
default-character-set=utf8mb4
本地客户端登录mysql的用户名
user=root
防止本地客户端输入密码后出现警告Warning Using a password on the command line interface can be insecure.
password=yourpassword
mysqldump
更多命令配置详见 https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
MySQL 支持以上选项,这些选项可以在命令行或选项文件的 mysql AND client 组中指定,只需去掉前面的 --
设置mysql客户端默认字符集
default-character-set=utf8mb4
设置备份时mysql输入的密码,防止出现 Warning Using a password on the command line interface can be insecure
password=yourpassword
client
更多命令配置详见 https://dev.mysql.com/doc/refman/8.0/en/mysql-command-options.html
MySQL 支持以上选项,这些选项可以在命令行或选项文件的 mysql AND client 组中指定,只需去掉前面的 --
port=3306
default-character-set=utf8mb4
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
https://dev.mysql.com/doc/refman/8.1/en/server-option-variable-reference.html
Custom config should go here
mysqld
wait_timeout=1814400
skip-log-bin
max_allowed_packet=64M
lower_case_table_names=1
max_connections=10000
collation_server=utf8mb4_0900_ai_ci
character_set_server=utf8mb4
mysql
default-character-set=utf8mb4
mysqldump
default-character-set=utf8mb4
password=your password
client
port=3306
default-character-set=utf8mb4
-
设置忽略表名大小写
-
关闭了binlog日志
-
设置了一些相关的字符集
docker启动创建mysql容器
docker run -p 3306:3306 --name test_mysql \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=root \
-v /mnt/data2/mysql/data:/var/lib/mysql \
-v /mnt/data2/mysql/conf.d:/etc/mysql/conf.d \
-v /mnt/data2/mysql/log:/var/log/mysql \
-d mysql:8
!NOTE
参数说明:
--privileged=true 启动时拥有root 相关的特权,注意位置不用放到 mysql:8 的后面
lower-case-table-names 忽略数据表明大小写, 只能首次初始化设置有效,如果你首次没有加此参数,第二次加的话会报错并且 my.cnf无法覆盖,解决方式:需要重新配置mysql,删除原有的数据库挂载目录且要备份挂载的数据文件,否则会死的很惨
-e MYSQL_ROOT_PASSWORD=<root_password> # 初始化root用户的密码
-v /mnt/data2/mysql/data:/var/lib/mysql # 挂载数据目录
-v /mnt/data2/mysql/conf.d:/etc/mysql/conf.d # 挂载配置文件目录
-v /mnt/data2/mysql/log:/var/log/mysql # 挂载日志目录
docker同镜像安装多mysql服务
只需设置 容器名字 和 主机映射端口不同即可启动多个mysql服务,具体如下:
docker run -p 33306:3306 --name test_mysql \
--privileged=true \
-e MYSQL_ROOT_PASSWORD=root \
-v /mnt/data2/mysql/data:/var/lib/mysql \
-v /mnt/data2/mysql/conf.d:/etc/mysql/conf.d \
-v /mnt/data2/mysql/log:/var/log/mysql \
-d mysql:8
安装、参数详解略
初始化后修改root密码
#进入mysql容器
docker exec -it mysql bash
#登录mysql
mysql -uroot -p'root'
#查询用户相关信息
select user, host, authentication_string from user;
#修改 root 密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'youpassord';
exit;
#重新登录
mysql -uroot -p'youpassord'
授权用户远程登录的权限
#进入mysql容器
docker exec -it mysql bash
#登录mysql
mysql -u root -p'youpassord'
#授予root远程登录权限
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;
#添加一个新的用户,并授予远程登录的权限
CREATE USER 'tuonioooo'@'%' IDENTIFIED WITH mysql_native_password BY 'Lzslov123!';
GRANT ALL PRIVILEGES ON *.* TO 'tuonioooo'@'%';
flush privileges;
如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。