2017-08-05 122 views
2

我试图使用mysql库将我的应用程序与我的数据库链接起来。这里是我的代码:链接应用程序与数据库

//Require Libraries 
const mysql = require('mysql'); 

//Set up MySql connection 
const connection = mysql.createConnection({ 
    host  : "255.255.255.255", //that ain't actually the ip :P 
    port  : "3306", 
    user  : "root", 
    password : "password :P", 
    database : "db_name" 
}); 

connection.connect(); //Connect to the database 

的代码提供的错误是:

events.js:182 
     throw er; // Unhandled 'error' event 
    ^

Error: connect ECONNREFUSED (MY IP IS IN THIS BIT):3306 
    at Object.exports._errnoException (util.js:1024:11) 
    at exports._exceptionWithHostPort (util.js:1047:20) 
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14) 
    -------------------- 
    at Protocol._enqueue (C:\file\location:145:48) 
    at Protocol.handshake (C:\file\location\Protocol.js:52:23) 
    at Connection.connect (C:\file\location\Connection.js:130:18) 
    at Object.<anonymous> (C:\file\location\index.js:25:12) 
    at Module._compile (module.js:569:30) 
    at Object.Module._extensions..js (module.js:580:10) 
    at Module.load (module.js:503:32) 
    at tryModuleLoad (module.js:466:12) 
    at Function.Module._load (module.js:458:3) 
    at Function.Module.runMain (module.js:605:10) 

如果你想知道,数据库肯定是跑,我可以使用MySQL工作台连接到它。还有一个简单的说明,即数据库是远程托管的,以防可能改变任何事情。

+0

尝试连接到本地实例,如果它的工作,那么它显然不是你的代码。您必须找出问题出在远程主机上。如果您没有备份远程服务器,请在本地进行故障排除。 – n3wb

回答

0

如果您的用户连接到远程数据库允许您从“主机”执行该操作,请检查MySQL工作台 - >服务器 - >用户和特权。有关更多信息,另请参阅this documentation

+0

我刚刚检查过,用户被允许离开我的主机。我也试图让一个新用户只能从我的IP连接,看看这是否会工作,但仍然没有。 –