无法从node.js服务器连接到localhost数据库

编程入门 行业动态 更新时间:2024-10-21 04:01:50
本文介绍了无法从node.js服务器连接到localhost数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

尝试连接到我的本地主机数据库时遇到很多麻烦.我尝试使用 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数据库

本文发布于:2023-10-24 14:31:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   服务器   数据库   node   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!