如何将本地托管的 MySQL 数据库与 docker 容器连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44543842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect locally hosted MySQL database with the docker container
提问by Prateek Naik
Through docker-compose.yml
I am able to run the application. Now we want to move the application to production, But we don't want to use the container database. So is there any way so that I can connect my local MySQL database with the application using docker-compose
?
通过docker-compose.yml
我能够运行该应用程序。现在我们想将应用程序移至生产环境,但我们不想使用容器数据库。那么有什么方法可以让我使用 将本地 MySQL 数据库与应用程序连接起来docker-compose
?
My docker-compose.yml looks like:
我的 docker-compose.yml 看起来像:
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
ports:
- 8080:8080
links:
- app-db
app-db:
build:
context: .
dockerfile: app-db/Dockerfile
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=Optimize
ports:
- 3306:3306
Instead of app-db
part I just want to connect to my locally hosted mysql
database.
而不是app-db
部分,我只想连接到我本地托管的mysql
数据库。
回答by Robert
Find the host machine ip in the docker network. If you use docker-compose.yml version: "3"
it's probably that that IP is: 172.18.0.1
, but confirm itsearching for the "Gateway" of your container (your host):
在docker网络中找到宿主机ip。如果您使用 docker-compose.ymlversion: "3"
可能是该 IP 是: 172.18.0.1
,但确认它正在搜索您的容器(您的主机)的“网关”:
docker inspect <container-id-or-name> | grep Gateway
"Gateway": "",
"IPv6Gateway": "",
"Gateway": "172.18.0.1",
"IPv6Gateway": "",
So inside your docker application point to MySQL as this: 172.18.0.1:3306
(maybe in a configuration file). Take into account that that IP is fixed as long as the docker network still the same (the network is created by docker-compose, and it is not removed unless you do docker-compose down
)
所以在你的 docker 应用程序中指向 MySQL 如下:(172.18.0.1:3306
可能在配置文件中)。考虑到该 IP 是固定的,只要 docker 网络仍然相同(网络是由 docker-compose 创建的,除非您这样做,否则它不会被删除docker-compose down
)
Also, check that your MySQL is listening to all of its interfaces. In your my.cnf
search for bind-address
that should be 0.0.0.0
(consider security issues if your server has public IP).
另外,检查您的 MySQL 是否正在侦听其所有接口。在您的my.cnf
搜索bind-address
中应该是0.0.0.0
(如果您的服务器具有公共 IP,请考虑安全问题)。
As an alternative you can bring to the container the same networking as your host, in order to share the localhost, so the container will find mysql there. Use network mode as "host":
作为替代方案,您可以将与主机相同的网络连接到容器中,以便共享本地主机,因此容器将在那里找到 mysql。使用网络模式作为“主机”:
version: '3'
services:
web-app:
build:
context: .
dockerfile: web-app/Dockerfile
ports:
- 8080:8080
network_mode: "host"
Then, point in your hibernate.properties
to mysql as this: localhost:3306
然后,将您hibernate.properties
的 mysql指向如下:localhost:3306
回答by Thanuja
Allow permission using mysqld.cnf
使用 mysqld.cnf 允许权限
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
save your file and restart mysql server
保存您的文件并重新启动 mysql 服务器
sudo systemctl restart mysql.service
login to Mysql
登录Mysql
mysql -h localhost -u root -p
GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
Find your IP
查找您的 IP
ip addr show
And use it in your docker container 192.168.x.x
并在您的 docker 容器 192.168.xx 中使用它
回答by Júlio Falbo
Just use host.docker.internal
instead of localhost
.
只需使用host.docker.internal
代替localhost
.
回答by Alex Kapustin
Just use 172.17.0.1 inside docker. Its static ip address of host machine
只需在 docker 中使用 172.17.0.1 即可。它的主机静态IP地址