Docker MySQL 主机卷

4

我发现Docker有一个官方的MySQL镜像,可以在这里找到。但是很遗憾,我无法使用主机卷启动容器。我已经尝试了以下脚本。

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服务正确启动?也许可以传递一个init脚本或向卷添加更多数据?

上下文:

  • Ubuntu 14.04 64位服务器
  • Docker版本1.5.0,构建a8a31ef

我看到有人将这个问题标记为不好,并推动关闭它。他们能解释一下为什么吗? - Programster
我没有看到任何错误。你期望发生什么事情却没有发生? - Adrian Mouat
@Adrian,对我来说容器正在运行,但我无法连接到mysql服务,而且service mysql status输出MySQL Community Server 5.6.23未运行。 - Programster
我意识到更新my.cnf文件中的绑定地址注释后,我可以从主机外部连接。我现在也意识到需要另一个卷用于data_dir,而脚本data_dir实际上是配置目录。当我有一个最终的工作解决方案时,我会发布它。 - Programster
2个回答

4
有几个问题,主要问题是下载的原始配置文件中有一个“bind-address”设置,导致mysql服务无法从主机外部连接。我已经更新了设置(注释掉它),并成功运行了服务,但是“service mysql status”仍然显示“MySQL Community Server 5.6.23未运行。”即使它正在运行。我99%确定这是“容器化”效应造成的。
此外,数据不保存在“/etc/mysql”中,因此为了实现持久性和更高的性能,我们需要添加另一个卷。最终工作脚本如下:
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

2
您看到此消息的原因是:
MySQL Community Server 5.6.23 is not running.

这是因为该容器中未加载ps命令。service mysql status命令(以及所有服务状态命令)依赖于ps来检查状态。
当我运行service mysql status时,我得到的完整消息是:
/etc/init.d/mysql: line 41: ps: command not found
MySQL Community Server 5.6.23 is not running.

如果我直接运行ps命令,我会得到以下结果:

bash: ps: command not found

不确定为什么他们没有在这个容器中提供ps,可能值得在此问题上开展工作。


安装PS:apt-get update && apt-get install -y procps - rump roast

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接