我有一个nodejs(带express)openshift应用程序,它有一个mysql数据库(因为这是我最熟悉的),我不知道如何在本地环境中连接到它。我的上一个项目是使用带有mamp的php框架连接到本地数据库。node的等价物是什么?这是我必须连接的代码:
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');
}
});
以下是尝试在本地连接时出现的错误:
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)
我猜是因为我试图本地连接到openshift设置的mysql数据库,所以我收到了错误。我只是不知道还有什么可以试试。我也试过没有密码的。
如果我切换到主机的本地主机,这是我收到的错误:
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)
很抱歉这篇文章篇幅太长,我只是不知道它为什么不起作用。
编辑:
我进入openshift shell查看mysql设置。当我执行“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
此密码与提供的openshift密码不同。
最佳答案
你好像在本地计算机上运行这个?如果要从本地计算机连接到openshift上的mysql数据库,需要使用rhc port forward命令,并查看它为mysql提供的端口,然后将IP设置为127.0.0.1,并将端口设置为3307或它在本地提供的任何内容。在测试连接时保持port forward命令运行。您还可以尝试将代码推到openshift并使用您拥有的值运行它(或者使用env grep mysql可以在gear上获得的mysql环境变量),然后查看它是否有效。
127.x.x.x不是公共可用的IP地址,它们是该服务器的私有地址,因此您无法从计算机连接到该服务器,需要使用rhc port forward命令
这对我有效:
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');
}
});