0

I'm trying to connect to MySQL database in OpenShift using Node JS server. And I have an error:

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object.exports._errnoException (util.js:870:11)
    at exports._exceptionWithHostPort (util.js:893:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1057:14)
    --------------------
    at Protocol._enqueue (D:\nodejs\node_modules\mysql\lib\protocol\Protocol.js:141:48)
    at Protocol.handshake (D:\nodejs\node_modules\mysql\lib\protocol\Protocol.js:52:41)
    at Connection.connect (D:\nodejs\node_modules\mysql\lib\Connection.js:123:18)
    at Object.<anonymous> (D:\nodejs\server.js:17:12)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)

That's my code:

var connection = mysql.createConnection({
  host     : process.env.OPENSHIFT_MYSQL_DB_HOST,
  user     : process.env.OPENSHIFT_MYSQL_DB_USERNAME,
  password : process.env.OPENSHIFT_MYSQL_DB_PASSWORD,
  port     : process.env.OPENSHIFT_MYSQL_DB_PORT,
  database : process.env.OPENSHIFT_APP_NAME
 });

connection.connect(function(err){
  if (err) {throw err;}
});

I read a lot about this error but I didn't find a solution. I can connect to MySQL using ssh, and I also can check these environmental variables.

HOST is 127.9.156.2 and NODEJS_IP is 127.9.156.1. PORT is 3306. PASS and USER are also ok.

Where is there a problem?

Lev S
  • 101
  • 2
  • 11

1 Answers1

0

This is most likely caused from two different issues. One of them is that the port is wrong or the MySQL instance is trying to connect to a port that is already busy. You need to change this, if this is what is causing it.

The other issue and is more likely is by default MySQL only allows 127.0.0.1 connections. You'll have to go into the config file for MySQL and set it up to allow for other host connections, here is a link to their docs on how to do this. http://dev.mysql.com/doc/refman/5.0/en/server-options.html#option_mysqld_bind-address .

Morgan G
  • 3,089
  • 4
  • 18
  • 26
  • I tried to change port to 3307, but nothing changed. I checked phpmyadmin, so there is a user that has host 127.9.156.2. Do I need to configure file? If so, how to do it? I didn't understand how to use it from this link – Lev S Mar 27 '16 at 14:53
  • 1
    here is a good stackoverflow answer on how to do it within PHPAdmin http://stackoverflow.com/questions/16801573/how-to-access-remote-server-with-local-phpmyadmin-client and a great website to tell you how to do it. https://wadsashika.wordpress.com/2015/01/06/manage-remote-mysql-database-locally-using-phpmyadmin/ – Morgan G Mar 27 '16 at 15:14
  • Thanks, I will try – Lev S Mar 27 '16 at 15:19
  • Here is a solution I find easier to understand but you go into MySQL's configuration file and change it there, and then go into the MySQL terminal on the machine and grant privileges. http://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql I will warn you be careful with how you grant privileges. You might want to look that up before you grant all privileges. – Morgan G Mar 27 '16 at 15:26