【发布时间】:2018-11-02 00:37:30
【问题描述】:
我一直在尝试使用节点 API 远程连接到我的 azure postgres 数据库。我正在使用最新版本的 node、objection.js、knex.js 和 pg。
我的主机地址是(databasePath):
project-name.database.windows.net
我已经设置了一个包装器来帮助进行 SQL 连接。我的连接参数在 .env 文件中定义。每当我尝试使用配置进行连接时,都会出现以下异常:
Unhandled rejection error: Invalid Username specified. Please check the Username and retry connection. The Username should be in <username@hostname> format.
at Connection.parseE (/Users/Eddie/projects/Giggle-Node/node_modules/pg/lib/connection.js:554:11)
at Connection.parseMessage (/Users/Eddie/projects/Giggle-Node/node_modules/pg/lib/connection.js:379:19)
at TLSSocket.<anonymous> (/Users/Eddie/projects/Giggle-Node/node_modules/pg/lib/connection.js:119:22)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:547:20)
如果我尝试使用连接字符串,DNS 将无法解析提供的地址。我尝试了两种不同形式的连接字符串:
postgres://userName@myHost:passWord@myHost.postgres.database.azure.com:5432/dbName?ssl=true
和
postgres://userName:passWord@myHost.postgres.database.azure.com:5432/dbName?ssl=true
例外是:
Unhandled rejection Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
我的包装如下
const { Model } = require( "objection" );
const Knex = require( "knex" );
const dotenv = require( "dotenv" );
dotenv.config();
class ObjectionSQLHelper
{
constructor()
{
this.SQLDatabaseType = process.env.SQL_VARIANT;
this.databasePath = process.env.DB_PATH;
this.dataBaseName = process.env.DB_NAME;
this.databasePort = process.env.DB_PORT;
this.databaseUserName = process.env.DB_USER_NAME;
this.databasePassword = process.env.DB_PASSWORD;
}
get connectionSchema()
{
const knexSchema =
{
client: this.SQLDatabaseType,
connection: {
//connectionString: this.connectionString,
userName: this.databaseUserName,
passWord: this.databasePassword,
server: this.databasePath,
host: this.databasePath,
options:
{
database: this.dataBaseName,
encrypt: true,
port: this.dataBasePort,
ssl: true
}
},
debug: true
}
return knexSchema;
}
get connectionString()
{
return `postgres://${ this.databaseUserName }:${ this.databasePassword }@${ this.databasePath }.postgres.database.azure.com:5432/${ this.dataBaseName }?ssl=true`
}
connect()
{
this.knexConnection = Knex( this.connectionSchema );
Model.knex( this.knexConnection );
}
}
const objectionHelperSinglton = new ObjectionSQLHelper();
objectionHelperSinglton.connect();
我的配置或连接字符串有问题吗? 我还在我的 azure DB 上为我当前的 IPv4 添加了防火墙例外。
【问题讨论】:
标签: javascript node.js azure azure-sql-database knex.js