Solved

Which account to use when connecting to Oracle DB from script?

Posted on 2011-02-27
6
802 Views
Last Modified: 2013-12-19
Hi,

please can somebody help me with the following?
I'm trying to connect to Oracle from PowerShell script. I believe the only problem is in the incorrect connection string and I don't know, which user account should be used in the string.

The hostname is ORA1 and there I have only the default database called "orcl".
If I use lsnrctl status, it displays the info about runninc instance "orcl", so I think this shouldn't be a problem.
I'm able to connect to database from sqlplus. I know almost nothing about Oracle, but it seems that I can connect successfully if I use the username "sys as sysdba".
I set all the passwords to be "abc" just for simplicity.

I doesn't let me in if I use the account sysdba directly - it works only as "sys as sysdba".

Now regarding the script - it's almost the same. If I have "sys" in the connection string, I get an error message saying "you should use 'sys as sysdba' as logon name".
If I change it in the connection string and use the username "sys as sysdba", then I get an error message about incorrect password/logon denied.

Could you please tell me how to fix it? I read hundred of pages about the connection strings, tried multiple combinations, but no avail so far.

Also I'm not sure what value should be in the connection string in "data source". I have "ora1\orcl", which is the hostname\db_name. Should I use something different?

Thank you very much in advance.

Martin
0
Comment
Question by:martin_babarik
6 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 34991745
sqlplus sys/abc@orcl as sysdba
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 34991750
if you are logged in as the oracle owner (usually a user called "oracle")


sqlplus / as sysdba
0
 
LVL 13

Author Comment

by:martin_babarik
ID: 34991968
Hi and thank you for the comment.

What you say works nice with sqlplus - I connect successfully.

But the question was more about what should be passed in the connection string. I realize I'm asking about scripting in the Oracle zone, but I believe the connection string is the same for any programming/scripting language.

I'm logged on as local administrator, it's a fresh install of Windows Server 2003 and Oracle, all default, only the password is set to 'abc'.

This problem is driving me crazy as I need to develop quite complex script and I'm stucked just first few lines where I have the connection attempt.

As long as I'm able to work with the database using sqlplus, I believe the database, services and listener are ok. Maybe it's something wrong with my script, but it's just a sample code googled on the Internet, nothing special, see below.

The table 'employees' probably doesn't exist, but didn't get so far to get an error message about this :-) It's just about the connection.

Thanks a lot.

Martin
cls
$connectionString= "Data Source=ora1/orcl;User Id='sys/abc@orcl as sysdba';Password=abc;"
[System.Reflection.Assembly]::LoadWithPartialName("System.Data.OracleClient")
$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)
$queryString = "SELECT COUNT(LAST_NAME) FROM EMPLOYEES"
$command = new-Object System.Data.OracleClient.OracleCommand($queryString, $connection)
$connection.Open()
$employeesNames = $command.ExecuteScalar()
echo "Number of employees: "$employeesNames
$connection.Close()

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 5

Accepted Solution

by:
jaiminpsoni earned 250 total points
ID: 34992358
what is the script?

Is it perl script? Or jython script or python script? (I am not much into it, so cant figure it from sample code)

For JDBC, we have following option...

 * user                String   The user name for logging into the
 *                                         database.
 * password            String   The password for logging into the
 *                                         database.
 * database            String   The connect string for the database.
 * internal_logon     String   A role, such as SYSDBA or SYSOPER,
 *                                         that allows you to log on as SYS.


But this is with JDBC, it will be different for string, but the property should remain same for all the DBC.

so try exploring more on internal_logon syntex for your scripting language...
0
 
LVL 12

Assisted Solution

by:praveencpk
praveencpk earned 250 total points
ID: 34992369
why you want to login as sys any particular reason?

if not why cann't you try as system user... if you know system password.

if you don't know the system password login to database with sys through sqlplus and change the password of system.

alter user system identified by password account unlock;

then you can use system user in your script.
0
 
LVL 13

Author Comment

by:martin_babarik
ID: 34992557
Jaiminpsoni: The script is powershell, but I could do it using any other language I know...ehm, actually VBScript, that's all :-)
Maybe there could be the concept I don't understand - the difference between user and internal_logon.

Praveencpk: I don't need to login as sys - as I mentioned I don't know Oracle and I thought, that this is the simplest way, when I'm working in the virtual environment just for development purposes - that I will not be limited by some restrictions or insufficient privilegues.

By system user you mean a user called "system"? I altered the system user by using your command, went ok, I can login from sqlplus using this account.

Actually as I can see now, the script works! I modified the connection string to be like below and used OleDB from another googled example:
$connString = "password=abc;User ID=SYSTEM;Data Source=orcl;Provider=OraOLEDB.Oracle"

Afaik I haven't tried to login with SYSTEM user account before, maybe this was the core of the problem.

Anyway thank you very much for your help guys, I'm gonna close this question and split the points....and ask some new questions :-)

Martin
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now