使用 Unix Socket 的 JDBC MySQL 连接 [英] JDBC MySQL connection using Unix Socket
问题描述
我在 Linux 上使用 --skip-networking 选项使用 MySQL.
尝试使用 JDBC 将我的基于 J2EE 的应用程序(使用 servlet)连接到 MySQL 数据库.
当使用带有 --skip-networking 选项禁用的 MySQL 时,我以如下方式连接到数据库:
Class.forName("com.mysql.jdbc.Driver");Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","myuser","mypassword");
启用 --skip-networking 选项后,我尝试将其连接为:
Class.forName("com.mysql.jdbc.Driver");Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase","myuser","mypassword");
但这似乎不起作用,当我尝试连接到应用程序中的数据库时,我收到 java.lang.NullPointerException.
在注释掉--skip-networking选项并使用旧的JDBC语句后,我可以连接到数据库.
注意 - 我可以通过启用 --skip-networking 选项的命令行 mysql 客户端连接到数据库.
谁能告诉我如何从 JDBC 连接到数据库?我尝试搜索它,但找不到任何令人满意的答案.提前致谢.
您根本无法做到这一点:MySQL JDBC 驱动程序仅支持 TCP/IP 和 - 在 Windows 上 - 连接到数据库的命名管道.因此指定 --skip-networking
将不允许您使用 JDBC MySQL Connector/J.
另见http://lists.mysql.com/java/8749::><块引用>
Java 本身不支持 unix 域套接字,但由于您在 Windows 上,您可以使用命名管道,[..]
上面帖子中的死链接现在是 http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html
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.
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
这篇关于使用 Unix Socket 的 JDBC MySQL 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!