无法仅从远程主机上的 Docker 容器中自定义构建的 MySQL 映像启动 MySQL

无法仅从远程主机上的 Docker 容器中自定义构建的 MySQL 映像启动 MySQL

我创建了一个 Dockerfile 来构建自定义 MySQL 映像。使用它,我可以在我的本地主机 Ubuntu 16.04 笔记本电脑上运行 MySQL。但不能在我的 DigitalOcean Ubuntu 16.04 droplet 上运行。在这个远程系统上,我无法启动 MySQL。

出于调试目的,映像本身不会启动 MySQL,而只是保持容器处于活动状态:

FROM ubuntu:16.04

RUN apt-get update \
  && apt-get install -y libncurses-dev \
  && apt-get install -y build-essential \
  && apt-get install -y cmake

COPY mysql-5.6.30.tar.gz /usr/bin/
WORKDIR /usr/bin/
RUN gzip -d mysql-5.6.30.tar.gz \
  && tar -xvf mysql-5.6.30.tar \
  && ln -s mysql-5.6.30 mysql

WORKDIR /usr/bin/mysql/
RUN mkdir install && mkdir install/data && mkdir install/var && mkdir install/etc && mkdir install/tmp

RUN cd /usr/bin/mysql/ \
  && cmake \
  -DCMAKE_INSTALL_PREFIX=/usr/bin/mysql/install \
  -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  -DMYSQL_DATADIR=/usr/bin/mysql/install/data \
  -DDOWNLOAD_BOOST=1 \
  -DWITH_BOOST=/usr/bin/mysql/install/boost \
  -DMYSQL_UNIX_ADDR=/usr/bin/mysql/install/tmp/mysql.sock \
  && make \
  && make install \
  && make clean

COPY my.cnf install/my.cnf
RUN ln -s /usr/bin/mysql/install/my.cnf ~/.my.cnf

ENTRYPOINT ["/usr/bin/tail", "-f", "/dev/null"]

在远程系统上我输入以下命令:

cd ~/dev/docker/projects/learnintouch
sudo rm -fr mysql/data/
sudo docker run -d -p 3307:3306 -v /home/stephane/dev/docker/projects/learnintouch/mysql/data:/usr/bin/mysql/install/data --name mysql thalasoft.com:5000/mysql:5.6.30
sudo docker exec -it mysql bash

然后,在正在运行的容器中,我输入命令:

root@4d0028607aeb:/usr/bin/mysql-5.6.30# /usr/bin/mysql/install/scripts/mysql_install_db \
  --no-defaults \
  --explicit_defaults_for_timestamp \
  --basedir=/usr/bin/mysql/install \
  --datadir=/usr/bin/mysql/install/data \
  --tmpdir=/usr/bin/mysql/install/tmp \
  --lc-messages-dir=/usr/bin/mysql/install/share
Installing MySQL system tables...2017-10-25 10:22:26 0 [Note] /usr/bin/mysql/install/bin/mysqld (mysqld 5.6.30) starting as process 28 ...
2017-10-25 10:22:26 28 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-10-25 10:22:26 28 [Note] InnoDB: The InnoDB memory heap is disabled
2017-10-25 10:22:26 28 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-10-25 10:22:26 28 [Note] InnoDB: Memory barrier is not used
2017-10-25 10:22:26 28 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-10-25 10:22:26 28 [Note] InnoDB: Using CPU crc32 instructions
2017-10-25 10:22:26 28 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-10-25 10:22:26 28 [Note] InnoDB: Completed initialization of buffer pool
2017-10-25 10:22:26 28 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-10-25 10:22:26 28 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-10-25 10:22:26 28 [Note] InnoDB: Database physically writes the file full: wait...
2017-10-25 10:22:26 28 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-10-25 10:22:26 28 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-10-25 10:22:26 28 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-10-25 10:22:26 28 [Warning] InnoDB: New log files created, LSN=45781
2017-10-25 10:22:26 28 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-10-25 10:22:26 28 [Note] InnoDB: Doublewrite buffer created
2017-10-25 10:22:26 28 [Note] InnoDB: 128 rollback segment(s) are active.
2017-10-25 10:22:26 28 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-10-25 10:22:26 28 [Note] InnoDB: Foreign key constraint system tables created
2017-10-25 10:22:26 28 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-10-25 10:22:26 28 [Note] InnoDB: Tablespace and datafile system tables created.
2017-10-25 10:22:26 28 [Note] InnoDB: Waiting for purge to start
2017-10-25 10:22:26 28 [Note] InnoDB: 5.6.30 started; log sequence number 0
2017-10-25 10:22:26 28 [Note] Binlog end
2017-10-25 10:22:26 28 [Note] InnoDB: FTS optimize thread exiting.
2017-10-25 10:22:26 28 [Note] InnoDB: Starting shutdown...
2017-10-25 10:22:28 28 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2017-10-25 10:22:28 0 [Note] /usr/bin/mysql/install/bin/mysqld (mysqld 5.6.30) starting as process 51 ...
2017-10-25 10:22:28 51 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-10-25 10:22:28 51 [Note] InnoDB: The InnoDB memory heap is disabled
2017-10-25 10:22:28 51 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-10-25 10:22:28 51 [Note] InnoDB: Memory barrier is not used
2017-10-25 10:22:28 51 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-10-25 10:22:28 51 [Note] InnoDB: Using CPU crc32 instructions
2017-10-25 10:22:28 51 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-10-25 10:22:28 51 [Note] InnoDB: Completed initialization of buffer pool
2017-10-25 10:22:28 51 [Note] InnoDB: Highest supported file format is Barracuda.
2017-10-25 10:22:28 51 [Note] InnoDB: 128 rollback segment(s) are active.
2017-10-25 10:22:28 51 [Note] InnoDB: Waiting for purge to start
2017-10-25 10:22:28 51 [Note] InnoDB: 5.6.30 started; log sequence number 1625977
2017-10-25 10:22:28 51 [Note] Binlog end
2017-10-25 10:22:28 51 [Note] InnoDB: FTS optimize thread exiting.
2017-10-25 10:22:28 51 [Note] InnoDB: Starting shutdown...
2017-10-25 10:22:29 51 [Note] InnoDB: Shutdown completed; log sequence number 1625987
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/mysql/install/bin/mysqladmin -u root password 'new-password'
  /usr/bin/mysql/install/bin/mysqladmin -u root -h 4d0028607aeb password 'new-password'

Alternatively you can run:

  /usr/bin/mysql/install/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/bin/mysql/install/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/bin/mysql/install/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/bin/mysql/install/my-new.cnf,
please compare it with your file and take the changes you need.

启动 MySQL 时显示以下输出:

root@4d0028607aeb:/usr/bin/mysql-5.6.30# /usr/bin/mysql/install/bin/mysqld_safe --defaults-file=/usr/bin/mysql/install/my.cnf
171025 10:37:32 mysqld_safe Logging to '/usr/bin/mysql/install/mysql.error.log'.
171025 10:37:32 mysqld_safe Starting mysqld daemon with databases from /usr/bin/mysql/install/data
171025 10:37:32 mysqld_safe mysqld from pid file /usr/bin/mysql/install/data/4d0028607aeb.pid ended

root@4d0028607aeb:/usr/bin/mysql-5.6.30# /usr/bin/mysql/install/bin/mysqld_safe  
171025 10:23:33 mysqld_safe Logging to '/usr/bin/mysql/install/mysql.error.log'.
171025 10:23:33 mysqld_safe Starting mysqld daemon with databases from /usr/bin/mysql/install/data
171025 10:23:34 mysqld_safe mysqld from pid file /usr/bin/mysql/install/data/4d0028607aeb.pid ended

日志并没有透露太多内容:

root@4d0028607aeb:/usr/bin/mysql-5.6.30# cat /usr/bin/mysql/install/mysql.error.log
171025 10:23:33 mysqld_safe Starting mysqld daemon with databases from /usr/bin/mysql/install/data
Killed
171025 10:23:34 mysqld_safe mysqld from pid file /usr/bin/mysql/install/data/4d0028607aeb.pid ended
171025 10:37:32 mysqld_safe Starting mysqld daemon with databases from /usr/bin/mysql/install/data
Killed
171025 10:37:32 mysqld_safe mysqld from pid file /usr/bin/mysql/install/data/4d0028607aeb.pid ended

my.cnf 文件包含:

root@4d0028607aeb:/usr/bin/mysql-5.6.30# cat /usr/bin/mysql/install/my.cnf 
[mysqld]
bind-address    = 0.0.0.0 # Allow client binding from any IP address instead of just 127.0.0.1
port            = 3306
sql_mode        = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION # This is strict mode: NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
socket          = /usr/bin/mysql/install/tmp/mysql.sock
user            = root
basedir         = /usr/bin/mysql/install
datadir         = /usr/bin/mysql/install/data
log-bin         = /usr/bin/mysql/install/mysql.bin.log
log-error       = /usr/bin/mysql/install/mysql.error.log
general-log-file     = /usr/bin/mysql/install/mysql.log
slow-query-log-file  = /usr/bin/mysql/install/mysql.slow.queries.log
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 1
sync_binlog = 1
innodb_flush_method = O_DIRECT
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init-connect = 'SET NAMES utf8mb4'
character-set-client-handshake = FALSE
wait_timeout = 28800 # amount of seconds during inactivity that MySQL will wait before it will close a connection on a non-interactive connection
interactive_timeout = 28800 # same, but for interactive sessions
[client]
socket = /usr/bin/mysql/install/tmp/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4

有一个链接:

root@4d0028607aeb:/usr/bin/mysql-5.6.30# cd
root@4d0028607aeb:~# ll
total 16
drwx------  2 root root 4096 Oct 25 08:06 ./
drwxr-xr-x 50 root root 4096 Oct 25 10:21 ../
-rw-r--r--  1 root root 3106 Oct 22  2015 .bashrc
lrwxrwxrwx  1 root root   29 Oct 25 08:06 .my.cnf -> /usr/bin/mysql/install/my.cnf
-rw-r--r--  1 root root  148 Aug 17  2015 .profile

这样就可以找到它:

root@4d0028607aeb:~# /usr/bin/mysql/install/bin/mysqladmin | grep my.cnf                                 
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/bin/mysql/install/etc/my.cnf ~/.my.cnf 

从容器内部访问的主机目录:

stephane@ubuntu-512mb-fra1-01:~/dev/docker/projects/learnintouch$ ll
total 24K
drwxr-xr-x 8 stephane 4.0K Oct 24 16:52 engine/
drwxrwxr-x 2 stephane 4.0K Oct 24 16:22 learnintouch/
drwxrwxr-x 2 stephane 4.0K Oct 24 16:22 learnintouch.com/
drwxrwxr-x 2 stephane 4.0K Oct 24 13:11 learnintouch.com-startup/
drwxrwxrwx 3 stephane 4.0K Oct 25 10:20 mysql/
drwxr-xr-x 3 root     4.0K Oct 24 16:36 website/
stephane@ubuntu-512mb-fra1-01:~/dev/docker/projects/learnintouch$ ll mysql/
total 4.0K
drwxr-xr-x 5 root 4.0K Oct 25 10:22 data/
stephane@ubuntu-512mb-fra1-01:~/dev/docker/projects/learnintouch$ ll mysql/data/
total 109M
-rw-rw---- 1 root  12M Oct 25 10:22 ibdata1
-rw-rw---- 1 root  48M Oct 25 10:22 ib_logfile0
-rw-rw---- 1 root  48M Oct 25 10:22 ib_logfile1
drwx------ 2 root 4.0K Oct 25 10:22 mysql/
drwx------ 2 root 4.0K Oct 25 10:22 performance_schema/
drwx------ 2 root 4.0K Oct 25 10:22 test/
-rw-r--r-- 1 root    0 Oct 25 10:21 created-from-within-the-container

我还添加了一个 2G 的交换文件并再次尝试,但仍然无法以相同的方式启动。

相关内容