(这可能是一个愚蠢的问题,因为我对Docker或mysql管理部门知之甚少,但由于我在这个问题上花了整整一个晚上,我敢问。)
简单地说,是
我想在一个码头容器中运行mysql,并从我的主机连接到它。到目前为止,我所取得的最好成绩是:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)详细信息
我正在使用以下Dockerfile
FROM ubuntu:14.04.3
RUN apt-get update && apt-get install -y mysql-server
# Ensure we won't bind to localhost only
RUN grep -v bind-address /etc/mysql/my.cnf > temp.txt \
&& mv temp.txt /etc/mysql/my.cnf
# It doesn't seem needed since I'll use -p, but it can't hurt
EXPOSE 3306
CMD /etc/init.d/mysql start && tail -F /var/log/mysql.log在有此文件的目录中,我可以成功地构建映像,并使用以下方法运行它:
> docker build -t my-image .
> docker run -d -p 12345:3306 my-image当我附加到图像上时,它似乎工作得很好:
# from the host
> docker exec -it <my_image_name> bash
#inside of the container now
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
[...]然而,我没有从主持人那里获得那么多的成功:
> mysql -P 12345 -uroot
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)更多细节
grep -v可能感觉怪怪的。诚然,可能有更清洁的方法来做到这一点。但是当我附上我的图像时,我可以观察到它实际上是正常工作的(即:删除了bind-address)。我可以在容器/var/log/mysql/error.log中看到
服务器主机名(绑定-地址):'0.0.0.0';端口: 3306 - '0.0.0.0‘解析为'0.0.0.0';在IP上创建服务器套接字:'0.0.0.0’。
发布于 2016-10-20 16:24:32
如果使用"127.0.0.1“而不是本地主机,mysql将使用tcp方法,并且应该能够将容器连接到:
mysql -h 127.0.0.1 -P 3306 -u root发布于 2015-12-28 08:11:25
如果您的Docker MySQL主机运行正常,您可以从本地机器连接到它,但是您应该指定主机、端口和协议,如下所示:
mysql -h localhost -P 3306 --protocol=tcp -u root将3306更改为从Docker容器转发的端口号(在您的示例中为12345)。
因为您在Docker容器中运行MySQL,所以套接字不可用,您需要通过TCP进行连接。在mysql命令中设置"--protocol“将改变这一点。
发布于 2017-01-02 18:36:11
我建议你去码头写作店看看。以下是它的工作原理:
创建一个名为docker-compose.yml的文件,如下所示:
version: '2'
services:
mysql:
image: mariadb:10.1.19
ports:
- 8083:3306
volumes:
- ./mysql:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: wp接下来,运行:
$docker-合成
备注:
现在,您可以访问mysql控制台了:
$ mysql -P 8083 -协议=tcp -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.5-10.1.19-MariaDB-1~jessie mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>备注:
https://stackoverflow.com/questions/33001750
复制相似问题