1. docker run -d \
  2. -p 3306:3306 \
  3. --name mysql \
  4. -e MYSQL_ROOT_PASSWORD=admin123 \
  5. -v /opt/mysql/datadir:/var/lib/mysql \
  6. -v /opt/mysql/mysql.conf.d:/etc/mysql/mysql.conf.d \
  7. mysql:5.7
  • 在宿主机中创建目录 /opt/mysql/mysql.conf.d,该目录中存储的是 MySQL 配置文件,用于替换容器内的默认配置。 ```bash $ ls mysql.conf.d/

mysqld.cnf

  1. - `mysqld.cnf` 就是普通的配置文件,也可以直接使用 `Docker` `copy` 命令将容器默认的配置文件(`/var/lib/mysql`)拷贝出来进行适当修改即可:
  2. ```bash
  3. $ docker container cp -a 容器:容器内部地址 宿主机地址
  • 示例:

    1. $ docker container cp -a mysq:/etc/mysql/mysql.conf.d /opt/temp
  • 之后就能在 /opt/temp 目录下看到配置文件内容了。

  • 示例配置文件: ```properties

    Copyright (c) 2014, 2016, 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, version 2.0,

    as published by the Free Software Foundation.

    #

    This program is also distributed with certain software (including

    but not limited to OpenSSL) that is licensed under separate terms,

    as designated in a particular file or component or in included license

    documentation. The authors of MySQL hereby grant you an additional

    permission to link the program and your derivative works with the

    separately licensed software that they have included with MySQL.

    #

    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, version 2.0, 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

[client]

password=

客户端编码

default-character-set=utf8mb4

[mysql]

default-character-set=utf8mb4

[mysqld]

character-set-server = utf8mb4 init_connect = “SET NAMES utf8mb4” collation-server = utf8mb4_unicode_ci character-set-client-handshake = false

pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql

log-error = /var/log/mysql/error.log

By default we only accept connections from localhost

bind-address = 127.0.0.1

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0 ```