Skip to content

Commit 5369bc1

Browse files
authored
Merge pull request oracle-samples#83 from OsBlaineOra/ruby-atp-example
Example showing how to connect to an Autonomous instance from Ruby
2 parents a534126 + 9033ab2 commit 5369bc1

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

ruby/autonomous-connection/README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
This example demonstrates how to connect to an Oracle Cloud Autonomous Transaction Processing Database (ATP). The same steps can be used to connect to an Autonomous Data Warehouse.
2+
## Prerequisites
3+
- Have an Oracle ATP instance provisioned and running. (If you don’t have a current instance, you can [sign up for a free trial](https://www.oracle.com/cloud/free/).)
4+
The examples will use an instance named DemoATP.
5+
- Have a database schema and password created that you can use for testing.
6+
- Have access to the Oracle ATP service panel or have someone with access available to help.
7+
- Download and install Oracle Database Instant Client.
8+
- (Optional but a good idea) Have Oracle SQLcl installed to verify the connection in a neutral environment.
9+
10+
## Download Client Credentials (Oracle Wallet)
11+
With the prerequisites complete, download the client credentials for your Oracle ATP Database instance.
12+
1. Go to the Service Console for your ATP instance.
13+
1. Click Administration.
14+
1. Click the Download Client Credentials (Wallet) button.
15+
1. Enter a password, and click Download.
16+
Remember this password. If you lose it, you will need to download a new credentials file.
17+
1. Save the file in a secure location. Remember, this file can be used to access your database, so keep it secure.
18+
1. Create a directory, and extract the client credentials zip file into that directory. You should now have the following files:
19+
- cwallet.sso
20+
- ewallet.p12
21+
- keystore.jks
22+
- ojdbc.properties
23+
- sqlnet.ora
24+
- tnsnames.ora
25+
- truststore.jks
26+
1. Edit the sqlnet.ora file. Set the DIRECTORY value to the directory used in step 6, for example:
27+
(DIRECTORY="/home/demouser/projects/ATP/Wallet_Creds")
28+
29+
The tnsnames.ora file includes auto-generated TNS Name values. You can refer to the documentation for an explanation of when to use each of these.
30+
The examples will use the DemoATP_TP TNS Name value.
31+
32+
## Test the Connection: Optional but Recommended
33+
Now test the connection from your Oracle SQLcl or Oracle SQL Developer tool.
34+
35+
### Oracle SQLcl.
36+
To test the connection from Oracle SQLcl, do the following:
37+
38+
1. Start Oracle SQLcl in nolog mode.
39+
40+
```
41+
sql /nolog
42+
```
43+
1. Set the location of your credentials zip file.
44+
```
45+
set cloudconfig /home/demouser/projects/ATP/Wallet_Creds/client_credentials.zip
46+
Operation is successfully completed.
47+
Using temp directory:/tmp/oracle_cloud_config903805845690230771
48+
```
49+
1. Connect with a schema/password that is safe for testing.
50+
```
51+
connect myschema/mypassword@DemoATP_TP
52+
Connected.
53+
```
54+
1. If all goes well, you should now be connected and able to run a test query.
55+
```
56+
select 'Connected to Oracle Autonomous Transaction Processing from SQLcl!' "Test It" from dual;
57+
Test It
58+
-----------------------------------------------------------------
59+
Connected to Oracle Autonomous Transaction Processing from SQLcl!
60+
```
61+
1. Exit Oracle SQLcl.
62+
```
63+
exit
64+
65+
Disconnected from Oracle Database 18c Enterprise Edition
66+
Release 18.0.0.0.0 – Production
67+
Version 18.4.0.0.0
68+
```
69+
## Connect From Ruby
70+
71+
1. Download and install the ruby-oci8 driver.
72+
1. Set the following environment variables.
73+
(Linux example shown, you may need to adjust for your OS)
74+
```
75+
export TNS_ADMIN="/home/demouser/projects/ATP/Wallet_Creds"
76+
export OADB_USER='demo'
77+
export OADB_PW='demoPassword'
78+
export OADB_SERVICE='DemoATP_TP'
79+
```
80+
1. Review the ruby-demo.rb file.
81+
1. Run the ruby-demo.rb file in Ruby with the following command:
82+
```
83+
ruby ruby-demo.rb
84+
```
85+
The following response confirms your connection from Ruby to Oracle Autonomous Transaction Processing:
86+
```
87+
Connected to Oracle Autonomous Transaction Processing from Ruby!
88+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'oci8'
2+
3+
con = OCI8.new(ENV['OADB_USER'],
4+
ENV['OADB_PW'],
5+
ENV['OADB_SERVICE']);
6+
7+
statement = "select 'Connected to Oracle Autonomous Transaction Processing from Ruby!'
8+
from dual";
9+
cursor = con.parse(statement)
10+
cursor.exec
11+
cursor.fetch() {|row|
12+
printf "%s\n", row[0]
13+
}

0 commit comments

Comments
 (0)