节点js无法连接到本地数据库

x3naxklr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(377)

我有一个node js应用程序,当我尝试使用本地数据库(mysql)运行node js时,我使用mamp并显示错误 Localhost refuse to connect 和日志显示 TypeError: Cannot read property 'query' of undefined . 但是,当我尝试在服务器中使用db运行节点js时,就可以了。
应用程序.js

  1. var mysql = require("mysql");
  2. function Connection() {
  3. this.pool = null;
  4. var konek = {
  5. host: 'localhost',
  6. user: 'root',
  7. password: 'root',
  8. database: 'wmc_wahana'
  9. };
  10. this.init = function() {
  11. this.pool = mysql.createPool(konek);
  12. }
  13. this.acquire = function(callback) {
  14. this.init();
  15. this.pool.getConnection(function(err, connection) {
  16. callback(err, connection);
  17. });
  18. };
  19. }
  20. module.exports = new Connection();

服务器.js

  1. var app = require('./app');
  2. var http = require('http').Server(app);
  3. var port = '8080';
  4. http.listen(port, function() {
  5. console.log('Server listening on port ' + port);
  6. });

谢谢您

bfhwhh0e

bfhwhh0e1#

请将“localhost”改为“127.0.0.1”

相关问题