abchk1234
abchk1234

Reputation: 265

JDBC MySQL connection using Unix Socket

I am using MySQL using the --skip-networking option on Linux.

Trying to connect my J2EE based application (using servlets) to the MySQL database using JDBC.

When was using MySQL with the --skip-networking option disabled, I was connecting to the database as:

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","myuser","mypassword");

After having enabled the --skip-networking option, I am trying to connect it as:

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase","myuser","mypassword");

But this does not seem to work, and I get java.lang.NullPointerException when I try to connect to the database in my application.

After commenting out the --skip-networking option and using the old JDBC statement, I can connect to the database.

Note - I am able to connect to the database via the command line mysql client with the --skip-networking option enabled.

Can anyone tell me how to connect to the database from JDBC? I tried searching for it but could not any satisfactory answer that worked. Thanks in advance.

Upvotes: 12

Views: 25121

Answers (4)

Robert
Robert

Reputation: 131

The JDBC Driver from the MariaDB project supports Unix domain sockets while remaining compatible with the MySQL Connector/J JDBC driver. Example jdbc url for the MariaDB driver is: jdbc:mariadb://localhost:3306/revmgt?localSocket=/var/run/mysqld/mysqld.sock

Worth noting that this requires including the JNA library as the MariaDB driver uses domain sockets via JNA internally. I saw speed improvements for CPU bound java processes when using the unix domain sockets. I believe this was largely from the offload of work from the java process to native code freeing up CPU cycles for the already CPU bottle necked java process.

Upvotes: 10

Devy
Devy

Reputation: 10189

As another answer pointed out, it's possible (at least in Intellij IDE) to use socket connection for Mysql JDBC Connector/J (I am using version 5.1) without having to provide a socketFactory,

jdbc:mysql://localhost:3306/database_name?socket=/tmp/mysql.sock

Upvotes: 4

hakre
hakre

Reputation: 197712

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

So this will vary with the implementation you use. 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.


You can find more Java UNIX socket library alternatives in related Q&A material:

Upvotes: 14

Mark Rotteveel
Mark Rotteveel

Reputation: 108992

You simply cannot do this: the MySQL JDBC driver only supports TCP/IP and - on Windows - named pipes to connect to the database. Therefor specifying --skip-networking will not allow you to use JDBC MySQL Connector/J at all.

See also http://lists.mysql.com/java/8749:

Java itself doesn't support unix domain sockets, but since you're on windows, you can use named pipes, [..]

The dead-link in the above post is now http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

Upvotes: 8

Related Questions