参考博客:Docker下安装Mysql-01

参考博客:Docker下安装Mysql-02

参考博客:Docker下安装Mysql-03

通过Docker拉取Mysql镜像

docker pull mysql:5.6

运行Docker下Mysql容器

mysql-5.6

docker run \
-d \
-p 3306:3306 \
--restart=always \
--privileged=true \
-v /home/docker/mysql/logs:/var/log/mysql \
-v /home/docker/mysql/conf:/etc/mysql/conf.d \
-v /home/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql_5_6 mysql:5.6 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_general_ci
  • -d 表示后台运行
  • -p 表示容器内部端口和服务器端口映射关联
  • –restart=always 重启docker时,自动启动相关容器
  • –privileged=true 设置root用户权限
  • -v 映射docker容器的目录到服务器目录

mysql-8.0

mysql-8.0默认是开启%远程访问

docker run \
-d \
-p 3307:3306 \
-v /home/docker/mysql_8_0/data:/var/lib/mysql \
-v /home/docker/mysql_8_0/conf/my.cnf:/etc/mysql/conf.d/my.cnf \
-e MYSQL_ROOT_PASSWORD=123456 \
--restart=always \
--privileged=true \
--name mysql_8_0 mysql \
--lower_case_table_names=1

配置文件my.cnf

[mysql]
#设置mysql客户端默认字符集
default-character-set=UTF8MB4
[mysqld]
#设置3306端口
port=3306
#允许最大连接数
max_connections=200
#允许连接失败的次数
max_connect_errors=10
#默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
#服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=UTF8MB4
#开启查询缓存
explicit_defaults_for_timestamp=true
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#等待超时时间秒
wait_timeout=60
#交互式连接超时时间秒
interactive-timeout=600
#大小写不敏感(注意:mysql8以后,大小写不敏感设置只能在容器初始化时候有效)
#lower_case_table_names=1
#设置时区
default-time-zone='+08:00'

配置Mysql远程访问

进入Docker-Mysql容器

docker exec -it 容器名称(mysql) bash

设置账号访问权限

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

刷新权限

flush privileges;

配置my.cnf文件

/home/docker/mysql/conf/目录下创建文件my.cnf

touch 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
default-time-zone = '+8:00'

#最大链接数
max_connections=1024

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names=1
log_bin_trust_function_creators=1
#启用log-bin
log-bin=mysql-bin

#设置日志格式
binlog_format=mixed

#设置binlog清理时间
expire_logs_days=7

# 数据表默认时区
default-time-zone='+08:00'

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

重启docker容器

docker restart 容器id