简体   繁体   中英

docker-compose, node issue connecting to mysql

I am trying to get node to connect to my local mysql from within docker-compose

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

my docker compose file

       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 . Since you have network configured in between containers, you just can use myql as the host of node config file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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