0
votes

I have an application on Openshift I'm trying to connect to MySQL DB my code is :

private static final String URL = "jdbc:mysql://127.11.240.130:3306/"
        + DB_NAME;
public static String initConnection() {
    if (connection == null) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, USER_NAME,
                    PASSWORD);
            return "Connection Initialized!";
        } catch (SQLException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            return e.getMessage();
        }
    }
    return "Connection Initialized!";
}

and in index.jsp code is :

<p><% out.print(""+com.measyou.DbManager.initConnection()); %> </p>

this code gives me

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

and Caused by :

Caused by: java.net.ConnectException: Connection refused: connect

I have referred This Link and This one as well, but Im unable to solve this problem. Please Help me in solving this one.

1

1 Answers

0
votes

Is this a scaled application, or is it running in a single gear? Either way, you should be using OPENSHIFT_MYSQL_DB_HOST and OPENSHIFT_MYSQL_DB_PORT instead of hard-coding the IP and port. And are you sure the mysqld is running? Run the following:

ps -ef | grep mysqld

and you should see running processes. Then run:

netstat -plnt | grep $OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT

if it's running properly, you should see a line like this:

tcp 0 0 127.11.240.130:3306 0.0.0.0:* LISTEN 459740/mysqld

if the process is not running, or it's not listening on the port as expected, then check the mysql logs in ~/mysql/log/ for errors.