MySql - 没有在 nodejs 中连接
MySql - not connecting in nodejs
我正在尝试设置它,以便我的 twitch 机器人可以连接到数据库来存储数据。但是我似乎无法弄清楚如何连接。我输入了所有正确的数据(我可以通过客户端连接凭据)但它仍然无法连接。
var sql = require('mssql');
var sqlConfig = {
server: "x",
username: "x",
password: "x",
database: "x"
};
(async function () {
try {
console.log("sql connecting......")
let pool = await sql.connect(sqlConfig)
let result = await pool.request()
.query('select * from Subject') // subject is my database table name
console.log(result )
} catch (err) {
console.log(err);
}
})()
错误:
{ ConnectionError: Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms
at Connection.tedious.once.err (H:\Node JS\TwitchBot\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.connectTimeout (H:\Node JS\TwitchBot\node_modules\tedious\lib\connection.js:924:12)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
code: 'ETIMEOUT',
originalError:
{ ConnectionError: Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms
at ConnectionError (H:\Node JS\TwitchBot\node_modules\tedious\lib\errors.js:12:12)
at Connection.connectTimeout (H:\Node JS\TwitchBot\node_modules\tedious\lib\connection.js:924:28)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
message: 'Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms',
code: 'ETIMEOUT' },
name: 'ConnectionError' }
结果 1 这没有用,因为它不是 mysql 是 mssql。我有点笨对不起大家:
throw new RangeError('Index out of range');
RangeError: Index out of range
at checkOffset (buffer.js:977:11)
at Buffer.readUInt8 (buffer.js:1015:5)
at Packet.isLast (H:\Node JS\TwitchBot\node_modules\tedious\lib\packet.js:116:29)
at ReadablePacketStream.<anonymous> (H:\Node JS\TwitchBot\node_modules\tedious\lib\message-io.js:101:18)
at emitOne (events.js:116:13)
at ReadablePacketStream.emit (events.js:211:7)
at addChunk (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:291:12)
at readableAddChunk (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:278:11)
at ReadablePacketStream.Readable.push (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:245:10)
at ReadablePacketStream.Transform.push (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:148:32)
输出 2:
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
--------------------
at Protocol._enqueue (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (H:\Node JS\TwitchBot\fb.js:11:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
输出 3:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'MY isp identifier' (using password: YES)
at Handshake.Sequence._packetToError (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\sequences\Handshake.js:130:18)
at Protocol._parsePacket (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
--------------------
at Protocol._enqueue (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (H:\Node JS\TwitchBot\fb.js:11:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
您正在使用 mssql npm 库连接到 mysql
数据库。这是两个完全不同的 DBMS 系统,因此不交叉兼容。您需要使用正确类型的客户端。
我知道您正在尝试连接到 mysql
数据库,因为在 URL 中我可以看到 https://www.freemysqlhosting.net/ 仅支持托管 `mysql 数据库.这意味着您需要使用正确类型的客户端。
为此,您可以使用 mysql npm 依赖项,代码块如下:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'x',
user : 'x',
password : 'x',
database : 'x'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
这将使用适当的协议打开一个连接并允许您访问您的数据库。
额外阅读
- 我阅读了有关 the concept of a DBMS 的内容,以加深您对为什么此代码块不起作用的理解。
问题已解决:
如果您根据 npmjs 使用 mssql。com/package/mssql 您应该在 sqlConfig
对象上将 host
替换为 server
。 (host
用于 mysql,server
用于 mssql)
在 sqlConfig
对象上设置 port
:
var sqlConfig = {
server: "x",
username: "x",
password: "x",
database: "x",
port: "x"
};
我正在尝试设置它,以便我的 twitch 机器人可以连接到数据库来存储数据。但是我似乎无法弄清楚如何连接。我输入了所有正确的数据(我可以通过客户端连接凭据)但它仍然无法连接。
var sql = require('mssql');
var sqlConfig = {
server: "x",
username: "x",
password: "x",
database: "x"
};
(async function () {
try {
console.log("sql connecting......")
let pool = await sql.connect(sqlConfig)
let result = await pool.request()
.query('select * from Subject') // subject is my database table name
console.log(result )
} catch (err) {
console.log(err);
}
})()
错误:
{ ConnectionError: Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms
at Connection.tedious.once.err (H:\Node JS\TwitchBot\node_modules\mssql\lib\tedious.js:216:17)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Connection.emit (events.js:211:7)
at Connection.connectTimeout (H:\Node JS\TwitchBot\node_modules\tedious\lib\connection.js:924:12)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
code: 'ETIMEOUT',
originalError:
{ ConnectionError: Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms
at ConnectionError (H:\Node JS\TwitchBot\node_modules\tedious\lib\errors.js:12:12)
at Connection.connectTimeout (H:\Node JS\TwitchBot\node_modules\tedious\lib\connection.js:924:28)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
message: 'Failed to connect to sql2.freemysqlhosting.net:1433 in 15000ms',
code: 'ETIMEOUT' },
name: 'ConnectionError' }
结果 1 这没有用,因为它不是 mysql 是 mssql。我有点笨对不起大家:
throw new RangeError('Index out of range');
RangeError: Index out of range
at checkOffset (buffer.js:977:11)
at Buffer.readUInt8 (buffer.js:1015:5)
at Packet.isLast (H:\Node JS\TwitchBot\node_modules\tedious\lib\packet.js:116:29)
at ReadablePacketStream.<anonymous> (H:\Node JS\TwitchBot\node_modules\tedious\lib\message-io.js:101:18)
at emitOne (events.js:116:13)
at ReadablePacketStream.emit (events.js:211:7)
at addChunk (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:291:12)
at readableAddChunk (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:278:11)
at ReadablePacketStream.Readable.push (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_readable.js:245:10)
at ReadablePacketStream.Transform.push (H:\Node JS\TwitchBot\node_modules\tedious\node_modules\readable-stream\lib\_stream_transform.js:148:32)
输出 2:
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
--------------------
at Protocol._enqueue (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (H:\Node JS\TwitchBot\fb.js:11:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
输出 3:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'MY isp identifier' (using password: YES)
at Handshake.Sequence._packetToError (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\sequences\Sequence.js:52:14)
at Handshake.ErrorPacket (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\sequences\Handshake.js:130:18)
at Protocol._parsePacket (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
--------------------
at Protocol._enqueue (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:145:48)
at Protocol.handshake (H:\Node JS\TwitchBot\node_modules\mysql\lib\protocol\Protocol.js:52:23)
at Connection.connect (H:\Node JS\TwitchBot\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (H:\Node JS\TwitchBot\fb.js:11:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
您正在使用 mssql npm 库连接到 mysql
数据库。这是两个完全不同的 DBMS 系统,因此不交叉兼容。您需要使用正确类型的客户端。
我知道您正在尝试连接到 mysql
数据库,因为在 URL 中我可以看到 https://www.freemysqlhosting.net/ 仅支持托管 `mysql 数据库.这意味着您需要使用正确类型的客户端。
为此,您可以使用 mysql npm 依赖项,代码块如下:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'x',
user : 'x',
password : 'x',
database : 'x'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].solution);
});
connection.end();
这将使用适当的协议打开一个连接并允许您访问您的数据库。
额外阅读
- 我阅读了有关 the concept of a DBMS 的内容,以加深您对为什么此代码块不起作用的理解。
问题已解决:
如果您根据 npmjs 使用 mssql。com/package/mssql 您应该在
sqlConfig
对象上将host
替换为server
。 (host
用于 mysql,server
用于 mssql)在
sqlConfig
对象上设置port
:
var sqlConfig = {
server: "x",
username: "x",
password: "x",
database: "x",
port: "x"
};