Keycloak migration from legacy to newer version

Hi,

I am running keycloak 4.1.0.Final and in process of upgrading to newer versions.
I have a docker compose setup that uses keycloak and mysql.

What I have found regardless of which version I use (19/18/17/16/15/14/13 etc), they call give me same problems.

At startup, I can see the db migration from liquibase kicking in and my table schema is updated, but after that I see a pattern across all the versions that for some reason I keep getting

Error during startup: org.keycloak.models.ModelException: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not prepare statement
followed by ROLLED_BACK and then my keycloak exits.

This is preventing me from upgrading keycloak to newer versions. Please help, as I have searched various forums and not able to get anything meaningful out.

When I try the same setup with fresh mysql database it works, but the moment I use my existing data in the database it gives me startup issues, I can confirm in databasechangelog, the migration ran and schema updated, but keycloak after that gives errors. I also tried running the same data setup for the version I am currently using and that works fine, only have problems when migrating existing data to newer version of keycloak

version: '3'

services:
  mysql:
      image: mysql:5.7
      ports:
        - 3366:3306
      command: --lower_case_table_names=1
      volumes:
        - mydb:/var/lib/mysql
        - ./data/KeyCloakLegacy.sql:/docker-entrypoint-initdb.d/initdb.sql
      environment:
        MYSQL_ROOT_PASSWORD: root
      healthcheck:
        test: "/usr/bin/mysql --user=<user>--password=<pwd>--database=keycloak --execute \"SELECT * FROM web_origins;\""
        interval: 10s
        timeout: 10s
        retries: 10
      
  keycloak:
      image: jboss/keycloak:13.0.0
      environment:
        JAVA_OPTS: "-server -Xms256m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djboss.as.management.blocking.timeout=7200 -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true"
        DB_VENDOR: MYSQL
        DB_ADDR: mysql
        DB_DATABASE: keycloak
        DB_USER: <user>
        DB_PASSWORD: <pwd>
        KEYCLOAK_ADMIN: admin
        KEYCLOAK_ADMIN_PASSWORD: admin
        KC_TRANSACTION_XA_ENABLED: false
        JDBC_PARAMS: "useSSL=false&connectTimeout=30000&allowPublicKeyRetrieval=true"
        KC_HTTP_ENABLED: true
        KC_HOSTNAME_STRICT: false
      command:
        ['-b', '0.0.0.0']
      ports:
        - 8080:8080
      depends_on:
        mysql:
          condition: service_healthy
      links:
        - mysql
      

volumes:
  mydb:
    driver: local

Maybe you could try upgrading version by version? From 4 to 5 to 6 and so on?

Thank you, we tried both major and minor version increments too, but kept having same issue.
Unfortunatly the logs don’t reflect much, but we think it may be due to corrupt data in DB

Thank you, it turned out the size of data being migrated was really large due to which the timeout period had to be extended for more than 4 hours. It worked and data is all migrated

1 Like