Unable to add MySQL data source to WildFly

I’m having some trouble adding a MySQL data source to WildFly. I’ve installed (perhaps incorrectly?) the MySQL driver as shown below:

MySQL driver as shown in WildFly

Then, when I go to add the data source, MySQL is missing from the list of available driver names:

MySQL driver is missing when trying to add datasource

I suspect I must have installed the driver incorrectly as the driver version is missing from my first screenshot, and nothing is shown under JDBC compliant. Not sure if this is normal.

I have followed a number of guides to install the driver, and this is how I have done it. I downloaded the driver from https://dev.mysql.com/downloads/connector/j/. The driver is located in the following file path:

wildfly-22.0.0.Finalmodulessystemlayersbasecommysqlmain

and this is what that folder structure looks like:

mysql main folder structure

This is the contents of module.xml:

<module name="com.mysql" xmlns="urn:jboss:module:1.5">
    <resources>
        <resource-root path="mysql-connector-java-8.0.23.jar">
    </resource-root></resources>
    <dependencies>
        <module name="javax.api">
        <module name="javax.transaction.api">
    </module></module></dependencies>
</module>

This is what the relevant driver section in standalone.xml looks like (including the h2 and derby-main drivers which seem to work fine):

<drivers>
   <driver name="h2" module="com.h2database.h2">
      <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
   </driver>
   <driver name="derby-mem" module="org.apache.derby.mem"/>
   <driver name="mysql" module="com.mysql">
      <driver-class>com.mysql.jdbc.Driver</driver-class>
      <xa-datasource-class>
         com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
      </xa-datasource-class>
   </driver>
</drivers>

I must be missing something obvious here.

Answer

I would strongly recommend using a script for this instead of the UI. It is easier to maintain and upgrade.

Create a file called something like create_resources.cli that contains:

embed-server --server-config=standalone.xml --std-out=echo
    
batch

#
# add the module
#
module add --name=com.mysql --resources=${user.home}/Downloads/mysql-connector-java-8.0.23/mysql-connector-java-8.0.23.jar --dependencies=javax.api,javax.transaction.api

#
# create the driver
#
/subsystem=datasources/jdbc-driver=mysql:add(driver-name="mysql",driver-module-name="com.mysql",driver-class-name=com.mysql.cj.jdbc.Driver)

#
# create the datasource
#
/subsystem=datasources/data-source=YourDSName/:add(connection-url=jdbc:mysql://localhost:3306/yourDBName,driver-name=mysql,jndi-name=java:jboss/datasources/YourDSName,password=thepassword,user-name=theuser)

run-batch

Now, with Wildfly stopped, change to the Wildfly installation directory and run

binjboss-cli.bat --file=create_resources.cli

This will do the following:

  1. Add the module for you. I have the driver in the Downloads directory in your home directory. Obviously it can be wherever you’d like.
  2. Add a driver named “mysql”
  3. Add a datasource named “YourDSName”.

The advantage of this is that you don’t need to run the UI to add the datasource. Additionally, I am constantly changing my Wildfly installation version and what is installed in a particular one. This way you can regenerate the changes very quickly.

Leave a Reply

Your email address will not be published. Required fields are marked *