Docker:除非我手动重启,否则MySQL会拒绝主机IP
Here is my Dockerfile:
这是我的Dockerfile:
FROM debian:jessie-backports
RUN apt-get update --yes && apt-get upgrade --yes
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
git \
curl \
build-essential\
libssl-dev\
ca-certificates\
mysql-server\
redis-server\
elasticsearch
USER root
ENV HOME /root
# MYSQL SETUP
RUN sed -i -e"s/^bind-address\s*=\s*127.0.0.1/#bind-address = 0.0.0.0/" /etc/mysql/my.cnf
RUN sed -i -e"s/^#max_connections\s*=\s*100/max_connections = 200/" /etc/mysql/my.cnf
RUN echo "\n[mysqld]\nskip-grant-tables\n" >> /etc/mysql/my.cnf
VOLUME ["/var/lib/mysql", "/var/log/mysql"]
EXPOSE 3306
# REDIS SETUP
RUN echo "daemonize yes\nbind 0.0.0.0" >> /etc/redis/redis-serve.conf
RUN sed 's/^daemonize no/daemonize yes/' -i /etc/redis/redis.conf \
&& sed 's/^bind 127.0.0.1/bind 0.0.0.0/' -i /etc/redis/redis.conf \
&& sed 's/^# unixsocket /unixsocket /' -i /etc/redis/redis.conf \
&& sed 's/^# unixsocketperm 755/unixsocketperm 777/' -i /etc/redis/redis.conf \
&& sed '/^logfile/d' -i /etc/redis/redis.conf
VOLUME ["/var/lib/redis", "/var/log/redis"]
EXPOSE 6379 6380
# ELASTICSEARCH SETUP
RUN sed 's/^#START_DAEMON=true/START_DAEMON=true/' -i /etc/default/elasticsearch
VOLUME ["/opt/elasticsearch/data", "/opt/elasticsearch/logs"]
EXPOSE 9200 9300
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN chmod 777 /usr/local/bin/docker-entrypoint.sh
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
The entrypoint:
#!/bin/bash
/etc/init.d/mysql start
/etc/init.d/redis-server start
/etc/init.d/elasticsearch start
/bin/bash
And the command I'm running to start the container:
我正在运行的命令启动容器:
docker run -i -t -p 3306:3306 -p 6379:6379 -p 9200:9200 -p 9300:9300 assethost
I want to connect to the MySQL server running in the container from the host, but this is what happens when I try to connect:
我想从主机连接到容器中运行的MySQL服务器,但这是我尝试连接时发生的情况:
ERROR 1130 (HY000): Host '192.168.99.1' is not allowed to connect to this MySQL server
错误1130(HY000):主机'192.168.99.1'不允许连接到此MySQL服务器
Both Redis and Elasticsearch expose themselves just fine. MySQL will only begin accepting external hosts if I restart it manually from the Bash shell that comes up from the entrypoint when I run the container. I have to run /etc/init.d/mysql restart
. Only then, can I successfully connect from the host using the MySQL client.
Redis和Elasticsearch都暴露自己很好。如果我从运行容器时从入口点出来的Bash shell手动重启它,MySQL才会开始接受外部主机。我必须运行/etc/init.d/mysql restart。只有这样,我才能使用MySQL客户端从主机成功连接。
Note that I am using docker-machine, so I have to give the MySQL client the host IP in order to connect.
请注意,我使用的是docker-machine,所以我必须给MySQL客户端提供主机IP才能连接。
How can I be able to connect from the host to the MySQL instance running in the container without having to manually restart MySQL?
如何能够从主机连接到容器中运行的MySQL实例,而无需手动重启MySQL?
Docker version 1.12.1, build 23cf638
Docker版本1.12.1,build 23cf638
3 个解决方案
#1
2
I figured out a way to get the skip-grant-tables option to be applied on start.
我想出了一种方法,可以在启动时应用skip-grant-tables选项。
Workaround
In docker-entrypoint.sh, I changed /etc/init.d/mysql start
to
在docker-entrypoint.sh中,我将/etc/init.d/mysql更改为
HOME=/etc/mysql /usr/bin/mysqld_safe > /dev/null 2>&1 &
HOME = / etc / mysql / usr / bin / mysqld_safe> / dev / null 2>&1&
Other changes
Since there was already a [mysqld]
section in my.cnf, I changed the following line in Dockerfile RUN echo "\n[mysqld]\nskip-grant-tables\n" >> /etc/mysql/my.cnf
由于my.cnf中已经存在[mysqld]部分,我在Dockerfile RUN echo中更改了以下行“\ n [mysqld] \ nskip-grant-tables \ n”>> /etc/mysql/my.cnf
to
RUN sed -i '/\[mysqld\]/ a skip-grant-tables\nskip-name-resolve' /etc/mysql/my.cnf
运行sed -i'/ \ [mysqld \] / a skip-grant-tables \ nskip-name-resolve'/etc/mysql/my.cnf
I added skip-name-resolve
so mysql doesn't attempt to resolve names for local ip addresses.
我添加了skip-name-resolve,因此mysql不会尝试解析本地IP地址的名称。
Caveats
The workaround no longer works when /etc/init.d/mysql restart
is executed, but /etc/init.d/mysql stop; /etc/init.d/mysql start
preserved the 'skip-grant-tables' option.
执行/etc/init.d/mysql restart时,解决方法不再有效,但/etc/init.d/mysql停止; /etc/init.d/mysql start保留了'skip-grant-tables'选项。
更多相关文章
- 使用mysql和PHP从本地主机连接到远程数据库
- 解决Linux主机上的 远程MySQL客户端无法连接的问题
- PHPMyWind教程:如何把本地测试好的程序发布到虚拟主机
- 如何将window.location设置为特定路径(没有主机)?
- JqueryUI Draggable - 仅自动垂直调整父容器大小
- 如何修复一个页面网站的容器/正文宽度(html,css和js)
- 如何访问远程节点。浏览器中的js应用程序,而不是本地主机
- 扩展子div时如何扩展容器div的高度
- PyGobject(五十六)布局容器之TreeView(上)