Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

java - Connect to MySQL with JDBC over network

I am trying to connect to MySQL database over a network. I have installed MySQL, and the service is running on the default port. I have also install the SQL connector to the jar file and added java JDK to the server machine. I am able to connect to my local database using the code:

private String dbUrl = "jdbc:mysql://localhost/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";

But when I try and connect to it over the network, with the IP address (eg: 192.168.1.45):

private String dbUrl = "jdbc:mysql://192.168.1.45/DatabaseName";
private String dbClass = "com.mysql.jdbc.Driver";

I get the connection error:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Does anyone know what this issue is? Do I need to add a different address? I have added the default port with the address but cannot get it to work.

Thanks for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By default, MySQL doesnt allow remote access and only allow local access. You will have to modify your /etc/mysql/my.cnf config (on Linux) with:

bind-address = 192.168.1.45 // Your Server IP address
# skip-networking // This should be commented .

See the whole procedure here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...