connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
Js
完整的connect.js程序代碼如下所示 -
let mysql = require('mysql');
let connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '123456',
database: 'todoapp'
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
Js
我們來運(yùn)行并測試一下connect.js程序 -
F:\worksp\mysql\nodejs\nodejs-connect>node connect.js
openssl config failed: error:02001003:system library:fopen:No such process
Connected to the MySQL server.
Shell
如果看到如上所示的“connected to the MySQL server”的消息,那么恭喜,您已經(jīng)從node.js應(yīng)用程序成功連接到MySQL數(shù)據(jù)庫服務(wù)器。
F:\worksp\mysql\nodejs\nodejs-connect>node connect.js
openssl config failed: error:02001003:system library:fopen:No such process
error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
更多建議: