Keycloak 在升级(8 --> 12)和第一次重启后不启动

Keycloak doesn't start after upgrade(8 -->12) and first restart

我正在尝试将 keycloak(独立模式下的运行)从版本 8 升级到版本 12。我已按照提到的步骤进行操作 here

我删除了 data/tx-object-store/t运行saction 目录,并从版本 8 复制了 standalone 目录。
我运行升级脚本。我可以看到没有失败,所有步骤都是成功的。

我尝试用这个命令启动服务器

sudo ./standalone.sh -b 0.0.0.0 &

服务器启动成功,我可以访问keycloak管理控制台,也可以登录。我可以看到数据(用户、组等)也已成功迁移。

在此之后,我停止了 keycloak

sudo ./jboss-cli.sh --connect command=:shutdown

哪个 运行 好的。现在,如果我再次尝试启动它,我会看到以下错误并且 keycloak 无法启动

 06:30:00,080 FATAL [org.keycloak.services] (ServerService Thread Pool -- 66) Error during startup: java.lang.RuntimeException: Failed to connect to database
        at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.getConnection(DefaultJpaConnectionProviderFactory.java:377)
        at org.keycloak.connections.jpa.updater.liquibase.lock.LiquibaseDBLockProvider.lazyInit(LiquibaseDBLockProvider.java:65)
        at org.keycloak.connections.jpa.updater.liquibase.lock.LiquibaseDBLockProvider.lambda$waitForLock(LiquibaseDBLockProvider.java:96)
        at org.keycloak.models.utils.KeycloakModelUtils.suspendJtaTransaction(KeycloakModelUtils.java:654)
        at org.keycloak.connections.jpa.updater.liquibase.lock.LiquibaseDBLockProvider.waitForLock(LiquibaseDBLockProvider.java:94)
        at org.keycloak.services.resources.KeycloakApplication.run(KeycloakApplication.java:136)
        at org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction(KeycloakModelUtils.java:228)
        at org.keycloak.services.resources.KeycloakApplication.startup(KeycloakApplication.java:129)
        at org.keycloak.provider.wildfly.WildflyPlatform.onStartup(WildflyPlatform.java:29)
        at org.keycloak.services.resources.KeycloakApplication.<init>(KeycloakApplication.java:115)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at org.jboss.resteasy.core.ConstructorInjectorImpl.construct(ConstructorInjectorImpl.java:152)
        at org.jboss.resteasy.spi.ResteasyProviderFactory.createProviderInstance(ResteasyProviderFactory.java:2815)
        at org.jboss.resteasy.spi.ResteasyDeployment.createApplication(ResteasyDeployment.java:371)
        at org.jboss.resteasy.spi.ResteasyDeployment.startInternal(ResteasyDeployment.java:283)
        at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:93)
        at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:140)
        at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:42)
        at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:117)
        at org.wildfly.extension.undertow.security.RunAsLifecycleInterceptor.init(RunAsLifecycleInterceptor.java:78)
        at io.undertow.servlet.core.LifecyleInterceptorInvocation.proceed(LifecyleInterceptorInvocation.java:103)
        at io.undertow.servlet.core.ManagedServlet$DefaultInstanceStrategy.start(ManagedServlet.java:305)
        at io.undertow.servlet.core.ManagedServlet.createServlet(ManagedServlet.java:145)
        at io.undertow.servlet.core.DeploymentManagerImpl.call(DeploymentManagerImpl.java:588)
        at io.undertow.servlet.core.DeploymentManagerImpl.call(DeploymentManagerImpl.java:559)
        at io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:42)
        at io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
        at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create[=12=](SecurityContextThreadSetupAction.java:105)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1530)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1530)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1530)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=12=](UndertowDeploymentInfoService.java:1530)
        at io.undertow.servlet.core.DeploymentManagerImpl.start(DeploymentManagerImpl.java:601)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:97)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:78)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
        at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1990)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1486)
        at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1377)
        at java.lang.Thread.run(Thread.java:748)
        at org.jboss.threads.JBossThread.run(JBossThread.java:513)
Caused by: java.sql.SQLException: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:jboss/datasources/KeycloakDS
        at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:159)
        at org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:64)
        at org.keycloak.connections.jpa.DefaultJpaConnectionProviderFactory.getConnection(DefaultJpaConnectionProviderFactory.java:371)
        ... 45 more
Caused by: javax.resource.ResourceException: IJ000453: Unable to get managed connection for java:jboss/datasources/KeycloakDS
        at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:690)
        at org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerImpl.getManagedConnection(TxConnectionManagerImpl.java:440)
        at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:789)
        at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:151)
        ... 47 more
Caused by: javax.resource.ResourceException: IJ031084: Unable to create connection
        at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:345)
        at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.getLocalManagedConnection(LocalManagedConnectionFactory.java:352)
        at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createManagedConnection(LocalManagedConnectionFactory.java:287)
        at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.createConnectionEventListener(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:1322)
        at org.jboss.jca.core.connectionmanager.pool.mcp.SemaphoreConcurrentLinkedDequeManagedConnectionPool.getConnection(SemaphoreConcurrentLinkedDequeManagedConnectionPool.java:499)
        at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getSimpleConnection(AbstractPool.java:632)
        at org.jboss.jca.core.connectionmanager.pool.AbstractPool.getConnection(AbstractPool.java:604)
        at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.getManagedConnection(AbstractConnectionManager.java:624)
        ... 50 more
Caused by: org.h2.jdbc.JdbcSQLException: Constraint "FK_OUSE064PLMLR732LXJCN1Q5F1" already exists; SQL statement:
ALTER TABLE PUBLIC.SCOPE_MAPPING ADD CONSTRAINT PUBLIC.FK_OUSE064PLMLR732LXJCN1Q5F1 FOREIGN KEY(CLIENT_ID) INDEX PUBLIC.FK_OUSE064PLMLR732LXJCN1Q5F1_INDEX_8 REFERENCES PUBLIC.CLIENT(ID) NOCHECK [90045-197]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
        at org.h2.message.DbException.get(DbException.java:179)
        at org.h2.message.DbException.get(DbException.java:155)
        at org.h2.command.ddl.AlterTableAddConstraint.tryUpdate(AlterTableAddConstraint.java:110)
        at org.h2.command.ddl.AlterTableAddConstraint.update(AlterTableAddConstraint.java:78)
        at org.h2.engine.MetaRecord.execute(MetaRecord.java:58)
        at org.h2.engine.Database.open(Database.java:775)
        at org.h2.engine.Database.openDatabase(Database.java:286)
        at org.h2.engine.Database.<init>(Database.java:280)
        at org.h2.engine.Engine.openSession(Engine.java:66)
        at org.h2.engine.Engine.openSession(Engine.java:179)
        at org.h2.engine.Engine.createSessionAndValidate(Engine.java:157)
        at org.h2.engine.Engine.createSession(Engine.java:140)
        at org.h2.engine.Engine.createSession(Engine.java:28)
        at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:351)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:124)
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:103)
        at org.h2.Driver.connect(Driver.java:69)
        at org.jboss.jca.adapters.jdbc.local.LocalManagedConnectionFactory.createLocalManagedConnection(LocalManagedConnectionFactory.java:321)
        ... 57 more

我在内存数据库中使用 H2。我尝试安装 keycloak 12 并多次升级。每次都遇到同样的问题,第一次启动成功,然后就失败了。

有人可以帮忙吗?看起来当我第二次启动服务器时,它正在尝试再次进行迁移。但是不确定。

1.Get H2 1.4.196 的副本(此处和 Keycloak H2 login failure: constraint already exists 处指出的错误与 4.1.197 有关)

https://repo1.maven.org/maven2/com/h2database/h2/1.4.196/h2-1.4.196.jar

2.Get H2 1.4.197 或更高版本的副本

https://repo1.maven.org/maven2/com/h2database/h2/1.4.199/h2-1.4.199.jar

  1. 从升级前获取数据库备份的副本(我们的是从 4.​​6.0 开始的) 使用以下内容创建转储(将创建 backup.sql):
java -cp ~/ba-docker/h2-1.4.196.jar org.h2.tools.Script -url "jdbc:h2:./kc-data/keycloak;AUTO_SERVER=TRUE" -user sa -password sa (user and password can be confirmed in your standalone file)
  1. 使用较新版本的 H2 恢复数据库 命令默认使用 backup.sql
    java -cp ~/ba-docker/h2-1.4.199.jar org.h2.tools.RunScript -url "jdbc:h2:./kcdata-restore;AUTO_SERVER=TRUE" -user sa -password sa
  1. 在KC数据目录
  • 删除keycloak.mv.db和keycloak.trace.db
     rm keycloak.trace.db
     rm keycloak.mv.db
  • 将步骤 4 中的 kcdata-restore.mv.db 复制到 keycloak.mv.db
        cp ../../kcdata-restore.mv.db keycloak.mv.db
  1. 按照标准 KC 升级
  • 删除 tx-object-store
    rm -Rf tx-object-store/

您现在应该可以毫无问题地启动、停止和重新启动。

复制的答案来自:https://keycloak.discourse.group/t/keycloak-10-0-2-doesnt-start-locally-twice/3320 礼貌:@lieven_wk

尝试将 3.1.0.Final 升级到 11.0.2 时遇到同样的问题。发现这是由 https://github.com/h2database/h2database/issues/1247 26.

引起的

作为解决方法,我在启动新版本之前转储并恢复了 keycloak H2 数据源文件:

鉴于:

  • 旧的 KeyCloak 安装在 keycloak-3.1 中。0.Final
  • keycloak-11.0.2 中安装了新的 KeyCloak
  • 运行 独立设置
  • 数据源配置有以下连接URL jdbc:h2:${jboss.server.data.dir}/keycloak;AUTO_SERVER=TRUE

转储旧 sql:

java -cp keycloak-3.1.0.Final/modules/system/layers/base/com/h2database/h2/main/h2-1.3.173.jar org.h2.tools.Script -url jdbc:h2:keycloak-3.1.0.Final/standalone/data/keycloak -user sa -password MYPASS -script ~/keycloak-testbackup.sql

还原到新的 H2 文件(确保文件在 运行 还原之前不存在):

java -cp keycloak-11.0.2/modules/system/layers/base/com/h2database/h2/main/h2-1.4.197.jar org.h2.tools.RunScript -url jdbc:h2:keycloak-11.0.2/standalone/data/keycloak -user sa -password MYPASS -script ~/keycloak-testbackup.sql

现在,在第一次启动应用程序服务器 (keycloak-11.0.2) 时,架构会迁移到最新版本,并在每次重新启动时保持 运行 正确。