我发现docker有官方的MySQL镜像here .不幸的是,我无法使用主机卷启动容器。我试过以下脚本。
DB_ROOT_PASS="myPassword"
DB_NAME="myDatabase"
DATA_DIR="$HOME/mysql_data"
mkdir -p $DATA_DIR
mkdir -p $DATA_DIR/conf.d
cd $DATA_DIR
wget -O my.cnf http://pastebin.com/raw.php?i=aV4pXRQD
sudo docker run \
-e MYSQL_ROOT_PASSWORD=$DB_ROOT_PASS \
-e MYSQL_DATABASE=$DB_NAME \
-p 3306:3306 \
-v $DATA_DIR:/etc/mysql \
mysql
结果如下:
Running mysql_install_db ...
Installing MySQL system tables...2015-02-14 07:49:52 0 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
2015-02-14 07:49:52 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
OK
Filling help tables...2015-02-14 07:49:55 0 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
2015-02-14 07:49:55 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h 2efc65e86051 password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file /usr/my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as /usr/my-new.cnf,
please compare it with your file and take the changes you need.
WARNING: Default config file /etc/mysql/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
Finished mysql_install_db
2015-02-14 07:49:57 0 [Warning] Using unique option prefix key_buffer instead of key_buffer_size is deprecated and will be removed in a future release. Please use the full name instead.
2015-02-14 07:49:57 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
我也尝试过不创建 conf.d
目录或 my.cnf
文件,这也会导致其他错误。
为什么需要卷?
主机卷提供“近金属”磁盘性能以及数据持久性,这对于 MySQL 数据库很重要。因此,不需要从容器备份数据,或者使用“数据容器”模式。
问题:
如何更新脚本以使用主机卷启动 Docker MySQL 容器,从而使 MySQL 服务正确启动?也许传递初始化脚本或向卷添加更多数据?
上下文
- Ubuntu 14.04 64 位服务器
- Docker 版本 1.5.0,构建 a8a31ef
最佳答案
有几个问题,主要是正在下载的原始配置文件有一个 bind-address
设置,导致无法从外部连接到 mysql 服务主持人。我已经更新了设置(将其注释掉)并使服务正常运行,但是 service mysql status
仍然显示 MySQL Community Server 5.6.23 is not running.
即使这是。我 99% 确定这是因为“容器化”效应。
另外,数据也没有保存在/etc/mysql
,所以为了持久化和更高的性能,我们需要再增加一个volume。最终的工作脚本如下:
DB_ROOT_PASS="myPassword"
DB_NAME="myDatabase"
CONFIG_DIR="$HOME/mysql_config"
DATA_DIR="$HOME/mysql_data"
mkdir -p $DATA_DIR
mkdir -p $CONFIG_DIR
mkdir -p $CONFIG_DIR/conf.d
cd $CONFIG_DIR
sudo wget -O my.cnf http://pastebin.com/raw.php?i=aV4pXRQD
sudo docker run \
-e MYSQL_ROOT_PASSWORD=$DB_ROOT_PASS \
-e MYSQL_DATABASE=$DB_NAME \
-p 3306:3306 \
-v $CONFIG_DIR:/etc/mysql \
-v $DATA_DIR:/var/lib/mysql \
-d \
mysql
关于mysql - Docker MySQL 主机卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28513058/