简体   繁体   中英

Dedicated server: why can't my JAVA application connect to my MySQL database?

I got a dedicated server under Debian (amd64).

I installed a MySQL database with phpmyadmin and a Tomcat 6 server to deploy the JAVA application I just developed.

When I run this application once deployed, an exception is thrown: org.hibernate.exception.GenericJDBCException: Cannot open connection

I thought maybe my database credentials were wrong, so I wrote a little PHP script that I put on my remote Apache2 server, to see if it can connect to the same database, and yes: it can.

This is my PHP script:

$link = mysql_connect('localhost', 'mylogin', 'mypassword');
mysql_select_db('mydatabase', $link);
$query = mysql_query('SELECT * FROM source');
$result = mysql_fetch_array($query);
var_dump($result);

The output is the list of the entries within my source table, alright. So why does my JAVA application can't connect to that #@!?€ database?

This is my pom.xml :

<jdbc.type>mysql</jdbc.type>
<jdbc.driver>com.mysql.jdbc.Driver</jdbc.driver>
<jdbc.url>jdbc:mysql://localhost/mydatabase</jdbc.url>
<jdbc.user>mylogin</jdbc.user>
<jdbc.password>mypassword</jdbc.password>
<hibernate.dialect>org.hibernate.dialect.MySQLInnoDBDialect</hibernate.dialect>

I also tried with

<jdbc.url>jdbc:mysql://localhost:3306/mydatabase</jdbc.url>

and

<jdbc.url>jdbc:mysql://012.345.678.9:3306/mydatabase</jdbc.url>

but it still doesn't work...

PS1: it works like a charm on a local server on my own machine.

PS2: everything is on the same remote machine (ie MySQL, Tomcat and Apache).

PS3: I bought the dedicated server to Online (maybe a bit too french for some of you...).

PS4: don't hesitate to ask me more details, I dunno what could be usefull to you to help me solving that problem...

I guess you have missed the port in the <jdbc.url> .

You might modify the url from <jdbc.url>jdbc:mysql://localhost/mydatabase</jdbc.url> to <jdbc.url>jdbc:mysql://localhost:port/mydatabase</jdbc.url> .

The port mostly is 3306 .

You could put in the ip of the MySQL server instead of localhost with the port and I think it should work.

There might be firewall rules in place. Quite common on server installations.

Check if you can telnet to the mysql port. If not, talk to the system administrator.

I finally deployed it using mvn tomcat:deploy , and it worked. I was uploading the WAR file via the manager/html interface of Tomcat before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM