No matter how you’re running your application server, one of the first configuration changes you do is likely the addition of a datasource. In this post, we show how to do it with CLI on WildFly, using MySQL.

The examples in this post assume there is a WildFly instance running on its default localhost:8080 and the management interface is listening on its default localhost:9990. A MySQL instance is assumed to be running and listening on its default localhost:3306. A database mydb exists and a user myuser with his password password has been granted access.

Let’s start by downloading the appropriate version of MySQL JDBC driver (e.g. 5.1.32):

Subsequently, we start the CLI:

We install the JDBC driver:

Next, we create a new datasource:

If you’re using global (distributed) transactions, execute the following to create an XA datasource:

Note that the data-source add and xa-data-source add examples above are not minimal in the sense of specifying the required arguments only, they also include optional arguments you’ll most likely want to specify. Other arguments I frequently use are jta, use-ccm, max-pool-size, blocking-timeout-wait-millis, and prepared-statements-cache-size (explained below).

Notice how non-XA and XA datasources are added via different commands using different sets of parameters:

Non-XA datasources:

XA datasources:

Now, let’s double-check that everything is set up correctly. We verify that both datasources exist:

Next, we make sure the connection can be established:

For simple monitoring, we can run the following:

The examples in this post were tested using WildFly 10.0.0.Final and MySQL 5.7.13.