[英]Kubernetes - NodeJs MySQL pod does not connect with MySQL pod
I have a MySQL pod up and running.我有一个 MySQL pod 正在运行。 I opened a terminal for this pod and created a database and a user.我为这个 pod 打开了一个终端并创建了一个数据库和一个用户。
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.我有这个Deployment
来为 MySQL pod 启动一个 NodeJs 客户端。 This is on my local minikube
installation.这是在我本地的minikube
安装中。
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. Dockerfile
传递环境变量供 NodeJs 客户端使用。
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.并且,NodeJs 客户端连接如下。
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
数据库连接不断失败,并显示一条消息,因为Database connection failed. connect ENOENT tcp://172.30.88.64:3306
Database connection failed. connect ENOENT tcp://172.30.88.64:3306
. Database connection failed. connect ENOENT tcp://172.30.88.64:3306
。 The TCP/IP address shown in this message is correct ie it matches with the service mysql.demo.svc
of the running MySQL pod.此消息中显示的 TCP/IP 地址是正确的,即它与正在运行的 MySQL pod 的服务mysql.demo.svc
匹配。
In the MySQL configuration files, I don't see bind-address
.在 MySQL 配置文件中,我没有看到bind-address
。 This should mean that, MySQL should accept connections from 'every where'.这应该意味着,MySQL 应该接受来自“任何地方”的连接。 I am creating the user without the location qualifier ie the user is 'demo'@'%'
.我正在创建没有位置限定符的用户,即用户是'demo'@'%'
。 The connection is, obviously, not through sockets as I am passing the host and port values for connection.显然,连接不是通过套接字,因为我正在传递用于连接的主机和端口值。
What am I missing?我错过了什么?
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. :rolleyes: 这个来自 RedHat 的例子是我见过的最接近的例子。
Also, I created the user with mysql_native_password
as that is the only plugin mechanism that is supported by NodeJs client.此外,我使用mysql_native_password
创建了用户,因为这是 NodeJs 客户端支持的唯一插件机制。 See here .见这里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.