Pergunta

I have a nodejs (with express) openshift app that has a MySQL database (because that is what I am most familiar with), and I cannot figure out how to connect to it on the local environment. My last project I used a php framework with mamp to connect to the local database. What is the equivalent for node? This is the code I have to connect:

 var mysql = require('mysql');

 var connection = mysql.createConnection({
  host     : '127.0.0.1',
  port     : '3307',
  database : 'test'

});

 connection.connect( function(err){
if (err){ 
    throw err;
}
else {
    console.log('Connected');
}
 });

Here is the error when trying to connect locally:

 Error: connect ETIMEDOUT
    at errnoException (net.js:901:11)
    at Object.afterConnect [as oncomplete] (net.js:892:19)
    --------------------
    at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
    at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
    at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
    at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

I am guessing that because I am trying to locally connect to the MySQL db set up by openshift, that I am receiving errors. I just dont know what else to try here. I have tried it without the password as well.

If I switch to localhost for the host, this is the error I receive:

Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''@'localhost' to database 'stembuds'
at Handshake.Sequence._packetToError (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/sequences/Handshake.js:91:18)
at Protocol._parsePacket (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:202:24)
at Parser.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.<anonymous> (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:72:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:736:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/brandonmoffitt/stembudsnode/node_modules/mysql/lib/Connection.js:98:18)
at Object.<anonymous> (/Users/brandonmoffitt/stembudsnode/server.js:16:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)

Sorry for the length of this post, I just have no idea why it isnt working.

EDIT:

I ssh'ed into the openshift shell to look at the mysql settings. When I do the 'show grants\g' command, this is what I receive:

  Grants for adminMYXaSuf@127.11.28.130

  GRANT ALL PRIVILEGES ON *.* TO 'adminMYXaSuf'@'127.11.28.130' IDENTIFIED BY PASSWORD '*D0CE5FA2AAD801A33C9190D0B615CC92F364BE6B' WITH GRANT OPTION

This password is different than the one openshift provided.

Foi útil?

Solução 2

It looks like you are running this on your local computer? If you want to connect to your mysql database on OpenShift from your local computer, you need to use the rhc port-forward command, and look at the port it gives you for mysql, so then you would set the ip to 127.0.0.1 and the port to something like 3307 or whatever it gives you locally. Keep the port-forward command running while you test the connection. You could also try pushing your code up to openshift and run it with the values that you have (or use the mysql environment variables that you can get with env | grep MYSQL on your gear) and then see if it works.

127.x.x.x are not publicly available ip addresses, they are private to that server, that is why you can't connect to it from your machine, and need to use the rhc port-forward command

This works for me:

var mysql = require('mysql');

 var connection = mysql.createConnection({
  host     : '127.0.0.1',
  port     : '3306',
  user     : 'admind4DxYD5',
  password : 'some_pass',
  database : 'developercorey'
 });

 connection.connect( function(err){
if (err){ 
    throw err;
}
else {
    console.log('Connected');
}
 });

Outras dicas

did you read their doc?. if you have load balancer, your mysql will install on another gear. you need to use the env variable when you install the cartridge

port
OPENSHIFT_MYSQL_DB_PORT=44331

host
OPENSHIFT_MYSQL_DB_HOST=12345600a4382ec97a10000f7-somehost.rhcloud.com

OPENSHIFT_MYSQL_DB_PASSWORD=somerandompassword

username
OPENSHIFT_MYSQL_DB_USERNAME=yourusername

pass the variable when you start your app as args.

look at the example for node https://github.com/openshift-quickstart/nodejs-example/blob/master/server.js

just use the env variable like process.env.OPENSHIFT_MYSQL_DB_HOST to replace the string in the database connection.

 var mysql = require('mysql');

 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
 });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top