LoginSignup
5
0

More than 3 years have passed since last update.

Python (cx_Oracle) から2つ目のAutonomous Database (ADWやATP)に接続してみる

Last updated at Posted at 2019-04-12

■■■Oracle Cloudのウェビナーシリーズは→こちら■■■

はじめに

Autonomous Database (ADW=Autonomous Data Warehouse や ATP=Autonomous Transaction Processing) では接続にはWalletファイルが必要です。接続先の Autonomous Database が複数ある場合に、Walletファイルの置き場所とその指定をTNS_ADMINで行い接続してみました。

ざっくりこんな感じ。

  • 接続するためのWallet_XXX.zipを、新しいディレクトリに配置しunzip
  • 新しいディレクトリ内にあるsqlnet.oraをディレクトリ名に合わせて編集
  • そのディレクトリをTNS_ADMINとして設定

環境

Oracle CloudにあるAutonomous Data WarehouseにJupyter Notebookから接続してみる

にて、1つ目のAutonomous Database(ADW)への接続設定が完了している。

設定した内容

以降、Oracle Instant Clientのバージョンは 18.5 を使っているが、ADWのマニュアルでは18.3をインストールしていたので、心配な方は18.3を推奨。

接続に必要なファイルをダウンロード

2つ目の Autonomous Database(ADW)に接続に必要な、Wallet_XXX.zip(クライアント・セキュリティ資格証明のファイル一式 )を、Download Client Credentials (Wallets) の手順でダウンロードする。

Wallet_XXX.zipを 新しいディレクトリに配置し展開

場所は任意。ここでは、1つ目の接続に使用するデフォルトの場所( $HOME/instantclient_18_5/network/admin )と並列になるようにadmin_cactusディレクトリを作成し、そこにunzipした。

※この場所(ディレクトリ)を、後でTNS_ADMINとして指定する。

$ mkdir /home/app/instantclient_18_5/network/admin_cactus

### Wallet_XXX.zipを配置しunzip
$ cd /home/app/instantclient_18_5/network/admin_cactus
$ unzip Wallet_XXX.zip

sqlnet.oraを編集

新しく作成し配置したディレクトリ(ここでは/home/app/instantclient_18_5/network/admin_cactus)にある、sqlnet.ora を編集する。

WALLET_LOCATION = をこのディレクトリを示すように編集。

sqlnet.ora
WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY="/home/app/instantclient_18_5/network/admin_cactus")))

環境変数 TNS_ADMINを Pythonプログラム内で指定

os.environ['TNS_ADMIN'] = '/home/app/instantclient_18_5/network/admin_cactus'

で、TNS_ADMINを指定している(このPythonプログラムの中でのみ有効な書き方)。

そのこと以外は1つ目のAutonomous Database接続時と同じように、conn=cx_Oracle.connect で接続先を指定する。

connect_sample.py
import cx_Oracle
import os

# 環境変数TNS_ADMINを指定
os.environ['TNS_ADMIN'] = '/home/app/instantclient_18_5/network/admin_cactus'
# [任意] 環境変数をprintして確認
print(os.environ)

# あとは通常通り cx_Oracle.connect() で指定
conn=cx_Oracle.connect(user='scott',password='tiger',dsn='cactus_high')

sql='select table_name, status from user_tables'
print(sql)
cur = conn.cursor()
for row in cur.execute(sql):
    print(row)

参考

5
0
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
5
0