简体   繁体   中英

keycloak standalone integration with mysql-innodb-cluster

Trying to get keycloak to work with mysql-innodb-cluster. I have configured the keycloak standalone.xml as per the documentation.

This is the datasource

            <datasource jndi-name="java:/jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true">
                <connection-url>jdbc:mysql://19.57.1.115:6446/keycloak?useSSL=false&amp;characterEncoding=UTF-8</connection-url>
                <driver>mysql</driver>
                <pool>
                    <min-pool-size>5</min-pool-size>
                    <max-pool-size>15</max-pool-size>
                </pool>
                <security>
                    <user-name>key</user-name>
                    <password>abababab</password>
                </security>
                <validation>
                    <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
                    <validate-on-match>true</validate-on-match>
                    <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
                </validation>
            </datasource>

This is the drivers

            <drivers>
                <driver name="mysql" module="com.mysql">
                    <driver-class>com.mysql.cj.jdbc.Driver</driver-class>
                </driver>
                <driver name="h2" module="com.h2database.h2">
                    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
                </driver>
            </drivers>

I've also added module.xml for packaging mysql jdbc driver (I'm using latest version mysql-connector-java-8.0.21.jar)

The error I'm getting when I run keycloak is

10:45:23,155 INFO  [org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider] (ServerService Thread Pool -- 66) Initializing database schema. Using changelog META-INF/jpa-changelog-master.xml
10:45:24,088 ERROR [org.keycloak.connections.jpa.updater.liquibase.conn.DefaultLiquibaseConnectionProvider] (ServerService Thread Pool -- 66) Change Set META-INF/jpa-changelog- 
1.0.0.Final.xml::1.0.0.Final-KEYCLOAK-5461::sthorger@redhat.com failed.  Error: Table 'APPLICATION_DEFAULT_ROLES' already exists [Failed SQL: CREATE TABLE keycloak.APPLICATION_DEFAULT_ROLES (APPLICATION_ID VARCHAR(36) NOT NULL, ROLE_ID VARCHAR(36) NOT NULL)]
10:45:24,414 FATAL [org.keycloak.services] (ServerService Thread Pool -- 66) java.lang.RuntimeException: Failed to update database
10:45:24,446 INFO  [org.jboss.as.server] (Thread-2) WFLYSRV0220: Server shutdown has been requested via an OS signal

Any help in this regards would be very helpful.

I think the error "Error: Table 'APPLICATION_DEFAULT_ROLES' already exists" you see is not the root cause. Keycloak uses Liquibase for database initialisation and upgrade management. Liquibase does not seem to support MySQL innodb cluster since it creates a table "DATABASECHANGELOG" without a primary key . If you check your MySLQ log you will probably see the following log message at the first start of keycloak when it creates its database tables:

[ERROR] Plugin group_replication reported: 'Table DATABASECHANGELOG does not have any PRIMARY KEY. This is not compatible with Group Replication' 

The table now exists on your database primary but has not been replicated to the other replication group members. If you then start Keycloak for a second time you will get error message "Table 'APPLICATION_DEFAULT_ROLES' already exists" you have seen.

You can work around this issue if you start with an empty database schema, create the DATABASECHANGELOG table with a primary key manually and then let Keycloak (that is Liquibase actually) create the rest of the tables.

You can use the following statement to create the table:

CREATE TABLE `DATABASECHANGELOG` (
  `ID` varchar(255) NOT NULL,
  `AUTHOR` varchar(255) NOT NULL,
  `FILENAME` varchar(255) NOT NULL,
  `DATEEXECUTED` datetime NOT NULL,
  `ORDEREXECUTED` int(11) NOT NULL,
  `EXECTYPE` varchar(10) NOT NULL,
  `MD5SUM` varchar(35) DEFAULT NULL,
  `DESCRIPTION` varchar(255) DEFAULT NULL,
  `COMMENTS` varchar(255) DEFAULT NULL,
  `TAG` varchar(255) DEFAULT NULL,
  `LIQUIBASE` varchar(20) DEFAULT NULL,
  `CONTEXTS` varchar(255) DEFAULT NULL,
  `LABELS` varchar(255) DEFAULT NULL,
  `DEPLOYMENT_ID` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`ID`,`AUTHOR`,`FILENAME`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM