gpt4 book ai didi

javascript - Nodejs node-mysql模块未连接到数据库

转载 作者:行者123 更新时间:2023-11-29 04:24:14 24 4
gpt4 key购买 nike

我正在运行这个脚本:

var mysql      = require('mysql');

var connection = mysql.createConnection("mysql://localhost/");

connection.connect();
console.log(connection)

但是出现错误...

{ config: 
{ host: 'localhost',
port: 3306,
socketPath: undefined,
user: undefined,
password: undefined,
database: '',
insecureAuth: false,
supportBigNumbers: false,
debug: undefined,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 193487 },
_socket:
{ _handle:
{ writeQueueSize: 0,
owner: [Circular],
onread: [Function: onread] },
_pendingWriteReqs: 0,
_flags: 0,
_connectQueueSize: 0,
destroyed: false,
errorEmitted: false,
bytesRead: 0,
_bytesDispatched: 0,
allowHalfOpen: undefined,
_connecting: true,
writable: true,
_events:
{ data: [Function: ondata],
end: [Object],
close: [Object],
error: [Object],
drain: [Function: ondrain] } },
_protocol:
{ readable: true,
writable: true,
_config:
{ host: 'localhost',
port: 3306,
socketPath: undefined,
user: undefined,
password: undefined,
database: '',
insecureAuth: false,
supportBigNumbers: false,
debug: undefined,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: undefined,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 193487 },
_connection: [Circular],
_callback: null,
_fatalError: null,
_quitSequence: null,
_handshakeSequence:
{ _callback: undefined,
_ended: false,
_callSite: ' at Handshake.Sequence (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)\n at new Handshake (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)\n at Protocol.handshake (/usr/local/lib/node_modules/mysql/lib/protocol/Protocol.js:41:50)\n at Connection.connect (/usr/local/lib/node_modules/mysql/lib/Connection.js:63:18)\n at Object.<anonymous> (/Users/itaccess/Desktop/test.js:5:12)\n at Module._compile (module.js:449:26)\n at Object.Module._extensions..js (module.js:467:10)\n at Module.load (module.js:356:32)\n at Function.Module._load (module.js:312:12)\n at Module.runMain (module.js:492:10)',
_config: [Object],
_handshakeInitializationPacket: null,
_events: [Object] },
_destroyed: false,
_queue: [ [Object] ],
_handshakeInitializationPacket: null,
_parser:
{ _supportBigNumbers: false,
_buffer: <Buffer >,
_longPacketBuffers: [],
_offset: 0,
_packetEnd: null,
_packetHeader: null,
_onPacket: [Function],
_nextPacketNumber: 0,
_encoding: 'utf-8',
_paused: false },
_events:
{ drain: [Object],
error: [Object],
end: [Object],
close: [Object],
data: [Function: ondata],
unhandledError: [Function] } },
_connectCalled: true }
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }

控制台中出现这个错误...

/Users/itaccess/Desktop/test.js:10
if (err) throw err;
^
Error: connect ECONNREFUSED
at errnoException (net.js:769:11)
at Object.afterConnect [as oncomplete] (net.js:760:19)
--------------------
at Handshake.Sequence (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Sequence.js:15:21)
at new Handshake (/usr/local/lib/node_modules/mysql/lib/protocol/sequences/Handshake.js:9:12)
at Protocol.handshake (/usr/local/lib/node_modules/mysql/lib/protocol/Protocol.js:41:50)
at Connection.connect (/usr/local/lib/node_modules/mysql/lib/Connection.js:63:18)
at Object.<anonymous> (/Users/itaccess/Desktop/test.js:5:12)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)

我确定用户名/密码不是必需的,我也尝试过各种我也确定的密码。我试过使用对象来设置设置和查询字符串。我已尝试显式添加端口,无论如何这是默认端口。

为什么连接不上?

最佳答案

需要设置socket路径

var mysql      = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
socketPath : '/var/run/mysqld/mysqld.sock',
});

关于javascript - Nodejs node-mysql模块未连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15117005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com