Conecte java com mysql no servidor ubuntu

1

Estou tentando conectar meu aplicativo Java ao MySql executado em um servidor Ubuntu 14.04 instalado no Virtual Box na mesma máquina que meu aplicativo. Eu instalei o mysql-server, mysql-client e libmysql-java no ubuntu e executando sql queries dentro do servidor ubuntu parece funcionar bem. Minha aplicação java é:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class UploadData {
 // The JDBC Connector Class.
  private static final String dbClassName = "com.mysql.jdbc.Driver";

  private static final String CONNECTION =
                          "jdbc:mysql://127.0.0.1/demodb";

  public static void main(String[] args) throws
                             ClassNotFoundException,SQLException
  {
    System.out.println(dbClassName);
    // Class.forName(xxx) loads the jdbc classes and
    // creates a drivermanager class factory
    Class.forName(dbClassName);

    // Properties for user and password. Here the user and password are both 'paulr'
    Properties p = new Properties();
    p.put("user","*******");
    p.put("password","******");

    // Now try to connect
    Connection c = DriverManager.getConnection(CONNECTION,p);

    System.out.println("It works !");
    c.close();
    }
}

O erro que recebo é:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1038)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:338)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2237)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2270)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2069)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at server.UploadData.main(UploadData.java:32)

 Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:213)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:297)
... 15 more

Qualquer sugestão será mais que útil,

Obrigado

    
por ggeo 10.06.2015 / 10:32

1 resposta

1

A exceção ocorre quando não há serviço mysql em execução ou escutando:

  • Inicie o servidor MySQL

  • Verifique o número da porta, você usa a porta padrão 3306 em seu código

    private static final String dbClassName = "jdbc:mysql://127.0.0.1:<your_port_number>/demodb";
    
  • Use o endereço IP da sua máquina virtual

    private static final String dbClassName = "jdbc:mysql://<host_name_or_ip_address_of_the_database_server>:<your_port_number>/demodb";
    
  • Verifique o firewall nas duas máquinas

  • Execute ping no host do servidor MySQL para verificar a conexão

  • Verifique o bind-address em /etc/mysql/my.cnf

Desde que você faça outra pergunta em seus comentários

  

Exceção no segmento "main" java.sql.SQLException: null, message from   servidor: "Host '192.168.1.7' não tem permissão para se conectar a este MySQL   servidor

aqui está a resposta.

  • Verifique as entradas na tabela MySQL mysql.user na coluna Host

    SELECT * FROM mysql.user;
    
por A.B. 10.06.2015 / 11:18