8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PythonからOracle接続をSID指定ではなく、SERVICE_NAME指定で接続する

Last updated at Posted at 2018-06-03

PythonからOracleに接続するには、cx_Oracleを使用します。

#SID指定で接続
サンプルでは、cx_Oracle.makedsn でサーバ名、ポート、SIDを指定して戻り値を
cx_Oracle.connect に渡してます。


import cx_Oracle

#サーバ名 / IP
HOST = "svdb"
#ポート
PORT = 1521
#SID
SID = "orcl1"


tns = cx_Oracle.makedsn(HOST, PORT, SID)
conn = cx_Oracle.connect("scott", "tiger", tns)

cx_Oracle.makedsn(HOST, PORT, SID) で返される値を確認

tns = cx_Oracle.makedsn(HOST, PORT, SID)
print(tns)


(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=svdb)(PORT=1521))(CONNECT_DATA=(SID=orcl1)))

SIDを指定した設定値になる

#SERVICE_NAME指定で接続
cx_Oracle.makedsnのドキュメント

cx_Oracle.makedsn(host, port, sid=None, service_name=None, region=None, sharding_key=None, super_sharding_key=None)

今までは引数の3番目がSIDだったので、SIDを指定していたわけですね。
service_nameを指定でいけそう


HOST = "svdb"
#ポート
PORT = 1521
#SERVICE_NAME
SVS = "orcl"

#SERVICE_NAMEを指定
tns = cx_Oracle.makedsn(HOST, PORT, service_name =SVS)
print(tns)


(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=svdb)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=orcl)))

SERVICE_NAME指定ができた!

最後に接続してみる

import cx_Oracle

HOST = "xxxxx"
#ポート
PORT = 1521
#SEVICE_NAME
SVS = "orcl"

#SERVICE_NAMEを指定
tns = cx_Oracle.makedsn(HOST, PORT, service_name =SVS)
conn = cx_Oracle.connect("scott", "tiger", tns)

cur = conn.cursor()

#SERVICE_NAMEを確認
cur.execute("""select name from v$database""")

rows = cur.fetchall()

for r in rows:
    print("SERVICE_NAME:%s" % (r[0]))

#SIDを確認
cur.execute("""select instance_name from v$instance""")

rows = cur.fetchall()

for r in rows:
    print("SID:%s" % (r[0]))

実行結果
サービス名で接続できた

SERVICE_NAME:ORCL
SID:ORCL1

#気がついたこと
cx_Oracle.makedsnを使用することで構成情報が簡単に作成できます。
作成された構成情報には、FAILOVER や LOAD_BALANCE が記載されません。

なので
直接書くことできるんじゃないかと検証

import cx_Oracle

#tns構成情報を作成
tns=" \
    (DESCRIPTION = \
    (ADDRESS = (PROTOCOL = TCP)(HOST = srv102n1-vip)(PORT = 1521)) \
    (ADDRESS = (PROTOCOL = TCP)(HOST = srv102n2-vip)(PORT = 1521)) \
    (LOAD_BALANCE = yes) \
    (FAILOVER=on) \
    (CONNECT_DATA = \
      (SERVER = DEDICATED) \
      (SERVICE_NAME = orcl) \
    ) \
  )"

#作成した構成情報を設定
conn = cx_Oracle.connect("scott", "tiger", tns)

cur = conn.cursor()

#SERVICE_NAMEを確認
cur.execute("""select name from v$database""")

rows = cur.fetchall()

for r in rows:
    print("SERVICE_NAME:%s" % (r[0]))

#SIDを確認
cur.execute("""select instance_name from v$instance""")

rows = cur.fetchall()

for r in rows:
    print("SID:%s" % (r[0]))

実行結果
接続できた!!

SERVICE_NAME:ORCL
SID:ORCL1

SERVICE_NAMEではなく、SID指定する方法は以下の投稿を参照ください。

8
6
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?