Keycloak Configuring MySQL Database

The new version of Keycloak comes with its own embedded dev-file database to persist data. This default database is designed to run instantly and so is suitable only for use in a test environment. In a production environment, more mature relational database must be used.

In this tutorial, you will learn how to configure Keycloak (powered by Quarkus) to use with MySQL database instead of its default dev-file database.

What You Need

Complete the steps below to set up Keycloak with MySQL:

Create New MySQL Database with a New User for Keycloak

Let's starts by creating a new MySQL database for Keycloak:

  1. Sign-in to your MySQL Server using your root MySQL credentials.
  2. Next, run the following MySQL commands one by one. The first command creates a new database keycloak, the second command creates a new user keycloak, and the third command grants all privileges to user keycloak:

CREATE DATABASE keycloak; 
CREATE USER 'keycloak'@'localhost' IDENTIFIED BY 'Keycloak123$';
GRANT ALL PRIVILEGES ON keycloak.* TO 'keycloak'@'localhost';

Configure Keycloak to use MySQL Database

  1. Go to conf directory of Keycloak Server.
  2. Open the keycloak.conf file with a text editor, such as Notepad.
  3. Uncomment and update the following properties in conf/keycloak.conf file:
  4. 
    db=mysql
    
    db-username=keycloak
    
    db-password=Keycloak123$
    
    #db-url=protocol//[host][/database]
    db-url=jdbc:mysql://localhost:3306/keycloak
    
    
  5. Next, go to bin directory:
  6. 
        cd keycloak-12.0.1/bin/
    
  7. Start the Keycloak Server:

  8. In Windows

    
        .\kc.bat start-dev
       

    In Linux / Ubuntu / Unix

    
        $ ./kc.sh start-dev
       

    It should start normally without any error if it was configured correctly.

  9. Go to browser and open http://localhost:8080. You should see the Keycloak Admin Console.

Learn what to do next here.