MySQL-docker安装

docker 运行 mysql

最近入职了新公司,还在搭建环境和了解项目阶段

配置环境肯定少不了 mysql

这次我打算在 docker 中安装 mysql5.7

步骤

  1. docker 安装(略)

  2. 拉取镜像

    1
    2
    docker search mysql
    docker install mysql:5.7
  3. 直接运行

    1
    docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

问题

这里运行之后发现 mysql 的配置文件都是默认的,比如字符集什么的都是 lain,解决方法可以是修改容器中的配置,但是这样就违背了 docker 的初衷,将 docker容器当做操作系统了

解决: 最好是挂载一个配置文件

对于 5.7 版本的配置文件官方解释如下

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.

如果想要自定义配置,建议向 /etc/mysql/conf.d 目录中创建 .cnf 文件。

所以创建一个目录用于挂载

这里注意要饭放在 docker与 mac share 的目录下,如果不是也可以添加 share 目录,问题不大

1
2
3
mkdir -p /Users/yiqing/Documents/programming/docker_v/mysql/conf
cd /Users/yiqing/Documents/programming/docker_v/mysql/conf
touch my.cnf

后期就在这个 my.cnf中修改配置

比如设定 mysql 字符集

1
2
3
4
5
6
7
8
9
[client]
default-character-set=utf8

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

[mysql]
default-character-set=utf8

done!