无法从node.js服务器连接到localhost数据库 [英] Can't connect to localhost database from node.js server

查看:371
本文介绍了无法从node.js服务器连接到localhost数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试连接到我的本地主机数据库时遇到很多麻烦.我尝试使用 mysql

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/...', ...

https://github.com/felixge/node-mysql 页面中进行检查(搜索"socketPathname")

Check that out in https://github.com/felixge/node-mysql page (search for "socketPathname")

希望,这是你的问题.

这篇关于无法从node.js服务器连接到localhost数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆