如何与在Docker中作为容器运行的MySQL DB连接? [英] How to connect with MySQL DB running as container in docker?

查看:94
本文介绍了如何与在Docker中作为容器运行的MySQL DB连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我关于如何与作为容器在虚拟机上的Docker运行的mysql数据库进行连接的任何想法吗?

Can some give me any idea about how to connect with my mysql db which is running as a container in Docker on a virtual machine?

我对此一无所知,请帮助我.

I have no idea about it, please help me.

我想做的是:-我正在本地计算机上编写一个Java程序,现在我想与mysql建立jdbc连接.我的MySQL数据库在虚拟机上作为docker容器运行.

What I am trying to do is :- I am writing a java program on my local machine and now I want to establish a jdbc connection with mysql. My MySQL DB is running as a docker container on a Virtual machine.

有人有什么主意吗?

连接con = DriverManager.getConnection("jdbc:mysql://10.0.2.15/"我应该在这里放置什么" ,"root","myrootpassword");

Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.15/"what should I put here","root","myrootpassword");

我的容器IP地址是172.17.0.2,我的访客IP是10.0.2.15.我的SQL在端口3306上运行.

my ip address for the container is 172.17.0.2 and my guest ip is 10.0.2.15. My sql is running on port 3306.

预先感谢

推荐答案

您的docker容器应该能够将其mysql端口绑定到VM上的任何端口.您可以使用docker run-p VMPort:containerPort选项来完成此操作.

Your docker container should be able to bind its mysql port to any port on the VM. You do it with the -p VMPort:containerPort option of docker run.

https://docs.docker.com/engine/reference /run/#expose-incoming-ports

所以这个命令

docker run -p 3306:3306 your-sql-container

将容器的3306端口发布到VM的3306端口.

Will publish the 3306 port of your container to the 3306 port of your VM.

到那时,您应该能够使用

At that point you should be able to hit your SQL with

Connection con = DriverManager.getConnection("jdbc:mysql://10.0.2.15:3306/databaseName","root","myrootpassword");

我使用了您的VM地址和VM上的绑定端口.您应该用数据库的实际名称替换databaseName.

I used your VM address and the binded port on the VM. You should replace databaseName with the actual name of your DB.

这篇关于如何与在Docker中作为容器运行的MySQL DB连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆