Kubernetes - NodeJs MySQL pod does not connect with MySQL pod

1/30/2020

I have a MySQL pod up and running. I opened a terminal for this pod and created a database and a user.

create database demodb;
create user demo identified by 'Passw0rd';
grant all on demodb.* to 'demo';

I have this Deployment to launch a NodeJs client for the MySQL pod. This is on my local minikube installation.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demos
spec:
  selector:
    matchLabels:
      app: demos
  template:
    metadata:
      labels:
        app: demos
    spec:
      containers:
      - name: demo-db
        image: 172.30.1.1:5000/demo/db-demos:0.1.0
        resources:
          limits:
            memory: "128Mi"
            cpu: "200m"
        ports:
        - containerPort: 4000
          name: probe-port
---
apiVersion: v1
kind: Service
metadata:
  name: demos
spec:
  selector:
    app: demos
  ports:
  - name: probe-port
    port: 4001
    targetPort: probe-port

The Dockerfile for the image passes the environment variables for the NodeJs client to use.

FROM node:alpine
ADD . .
RUN npm i

WORKDIR /app

ENV PROBE_PORT 4001

ENV MYSQL_HOST "mysql.demo.svc"
ENV MYSQL_PORT "3306"
ENV MYSQL_USER "demo"
ENV MYSQL_PASSWORD "Passw0rd"
ENV MYSQL_DATABASE "demodb"

CMD ["node", "index.js"]

And, the NodeJs client connects as follows.

const mysql = require('mysql')

const connection = mysql.createConnection({
    host: process.env.MYSQL_HOST,
    port: process.env.MYSQL_PORT,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE
});

connection.connect((err) => {
    if (err) {
        console.log('Database connection failed. ' + err.message)
    } else {
        console.log('Database connected.')
    }
});

The database connection keeps failing with a message as Database connection failed. connect ENOENT tcp://172.30.88.64:3306. The TCP/IP address shown in this message is correct i.e. it matches with the service mysql.demo.svc of the running MySQL pod.

In the MySQL configuration files, I don't see bind-address. This should mean that, MySQL should accept connections from 'every where'. I am creating the user without the location qualifier i.e. the user is 'demo'@'%'. The connection is, obviously, not through sockets as I am passing the host and port values for connection.

What am I missing?

-- cogitoergosum
database-connection
docker
kubernetes
mysql
node.js

1 Answer

2/1/2020

I got it working as follows.

const mysql = require('mysql')

const connection = mysql.createConnection({
    host: process.env.MYSQL_HOST,
//    port: process.env.MYSQL_PORT,
    user: process.env.MYSQL_USER,
    password: process.env.MYSQL_PASSWORD,
    database: process.env.MYSQL_DATABASE
});

connection.connect((err) => {
    if (err) {
        console.log('Database connection failed. ' + err.message)
    } else {
        console.log('Database connected.')
    }
});

That's right; I removed the port number from the option. :rolleyes: This example from RedHat is closest I have seen.

Also, I created the user with mysql_native_password as that is the only plugin mechanism that is supported by NodeJs client. See here.

-- cogitoergosum
Source: StackOverflow