MySQL/MariaDB unix socket connect

Current MariaDB extension in Lucee works well, yet it would be even better to have the possibility of specifying a unix socket between these databases and Lucee for a secure and super fast local connection, in the web admin (feature request!)

I suspect there already might be a way of doing this outside the web admin from Lucee settings or code, since when changing the Host/Server to /tmp/mariadb/mysqld.sock you get the message:

host, pipe or local socket must be set to connect socket

lucee.runtime.exp.NativeException: host, pipe or local socket must be set to connect socket at org.mariadb.jdbc.Driver.connect(Driver.java:69)

So if this is currently possible with the existing mariadb.jdbc driver extension, where and how could the path of a socket be configured, instead of a TCP port?

=========================================
OS: Linux (6.12.0-211.61.1.el10_2.x86_64_v2) 64bit
Java Version: 25.0.3
Tomcat Version: 11.0.25
Lucee Version: 7.1.0.204

The only suggestion I found so far in making such a connection:

If you want to use UNIX sockets with the Mysql JDBC Connector/J you need to provide a socketFactory.

jdbc:mysql:///?user=test&password=test&socketFactory=<classname>&<socket>=/tmp/mysql.sock

By default, Mysql does not ship with any implementation for that, just provides an example for such a factory in it’s source-code.

There is an existing UNIX socket Java library named junixsocket which also has such a socketFactory class implementation. An example is outlined in Connecting to a MySQL database via Unix Domain Sockets which is part of their documentation.

https://stackoverflow.com/questions/25918416/jdbc-mysql-connection-using-unix-socket

Hi Stevie,

Good news: the MariaDB driver can already do this. It just takes a couple of extra steps, because the admin form and the extension don’t cover it yet. I tested it on Lucee 7.1.0.204 / Java 25 / Tomcat 11 with the MariaDB extension 3.5.3 (Connector/J 3.5.3), with skip-networking on in MariaDB, so the socket was the only way in.

Why the Host field doesn’t work

The MariaDB form builds jdbc:mariadb://{host}:{port}/{database}, so a socket path in Host leaves the driver without a valid host, and you get that error. The driver wants the socket as the localSocket URL option instead.

Step 1: make JNA available to the driver

Connector/J uses JNA for Unix sockets, and neither Lucee nor the extension ships it. Without JNA, the driver quietly falls back to TCP and fails with connect: The address can't be null. To fix that:

  1. Put the JNA jar (net.java.dev.jna:jna, any 5.x from 5.15 up; I used 5.17.0) into Tomcat’s lib/ folder.
  2. Let the driver bundle see it through OSGi boot delegation with this JVM argument, e.g. in bin/setenv.sh:
CATALINA_OPTS="$CATALINA_OPTS -Dorg.osgi.framework.bootdelegation=com.sun.jna,com.sun.jna.*"

This adds to Lucee’s default list, it doesn’t replace it. Restart Tomcat afterwards.

Step 2: a datasource with your own connection string

In Application.cfc:

this.datasources["mydsn"] = {
	class: "org.mariadb.jdbc.Driver",
	bundleName: "mariadb-java-client",
	bundleVersion: "3.5.3", // match the version of your installed MariaDB extension
	connectionString: "jdbc:mariadb://localhost/mydb?localSocket=/tmp/mariadb/mysqld.sock",
	username: "myuser",
	password: "mypassword"
};

To set it up in the admin instead, choose the “Other - JDBC Driver” type and enter the same class, connection string, bundle name and bundle version. That worked the same way in my test.

The localhost in the URL is just a placeholder, and no port is needed. select @@socket should return your socket path once connected.

Things to check

  • The path must be the server’s socket (SELECT @@socket), and the user Lucee/Tomcat runs as needs read/write access to the socket file and access to its directory. If MariaDB runs in a container, mount the socket directory into the Lucee container (e.g. -v /tmp/mariadb:/tmp/mariadb).
  • Over the socket, MariaDB authenticates the user as 'myuser'@'localhost', not 'myuser'@'127.0.0.1' or a container IP, so the grants need to match.
  • JNA unpacks a small native library on first use (into ~/.cache/JNA, or -Djna.tmpdir=...), so that location needs to be writable.

MySQL driver

You’re right about MySQL Connector/J: it has no Unix socket support of its own and needs a socket factory like junixsocket’s. Since the MariaDB driver talks to MySQL servers too, the route above is simpler.

Feature request

The MariaDB extension is a community extension by Julian Halliwell (cfsimplicity/lucee-mariadb), so a socket field in the form, and shipping JNA so Step 1 isn’t needed, would be a request on its GitHub issues. I checked that bundling JNA in the extension makes sockets work without the JVM argument, so it’s a small change.

Cheers,
Gian

1 Like

This is brilliant! Thank you @GianTschingt for this solution you provided (and tested out), plus the very detailed instructions and behind-the-scenes explanation on how it works, current limitations of the extension.

I will test it out while still hoping it will be implemented officially in the next builds / docker images of Lucee (as well as in the Web admin), so that we can have Unix socket connectivity with databases out-of-the-box, without any additional hacks, perhaps if we can request @Julian_Halliwell to help us.

I’m afraid I don’t know much about UNIX sockets, but if it’s just a question of adding the latest JNA jar to the MariaDB extension’s jars directory then I’m happy to oblige. I’m assuming that’s all that’s needed to make it visible?

I’m on Windows so can’t test it, which also makes changing the admin form tricky. But presumably if you want to use a local socket you can just define your datasources in Application.cfc.

1 Like

Hi Julian,

Adding the jar alone isn’t quite enough, and I need to correct what I wrote above. I tested it on Linux with Lucee 7.1.2.40-SNAPSHOT, Java 25 and Tomcat 11, against MariaDB 11.8 with skip-networking on, so the socket was the only way in. Nothing was added to Tomcat’s lib/ and no JVM arguments were set.

JNA jar in jars/ only: sockets still fail with connect: The address can't be null. Lucee installs JNA as its own bundle, but only on demand. The driver’s manifest imports com.sun.jna as an optional package, and OSGi wires optional imports only when the driver bundle is resolved. When the driver loads, JNA isn’t installed yet, so the driver never sees it. A restart doesn’t change that.

JNA jar plus start-bundles: true: works. The extension’s MANIFEST.MF currently has start-bundles: false. With true (or with the line removed, since true is Lucee’s default), Lucee installs every bundle of the extension at startup, JNA included, so the driver’s optional import gets wired. It worked on a fresh install and after two restarts.

So the change is just:

  1. Add jna-5.19.1.jar (net.java.dev.jna:jna, the latest; the driver accepts 5.15 up to 6.0) to extension/jars/. It’s already an OSGi bundle (com.sun.jna), so nothing needs repackaging.
  2. In extension/META-INF/MANIFEST.MF, change start-bundles: false to start-bundles: true.

You don’t need to change the driver jar. TCP datasources aren’t affected, and on Windows the extra bundle just sits there unused. The .lex gets about 2 MB bigger.

One thing to know for the release: when I upgraded the extension in place on a running server, existing MariaDB datasources failed until Tomcat was restarted. After the restart everything worked, so a restart after the update is the safe advice.

On the admin form, I agree it can stay as it is. Besides Application.cfc with connectionString: "jdbc:mariadb://localhost/mydb?localSocket=/path/to/mysqld.sock", the existing MariaDB type also works if localSocket is passed as a custom parameter (custom: { localSocket: "/path/to/mysqld.sock" }), with host localhost. The port is then ignored. I tested both.

I’ve opened a PR with the two changes: Bundle JNA so Unix socket connections work without JVM args by GianTschingt · Pull Request #14 · cfsimplicity/lucee-mariadb · GitHub

OK, I’ve merged your PR and released it to Forgebox along with an update to the latest driver.

Having a new version number for the extension might obviate the need to restart Lucee. I didn’t need to on upgrading, but then I only use application-defined datasources (not admin-defined).

1 Like

Thanks Julian, much appreciated, both for merging and releasing it so quickly and for updating the driver at the same time.

1 Like