使用jdbc [重复]在docker中连接mysql时出现问题

问题描述 投票:0回答:1

我对Docker和Java都是陌生的。我正在尝试从本地Ubuntu计算机上使用JDBC连接到Docker内部的mysql服务器。我用于在docker中启动mysql服务器的命令是:

docker network create --subnet=172.18.0.0/16 master_network    
docker run --net master_network --ip 172.18.0.22 --name docker-mysql -e MYSQL_ROOT_PASSWORD=password -d -p=3306:3306   mysql:latest 

我用于从docker连接到mysql服务器的命令如下:

String connectionUrl = "jdbc:mysql://172.18.0.22:3306/hb_student_tracker?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
Connection conn =  DriverManager.getConnection(connectionUrl,user,password);

但是我收到以下错误:

java.sql.SQLException: Access denied for user 'hbstudent'@'172.18.0.1' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at HibernateBasics.TestingConnectionToMysql.main(TestingConnectionToMysql.java:15)

docker inspect命令给我以下信息

[
{
    "Id": "90c8f3d5191fd207578a0b40e8a2369de949dc3a9c6fd2fdcf2ba044c32bdca5",
    "Created": "2020-02-15T01:19:14.021480835Z",
    "Path": "docker-entrypoint.sh",
    "Args": [
        "mysqld"
    ],
    "State": {
        "Status": "running",
        "Running": true,
        "Paused": false,
        "Restarting": false,
        "OOMKilled": false,
        "Dead": false,
        "Pid": 5105,
        "ExitCode": 0,
        "Error": "",
        "StartedAt": "2020-02-15T01:19:14.526528758Z",
        "FinishedAt": "0001-01-01T00:00:00Z"
    },
    "Image": "sha256:791b6e40940cd550af522eb4ffe995226798204504fe495743445b900e417a51",
    "ResolvConfPath": "/var/lib/docker/containers/90c8f3d5191fd207578a0b40e8a2369de949dc3a9c6fd2fdcf2ba044c32bdca5/resolv.conf",
    "HostnamePath": "/var/lib/docker/containers/90c8f3d5191fd207578a0b40e8a2369de949dc3a9c6fd2fdcf2ba044c32bdca5/hostname",
    "HostsPath": "/var/lib/docker/containers/90c8f3d5191fd207578a0b40e8a2369de949dc3a9c6fd2fdcf2ba044c32bdca5/hosts",
    "LogPath": "/var/lib/docker/containers/90c8f3d5191fd207578a0b40e8a2369de949dc3a9c6fd2fdcf2ba044c32bdca5/90c8f3d5191fd207578a0b40e8a2369de949dc3a9c6fd2fdcf2ba044c32bdca5-json.log",
    "Name": "/docker-mysql",
    "RestartCount": 0,
    "Driver": "overlay2",
    "Platform": "linux",
    "MountLabel": "",
    "ProcessLabel": "",
    "AppArmorProfile": "docker-default",
    "ExecIDs": null,
    "HostConfig": {
        "Binds": null,
        "ContainerIDFile": "",
        "LogConfig": {
            "Type": "json-file",
            "Config": {}
        },
        "NetworkMode": "master_network",
        "PortBindings": {
            "3306/tcp": [
                {
                    "HostIp": "",
                    "HostPort": "3306"
                }
            ]
        },
        "RestartPolicy": {
            "Name": "no",
            "MaximumRetryCount": 0
        },
        "AutoRemove": false,
        "VolumeDriver": "",
        "VolumesFrom": null,
        "CapAdd": null,
        "CapDrop": null,
        "Capabilities": null,
        "Dns": [],
        "DnsOptions": [],
        "DnsSearch": [],
        "ExtraHosts": null,
        "GroupAdd": null,
        "IpcMode": "private",
        "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": [],
        "BlkioDeviceReadBps": null,
        "BlkioDeviceWriteBps": null,
        "BlkioDeviceReadIOps": null,
        "BlkioDeviceWriteIOps": null,
        "CpuPeriod": 0,
        "CpuQuota": 0,
        "CpuRealtimePeriod": 0,
        "CpuRealtimeRuntime": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "Devices": [],
        "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": "/var/lib/docker/overlay2/794c33dc3dba3bd3f3b59c2fa0751abb4a08d256716791442e048eba8dad4718-init/diff:/var/lib/docker/overlay2/08cff1074288c594a797ee81fc4f8e6d65e56e3815ed779c59d6a12b69c4bef3/diff:/var/lib/docker/overlay2/d4965f2d4dc55cc40e6b33b7370ea8f735e06ba02bc6e9d9f302a023e2f0ab40/diff:/var/lib/docker/overlay2/2c4fa859d3bf60b43e2ad97889fa850cc02d82e55a3c61d951a1fa7fa8a37a23/diff:/var/lib/docker/overlay2/e0f465f72028a167ef41bf541b693e70c004053d429ee8c8992e690a7138d419/diff:/var/lib/docker/overlay2/28b6d7f4d734c945bc670e4d9741012bcf0a4e779e1797e0687f76969c413a95/diff:/var/lib/docker/overlay2/3d0721a625aca9d5a9c11d2d07a9ce365f0d03cf8033975557435ed43a6f0465/diff:/var/lib/docker/overlay2/58393e75098f068ca6b95c79f62f31f0774af97ebe53dc191b8b34c9c79b0ce4/diff:/var/lib/docker/overlay2/c656f68c525873355be411b15e2af708f47758f0efcc55e0dcf0de87f4f3859a/diff:/var/lib/docker/overlay2/a9b4a61a797212e7ebc6816a7b2f94fbfe74a9572e61fbf803748590eee89a5a/diff:/var/lib/docker/overlay2/e2db5f0234d764243d37f466e65b2e87913ece5744d806ecafcb596c937094ce/diff:/var/lib/docker/overlay2/06cc3c7e20d47b12dffed61bb7b95bf8055468bc65c981cb4f0d0a7080cc9880/diff:/var/lib/docker/overlay2/72eb0da71371fe94e46795d36ad0d103b92c6ee0e0aec48ee3888c16a1e72837/diff",
            "MergedDir": "/var/lib/docker/overlay2/794c33dc3dba3bd3f3b59c2fa0751abb4a08d256716791442e048eba8dad4718/merged",
            "UpperDir": "/var/lib/docker/overlay2/794c33dc3dba3bd3f3b59c2fa0751abb4a08d256716791442e048eba8dad4718/diff",
            "WorkDir": "/var/lib/docker/overlay2/794c33dc3dba3bd3f3b59c2fa0751abb4a08d256716791442e048eba8dad4718/work"
        },
        "Name": "overlay2"
    },
    "Mounts": [
        {
            "Type": "volume",
            "Name": "63948ece9ac10c7e0f6a5761684a5861051cfad101dec1374529ba016bb6973a",
            "Source": "/var/lib/docker/volumes/63948ece9ac10c7e0f6a5761684a5861051cfad101dec1374529ba016bb6973a/_data",
            "Destination": "/var/lib/mysql",
            "Driver": "local",
            "Mode": "",
            "RW": true,
            "Propagation": ""
        }
    ],
    "Config": {
        "Hostname": "90c8f3d5191f",
        "Domainname": "",
        "User": "",
        "AttachStdin": false,
        "AttachStdout": false,
        "AttachStderr": false,
        "ExposedPorts": {
            "3306/tcp": {},
            "33060/tcp": {}
        },
        "Tty": false,
        "OpenStdin": false,
        "StdinOnce": false,
        "Env": [
            "MYSQL_ROOT_PASSWORD=password",
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "GOSU_VERSION=1.7",
            "MYSQL_MAJOR=8.0",
            "MYSQL_VERSION=8.0.19-1debian9"
        ],
        "Cmd": [
            "mysqld"
        ],
        "Image": "mysql:latest",
        "Volumes": {
            "/var/lib/mysql": {}
        },
        "WorkingDir": "",
        "Entrypoint": [
            "docker-entrypoint.sh"
        ],
        "OnBuild": null,
        "Labels": {}
    },
    "NetworkSettings": {
        "Bridge": "",
        "SandboxID": "45769ba1596268ba02b3d6f2e50eaadd950b1113faed28f25f981408370272ab",
        "HairpinMode": false,
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "Ports": {
            "3306/tcp": [
                {
                    "HostIp": "0.0.0.0",
                    "HostPort": "3306"
                }
            ],
            "33060/tcp": null
        },
        "SandboxKey": "/var/run/docker/netns/45769ba15962",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null,
        "EndpointID": "",
        "Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "IPAddress": "",
        "IPPrefixLen": 0,
        "IPv6Gateway": "",
        "MacAddress": "",
        "Networks": {
            "master_network": {
                "IPAMConfig": {
                    "IPv4Address": "172.18.0.22"
                },
                "Links": null,
                "Aliases": [
                    "90c8f3d5191f"
                ],
                "NetworkID": "25371b13fc9c6f11c23e1d795364bbd5cc5a160de866335b058b807c9f6ce552",
                "EndpointID": "b3cb70c1cbee99170886dd9a60f2ca87534831694f947949fed596919712d668",
                "Gateway": "172.18.0.1",
                "IPAddress": "172.18.0.22",
                "IPPrefixLen": 16,
                "IPv6Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "MacAddress": "02:42:ac:12:00:16",
                "DriverOpts": null
            }
        }
    }
}

]

我已经提到过类似的stackoverflow问题,但仍无法弄清我的错误是什么。

mysql docker jdbc
1个回答
0
投票

java.sql.SQLException:对用户'hbstudent'@'172.18.0.1'的访问被拒绝(使用密码:是)

没有这样的用户hbstudent,或者在MySQL启动后没有正确定义凭据

© www.soinside.com 2019 - 2024. All rights reserved.