Connect mysql server to keycloak 
Published: 2020-07-07 19:35:17

Keycloak works with embedded H2 database. This is the default database that Keycloak uses in the development environment. But for production, the proper thing to do is to connect with an external database like mysql, mongo db. Below we will see the steps for using MySQL Server:
**MYSQL SETUP**
Create a database for keycloak
1. mysql> CREATE USER ‘keycloak’@’%’ IDENTIFIED BY ‘keycloak’;
2. mysql> CREATE DATABASE keycloak CHARACTER SET utf8 COLLATE utf8_unicode_ci

3.mysql> GRANT ALL PRIVILEGES ON keycloak.* TO ‘keycloak’@’%’;a

**JDBC SETUP**

**Step 1**: Download the mysql Connector from
https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.42.zip
**Step 2**: Create a file named module.xml( the file exention should be .xml)
**Step 3**: Copy and paste the code below it inside the file you created above
```
<?xml version=”1.0" ?>
<module xmlns=”urn:jboss:module:1.3" name=”com.mysql”>
<resources>
<resource-root path=”mysql-connector-java-5.1.42-bin.jar” />
</resources>
<dependencies>
<module name=”javax.api”/>
<module name=”javax.transaction.api”/>
</dependencies>
</module>
```
**Step 4**:Cd to KEYCLOAK_HOME/modules/system/layers/keycloak/com
**Step 5**: Create a folder named ‘mysql’ inside ‘com’ folder then create a folder named ‘main’ inside the mysql folder
it should like this “KEYCLOAK_HOME/modules/system/layers/keycloak/com/mysql/main”
**Step 6**: Copy the mysql connector you downloaded and paste it inside the main folder
**Step 7**: Copy the module.xml file you created above, and paste inside the main folder
**Step 8**: Declare your packeaged JBDC driver .
You have to edit “KEYCLOAK_HOME/standalone/configuration/standalone.xml”
Inside drivers XML block within the datasources subsystem, you should see a pre-defined driver declared for the H2 JDBC driver. This is where you’ll declare the MySQL JDBC driver.
You should have something like this after declaring your mysql JBDC driver in the datasouces subsystem
```
<subsystem xmlns=”urn:jboss:domain:datasources:4.0">
<datasources>
…
<drivers>
<driver name=”mysql” module=”com.mysql”>
<driver-class>com.mysql.jdbc.Driver</driver-class>
</driver>
<driver name=”h2" module=”com.h2database.h2">
<xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
</driver>
</drivers>
</datasources>
</subsystem>
```
**Step 9**: Now we have to modify the existing datasource configuration that Keycloak uses to connect it to MySQL. This has to be done in the same file registered the mysql jdbc driver
Add the data source configuration to change from h2 database to MySQL.
My Sample configuration looks like this.
```
<datasource jndi-name=”java:/jboss/datasources/KeycloakDS” pool-name=”KeycloakDS” enabled=”true”>
<connection-url>jdbc:mysql://localhost:3306/keycloak?useSSL=false&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>keycloak</user-name>
<password>keycloak</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>
```
NOTE: make sure that you changed the database server IP, Username and Password for the database server as per your configurations.
You will have to comment out this place
```
<! — <datasource jndi-name=”java:jboss/datasources/KeycloakDS” pool-name=”KeycloakDS” enabled=”true” use-java-context=”true” statistics-enabled=”${wildfly.datasources.statistics-enabled:${wildfly.statistics-enabled:false}}”>
<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> !-->

```
**Step 10**: Lastly, you have to increase keyclaok server’s timeout, defaultly its 5 minutes, but its takes more than 5 minutes for keycloak to connect with mysql initially, so it keeps rolling back when its 5 minutes exactly.
To do this, You have to edit KEYCLOAK_HOME/standalone/configuration/standalone-ha.xml
* Change the session-bean defualt accesstime out to 20000 from 5000

```
<session-bean>
<stateless>
<bean-instance-pool-ref pool-name=”slsb-strict-max-pool”/>
</stateless>
<stateful default-access-timeout=”20000" cache-ref=”distributable” passivation-disabled-cache-ref=”simple”/>
<singleton default-access-timeout=”20000"/>
</session-bean>

```
* Change the pool instance-acquisition-timeout in the pool tag from 5 minutes to 20 minutes
```
<pools>
<bean-instance-pools>
<strict-max-pool name=”mdb-strict-max-pool” derive-size=”from-cpu-count” instance-acquisition-timeout=”20" instance-acquisition-timeout-unit=”MINUTES”/>
<strict-max-pool name=”slsb-strict-max-pool” derive-size=”from-worker-pools” instance-acquisition-timeout=”20" instance-acquisition-timeout-unit=”MINUTES”/>
</bean-instance-pools>
</pools>

```
* Change the distributed-cache expiration interval from 300000 900000 minutes
```
<distributed-cache name=”actionTokens” owners=”2">
<object-memory size=”-1"/>
<expiration max-idle=”-1" interval=”900000"/>
</distributed-cache>
```
Restart your server after this process, BOOM!!! your keyclaok server is connected. Thank you. I hope this help