Docker Learning Notes (05)

Example of installing MySQL

Posted by Chen Xingxu on June 17, 2020

5.Docker入门笔记——安装MySQL示例

获取 MySQL

docker pull mysql:5.5

错误的启动

docker run --name mysql01 -d mysql:5.5

查看日志

docker logs 42f09819908b
error: database is uninitialized and password option is not specified 
  You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD;

正确的启动

无端口映射

docker run --name mysql01 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5

查看运行的容器

docker ps

端口映射

docker run -p 3306:3306 --name mysql02 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

部分高级操作

Using a custom MySQL configuration file

The default configuration for MySQL can be found in /etc/mysql/my.cnf, which may !includedir additional directories such as /etc/mysql/conf.d or /etc/mysql/mysql.conf.d. Please inspect the relevant files and directories within the mysql image itself for more details.

If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
#示例
$ docker run --name mysql02 -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=root -d mysql:5.5
#-v 把主机的/my/custom文件夹挂载到 MySQL Docker容器的 /etc/mysql/conf.d 文件夹里面
#改MySQL的配置文件就只需要把MySQL配置文件放在自定义的文件夹下(/my/custom)

This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.

Configuration without a cnf file

Many configuration options can be passed as flags to mysqld. This will give you the flexibility to customize the container without needing a cnf file. For example, if you want to change the default encoding and collation for all tables to use UTF-8 (utf8mb4) just run the following:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

If you would like to see a complete list of available options, just run:

$ docker run -it --rm mysql:tag --verbose --help

参考:

[https://hub.docker.com//mysql](https://hub.docker.com//mysql)