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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章