Thursday, April 11, 2019

How can I access Oracle database from Python language using unique way ?


How can I access Oracle database from Python language?


we usually get issue while working with oracle database using python langaugae . The big issue how to connect with database .

There are many articles are available but when you run inti issue very few helps on out . 
if you are here then you issue is solved to how to connect with oracle database using python langaugae ?



* First you need to install python (in my case python version is  3.7.2)
* Install Oracle client (in my case oracle 18c)
* Update network configuration file tnanames.ora using db details 
* Stop firewall on linux (db install on linux in my case )
* Install oracle cx_Oracle  module in python using pip command as below 
* Start listener (db service) on oracle server
 

save the file as oracle_test.py

from __future__ import print_function

import cx_Oracle

# Connect as user "hr" with password "hr" to the "ind" service running on this server.
connection = cx_Oracle.connect("hr", "hr", "192.168.43.68/ind")

cursor = connection.cursor()
cursor.execute("""
    SELECT first_name, last_name
    FROM employees
    WHERE department_id = :did AND employee_id > :eid""",
    did = 55,
    eid = 180)
for fname, lname in cursor:
    print("Values:", fname, lname)


* you can upgrade pip module using below snapshot:

 

Wednesday, April 10, 2019

Oracle database network configuration files - listener.ora and tnsnames.ora


DB Network Configuration file 



tnsnames.ora

[oracle@pri admin]$ pwd
/u01/app/oracle/product/18.0.0/db/network/admin


[oracle@pri admin]$


[oracle@pri admin]$ cat tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/18.0.0/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

IND =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.43.68)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = IND)
    )
  )

##LISTENER_IND =
##  (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.43.68)(PORT = 1521))

USA =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.43.69)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = USA)
    )
  )


listener.ora


[oracle@pri admin]$ cat listener.ora
# listener.ora Network Configuration File: /u01/app/oracle/product/18.0.0/db/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.43.68)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = IND)
      (ORACLE_HOME = /u01/app/oracle/product/18.0.0/db)
      (SID_NAME = IND)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = USA)
      (ORACLE_HOME = /u01/app/oracle/product/18.0.0/db)
      (SID_NAME = USA)
    )
  )

ADR_BASE_LISTENER = /u01/app/oracle




[oracle@pri admin]$


[oracle@pri ~]$ lsnrctl status

LSNRCTL for Linux: Version 18.0.0.0.0 - Production on 10-APR-2019 22:46:01

Copyright (c) 1991, 2018, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.43.68)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 18.0.0.0.0 - Production
Start Date                10-APR-2019 17:03:30
Uptime                    0 days 5 hr. 42 min. 30 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/18.0.0/db/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/pri/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.43.68)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "IND" has 1 instance(s).
  Instance "IND", status UNKNOWN, has 1 handler(s) for this service...
Service "USA" has 1 instance(s).
  Instance "USA", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@pri ~]$

Oracle 18c client installation

Oracle 18c client installation 


*  Download oracle client software .
* unzip the software 

follow below steps for installation :-

setup run by administrator 




you can use different user as you requirement 












Add database details in tnanames.ora



C:\Users\ANURAG>
C:\Users\ANURAG>sqlplus hr/hr@ind

SQL*Plus: Release 18.0.0.0.0 - Production on Wed Apr 10 22:42:30 2019
Version 18.3.0.0.0

Copyright (c) 1982, 2018, Oracle.  All rights reserved.

Last Successful login time: Wed Apr 10 2019 20:48:43 +05:30

Connected to:
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.5.0.0.0

SQL> select name,open_mode from v$database;

NAME      OPEN_MODE
--------- --------------------
IND       READ WRITE


SQL>


ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [], [], [], []

 Resolve   ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [], [], [], []


[oracle@pri ~]$ sqlplus "/as sysdba"

SQL*Plus: Release 18.0.0.0.0 - Production on Wed Apr 10 16:05:24 2019
Version 18.5.0.0.0

Copyright (c) 1982, 2018, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 1526725696 bytes
Fixed Size                  8657984 bytes
Variable Size             503316480 bytes
Database Buffers         1006632960 bytes
Redo Buffers                8118272 bytes
Database mounted.
ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [],
[], [], [], [], [], [], [], []


SQL>


in alert log
===============
Database mounted in Exclusive Mode
Lost write protection disabled
.... (PID:3113): Using STANDBY_ARCHIVE_DEST parameter default value as USE_DB_RECOVERY_FILE_DEST [krsd.c:17733]
2019-04-10T16:35:57.598943+05:30
Completed: ALTER DATABASE   MOUNT
2019-04-10T16:35:57.703170+05:30
ALTER DATABASE OPEN
Ping without log force is disabled:
  instance mounted in exclusive mode.
2019-04-10T16:35:58.087355+05:30
Beginning crash recovery of 1 threads
 Thread 1: Recovery starting at checkpoint rba (logseq 34 block 1464), scn 0
2019-04-10T16:35:58.236926+05:30
Started redo scan
2019-04-10T16:35:59.829966+05:30
Hex dump of (file 3, block 72034) in trace file /u01/app/oracle/diag/rdbms/ind/IND/trace/IND_ora_3118.trc

Reading datafile '/u01/app/oracle/oradata/IND/sysaux01.dbf' for corrupt data at rdba: 0x00c11962 (file 3, block 72034)
Reread (file 3, block 72034) found same corrupt data (logically corrupt)
Write verification failed for File 3 Block 72034 (rdba 0xc11962)
*****************************************************************
An internal routine has requested a dump of selected redo.
This usually happens following a specific internal error, when
analysis of the redo logs will help Oracle Support with the
diagnosis.
It is recommended that you retain all the redo logs generated (by
all the instances) during the past 12 hours, in case additional
redo dumps are required to help with the diagnosis.
*****************************************************************
2019-04-10T16:36:02.015670+05:30
Errors in file /u01/app/oracle/diag/rdbms/ind/IND/trace/IND_ora_3118.trc  (incident=69873):
ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [], [], [], [], [], [], [], [], []
Incident details in: /u01/app/oracle/diag/rdbms/ind/IND/incident/incdir_69873/IND_ora_3118_i69873.trc
2019-04-10T16:36:06.342365+05:30
Use ADRCI or Support Workbench to package the incident.
See Note 411.1 at My Oracle Support for error and packaging details.
2019-04-10T16:36:06.342847+05:30
Aborting crash recovery due to error 600
2019-04-10T16:36:06.343449+05:30
Errors in file /u01/app/oracle/diag/rdbms/ind/IND/trace/IND_ora_3118.trc:
ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [], [], [], [], [], [], [], [], []
2019-04-10T16:36:06.344570+05:30
Errors in file /u01/app/oracle/diag/rdbms/ind/IND/trace/IND_ora_3118.trc:
ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [], [], [], [], [], [], [], [], []
ORA-600 signalled during: ALTER DATABASE OPEN...
2019-04-10T16:36:07.019702+05:30
Dumping diagnostic data in directory=[cdmp_20190410163607], requested by (instance=1, osid=3118), summary=[incident=69873].
2019-04-10T16:36:23.886376+05:30
TMON (PID:3112): STARTING ARCH PROCESSES
Starting background process ARC0
Starting background process ARC1
2019-04-10T16:36:23.938449+05:30
ARC0 started with pid=35, OS id=3121
Starting background process ARC2
2019-04-10T16:36:23.974871+05:30
ARC1 started with pid=36, OS id=3124
Starting background process ARC3
2019-04-10T16:36:24.003033+05:30
ARC2 started with pid=37, OS id=3126
TMON (PID:3112): ARC0: Archival started
TMON (PID:3112): ARC1: Archival started
TMON (PID:3112): ARC2: Archival started
2019-04-10T16:36:24.026025+05:30
ARC0 (PID:3121): Becoming a 'no FAL' ARCH
ARC0 (PID:3121): Becoming the 'no SRL' ARCH
2019-04-10T16:36:24.026857+05:30
ARC3 started with pid=38, OS id=3128
2019-04-10T16:36:24.036139+05:30
TMON (PID:3112): ARC3: Archival started
TMON (PID:3112): STARTING ARCH PROCESSES COMPLETE
2019-04-10T16:36:24.054208+05:30
TT00 (PID:3130): Gap Manager starting




Solution;

[oracle@pri ~]$ sqlplus "/as sysdba"

SQL*Plus: Release 18.0.0.0.0 - Production on Wed Apr 10 16:05:24 2019
Version 18.5.0.0.0

Copyright (c) 1982, 2018, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup
ORACLE instance started.

Total System Global Area 1526725696 bytes
Fixed Size                  8657984 bytes
Variable Size             503316480 bytes
Database Buffers         1006632960 bytes
Redo Buffers                8118272 bytes
Database mounted.
ORA-00600: internal error code, arguments: [kcratr_scan_lastbwr], [], [], [],
[], [], [], [], [], [], [], []


SQL> shut abort;
ORACLE instance shut down.
SQL>
SQL>
SQL> startup mount
ORACLE instance started.

Total System Global Area 1526725696 bytes
Fixed Size                  8657984 bytes
Variable Size             503316480 bytes
Database Buffers         1006632960 bytes
Redo Buffers                8118272 bytes
Database mounted.
SQL>
SQL> recover database;
Media recovery complete.
SQL> alter database open;

Database altered.

SQL> select name,open_mode from v$database;

NAME      OPEN_MODE
--------- --------------------
IND       READ WRITE

SQL>