# Connecting to an external mysql database

**URL:** <https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201>\
**Category:** General Discussions\
**Created:** [October 2, 2019, 4:38pm UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201 "2019-10-02T16:38:07Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![cultofdev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/cultofdev/32/3841_2.png) [@cultofdev](https://discuss.kubernetes.io/u/cultofdev)\
**Post date:** [October 2, 2019, 4:38pm UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/1 "2019-10-02T16:38:07Z")

</div>

I am trying to connect my spring-boot api inside a kubernetes pod to an external database (a separate machine but within the local network), however when running, I get SQLNonTransientConnectionException, UknownHostException: mysql-svc, the api cannot connect to the database but whenever I ping the database address from inside the pod using kubectl exec -it, I can ping it successfully, anybody who has experienced the same error as mine?

here is my application.properties connection uri and kubernetes objects:

```auto
spring.datasource.url=jdbc:mysql://mysql-svc/test?autoReconnect=true

---
apiVersion: v1
kind: Service
metadata:
 name: mysql-svc
spec:
 type: ClusterIP
 ports:
 - port: 3306
   targetPort: 3306
---
apiVersion: v1
kind: Endpoints
metadata:
 name: mysql-svc
subsets:
 - addresses:
   - ip: 10.0.123.28
   ports:
   - port: 3306
     name: mysql-svc

```

Cluster information:  
Kubernetes version: 1.16  
Cloud being used: VMs on local network  
Host OS: CentOS 7  
CNI and version: Flannel latest  
CRI and version: Docker 19.03.2

---

<div class="post-metadata">

**Author:** ![feloy](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/feloy/32/3669_2.png) [@feloy](https://discuss.kubernetes.io/u/feloy)\
**Post date:** [October 2, 2019, 5:07pm UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/2 "2019-10-02T17:07:28Z")

</div>

Hi,

Are you ping’ing the IP address, or the mysql-mvc name?

It seems to be a problem to dns-resolve the name mysql-mvc. Perhaps because your pod is not in the same namespace as your service?

Philippe

---

<div class="post-metadata">

**Author:** ![cultofdev](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/cultofdev/32/3841_2.png) [@cultofdev](https://discuss.kubernetes.io/u/cultofdev)\
**Post date:** [October 3, 2019, 12:48am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/3 "2019-10-03T00:48:11Z")

</div>

yes the ip address, I was able to ping the database server via ip address, I haven’t tried pinging with the mysql-svc name…

---

<div class="post-metadata">

**Author:** ![CrankyCoder](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/crankycoder/32/2373_2.png) [@CrankyCoder](https://discuss.kubernetes.io/u/CrankyCoder)\
**Post date:** [October 7, 2019, 2:51am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/4 "2019-10-07T02:51:16Z")

</div>

why would you need to create a service for a mysql server that’s outside of the kubernetes cluster? just provide the ip or the endpoint, i don’t think you do a service as that setups an ip inside the k cluster right?

---

<div class="post-metadata">

**Author:** ![rata](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/rata/32/18244_2.png) [@rata](https://discuss.kubernetes.io/u/rata)\
**Post date:** [October 8, 2019, 12:15am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/5 "2019-10-08T00:15:27Z")

</div>

If the database is external to the cluster, then m the service type cluster IP won’t help. That will try to match pods inside the cluster (there are none, as it is an external db).

You can use the IP to try to connect and make sure this works. Once this works, we can follow up with improvements. But let’s get started with the basic 🙂

---

<div class="post-metadata">

**Author:** ![Sharanya\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/sharanya_m/32/4162_2.png) [@Sharanya\_M](https://discuss.kubernetes.io/u/Sharanya_M)\
**Post date:** [June 5, 2020, 6:00am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/6 "2020-06-05T06:00:35Z")

</div>

Hi,

Good day…  
Have you got solution for this issue…? Am also trying to connect external oracle database in spring boot application deployed in kubernetes container. I created a service to connect the external oracle database and am able ping the oracle server inside the kubernetes container. But Application is not able to connect the oracle server and throwing socket time out exception.  
Please help here to proceed.? thanks much.

**application.yml**  
spring:  
#profiles: dev  
datasource:  
jdbc-url: ‘jdbc:oracle:thin:@oracle-server:port/servicename’  
username: —  
password: —  
driver-class-name: oracle.jdbc.OracleDriver

**oracle-service.yml**

kind: Service  
apiVersion: v1  
metadata:  
name: oracle-server  
spec:  
type: ExternalName  
externalName: oracle-server-hostnmae

**Inside Kubernetes container**

root@tomcat-springboot-pks-stateful-0:/# ping oracle-server-hostname  
PING oracle-server-hostname (----ip----------) 56(84) bytes of data.  
64 bytes from ----ip---------- (----ip----------): icmp\_seq=1 ttl=49 time=31.5 ms  
64 bytes from ----ip---------- (----ip----------): icmp\_seq=2 ttl=49 time=30.9 ms  
64 bytes from ----ip---------- (----ip----------): icmp\_seq=3 ttl=49 time=31.0 ms  
64 bytes from ----ip---------- (----ip----------): icmp\_seq=4 ttl=49 time=31.1 ms

**Kubernetes Pod Logs**

18:16:18.405 [http-nio-8092-exec-5] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLRecoverableException: IO Error: Socket read timed out] with root cause  
oracle.net.ns.NetException: Socket read timed out  
at oracle.net.ns.Packet.receive(Packet.java:350)  
at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:159)  
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:264)  
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)  
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)  
at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)  
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)

---

<div class="post-metadata">

**Author:** ![Audumbar\_Nevarekar](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/audumbar_nevarekar/32/5432_2.png) [@Audumbar\_Nevarekar](https://discuss.kubernetes.io/u/Audumbar_Nevarekar)\
**Post date:** [June 29, 2020, 2:05pm UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/7 "2020-06-29T14:05:17Z")

</div>

Did you find the solution for this? I’m also facing this issue.

---

<div class="post-metadata">

**Author:** ![Sharanya\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/sharanya_m/32/4162_2.png) [@Sharanya\_M](https://discuss.kubernetes.io/u/Sharanya_M)\
**Post date:** [July 13, 2020, 9:19am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/8 "2020-07-13T09:19:03Z")

</div>

Yes… I found… I created the external service for the oracle database server and connected with the springboot application with service name and it works… 🙂

---

<div class="post-metadata">

**Author:** ![Subhra-M](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Subhra-M](https://discuss.kubernetes.io/u/Subhra-M)\
**Post date:** [July 17, 2020, 5:33am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/9 "2020-07-17T05:33:04Z")

</div>

Hi @Sharanya_M, please share the service and application yaml details. Facing same issue.

---

<div class="post-metadata">

**Author:** ![Sharanya\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/sharanya_m/32/4162_2.png) [@Sharanya\_M](https://discuss.kubernetes.io/u/Sharanya_M)\
**Post date:** [July 17, 2020, 9:09am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/10 "2020-07-17T09:09:48Z")

</div>

sure… here you go…

kind: Service  
apiVersion: v1  
metadata:  
name: /\*\* oracle server name which is configured in springboot application.yml file … Ex - oracleserver **/  
spec:  
type: ExternalName  
externalName: /** oracle hostname. Ex - [oracleserver.xx.yyy.com](http://oracleserver.xx.yyy.com) \*\*/

## Springboot application.yml

spring:  
#profiles: dev  
datasource:  
jdbc-url: ‘jdbc:oracle:thin:@oracleserver.xx.yyy.com:port/service\_id’  
username: xxxx  
password: yyy  
driver-class-name: oracle.jdbc.OracleDriver

---

<div class="post-metadata">

**Author:** ![Subhra-M](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Subhra-M](https://discuss.kubernetes.io/u/Subhra-M)\
**Post date:** [July 17, 2020, 9:27am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/11 "2020-07-17T09:27:43Z")

</div>

> [@Sharanya\_M](#):
>
> server name which is configured in springboot application.

Thank you.

---

<div class="post-metadata">

**Author:** ![Sharanya\_M](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/sharanya_m/32/4162_2.png) [@Sharanya\_M](https://discuss.kubernetes.io/u/Sharanya_M)\
**Post date:** [July 17, 2020, 9:59am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/12 "2020-07-17T09:59:04Z")

</div>

your oracle server name…

---

<div class="post-metadata">

**Author:** ![Chanh\_Tran](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/chanh_tran/32/5688_2.png) [@Chanh\_Tran](https://discuss.kubernetes.io/u/Chanh_Tran)\
**Post date:** [August 13, 2020, 4:38pm UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/13 "2020-08-13T16:38:46Z")

</div>

Do I can use external for ip addr? I’m facing this problem when I try to connect external oracle database.

I can ping Database IP from inside container but the app cannot.

I create the Cluster Service and EndPoint for it but it doesn’t work.

---

<div class="post-metadata">

**Author:** ![bappa0125](https://avatars.discourse-cdn.com/v4/letter/b/a6a055/32.png) [@bappa0125](https://discuss.kubernetes.io/u/bappa0125)\
**Post date:** [September 10, 2020, 3:28am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/14 "2020-09-10T03:28:28Z")

</div>

You cannot use it with ip you have to have a dns name then only it will get resolved bt core DNS

---

<div class="post-metadata">

**Author:** ![revathi77](https://avatars.discourse-cdn.com/v4/letter/r/5fc32e/32.png) [@revathi77](https://discuss.kubernetes.io/u/revathi77)\
**Post date:** [September 23, 2020, 11:38am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/15 "2020-09-23T11:38:43Z")

</div>

Hi

I am trying to connect my external oracle database from the container using Proc C. I did the same step, it is not connecting getting tns destination host unreachable. Could any one help on this.

service.yaml  
kind: “Service”  
apiVersion: “v1”  
metadata:  
name: “external-mysql-service”  
spec:  
ports:  
-  
name: “mysql”  
protocol: “TCP”  
port: 1525  
targetPort: 1525  
nodePort: 0  
selector: {}

Endpoint.yaml  
kind: “Endpoints”  
apiVersion: “v1”  
metadata:  
name: “external-mysql-service”  
subsets:  
-  
addresses:  
-  
ip: hostipadd  
ports:  
-  
port: 1525  
name: “mysql”

proc application:  
username : myuser  
password: mypwd  
connectionstring: external-mysql-service/servicename  
(Actual connectionstring : ip:port/servicenamee")  
EXEC SQL CONNECT :username IDENTIFIED BY :password USING :connectionstring

Thanks

---

<div class="post-metadata">

**Author:** ![stefano\_terzon](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.kubernetes.io/stefano_terzon/32/5235_2.png) [@stefano\_terzon](https://discuss.kubernetes.io/u/stefano_terzon)\
**Post date:** [December 12, 2020, 10:27am UTC](https://discuss.kubernetes.io/t/connecting-to-an-external-mysql-database/8201/16 "2020-12-12T10:27:04Z")

</div>

```auto
To connect my app to the external db
## Is this service right for my app?

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 9376 ## the port of the pod can be the same as that of the external db ?? ?
  externalIPs:
    - 80.11.12.10 ## can it be the ip of the db ???

```
