You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java新手求助:Keycloak自定义用户存储Provider连接MySQL失败

Fixing "Unable to acquire JDBC Connection" for Keycloak JPA User Storage with MySQL

Hey there! Let's troubleshoot this MySQL connection issue you're hitting with your Keycloak JPA user storage provider. That org.hibernate.exception.GenericJDBCException usually boils down to a handful of common configuration or environment issues—let's go through them one by one.

1. Update the MySQL Driver Class (Critical for 8.x Connector)

Your pom uses MySQL Connector/J 8.0.11, but your persistence.xml is still using the old driver class com.mysql.jdbc.Driver. MySQL 8.x changed this to com.mysql.cj.jdbc.Driver. Update that property:

<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />

2. Fix the JDBC URL with Timezone Parameter

MySQL 8.x requires a timezone setting in the JDBC URL to avoid connection errors. Add serverTimezone (use your local timezone or UTC) to your URL:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:32773/customer?serverTimezone=UTC" />

(If you're in a different timezone, replace UTC with something like Asia/Shanghai.)

3. Use the Correct Hibernate Dialect for MySQL 8.x

You're using org.hibernate.dialect.MySQLDialect, which is outdated for MySQL 8.x. Switch to the version-specific dialect:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />

4. Ensure MySQL Connector is Packaged with Your Provider

Keycloak needs the MySQL driver to be present in your user storage provider JAR. By default, Maven might not include it, so add the maven-shade-plugin to your pom.xml to bundle the dependency:

<build>
    <plugins>
        <!-- Existing Keycloak plugin -->
        <plugin>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-maven-plugin</artifactId>
            <version>${keycloak.version}</version>
            <executions>
                <execution>
                    <id>build</id>
                    <goals>
                        <goal>build</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <!-- Shade plugin to bundle MySQL driver -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <includes>
                                <include>mysql:mysql-connector-java</include>
                            </includes>
                        </artifactSet>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

5. Verify Basic MySQL Connectivity

Before blaming your Keycloak config, double-check that you can reach the MySQL instance:

  • Confirm the MySQL service is running (either locally or in Docker, since your port is 32773 which looks like a Docker mapped port).
  • Use a MySQL client to test the connection:
    mysql -h localhost -P 32773 -u root -p root customer
    
  • Make sure the customer database actually exists (if not, create it first).

6. Check for Firewall/Network Issues

If MySQL is running in a Docker container or on a remote machine, ensure port 32773 is open and accessible from your Keycloak server.

After making these changes, rebuild your provider JAR, deploy it to Keycloak's providers directory, and restart Keycloak. That should resolve the JDBC connection issue.

内容的提问来源于stack exchange,提问作者titimoun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:35:37