我有一臺Mac電腦。我已經成功安裝了MySQL。我知道這是因爲我能夠從終端使用mysql。我什至運行查詢,它工作正常。使用MySQL與WebStorm不起作用
我正在嘗試使用WebStorm學習Node.js。因此,在WebStorm創建我的項目,package.json
{
"dependencies":{
"mysql":"~2.0.0"
}
}
,然後我跑npm install
即安裝一個mysql模塊。我運行以下代碼,該代碼將記錄undefined
保存爲results
。任何想法缺少什麼?
'use strict';
var mysql = require('mysql');
var connection = mysql.createConnection({
host:'localhost',
user:'root',
password:'myPassword'
});
connection.query(
'SELECT "foo" as first_field, "bar" as second_field',
function(err,results,fields){
console.log(results);
connection.end();
}
);
當我日誌中的錯誤,我得到了以下跟蹤:
resutls is:: undefined
{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
at Handshake.Sequence._packetToError (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
at Handshake.ErrorPacket (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/sequences/Handshake.js:67:18)
at Protocol._parsePacket (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/Protocol.js:197:24)
at Parser.write (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/Parser.js:62:12)
at Protocol.write (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/Protocol.js:37:16)
at Socket.ondata (_stream_readable.js:555:20)
at emitOne (events.js:101:20)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
--------------------
at Protocol._enqueue (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/Protocol.js:110:26)
at Protocol.handshake (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/protocol/Protocol.js:42:41)
at Connection.connect (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/Connection.js:81:18)
at Connection._implyConnect (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/Connection.js:222:10)
at Connection.query (/Users/mename/WebstormProjects/nodesql/node_modules/mysql/lib/Connection.js:137:8)
at Object.<anonymous> (/Users/mename/WebstormProjects/nodesql/index.js:10:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
fatal: true }
此外,當我嘗試'選擇用戶,主機,從mysql.user密碼;'從終端,它說沒有'password'字段 –
錯誤清楚地表明用戶'root'不允許在這裏連接。很有可能'root'用戶被禁止通過TCP連接進行登錄 - 僅限套接字或類似。這全是爲了安全目的。因此,調試這種情況的最佳方式是創建普通用戶(例如'my_user')並僅授予所需數據庫的權限(或您希望在此申請的任何限制)。從這個用戶的任何主機/ IP授予訪問權限。 – LazyOne