在 Ubuntu16 上使用 nginx、phpmyadmin 和 mysql 设置后,Node 应用程序无法连接到数据库

Node app can't connect to database after setting up with nginx, phpmyadmin, and mysql on Ubuntu16

我已经使用 nginx 反向代理、mysql 和 phpmyadmin 成功设置了一个节点应用程序。使用phpmyadmin访问数据库或访问网站都没有问题。

尝试登录时收到的错误消息:

Error: connect ECONNREFUSED my-ip-address:3306

这是 mysql 连接的问题吗?我已经用 mysql 连接检查了代码,但无法找出问题所在。

  var connection = require("mysql").createPool({
    host: "my-ip-address",
    user: "root",
    password: "my-password",
    database: "database-name",
    dateStrings: true,
    charset: "utf8mb4"
  }); 

/etc/nginx/sites-available/default 文件

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;
    server_name "my ip address";

    location / {
        proxy_pass http://localhost:8010/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    location /phpmyadmin {
      root /var/www/html;
      index index.php index.html index.htm index.nginx-debian.html;

      location ~ ^/phpmyadmin/(.+\.php)$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name;

      }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        root /usr/share/;
      }
    }


    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/run/php/php7.0-fpm.sock; 
    }

    location ~ /\.ht {
            deny all;
         }
}

您正在尝试通过外部 IP 地址 (my-ip-address) 连接到数据库,但数据库仅在本地环回接口(127.0.0.1 或本地主机)上侦听。

尝试将节点应用程序中的主机更改为 127.0.0.1localhost

重要:不要让数据库监听所有端口(例如绑定到地址 0.0.0.0)防火墙。