Set keycloak to use mysql database

 

keycloak uses the embedded H2 database by default,

In a production environment, keycloak recommends using an external, more mature database.

 

keycloak implements data persistence and uses two layers of technology.

The bottom layer uses JDBC (when switching databases, you need to modify the relevant configuration).

The upper layer uses Hibernate JPA (configuration generally does not need to be modified).

 

Switch database steps:

1. Download the JDBC driver package for the database to be used.

2. Package the jdbc driver package into a module.

3. Configure the driver in the configuration file.

4. Configure the data source to use the above driver.

 

1. Download the JDBC driver package of the database to be used , for example: mysql-connector-java-5.1.18-bin.jar

2. Package the jdbc driver package into a module .

    2.1. Create new directories in sequence under the path keycloak\modules\system\layers\keycloak\:

            com\mysql\main (the directory name can be specified arbitrarily for easy understanding, and it is generally recommended to use the package path of the driver class)

    2.2. Copy mysql-connector-java-5.1.18-bin.jar to the com\mysql\main path.

    2.3. Create a new module.xml file under the path com\mysql\main (the format of the module.xml file can refer to other deployed modules)

 

            <?xml version="1.0" encoding="UTF-8"?>

            <!-- Specify the module name, which will be referenced by the driver configuration later -->

            <module xmlns="urn:jboss:module:1.3" name="com.mysql">

                <resources>

                      <resource-root path="mysql-connector-java-5.1.18-bin.jar"/> <!-- driver package name -->

                </resources>

               <dependencies>

                    <module name="javax.api"/>

                    <module name="javax.transaction.api"/>

               </dependencies>

           </module>

 

   2.4. After the creation is completed, it is as follows:

 

 

 

3. Configure the driver in the configuration file.

Find the standalone.xml file and add the mysql driver configuration.

 <drivers>

      <driver name="h2" module="com.h2database.h2">

           <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>

       </driver>

      <!-- 配置driver名称,指定引用的module名称  -->

       <driver name="mysql" module="com.mysql">

            <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>

       </driver>

</drivers>

 

4、配置数据源使用上面的驱动。

先注释原有h2数据源配置,再添加mysql数据源配置。

<!--

 <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" use-java-context="true">

         <connection-url>jdbc:h2:${jboss.server.data.dir}/keycloak;AUTO_SERVER=TRUE</connection-url>

          <driver>h2</driver>

          <security>

              <user-name>sa</user-name>

               password>sa</password>

          </security>

</datasource>

-->

<datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true" use-java-context="true">

                  <!-- database keycloak 需要事先创建好  -->

                   <connection-url>jdbc:mysql://localhost:3306/keycloak</connection-url>

                 <!--  指定引用的driver名称  -->

                  <driver>mysql</driver>

                  <pool>

                   <max-pool-size>20</max-pool-size>

                  </pool>

                  <security>

                    <user-name>root</user-name>

                    <password>123456a?</password>

                 </security>

 </datasource>

 

 

 5、启动keycloak,启动日志:

Calling "C:\Users\huangqiqing\Desktop\keycloak\安装包\keycloak-demo-3.4.3.Final-new\keycloak\bin\standalone.conf.bat"

Setting JAVA property to "C:\Program Files\Java\jdk1.8.0_131\bin\java"

===============================================================================

 

  JBoss Bootstrap Environment

 

  JBOSS_HOME: "C:\Users\huangqiqing\Desktop\keycloak\安装包\keycloak-demo-3.4.3.Final-new\keycloak"

 

  JAVA: "C:\Program Files\Java\jdk1.8.0_131\bin\java"

 

  JAVA_OPTS: "-Dprogram.name=standalone.bat -Xms64M -Xmx512M -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman"

 

===============================================================================

 

19:28:11,115 INFO  [org.jboss.modules] (main) JBoss Modules version 1.6.1.Final

19:28:11,550 INFO  [org.jboss.msc] (main) JBoss MSC version 1.2.7.SP1

19:28:11,679 INFO  [org.jboss.as] (MSC service thread 1-8) WFLYSRV0049: WildFly Full 11.0.0.Final (WildFly Core 3.0.8.Final) starting

19:28:14,294 INFO  [org.jboss.as.controller.management-deprecated] (Controller Boot Thread) WFLYCTL0028: Attribute 'security-realm' in the resource at address '/core-service=management/management-interface=http-interface' is deprecated, and may be removed in future version. See the attribute description in the output of the read-resource-description operation to learn more about the deprecation.

19:28:14,344 INFO  [org.wildfly.security] (ServerService Thread Pool -- 32) ELY00001: WildFly Elytron version 1.1.6.Final

19:28:14,369 INFO  [org.jboss.as.controller.management-deprecated] (ServerService Thread Pool -- 5) WFLYCTL0028: Attribute 'security-realm' in the resource at address '/subsystem=undertow/server=default-server/https-listener=https' is deprecated, and may be removed in future version. See the attribute description in the output of the read-resource-description operation to learn more about the deprecation.

19:28:14,766 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0039: Creating http management service using socket-binding (management-http)

19:28:14,833 INFO  [org.xnio] (MSC service thread 1-5) XNIO version 3.5.4.Final

19:28:14,846 INFO  [org.xnio.nio] (MSC service thread 1-5) XNIO NIO Implementation Version 3.5.4.Final

19:28:14,932 WARN  [org.jboss.as.txn] (ServerService Thread Pool -- 61) WFLYTX0013: The node-identifier attribute on the /subsystem=transactions is set to the default value. This is a danger for environments running multiple servers. Please make sure the attribute value is unique.

19:28:15,053 INFO  [org.jboss.as.jaxrs] (ServerService Thread Pool -- 46) WFLYRS0016: RESTEasy version 3.0.24.Final

19:28:15,033 INFO  [org.jboss.as.jsf] (ServerService Thread Pool -- 51) WFLYJSF0007: Activated the following JSF Implementations: [main]

19:28:15,007 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 45) WFLYCLINF0001: Activating Infinispan subsystem.

19:28:14,997 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 53) WFLYNAM0001: Activating Naming Subsystem

19:28:14,975 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 63) WFLYWS0002: Activating WebServices Extension

19:28:14,973 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 60) WFLYSEC0002: Activating Security Subsystem

19:28:15,234 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 39) WFLYJCA0004: Deploying JDBC-compliant driver class org.h2.Driver (version 1.4)

19:28:15,182 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0003: Undertow 1.4.18.Final starting

19:28:15,253 INFO  [org.jboss.as.connector] (MSC service thread 1-8) WFLYJCA0009: Starting JCA Subsystem (WildFly/IronJacamar 1.4.6.Final)

19:28:15,267 INFO  [org.jboss.as.security] (MSC service thread 1-6) WFLYSEC0001: Current PicketBox version=5.0.2.Final

19:28:15,293 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-7) WFLYJCA0018: Started Driver service with driver-name = h2

19:28:15,276 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 39) WFLYJCA0005: Deploying non-JDBC-compliant driver class com.mysql.jdbc.Driver (version 5.1)

19:28:15,347 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) WFLYJCA0018: Started Driver service with driver-name = mysql

19:28:15,384 INFO  [org.jboss.as.naming] (MSC service thread 1-8) WFLYNAM0003: Starting Naming Service

19:28:15,394 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-3) WFLYMAIL0001: Bound mail session [java:jboss/mail/Default]

19:28:15,655 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 62) WFLYUT0014: Creating file handler for path 'C:\Users\huangqiqing\Desktop\keycloak\安装包\keycloak-demo-3.4.3.Final-new\keycloak/welcome-content' with options [directory-listing: 'false', follow-symlink: 'false', case-sensitive: 'true', safe-symlink-paths: '[]']

19:28:16,580 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 44) WFLYIO001: Worker 'default' has auto-configured to 8 core threads with 64 task threads based on your 4 available processors

19:28:16,585 INFO  [org.jboss.as.ejb3] (MSC service thread 1-6) WFLYEJB0481: Strict pool slsb-strict-max-pool is using a max instance size of 64 (per class), which is derived from thread worker pool sizing.

19:28:16,588 INFO  [org.jboss.as.ejb3] (MSC service thread 1-1) WFLYEJB0482: Strict pool mdb-strict-max-pool is using a max instance size of 16 (per class), which is derived from the number of CPUs on this host.

19:28:16,690 INFO  [org.jboss.remoting] (MSC service thread 1-3) JBoss Remoting version 5.0.5.Final

19:28:16,734 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-5) WFLYUT0012: Started server default-server.

19:28:16,802 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0018: Host default-host starting

19:28:16,818 WARN  [org.jboss.as.domain.management.security] (MSC service thread 1-1) WFLYDM0111: Keystore C:\Users\huangqiqing\Desktop\keycloak\安装包\keycloak-demo-3.4.3.Final-new\keycloak\standalone\configuration\application.keystore not found, it will be auto generated on first use with a self signed certificate for host localhost

19:28:16,820 INFO  [org.jboss.as.patching] (MSC service thread 1-2) WFLYPAT0050: WildFly Full cumulative patch ID is: base, one-off patches include: none

19:28:16,841 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-7) WFLYDS0013: Started FileSystemDeploymentService for directory C:\Users\huangqiqing\Desktop\keycloak\安装包\keycloak-demo-3.4.3.Final-new\keycloak\standalone\deployments

19:28:16,868 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-6) WFLYSRV0027: Starting deployment of "keycloak-server.war" (runtime-name: "keycloak-server.war")

19:28:16,911 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-5) WFLYUT0006: Undertow HTTP listener default listening on 127.0.0.1:8080

19:28:16,970 INFO  [org.jboss.as.ejb3] (MSC service thread 1-3) WFLYEJB0493: EJB subsystem suspension complete

19:28:17,161 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]

19:28:17,168 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Bound data source [java:jboss/datasources/KeycloakDS]

19:28:17,852 INFO  [org.infinispan.factories.GlobalComponentRegistry] (MSC service thread 1-2) ISPN000128: Infinispan version: Infinispan 'Chakra' 8.2.8.Final

19:28:17,889 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) WFLYUT0006: Undertow HTTPS listener https listening on 127.0.0.1:8443

19:28:18,185 INFO  [org.jboss.ws.common.management] (MSC service thread 1-4) JBWS022052: Starting JBossWS 5.1.9.Final (Apache CXF 3.1.12)

19:28:18,379 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 68) WFLYCLINF0002: Started keys cache from keycloak container

19:28:18,387 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 77) WFLYCLINF0002: Started work cache from keycloak container

19:28:18,387 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 72) WFLYCLINF0002: Started realms cache from keycloak container

19:28:18,386 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 74) WFLYCLINF0002: Started offlineSessions cache from keycloak container

19:28:18,386 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 75) WFLYCLINF0002: Started actionTokens cache from keycloak container

19:28:18,385 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 76) WFLYCLINF0002: Started sessions cache from keycloak container

19:28:18,386 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 78) WFLYCLINF0002: Started authenticationSessions cache from keycloak container

19:28:18,385 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 80) WFLYCLINF0002: Started authorization cache from keycloak container

19:28:18,384 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 73) WFLYCLINF0002: Started offlineClientSessions cache from keycloak container

19:28:18,385 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 69) WFLYCLINF0002: Started clientSessions cache from keycloak container

19:28:18,384 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 70) WFLYCLINF0002: Started loginFailures cache from keycloak container

19:28:18,384 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 71) WFLYCLINF0002: Started users cache from keycloak container

19:28:18,390 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 79) WFLYCLINF0002: Started client-mappings cache from ejb container

19:28:19,291 INFO  [org.keycloak.services] (ServerService Thread Pool -- 77) KC-SERVICES0001: Loading config from standalone.xml or domain.xml

19:28:19,654 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 77) WFLYCLINF0002: Started realmRevisions cache from keycloak container

19:28:19,666 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 77) WFLYCLINF0002: Started userRevisions cache from keycloak container

19:28:19,699 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 77) WFLYCLINF0002: Started authorizationRevisions cache from keycloak container

19:28:19,699 INFO  [org.keycloak.connections.infinispan.DefaultInfinispanConnectionProviderFactory] (ServerService Thread Pool -- 77) Node name: huangqiqing, Site name: null

19:28:23,193 INFO  [org.keycloak.connections.jpa.updater.liquibase.LiquibaseJpaUpdaterProvider] (ServerService Thread Pool -- 77) Initializing database schema. Using changelog META-INF/jpa-changelog-master.xml

19:29:54,976 INFO  [org.hibernate.jpa.internal.util.LogHelper] (ServerService Thread Pool -- 77) HHH000204: Processing PersistenceUnitInfo [

        name: keycloak-default

        ...]

19:29:55,047 INFO  [org.hibernate.Version] (ServerService Thread Pool -- 77) HHH000412: Hibernate Core {5.1.10.Final}

19:29:55,050 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 77) HHH000206: hibernate.properties not found

19:29:55,053 INFO  [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 77) HHH000021: Bytecode provider name : javassist

19:29:55,089 INFO  [org.hibernate.annotations.common.Version] (ServerService Thread Pool -- 77) HCANN000001: Hibernate Commons Annotations {5.0.1.Final}

19:29:55,272 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 77) HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect

19:29:55,320 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 77) Envers integration enabled? : true

19:29:55,875 INFO  [org.hibernate.validator.internal.util.Version] (ServerService Thread Pool -- 77) HV000001: Hibernate Validator 5.3.5.Final

19:29:56,854 INFO  [org.hibernate.hql.internal.QueryTranslatorFactoryInitiator] (ServerService Thread Pool -- 77) HHH000397: Using ASTQueryTranslatorFactory

19:29:58,467 INFO  [org.keycloak.services] (ServerService Thread Pool -- 77) KC-SERVICES0050: Initializing master realm

19:29:59,739 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002225: Deploying javax.ws.rs.core.Application: class org.keycloak.services.resources.KeycloakApplication

19:29:59,743 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002200: Adding class resource org.keycloak.services.resources.ThemeResource from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,744 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002200: Adding class resource org.keycloak.services.resources.JsResource from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,745 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002205: Adding provider class org.keycloak.services.filters.KeycloakTransactionCommitter from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,746 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002205: Adding provider class org.keycloak.services.error.KeycloakErrorHandler from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,746 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002220: Adding singleton resource org.keycloak.services.resources.WelcomeResource from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,747 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002210: Adding provider singleton org.keycloak.services.util.ObjectMapperResolver from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,747 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002220: Adding singleton resource org.keycloak.services.resources.RobotsResource from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,749 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002220: Adding singleton resource org.keycloak.services.resources.RealmsResource from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,750 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002220: Adding singleton resource org.keycloak.services.resources.admin.AdminRoot from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,753 INFO  [org.jboss.resteasy.resteasy_jaxrs.i18n] (ServerService Thread Pool -- 77) RESTEASY002220: Adding singleton resource org.keycloak.services.resources.ServerVersionResource from Application class org.keycloak.services.resources.KeycloakApplication

19:29:59,854 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 77) WFLYUT0021: Registered web context: '/auth' for server 'default-server'

19:29:59,879 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 65) WFLYSRV0010: Deployed "keycloak-server.war" (runtime-name : "keycloak-server.war")

19:29:59,966 INFO  [org.jboss.as.server] (Controller Boot Thread) WFLYSRV0212: Resuming server

19:29:59,975 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management

19:29:59,976 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990

 

19:29:59,977 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 11.0.0.Final (WildFly Core 3.0.8.Final) started in 109309ms - Started 576 of 915 services (610 services are lazy, passive or on-demand)

 

 

通过数据库客户端工具,连接数据库,查看(会发现自动创建了91张表):




 
 
 

 

至此,keycloak配置使用mysql数据库完成。
 

 

 

 

更多信息参考官网:

https://www.keycloak.org/docs/latest/server_installation/index.html#_database

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325335253&siteId=291194637