java 使用 Unix Socket 的 JDBC MySQL 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25918416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JDBC MySQL connection using Unix Socket
提问by abchk1234
I am using MySQL using the --skip-networkingoption on Linux.
我在 Linux 上使用--skip-networking选项使用 MySQL 。
Trying to connect my J2EE based application (using servlets) to the MySQL database using JDBC.
尝试使用 JDBC 将我的基于 J2EE 的应用程序(使用 servlet)连接到 MySQL 数据库。
When was using MySQL with the --skip-networkingoption disabled, I was connecting to the database as:
在禁用--skip-networking选项的情况下使用 MySQL 时,我以以下方式连接到数据库:
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","myuser","mypassword");
After having enabled the --skip-networkingoption, I am trying to connect it as:
启用--skip-networking选项后,我尝试将其连接为:
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.NullPointerExceptionwhen I try to connect to the database in my application.
但这似乎不起作用,当我尝试连接到我的应用程序中的数据库时,我得到java.lang.NullPointerException。
After commenting out the --skip-networkingoption and using the old JDBC statement, I can connect to the database.
注释掉--skip-networking选项并使用旧的 JDBC 语句后,我可以连接到数据库。
Note- I am able to connect to the database via the command line mysql client with the --skip-networkingoption enabled.
注- 我能够通过启用--skip-networking选项的命令行 mysql 客户端连接到数据库。
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.
谁能告诉我如何从 JDBC连接到数据库?我尝试搜索它,但找不到任何令人满意的答案。提前致谢。
采纳答案by Mark Rotteveel
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.
您根本无法做到这一点:MySQL JDBC 驱动程序仅支持 TCP/IP 和 - 在 Windows 上 - 连接到数据库的命名管道。因此,指定--skip-networking
将根本不允许您使用 JDBC MySQL Connector/J。
See also http://lists.mysql.com/java/8749:
另见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, [..]
Java 本身不支持 unix 域套接字,但由于您在 Windows 上,您可以使用命名管道,[..]
The dead-link in the above post is now http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
上面帖子中的死链接现在是http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
回答by hakre
If you want to use UNIX sockets with the Mysql JDBC Connector/J you need to provide a socketFactory.
如果要将 UNIX 套接字与 Mysql JDBC Connector/J 一起使用,则需要提供socketFactory。
jdbc:mysql:///?user=test&password=test&socketFactory=<classname>&<socket>=/tmp/mysql.sock
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.
因此,这将因您使用的实现而异。默认情况下,Mysql 不附带任何实现,只是在其源代码中提供了此类工厂的示例。
There is an existing UNIX socket Java library named junixsocketwhich also has such a socketFactoryclass implementation. An example is outlined in Connecting to a MySQL database via Unix Domain Socketswhich is part of their documentation.
有一个名为junixsocket的现有 UNIX 套接字 Java 库,它也有这样的socketFactory类实现。通过 Unix 域套接字连接到 MySQL 数据库中概述了一个示例,这是他们文档的一部分。
You can find more Java UNIX socket library alternatives in related Q&A material:
您可以在相关问答材料中找到更多 Java UNIX 套接字库替代方案:
回答by Robert
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
MariaDB 项目的 JDBC 驱动程序支持 Unix 域套接字,同时保持与 MySQL Connector/J JDBC 驱动程序兼容。MariaDB 驱动程序的示例 jdbc url 是:
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.
值得注意的是,这需要包含 JNA 库,因为 MariaDB 驱动程序在内部通过 JNA 使用域套接字。在使用 unix 域套接字时,我看到了 CPU 绑定 Java 进程的速度改进。我相信这主要是由于将工作从 java 进程卸载到本机代码,从而为已经陷入 CPU 瓶颈的 java 进程释放了 CPU 周期。
回答by Devy
As another answerpointed out, it's possible (at least in Intellij IDE) to use socket connection for Mysql JDBC Connector/J(I am using version 5.1) withouthaving to provide a socketFactory,
正如另一个答案所指出的那样,可以(至少在 Intellij IDE 中)将套接字连接用于Mysql JDBC Connector/J(我使用的是 5.1 版)而无需提供 socketFactory,
jdbc:mysql://localhost:3306/database_name?socket=/tmp/mysql.sock
jdbc:mysql://localhost:3306/database_name?socket=/tmp/mysql.sock