简体   繁体   English

无法使用nodejs连接到MySQL数据库(openshift app)

[英]Cannot connect to MySQL db with nodejs (openshift app)

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. 我有一个nodejs(带快递)openshift应用程序,它有一个MySQL数据库(因为这是我最熟悉的),我无法弄清楚如何在本地环境中连接它。 My last project I used a php framework with mamp to connect to the local database. 我的上一个项目我使用php框架和mamp连接到本地数据库。 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. 我猜这是因为我试图本地连接到openshift设置的MySQL数据库,我收到错误。 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: 如果我切换到主机的localhost,这是我收到的错误:

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. 我ssh'ed进入openshift shell来查看mysql设置。 When I do the 'show grants\\g' command, this is what I receive: 当我执行'show grants \\ g'命令时,这就是我收到的内容:

  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. 此密码与提供的openhift不同。

did you read their doc?. 你读过他们的博士吗? if you have load balancer, your mysql will install on another gear. 如果你有负载均衡器,你的mysql将安装在另一个齿轮上。 you need to use the env variable when you install the cartridge 安装墨盒时需要使用env变量

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. 当您以args启动应用程序时传递变量。

look at the example for node https://github.com/openshift-quickstart/nodejs-example/blob/master/server.js 查看节点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. 只需使用像process.env.OPENSHIFT_MYSQL_DB_HOST这样的env变量来替换数据库连接中的字符串。

 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
 });

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. 如果要从本地计算机连接到OpenShift上的mysql数据库,则需要使用rhc port-forward命令,并查看它为mysql提供的端口,然后将ip设​​置为127.0.0.1和端口到3307之类的东西,或者它给你的本地东西。 Keep the port-forward command running while you test the connection. 在测试连接时保持port-forward命令运行。 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. 您也可以尝试将代码推送到openshift并使用您拥有的值运行它(或使用您可以使用env | grep MYSQL在您的齿轮上获得的mysql环境变量),然后查看它是否有效。

127.xxx 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 127.xxx不是公共可用的ip地址,它们是该服务器专用的,这就是为什么你无法从你的机器连接到它,并且需要使用rhc port-forward命令

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');
}
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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