简体   繁体   English

docker-compose,连接到 mysql 的节点问题

[英]docker-compose, node issue connecting to mysql

I am trying to get node to connect to my local mysql from within docker-compose我正在尝试让节点从 docker-compose 中连接到我的本地 mysql

I have verified node connects fine from the command line, and docker compose is loading node fine - I can access functions which don't use mysql via localhost我已经从命令行验证了节点连接正常,并且 docker 组合正在加载节点正常 - 我可以通过 localhost 访问不使用 mysql 的功能

my docker compose file我的 docker 撰写文件

       version: '3.6'
        
        services:
          mysql:
            container_name: mysql
            image: mysql:8.0.29
            ports:
              - "3306:3306"
            command: --default-authentication-plugin=mysql_native_password
            restart: always
            environment:
              MYSQL_ROOT_PASSWORD: rootpw
              MYSQL_USER: user
              MYSQL_PASSWORD: paass
              MYSQL_ROOT_HOST: "%"
              MYSQL_DATABASE: cine
            volumes:
              - './services/db/schema:/docker-entrypoint-initdb.d'
        
          node:
            container_name: node
            build:
              context: ./services/node
              dockerfile: Dockerfile-dev
            volumes:
              - './services/node:/usr/src/app'
              - '/usr/src/app/node_modules'
            ports:
              - 3001:3001
            environment:
              - NODE_ENV=development
              - CHOKIDAR_USEPOLLING=true
            depends_on:
              - mysql
        
          client:
            container_name: client
            labels:
              traefik.frontend.passHostHeader: 'true'
            build:
              context: ./services/client
              dockerfile: Dockerfile-dev
            volumes:
              - './services/client:/usr/src/app'
              - '/usr/src/app/node_modules'
            environment:
              - NODE_ENV=development
              - CHOKIDAR_USEPOLLING=true
              - REACT_APP_API_URL=${REACT_APP_API_URL}
            depends_on:
              - node
        
          nginx:
            container_name: nginx
            build:
              context: ./services/nginx
              dockerfile: Dockerfile-dev
            restart: always
            ports:
              - 3007:80
            depends_on:
              - node
              - client

and my node file和我的节点文件

const app = express();
const bodyParser = require('body-parser');
const PORT = 3001;
const nodemailer = require('nodemailer');

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

require('dotenv-extended').load();

var mysql = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'usser',
  password : 'pass!',
  database : 'cine',
  port     : '3306',
  socketPath: '/var/run/mysqld/mysqld.sock'
});


console.log('this works');

app.use(function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});


app.get('/api', function (req, res) {
  res.json({ response: "hello!" });
});


app.get('/api/listfilms', function (req, res) {
sql="select * from films;";
  console.log(sql);
  connection.query(sql, function (error, results, fields) {
    if (error) throw error;
    console.log(results);
    results=JSON.parse(JSON.stringify(results))
    console.log(results);
    if (results == ""){
        var response="error"; 
        var resp = [{
            "id": 0,
            "resp": response,
         },];
    }else{
    var resp = [{
           "id": results.id,
            "resp": results.name,
        },];
     }
     res.json(resp);
  })
})

the error i get is我得到的错误是

node      | Error: connect ENOENT /var/run/mysqld/mysqld.sock
node      |     at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)

I have looked at other similar posts and tried their recommendations but none appear to work我查看了其他类似的帖子并尝试了他们的建议,但似乎没有一个有效

Thanks谢谢

In your node config file the host must be mysql not localhost .在您的节点配置文件中,主机必须是mysql而不是localhost Since you have network configured in between containers, you just can use myql as the host of node config file由于您在容器之间配置了网络,因此您可以使用 myql 作为节点配置文件的主机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM