Connect to MySQL on localhost from Docker container

前端 未结 2 796
栀梦
栀梦 2020-12-14 10:41

I have mysql running on my localhost I can connect it by running:

mysql -h 127.0.0.1 -P 3306 -u root -p

I also ran docker container with co

2条回答
  •  臣服心动
    2020-12-14 10:48

    Use the default route. For example:

    ~# ip route show | grep "default" | awk '{print $3}'
    172.18.0.1
    

    then

    mysql -h 172.18.0.1 -P 3306 -u root -p
    

    if you need to automate that, let's say, use that IP on a shell script, send it to a shell script, take it as:

    host=$(ip route show | grep \"default\" | awk '{print $3}' | xargs echo -n)
    

    then you have the host ip addr on $host

提交回复
热议问题