简体   繁体   English

Kube.netes上运行的MySQL如何连接

[英]How to connect MySQL running on Kubernetes

I have deployed my application on Google gcloud container engine.我已经在 Google gcloud容器引擎上部署了我的应用程序。 My application required MySQL .我的申请需要MySQL Application is running fine and connecting to MySQL correctly.应用程序运行正常并正确连接到MySQL But I want to connect MySQL database from my local machine using MySQL Client ( Workbench , or command line), Can some one help me how to expose this to local machine?但是我想使用MySQL客户端( Workbench或命令行)从我的本地计算机连接MySQL数据库,有人可以帮助我如何将其公开给本地计算机吗? and how can I open MySQL command line on gcloud shell?以及如何在gcloud shell 上打开MySQL命令行?

I have run below command but external ip is not there:我已经在命令下运行,但外部 ip 不存在:

$ kubectl get deployment
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
app-mysql   1         1         1            1           2m
$ kubectl get pods
NAME                         READY     STATUS             RESTARTS   AGE
app-mysql-3323704556-nce3w   1/1       Running            0          2m
$ kubectl get service
NAME        CLUSTER-IP    EXTERNAL-IP       PORT(S)    AGE
app-mysql   11.2.145.79   <none>            3306/TCP   23h

EDIT编辑

I am using below yml file:我正在使用下面的yml文件:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app-mysql
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: app-mysql
    spec:
      volumes:
      - name: data
        emptyDir: {}
      containers:
      - name: mysql
        image: mysql:5.6.22
        env:
        - name: MYSQL_USER
          value: root
        - name: MYSQL_DATABASE
          value: appdb
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql/
---
apiVersion: v1
kind: Service
metadata:
  name: app-mysql
spec:
  selector:
    app: app-mysql
  ports:
  - port: 3306

Try the kubectl port-forward command.试试kubectl port-forward命令。

In your case;在你的情况下; kubectl port-forward app-mysql-3323704556-nce3w 3306:3306

See The documentation for all available options.有关所有可用选项,请参阅文档

There are 2 steps involved:涉及两个步骤:

1 ) You first perform port forwarding from localhost to your pod: 1 ) 您首先执行从本地主机到您的 pod 的端口转发:

kubectl port-forward <your-mysql-pod-name> 3306:3306 -n <your-namespace>

2 ) Connect to database: 2)连接数据库:

mysql -u root -h 127.0.0.1 -p <your-password>
 

Notice that you might need to change 127.0.0.1 to localhost - depends on your setup.请注意,您可能需要将127.0.0.1更改为localhost - 取决于您的设置。

If host is set to:如果主机设置为:
localhost - then a socket or pipe is used. localhost - 然后使用套接字或 pipe。
127.0.0.1 - then the client is forced to use TCP/IP. 127.0.0.1 - 然后客户端被迫使用 TCP/IP。

You can check if your database is listening for TCP connections with netstat -nlp .您可以使用netstat -nlp检查您的数据库是否正在侦听 TCP 连接。


Read more in:阅读更多:

Cant connect to local mysql server through socket tmp mysql sock 无法通过套接字 tmp mysql sock 连接到本地 mysql 服务器

Can not connect to server 无法连接到服务器

To add to the above answer, when you add --address 0.0.0.0 kubectl should open 3306 port to the INTERNET too (not only localhost)!要添加到上述答案中,当您添加--address 0.0.0.0 kubectl 时,还应该打开 3306 到 INTERNET 的端口(不仅是本地主机)!

kubectl port-forward POD_NAME 3306:3306 --address 0.0.0.0

Use it with caution for short debugging sessions only, on development systems at most.仅在短期调试会话中谨慎使用它,最多在开发系统上使用。 I used it in the following situation:我在以下情况下使用它:

  • colleague who uses Windows使用 Windows 的同事
  • didn't have ssh key ready没有准备好 ssh 密钥
  • environment was a playground I was not afraid to expose to the world环境是我不怕暴露给世界的游乐场

You need to add a service to your deployment.您需要向部署添加服务。 The service will add a load balancer with a public ip in front of your pod, so it can be accessed over the public internet.该服务将在您的 pod 前添加一个带有公共 ip 的负载均衡器,以便可以通过公共互联网访问它。

See the documentation on how to add a service to a Kubernetes deployment .请参阅有关如何向 Kubernetes 部署添加服务的文档 Use the following code to add a service to your app-mysql deployment:使用以下代码向您的 app-mysql 部署添加服务:

kubectl expose deployment/app-mysql

You may also need to configure your MySQL service so it allows remote connections.您可能还需要配置 MySQL 服务以允许远程连接。 See this link on how to enable remote access on MySQL server :请参阅有关如何在 MySQL 服务器上启用远程访问的链接:

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

相关问题 如何通过 Kubernetes 连接到云 SQL - How to Connect to Cloud SQL Through Kubernetes 如何在 AWS 中编写一个 Lamdda function 以允许我连接到运行 MySQL 的 EC2 实例 - How do I write a Lamdda function in AWS that allows me to connect to an EC2 instance that is running MySQL 如何让容器在 Kubernetes 上运行? - How can I keep a container running on Kubernetes? 从本地运行的 NodeJS / TypeORM 连接到 Google Cloud MySQL 实例 - Connect to Google Cloud MySQL instance from local running NodeJS / TypeORM 如何在 Kube.netes 中将 PostgresSQL pod 连接到 Ruby on Rails pod - How to connect PostgresSQL pod to Ruby on Rails pod in Kubernetes lambda nodejs中如何连接mysql - How to connect mysql in lambda nodejs 带插件的 Kibana 在 Kube.netes 上运行 - Kibana with plugins running on Kubernetes 如何确保每个 kube.netes 节点运行 2 个或更多 pod? - How to ensure every kubernetes node running 2 or more pods? 如何将PowerBI桌面连接到Azure MySQL和SSL - How to connect PowerBI desktop to Azure MySQL with SSL 如何使用 Terraform 修复 Azure Kube.netes 服务“错误拨号 tcp 127.0.0.1:80:连接:连接被拒绝”? - How to fix Azure Kubernetes Services with Terraform 'error dial tcp 127.0.0.1:80: connect: connection refused'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM