前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Docker Compose Mysql

Docker Compose Mysql

作者头像
李郑
发布2019-12-04 21:12:06
4.6K0
发布2019-12-04 21:12:06
举报

因开发需要,安装 Mysql 环境并使用 Docker Compose 容器化 Just lookup in github - raphaelli/Dockerfiles/mysql/

Mysql 的 Docker Compose 安装

拉取镜像:

docker pull mysql:5.7

docker images | grep mysql # 查看镜像是否已经安装了
配置docker-compose 文件

创建需要的目录:

mkdir -p /docker/mysql/data    #数据存储目录
mkdir -p /docker/mysql/conf    #配置目录

新建目录后,将你需要的mysql配置文件 mymysqld.cnf 放到conf目录里:

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

[mysqld_safe]
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
#
# * Basic Settings
#
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
skip-external-locking
#
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address		= 0.0.0.0
#
# * Fine Tuning
#
key_buffer_size		= 16M
max_allowed_packet	= 16M
thread_stack		= 192K
thread_cache_size       = 8
# This replaces the startup script and checks MyISAM tables if needed
# the first time they are touched
myisam-recover-options  = BACKUP
#max_connections        = 100
#table_cache            = 64
#thread_concurrency     = 10
#
# * Query Cache Configuration
#
query_cache_limit	= 1M
query_cache_size        = 16M
#
# * Logging and Replication
#
# Both location gets rotated by the cronjob.
# Be aware that this log type is a performance killer.
# As of 5.1 you can enable the log at runtime!
#general_log_file        = /var/log/mysql/mysql.log
#general_log             = 1
#
# Error log - should be very few entries.
#
log_error = /var/log/mysql/error.log
#
# Here you can see queries with especially long duration
#log_slow_queries	= /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes
#
# The following can be used as easy to replay backup logs or for replication.
# note: if you are setting up a replication slave, see README.Debian about
#       other settings you may need to change.
#server-id		= 1
#log_bin			= /var/log/mysql/mysql-bin.log
expire_logs_days	= 10
max_binlog_size   = 100M
#binlog_do_db		= include_database_name
#binlog_ignore_db	= include_database_name
#
# * InnoDB
#
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.
# Read the manual for more InnoDB related options. There are many!
#
# * Security Features
#
# Read the manual, too, if you want chroot!
# chroot = /var/lib/mysql/
#
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
#
# ssl-ca=/etc/mysql/cacert.pem
# ssl-cert=/etc/mysql/server-cert.pem
# ssl-key=/etc/mysql/server-key.pem

上面的配置是默认的配置, 只修改了 bind-address = 0.0.0.0, 用于远程链接。

编写docker-compose 文件docker-compose.yml:

version: '3'
services:
  mysql_compose:
    image: mysql:5.7
    container_name: mysql_compose
    ports:
      - 6606:3306
    volumes:
      - /docker/mysql/data:/var/lib/mysql
      - /docker/mysql/conf/mymysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf
    environment:
      MYSQL_ROOT_PASSWORD: "123456"

注意: 端口(ports) 和 卷(volumes) 的配置。以及 root 密码!

Docker使用-p选项允许容器上的端口映射到主机上的端口。如果你如上所述启动容器(6606:3306),你可以通过将客户机连接到主机上的端口(6606)来连接到数据库。

运行mysql image

进入到上面编写的docker-compose.yml文件的目录,运行:

docker-compose up -d

开机自动运行

找到/etc/rc.local文件(exit 0)之前添加,添加以下脚本

/usr/local/bin/docker-compose -f /docker/mysql/docker-compose.yml up -d

其中-f参数是指定docker-compose.yml文件的参数 设置完以后,重启操作系统,耐心等待一会,就可以看到对应的docker容器都启动起来了

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Mysql 的 Docker Compose 安装
    • 运行mysql image
    • 开机自动运行
    相关产品与服务
    容器镜像服务
    容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档