[英]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.