我构建了一个MySQL服务器映像并且可以运行它的容器,我也可以在上周的某些日子里从另一个容器中访问它,所有这些都在我的笔记本电脑本地运行Docker 17.05.0-ce
Ubuntu 16.04
}。然后,昨天我无法从外部访问MySQL。它开始给我错误:ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"
以下是Dockerfile
内容:
$ cat mysql/Dockerfile
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
EXPOSE 3306
COPY startup.sh install/startup.sh
COPY my.cnf install/my.cnf
ENTRYPOINT ["/bin/bash", "install/startup.sh"]
my.cnf
文件内容:
[mysqld]
bind-address = 0.0.0.0 # Allow client binding from any IP address instead of just 127.0.0.1
port = 3307
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
startup.sh
文件内容:
#!/bin/bash -x
if [ ! -f /usr/bin/mysql/install/data/ibdata1 ]; then
chmod 755 /usr/bin/mysql/install/scripts/mysql_install_db
sleep 1 # Needed to avoid running the script when this mysql_install_db file has not yet been released by the permissions change
/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
/usr/bin/mysql/install/bin/mysqld_safe --defaults-file=/usr/bin/mysql/install/my.cnf &
sleep 10s
echo "GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; FLUSH PRIVILEGES" | /usr/bin/mysql/install/bin/mysql
tail -f /etc/hosts
else
/usr/bin/mysql/install/bin/mysqld_safe --defaults-file=/usr/bin/mysql/install/my.cnf
fi
我可以使用以下命令运行它:
docker run -d -p 3307:3306 -v /home/stephane/dev/php/learnintouch/docker/mysql/data:/usr/bin/mysql/install/data --name mysql stephaneeybert/mysql:5.6.30
我可以看到端口处于打开状态:
nmap -p 3307 localhost
我可以输入它的bash shell:
docker exec -it mysql bash
而且,从容器中,我登录就好了:
/usr/bin/mysql/install/bin/mysql -u root -p
但如果我想从主机登录:
cd programs/install/mariadb/; bin/mysql -h localhost -P 3307 --protocol=tcp -u root -p
它给了我一条信息:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0 "Internal error/check (Not system error)"
答案 0 :(得分:0)
容器暴露端口3306。
FROM ubuntu:16.04
# ...
EXPOSE 3306
# ...
mysql正在侦听容器中的端口3307,这里应该是3306。
[mysqld]
bind-address = 0.0.0.0
port = 3307
然后将货柜码头转发至货柜3307。
docker run -d -p 3307:3306