尝试连接到我的本地主机数据库时遇到很多麻烦.我尝试使用 mysql 和 mysql-simple 节点模块,但在两种情况下,我都无法连接它.
Been having a lot of trouble trying to connect to to my localhost database. I've tried using the mysql and mysql-simple node modules but in both cases I just can't get it to connect.
这是我在'mysql'模块中使用的内容:
Here's what I used with the 'mysql' module:
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', port : '8000', user : 'uber', password : 'pass', }); connection.connect(function(err) { if (err) throw err; console.log('Connection Successful'); }); connection.query('USE someDB', function(err) { if (err) throw err; console.log('Query Successful'); });这是我在'mysql-simple'模块中使用的内容:
And here' what I used with the 'mysql-simple' module:
var database = require('mysql-simple'); database.init('uber', 'pass', 'mysql', 'localhost', 8000); database.querySingle('SELECT Host FROM user', function(err, results) { if (err) { console.log('error fetching some active users: ' + err); return; } log('Query Successful'); for (var i = 0; i < results.length; i++) console.log('got active user ' + results[i]); }在两种情况下,当我运行node.js服务器时,它都不会记录其已连接.我尝试用"127.0.01"替换localhost并创建一个新用户以确保密码正确,但无济于事.为什么不连接?
In both cases, when I run my node.js server, it never logs that its connected. I've tried replacing localhost with '127.0.01' and creating a new user to make sure the password is correct, but to no avail. Why isn't it connecting?
谢谢
推荐答案最有可能关闭了网络连接,这意味着mysql服务器通过UNIX套接字而不是通过TCP/IP与客户端进行通信.您可以检查出正在运行的mysql客户端并运行"status"命令.如果您在此处看到端口号,则您的mysql服务器将通过TCP/IP进行通信,否则您将看到类似"socket pathname…"的信息,获取该路径名并将其提供给node.js连接参数,例如
It's most likely that networking is turned off, that means that mysql server communicates with clients via UNIX sockets and not via TCP/IP. You can check that out running mysql client and run "status" command. If you see port number there, then your mysql server communicates via TCP/IP, or else you'll see something like "socket pathname…", get the pathname and give it to node.js connection parameters, e.g.
... socketPathname:'/opt/lampp/var/...', ...
... socketPathname: '/opt/lampp/var/...', ...
在 github/felixge/node-mysql 页面中进行检查(搜索"socketPathname")
Check that out in github/felixge/node-mysql page (search for "socketPathname")
希望,这是你的问题.
更多推荐
无法从node.js服务器连接到localhost数据库
发布评论