当连接到 Kubernetes 的 MySQL 服务时,出现了 go-mysql 连接被拒绝的问题。

huangapple go评论111阅读模式
英文:

go-mysql connection refused when connecting to kubernetes mysql service

问题

我在使用标准包连接到一个 MySQL 实例时遇到了问题。这是我的连接字符串/日志:

[13 Nov 16 13:53 +0000] [INFO] connecting to MySQL.. root:awsomepass@tcp(a-mysql-0:3340)/db?charset=utf8&parseTime=True&loc=Local
2016/11/13 13:53:25 dial tcp 10.108.1.35:3340: getsockopt: connection refused

我尝试了以下操作:

GRANT ALL PRIVILEGES ON . TO 'root'@'%' WITH GRANT OPTION;

这是我建立连接的方式,只是基本的字符串拼接:

db, err := sql.Open("mysql", "root:awsomepass@tcp(a-mysql-0:3340)/db?charset=utf8&parseTime=True&loc=Local")
if err != nil {
log.Fatal(err)
}

我可以通过不同的 pod 使用 mysql-client 连接到该服务。

可以连接到服务,不需要端口

/ # mysql -u root -h a-mysql-0 -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> Ctrl-C -- exit!
Aborted

无法使用端口连接到服务

/ # mysql -u root -h a-mysql-0:3340 -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'a-mysql-0:3340' (-3)

还有 mysql-service:

kubectl describe svc a-mysql-0
Name: a-mysql-0
Namespace: default
Labels: name=a-mysql-0
tier=database
type=mysql
Selector: name=a-mysql-0,tier=database
Type: ClusterIP
IP: None
Port: a-mysql-0 3340/TCP
Endpoints: 10.108.1.35:3340
Session Affinity: None
No events.

我是否遗漏了什么或者权限有问题?

英文:

I have a problem when connecting to a mysql instance with a go app using standard package.
This is my connection string/log

  1. [13 Nov 16 13:53 +0000] [INFO] connecting to MySQL.. root:awsomepass@tcp(a-mysql-0:3340)/db?charset=utf8&parseTime=True&loc=Local
  2. 2016/11/13 13:53:25 dial tcp 10.108.1.35:3340: getsockopt: connection refused

I tried

  1. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

here is how I make connection, just basic, with string concatenation only

  1. db, err := sql.Open("mysql", "root:awsomepass@tcp(a-mysql-0:3340)/db?charset=utf8&parseTime=True&loc=Local")
  2. if err != nil {
  3. log.Fatal(err)
  4. }

I can ping the service, connect to it with mysql-client from a different pod.

  1. # can connect without port for service
  2. / # mysql -u root -h a-mysql-0 -p
  3. Enter password:
  4. Welcome to the MariaDB monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 11
  6. Server version: 5.7.16 MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9. MySQL [(none)]> Ctrl-C -- exit!
  10. Aborted
  11. # can't' connect with port for service
  12. / # mysql -u root -h a-mysql-0:3340 -p
  13. Enter password:
  14. ERROR 2005 (HY000): Unknown MySQL server host 'a-mysql-0:3340' (-3)

and the mysql-service

  1. stg git:(develop) kubectl describe svc a-mysql-0
  2. Name: a-mysql-0
  3. Namespace: default
  4. Labels: name=a-mysql-0
  5. tier=database
  6. type=mysql
  7. Selector: name=a-mysql-0,tier=database
  8. Type: ClusterIP
  9. IP: None
  10. Port: a-mysql-0 3340/TCP
  11. Endpoints: 10.108.1.35:3340
  12. Session Affinity: None
  13. No events.

Is there anything I have missed or permission?

答案1

得分: 2

我从 Kubernetes Slack 收到了来自 mav 的回复。我正在访问 mysql-service 的错误 container-port。默认的 MySQL 端口是 3306,我以为我正在使用一个自定义容器,它暴露了 3340 端口。

英文:

got a response from kubernetes-slack, from mav. I am accessing the mysql-service to a wrong container-port. default mysql port was 3306. I thought I was using a custom container that exposes 3340.

huangapple
  • 本文由 发表于 2016年11月13日 22:01:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/40574651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定