Aaditya Bagga Aaditya Bagga - 6 months ago 68
Java Question

JDBC MySQL connection using Unix Socket

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

I am trying to connect my J2EE based application (using servlets) to the MySQL database using JDBC.

Earlier when I 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 was 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.

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.

Answer

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

Comments