简体   繁体   English

没有主机模式,mysql从docker到localhost的jdbc url不起作用

[英]jdbc url for mysql from docker to localhost does not work without host mode

I have a local spring boot application that connects to local MySQL and it works fine. 我有一个连接到本地MySQL的本地spring boot应用程序,它工作正常。

For connection I use the following property: 对于连接,我使用以下属性:

spring.datasource.url=jdbc:mysql://localhost:3306/pitstop?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

I would like to put my app in docker and try to connect to local DB. 我想将我的应用程序放在docker中并尝试连接到本地数据库。
So I need to modify MySQL url . 所以我需要修改MySQL url

I used this command to obtain local IP ip route show | grep "default" | awk '{print $3}' 我使用此命令获取本地IP ip route show | grep "default" | awk '{print $3}' ip route show | grep "default" | awk '{print $3}' ip route show | grep "default" | awk '{print $3}' the result is 192.168.1.1 . ip route show | grep "default" | awk '{print $3}'结果为192.168.1.1 I modify my url like this 我这样修改我的网址

spring.datasource.url=jdbc:mysql://192.168.1.1:3306/pitstop?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=UTC

and try to start docker container with my app by command docker run -p 9001:9001 --network=bizon4ik --rm bizon4ik/mycontainer the result is the exception: 然后尝试通过命令docker run -p 9001:9001 --network=bizon4ik --rm bizon4ik/mycontainer用我的应用程序启动docker run -p 9001:9001 --network=bizon4ik --rm bizon4ik/mycontainer ,结果是例外:

Caused by: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

I had tried to find another IP. 我试图找到另一个IP。 I used ip addr show command and found the next record: 我使用ip addr show命令并找到下一条记录:

 3: wlp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 30:52:cb:db:8d:e0 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.102/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp3s0

So based on it I took 192.168.1.102 and modify my url but the result is the same exception. 因此,基于此,我使用了192.168.1.102并修改了我的网址,但结果是相同的异常。

To be sure that founded IPs are correct I run new one container with pure ubuntu docker run -it --rm --network=bizon4ik ubuntu and checked the mentioned above IP: 为了确保已建立的IP是正确的,我使用纯ubuntu docker run -it --rm --network=bizon4ik ubuntu运行了一个新容器并检查了上述IP:

root@268c4d544328:/# nmap -p 3306 192.168.1.1

Starting Nmap 7.60 ( https://nmap.org ) at 2019-07-28 17:28 UTC
Nmap scan report for 192.168.1.1
Host is up (0.053s latency).

PORT     STATE    SERVICE
3306/tcp filtered mysql
root@268c4d544328:/# nmap -p 3306 192.168.1.102   

Starting Nmap 7.60 ( https://nmap.org ) at 2019-07-28 15:56 UTC
Nmap scan report for my-host (192.168.1.102)
Host is up (0.000088s latency).

PORT     STATE  SERVICE
3306/tcp closed mysql

So looks fine, the ubuntu container can ping my DB. 这样看起来很好,ubuntu容器可以ping我的数据库。 Do you have any ideas on why I cannot connect through the app to DB? 您对我为什么无法通过应用程序连接到数据库有任何想法?

NB: 注意:

I checked in DB SHOW GRANTS; 我检查了DB SHOW GRANTS; the result is GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION 结果是将GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION

I also checked /etc/mysql/my.cnf file. 我还检查了/etc/mysql/my.cnf文件。 It has only these records: 它只有以下记录:

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

I don't have ~/.my.cnf 我没有~/.my.cnf

The problem was in bind-address for MySQL. 问题出在MySQL的bind-address中。 I looked it in the /etc/mysql/my.cnf however, the right place is /etc/mysql/mysql.conf.d/mysqld.cnf 我在/etc/mysql/my.cnf查看了它,但是正确的位置是/etc/mysql/mysql.conf.d/mysqld.cnf

暂无
暂无

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

相关问题 Docker-Compose:将 JDBC 从 localhost 更改为 mysql - Docker-Compose: Change JDBC from localhost to mysql 如何使用从主机到 docker mysql 服务器的 JDBC 连接 - How to use JDBC connection from host to a docker mysql server 如何从docker容器连接主机&#39;localhost&#39; - How to connect host 'localhost' from docker container 无法为连接URL&#39;jdbc:mysql // localhost:3306 /创建类&#39;com.mysql.jdbc.Driver&#39;的JDBC驱动程序 - Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/ Spring + Hibernate + Maven:j无法为连接URL“ jdbc:mysql // localhost:3306 / test”创建类“ com.mysql.jdbc.Driver”的JDBC驱动程序 - Spring + Hibernate + Maven: jCannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/test' 不使用 Docker 命令将文件从主机复制到 Docker 容器 - Copy files from host to Docker container without using Docker command 驱动程序“ com.mysql.jdbc.Driver”不支持URL“空” - Driver “com.mysql.jdbc.Driver” does not support the url “null” 无法为连接 URL &#39;jdbc:mysql://localhost:3306/web13?useSSL=false&amp;serverTimezone=UTC&#39; 创建类 &#39;&#39; 的 JDBC 驱动程序 - Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://localhost:3306/web13?useSSL=false&serverTimezone=UTC' 在Java程序中使用JDBC驱动程序是否在没有反射的情况下工作? - Does using a JDBC driver in a Java program work without reflection? 为什么 localhost 可以工作,但外部 URL 在 tomcat 中不起作用? - Why does localhost work but external URL doesn't in tomcat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM