简体   繁体   English

Docker将应用程序连接到mysql主机

[英]Docker connect app to mysql host

I have an Ubuntu 16.04 installation in a vm and I'm trying to run an app inside a docker container and connect it to a MySQL database on the host 我在vm中安装了Ubuntu 16.04,我正在尝试在docker容器中运行一个app并将其连接到主机上的MySQL数据库

I have something like this.. on the host I have the MySQL: 我有这样的东西..在主机上我有MySQL:

And when I try to connect my app to the MySQL I got this: 当我尝试将我的应用程序连接到MySQL时,我得到了这个:

java.sql.exception: cannot create poolableConnectionFactory ( Could not create connection to database server. Attempted reconnect 3 times java.sql.exception:无法创建poolableConnectionFactory(无法创建与数据库服务器的连接。尝试重新连接3次

I execute my app using a docker-compose: 我使用docker-compose执行我的应用程序:

version: '2'
services:
  exo:
    image: exoplatform/exo-community:5.0
    environment:
      EXO_DB_TYPE: mysql
      EXO_DB_NAME: user
      EXO_DB_USER: root
      EXO_DB_PASSWORD: "my-pass"
      EXO_DB_HOST: "127.0.0.1"
      EXO_ADDONS_LIST: exo-jdbc-driver-mysql
    expose:
      - "8080"
      - "3306"
    ports:
      - "8080:8080"
    volumes:
      - exo_data:/srv/exo
      - exo_logs:/var/log/exo
volumes:
  exo_data:
    external:
      name: exo_data
  exo_logs:
    external:
      name: exo_logs
networks:
  default:
      driver: bridge

As you can see I try to connect to the MySQL default port... And I just saw that 3306 port is listening on the host. 正如您所看到的,我尝试连接到MySQL默认端口......而我刚看到3306端口正在侦听主机。

I also tried to add the following line ( saw on another post) 我还尝试添加以下行(在另一篇文章中看到)

In my.cnf I have the bind-address 0.0.0.0. 在my.cnf中,我有绑定地址0.0.0.0。

And MySQL is running: 并且MySQL正在运行:

root@xxx/# mysqladmin -u root -p status

Uptime: 4784  Threads: 4  Questions: 17  Slow queries: 0  Opens: 114  Flush tables: 1  Open tables: 33  Queries per second avg: 0.003

I'm using docker on bridge so I guess I just need to add the port on docker composer in order to connect docker and host. 我在桥上使用docker所以我想我只需要在docker composer上添加端口以连接docker和host。 But nothing seems to works. 但似乎没有任何作用。 Can someone point me to right direction? 有人能指出我正确的方向吗?

The problem is that you are trying to connect to the host from the container using localhost . 问题是您尝试使用localhost从容器连接到主机。 This won't work as the container has its own ip and localhost will hit the container and not the host. 这不起作用,因为容器有自己的ip,localhost将命中容器而不是主机。

Connecting from container to host is a very frequent question on stackoverflow and you can find many answers in From inside of a Docker container, how do I connect to the localhost of the machine? 从容器到主机的连接是一个非常常见的stackoverflow问题,你可以在Docker容器里面找到很多答案,如何连接到机器的本地主机?

The easiest way (though not recommended) is to use network_mode: host inside docker-compose file. 最简单的方法(虽然不推荐)是在docker-compose文件中使用network_mode: host In this case, the container will share the networking with the host and localhost will hit the host machine as well. 在这种情况下,容器将与主机共享网络,localhost也将与主机共享。

I think you need specify IP address: 我认为你需要指定IP地址:

mysql -u root -p -h 44.55.66.77

and give privileges for your: 并为您提供特权:

DB GRANT ALL ON yourDatabase.* TO root@’1.2.3.4’ IDENTIFIED BY ‘my-pass’;

to get IP address inside container you can run this: 要获取容器内的IP地址,您可以运行此命令:

awk 'END{print $1}' /etc/hosts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM