title: connecting to MYSQL in Docker with Intellij
body: <p>I am just starting to do projects with the help of Docker (well Docker Toolbox not Docker Desktop). I have had some problems connecting with a mysql database running inside a container. Basically I cannot do it. I already changed from Localhost to 127.0.0.1 as can be seen in this <a href="https://i.stack.imgur.com/ozlwP.png" rel="nofollow noreferrer">screenshot</a> <strong>This connection is made with the use of the IntelliJ GUI</strong> hence the screenshots to make it easier to understand
  This gave the error: The specified database user/password combination is rejected: com.mysql.cj.exceptions.CJException: Access denied for user 'User'@'192.168.99.1' (using password: YES)</p>
  
  <p>The docker-compose.yml is: </p>
  
  <pre><code>version: '3'
  services:
    mysql:
      image: 'mysql:8'
      ports:
        - '3306:3306'
      environment:
        - MYSQL_ROOT_PASSWORD=root
        - MYSQL_DATABASE=WebShopDatabase
        - MYSQL_USER=User
        - MYSQL_PASSWORD=Password
  </code></pre>
  
  <p>This container is up and running. I checked it with the docker ps, and afterwards the docker inspect docker_mysql_1: which showed the following:</p>
  
  <pre><code>[
  {
      "Id": "847ef4819da9e29b5ce6d0102ef8175c20b6d0a277583721f4bb28ddc3d3c2f6",
      "Created": "2020-05-23T19:17:21.33191426Z",
      "Path": "docker-entrypoint.sh",
      "Args": [
          "mysqld"
      ],
      "State": {
          "Status": "running",
          "Running": true,
          "Paused": false,
          "Restarting": false,
          "OOMKilled": false,
          "Dead": false,
          "Pid": 18196,
          "ExitCode": 0,
          "Error": "",
          "StartedAt": "2020-05-23T19:17:21.89238081Z",
          "FinishedAt": "0001-01-01T00:00:00Z"
      },
      "Image": "sha256:30f937e841c82981a9a6363f7f6f35ed6b9d5e3f16df50a72207e4a2a389983f",
      "ResolvConfPath": "/mnt/sda1/var/lib/docker/containers/847ef4819da9e29b5ce6d0102ef8175c20b6d0a277583721f4bb28ddc3d3c2f6/resolv.conf",
      "HostnamePath": "/mnt/sda1/var/lib/docker/containers/847ef4819da9e29b5ce6d0102ef8175c20b6d0a277583721f4bb28ddc3d3c2f6/hostname",
      "HostsPath": "/mnt/sda1/var/lib/docker/containers/847ef4819da9e29b5ce6d0102ef8175c20b6d0a277583721f4bb28ddc3d3c2f6/hosts",
      "LogPath": "/mnt/sda1/var/lib/docker/containers/847ef4819da9e29b5ce6d0102ef8175c20b6d0a277583721f4bb28ddc3d3c2f6/847ef4819da9e29b5ce6d0102ef8175c20b6d0a277583721f4bb28ddc3d3c2f6-json.log",
      "Name": "/docker_mysql_1",
      "RestartCount": 0,
      "Driver": "overlay2",
      "Platform": "linux",
      "MountLabel": "",
      "ProcessLabel": "",
      "AppArmorProfile": "",
      "ExecIDs": null,
      "HostConfig": {
          "Binds": [
              "673702fc973d8e1178ed0d791a7952fe49cf4f8f851149d0d0b9125d391e6390:/var/lib/mysql:rw"
          ],
          "ContainerIDFile": "",
          "LogConfig": {
              "Type": "json-file",
              "Config": {}
          },
          "NetworkMode": "docker_default",
          "PortBindings": {
              "3306/tcp": [
                  {
                      "HostIp": "",
                      "HostPort": "3306"
                  }
              ]
          },
          "RestartPolicy": {
              "Name": "",
              "MaximumRetryCount": 0
          },
          "AutoRemove": false,
          "VolumeDriver": "",
          "VolumesFrom": [],
          "CapAdd": null,
          "CapDrop": null,
          "Capabilities": null,
          "Dns": null,
          "DnsOptions": null,
          "DnsSearch": null,
          "ExtraHosts": null,
          "GroupAdd": null,
          "IpcMode": "shareable",
          "Cgroup": "",
          "Links": null,
          "OomScoreAdj": 0,
          "PidMode": "",
          "Privileged": false,
          "PublishAllPorts": false,
          "ReadonlyRootfs": false,
          "SecurityOpt": null,
          "UTSMode": "",
          "UsernsMode": "",
          "ShmSize": 67108864,
          "Runtime": "runc",
          "ConsoleSize": [
              0,
              0
          ],
          "Isolation": "",
          "CpuShares": 0,
          "Memory": 0,
          "NanoCpus": 0,
          "CgroupParent": "",
          "BlkioWeight": 0,
          "BlkioWeightDevice": null,
          "BlkioDeviceReadBps": null,
          "BlkioDeviceWriteBps": null,
          "BlkioDeviceReadIOps": null,
          "BlkioDeviceWriteIOps": null,
          "CpuPeriod": 0,
          "CpuQuota": 0,
          "CpuRealtimePeriod": 0,
          "CpuRealtimeRuntime": 0,
          "CpusetCpus": "",
          "CpusetMems": "",
          "Devices": null,
          "DeviceCgroupRules": null,
          "DeviceRequests": null,
          "KernelMemory": 0,
          "KernelMemoryTCP": 0,
          "MemoryReservation": 0,
          "MemorySwap": 0,
          "MemorySwappiness": null,
          "OomKillDisable": false,
          "PidsLimit": null,
          "Ulimits": null,
          "CpuCount": 0,
          "CpuPercent": 0,
          "IOMaximumIOps": 0,
          "IOMaximumBandwidth": 0,
          "MaskedPaths": [
              "/proc/asound",
              "/proc/acpi",
              "/proc/kcore",
              "/proc/keys",
              "/proc/latency_stats",
              "/proc/timer_list",
              "/proc/timer_stats",
              "/proc/sched_debug",
              "/proc/scsi",
              "/sys/firmware"
          ],
          "ReadonlyPaths": [
              "/proc/bus",
              "/proc/fs",
              "/proc/irq",
              "/proc/sys",
              "/proc/sysrq-trigger"
          ]
      },
      "GraphDriver": {
          "Data": {
              "LowerDir": "/mnt/sda1/var/lib/docker/overlay2/271d75cd7adccad82d1f498bf979541eb98a7680b578868a1342227b68afd950-init/diff:/mnt/sda1/var/lib/docker/overlay2/247631e3dcb755e0c6d06b98817d870d33afc3f0e56395589b635071f20ad7bc/diff:/mnt/sda1/var/lib/docker/overlay2/14fa4bd9669e091e620e16d692556fa90c6cf483e046d34b6c2b6005eb5967a8/diff:/mnt/sda1/var/lib/docker/overlay2/59b2da9f3817cbfafc7db9f9aa7ad66a07ce2078caf7d742eac74f0d38aa9ddb/diff:/mnt/sda1/var/lib/docker/overlay2/e05aec6f07c8db106fd4fa34d908af1b2c51fe7ee83b6a688038c4252dd1cb31/diff:/mnt/sda1/var/lib/docker/overlay2/f7b04fe4d47c0c4f6f4ae818b8758449ed5d1c67dfb41384a7c5778e18e0ccf2/diff:/mnt/sda1/var/lib/docker/overlay2/df137f272a8499c320bc6fe053e3b3823d9966922a84f17fc5833b4ab117df8c/diff:/mnt/sda1/var/lib/docker/overlay2/8a1f20096ed32eb7a47ad598e2f211cd7c0581dbf6d4e12867dcb68d4a088f6c/diff:/mnt/sda1/var/lib/docker/overlay2/0a6304a0808d7bff98748b6893419d1e6b707bd8a8b0c4311c859eb7913ea21c/diff:/mnt/sda1/var/lib/docker/overlay2/d798769efed5fe7612b7fddbb29f155f576a3351b6a4e19b063d0147592e0a96/diff:/mnt/sda1/var/lib/docker/overlay2/335fbab71322fc0bc7c4562f36ec985399ae8ab3fb3cc099326ab2dc9ec7c570/diff:/mnt/sda1/var/lib/docker/overlay2/8b563e8933ebf3a2a0656d54542cf8cc6624f3a3a38fecd8f734304e87d0c30e/diff:/mnt/sda1/var/lib/docker/overlay2/cf35e43c86791fd9332fcb49f99c03cc4fae513c1e73e87f46c670733aa64922/diff",
              "MergedDir": "/mnt/sda1/var/lib/docker/overlay2/271d75cd7adccad82d1f498bf979541eb98a7680b578868a1342227b68afd950/merged",
              "UpperDir": "/mnt/sda1/var/lib/docker/overlay2/271d75cd7adccad82d1f498bf979541eb98a7680b578868a1342227b68afd950/diff",
              "WorkDir": "/mnt/sda1/var/lib/docker/overlay2/271d75cd7adccad82d1f498bf979541eb98a7680b578868a1342227b68afd950/work"
          },
          "Name": "overlay2"
      },
      "Mounts": [
          {
              "Type": "volume",
              "Name": "673702fc973d8e1178ed0d791a7952fe49cf4f8f851149d0d0b9125d391e6390",
              "Source": "/mnt/sda1/var/lib/docker/volumes/673702fc973d8e1178ed0d791a7952fe49cf4f8f851149d0d0b9125d391e6390/_data",
              "Destination": "/var/lib/mysql",
              "Driver": "local",
              "Mode": "rw",
              "RW": true,
              "Propagation": ""
          }
      ],
      "Config": {
          "Hostname": "847ef4819da9",
          "Domainname": "",
          "User": "",
          "AttachStdin": false,
          "AttachStdout": false,
          "AttachStderr": false,
          "ExposedPorts": {
              "3306/tcp": {},
              "33060/tcp": {}
          },
          "Tty": false,
          "OpenStdin": false,
          "StdinOnce": false,
          "Env": [
              "MYSQL_ROOT_PASSWORD=root",
              "MYSQL_DATABASE=WebShopDatabase",
              "MYSQL_USER=User",
              "MYSQL_PASSWORD=Password",
              "affinity:container==e139d12a2fab507df99eb64c707735b9fd002c90807c3129b5abaf790ae17dca",
              "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
              "GOSU_VERSION=1.12",
              "MYSQL_MAJOR=8.0",
              "MYSQL_VERSION=8.0.20-1debian10"
          ],
          "Cmd": [
              "mysqld"
          ],
          "Image": "mysql:8",
          "Volumes": {
              "/var/lib/mysql": {}
          },
          "WorkingDir": "",
          "Entrypoint": [
              "docker-entrypoint.sh"
          ],
          "OnBuild": null,
          "Labels": {
              "com.docker.compose.config-hash": "47a45119997895f95f6a3be2f1420dec2853da47cdad9b8bd363c36fdfa20c0d",
              "com.docker.compose.container-number": "1",
              "com.docker.compose.oneoff": "False",
              "com.docker.compose.project": "docker",
              "com.docker.compose.service": "mysql",
              "com.docker.compose.version": "1.24.1"
          }
      },
      "NetworkSettings": {
          "Bridge": "",
          "SandboxID": "223adce42cbcf45c72b4ca47f301cac3c5774e6cc809679f4f029fc664727283",
          "HairpinMode": false,
          "LinkLocalIPv6Address": "",
          "LinkLocalIPv6PrefixLen": 0,
          "Ports": {
              "3306/tcp": [
                  {
                      "HostIp": "0.0.0.0",
                      "HostPort": "3306"
                  }
              ],
              "33060/tcp": null
          },
          "SandboxKey": "/var/run/docker/netns/223adce42cbc",
          "SecondaryIPAddresses": null,
          "SecondaryIPv6Addresses": null,
          "EndpointID": "",
          "Gateway": "",
          "GlobalIPv6Address": "",
          "GlobalIPv6PrefixLen": 0,
          "IPAddress": "",
          "IPPrefixLen": 0,
          "IPv6Gateway": "",
          "MacAddress": "",
          "Networks": {
              "docker_default": {
                  "IPAMConfig": null,
                  "Links": null,
                  "Aliases": [
                      "mysql",
                      "847ef4819da9"
                  ],
                  "NetworkID": "6cf406c0a4f20bc90d959131eead019d19a19c2c17aa054dad9843c470a21d6f",
                  "EndpointID": "109e141ca8096fcf2350093183f657cf457e80afae04085087fb3c6fb5903d41",
                  "Gateway": "172.18.0.1",
                  "IPAddress": "172.18.0.2",
                  "IPPrefixLen": 16,
                  "IPv6Gateway": "",
                  "GlobalIPv6Address": "",
                  "GlobalIPv6PrefixLen": 0,
                  "MacAddress": "02:42:ac:12:00:02",
                  "DriverOpts": null
              }
          }
      }
  }
  </code></pre>
  
  <p>Which clearly contains the docker-compose.yml stated here above. The IP of the container is 172.18.0.2. </p>
  
  <p>Since this did not work, I tried using the solution proposed <a href="https://stackoverflow.com/questions/54471608/unable-to-connect-from-intellij-to-mysql-running-in-docker-container-specifie">here</a>. Which gave me the following 
  <a href="https://i.stack.imgur.com/db2vc.png" rel="nofollow noreferrer">error see screenshot</a>. The IP of the docker container docker_mysql_1 (see screenshot all the way at the bottom and the output of the docker inspect just above) is 172.18.0.2. The error it gave was: [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  java.net.ConnectException: Connection timed out: connect.</p>
  
  <p>Afterwards it was pointed out that I should connect to the Docker Toolbox IP which is 192.168.99.100. I did this in an hellowrold project (of which the screenshots are, but I now have the same errors and only changed the docker-compose.yml in the new project whilst copying every other class so nothing has changed except for the docker-copose.yml which I have given above), and now started another project (again of which I only changed the docker-compose.yml to what it is above, and copied all other classes) and have the following error for as well as the localhost as the 192.168.99.100 connection: [08001] Could not create connection to database server. Attempted reconnect 3 times. Giving up.
  java.net.ConnectException: Connection timed out: connect. I checked if there were other containers running, there were none. </p>