Check Database Connection with Python

Buat Environment database, dapat gunakan apa saja untuk melakukan testing. disini menggunakan kubernetes

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql-database
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql-database
    spec:
     containers:
      - name: mysql-database
        image: mysql/mysql-server
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: rootpw
        - name: MYSQL_DATABASE
          value: db1
        - name: MYSQL_USER
          value: myuser
        - name: MYSQL_PASSWORD
          value: mypass
---
kind: Service
apiVersion: v1
metadata:
  name: mysql-server
spec:
  ports:
  - port: 3306
    protocol: TCP
  selector:
    app: mysql-database
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgre-database
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgre-database
    spec:
     containers:
      - name: postgre-database
        image: postgres
        ports:
        - containerPort: 5423
        env:
        - name: POSTGRES_PASSWORD
          value: mypass
        - name: POSTGRES_DB
          value: db1
        - name: POSTGRES_USER
          value: myuser
---
kind: Service
apiVersion: v1
metadata:
  name: postgre-server
spec:
  ports:
  - port: 5432
    protocol: TCP
  selector:
    app: postgre-database

Lakukan deploy

ubuntu@master:~$ kubectl create -f database.yaml 
deployment.extensions "mysql-database" created
service "mysql-server" created
deployment.extensions "postgre-database" created
service "postgre-server" created
ubuntu@master:~$ kubectl get pod
NAME                                READY     STATUS    RESTARTS   AGE
mysql-database-858b54c6bf-jg7xb     1/1       Running   0          1m
postgre-database-6cf557b5d6-4qsjn   1/1       Running   0          1m
ubuntu@master:~$ kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP    8d
mysql-server     ClusterIP   10.101.194.152   <none>        3306/TCP   1m
postgre-server   ClusterIP   10.98.79.78      <none>        5432/TCP   1m

MySQL

Install module

pip install mysql-connector-python

program check database

import mysql.connector
from mysql.connector import Error
 
 
def connect():
    """ Connect to MySQL database """
    try:
        conn = mysql.connector.connect(host='10.101.194.152',
                                       database='db1',
                                       user='myuser',
                                       password='mypass')
        if conn.is_connected():
            print('Connected to MySQL database')
 
    except Error as e:
        print(e)
 
    finally:
        conn.close()
 
 
if __name__ == '__main__':
    connect()

jalankan

ubuntu@master:~$ python db-sql.py 
Connected to MySQL database

PostgreSQL

InstallĀ  Module

pip install psycopg2 psycopg2-binary

Buat Program

import psycopg2
try:
    db = psycopg2.connect("dbname='db1' user='myuser' host='10.98.79.78' password='mypass'")
except:
    exit(1)

exit(0)

jalankan

ubuntu@master:~$ python db-post.py && echo 'OK' || echo 'FAIL'
OK

Comments are closed.