Ruby Sequel AS/400 connection issues - ruby-on-rails

I need to connect to a AS/400 system using ODBC connection, and I'm using the Ruby sequel gem. My environment is just Ubuntu 14.04 running in virtual box. I'm able to run isql -v my.host.com and drop in to a sql console. When I try and do this from IRB, I get the error message.
require 'sequel'
db = Sequel.odbc(:drvconnect=>'driver={IBM i Access ODBC Driver};system=my.host.com;database=MYDBNAME;uid=MYUSERNAME;password=MYPASSWORD;DefaultLibraries=, *usrlibl;authentication=server')
tables = db[:QSYS2__SYSTABLES].where("TABLE_NAME like ?", "SYS%")
tables.each do |record|
puts record[:table_name]
end
#=> Sequel::DatabaseConnectionError: ODBC::Error: IM002 (0) [unixODBC][Driver Manager]Data source name not found, and no default driver specified
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:21:in `drvconnect'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:21:in `connect'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool.rb:103:in `make_new'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:224:in `make_new'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:197:in `available'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:133:in `_acquire'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:147:in `block in acquire'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:265:in `block in sync'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:265:in `synchronize'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:265:in `sync'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:146:in `acquire'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/connection_pool/threaded.rb:104:in `hold'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/database/connecting.rb:251:in `synchronize'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:44:in `execute'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/dataset/actions.rb:952:in `execute'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/adapters/odbc.rb:97:in `fetch_rows'
from /home/vagrant/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/sequel-4.32.0/lib/sequel/dataset/actions.rb:141:in `each'
I'm not familiar at all with AS/400 or using ODBC connections, but if isql works then maybe it's a user permission error?
Here is my /etc/odbc.ini
[my.host.com]
Description = IBM i Access ODBC Driver
Driver = IBM i Access ODBC Driver
System = my.host.com
UserID = MYUSERNAME
Password = MYPASSWORD
Naming = 1
DefaultLibraries = *usrlibl;
Database = MYDBNAME
I should also mention that the AS/400 system is located remotely at the my.host.com, so I'm not trying to connect to some local running service. I am able to ping the IP address of that host though.
Edit: I've changed my driver in the drvconnect to match what's in the odbc.ini as per the suggestion below. Also I should note that I have a /etc/odbcinst.ini file with the following
[IBM i Access ODBC Driver]
Description = IBM i Access for Linux ODBC Driver
Driver = /opt/ibm/iaccess/lib/libcwbodbc.so
Setup = /opt/ibm/iaccess/lib/libcwbodbcs.so
Driver64 = /opt/ibm/iaccess/lib64/libcwbodbc.so
Setup64 = /opt/ibm/iaccess/lib64/libcwbodbcs.so
Threading = 0
DontDLClose = 1
UsageCount = 1
[IBM i Access ODBC Driver 64-bit]
Description = IBM i Access for Linux 64-bit ODBC Driver
Driver = /opt/ibm/iaccess/lib64/libcwbodbc.so
Setup = /opt/ibm/iaccess/lib64/libcwbodbcs.so
Threading = 0
DontDLClose = 1
UsageCount = 1

You have this:
require 'sequel'
db = Sequel.odbc(:drvconnect=>'driver={iSeries Access ODBC Driver}; system=my.host.com;...
But then you show this:
[my.host.com]
Description = IBM i Access ODBC Driver
Driver = IBM i Access ODBC Driver
If you actually have the IBM i Access ODBC Driver, Then that's what you should reference in your code, rather than iSeries Access ODBC Driver.

Figured it out! It turns out that when you have your /etc/odbc.ini defined with all your connection info, you don't need a connection string.
require 'sequel'
db = Sequel.odbc(drvconnect: '')
tables = db[:QSYS2__SYSTABLES].where("TABLE_NAME like ?", "SYS%")
tables.each { |r| puts r[:table_name] }
This query takes a stupid long time to run on my machine, but it does return all tables that start with "SYS". The only change I did need to make to my /etc/odbc.ini for this to work was to rename the DSN to "default"
[default]
Description = IBM i Access ODBC Driver
Driver = IBM i Access ODBC Driver
System = my.host.com
UserID = MYUSERNAME
Password = MYPASSWORD
Naming = 1
DefaultLibraries = *usrlibl;
Database = MYDBNAME
Just for anyone else that may run into this issue, here are a few other things I did along the way to get things working. My setup is Ubuntu 14.04
Install unixodbc and unixodbc-dev
My my.host.com needed to be added to my /etc/hosts since it didn't resolve on public DNS.
The driver filename I installed is called ibm-iaccess-1.1.0.4-1.0.amd64.deb
running ldd /opt/ibm/iaccess/lib64/libcwbodbc.so showed that there was a missing symlink.`
Added missing symlink for /usr/lib/x86_64-linux-gnu/libodbcinst.so.1 to /usr/lib/x86_64-linux-gnu/libodbcinst.so.2
That's it. Hope that helps other people too.

Related

Try to connect to Informix database Cisco UCCX with unix ODBC - no success

Try to use Informix driver to connect over ODBC to Cisco UCCX database. Install IBM informix SDK
Files of SDK /opt/IBM/Informix_Client-SDK
odbcinst -j
unixODBC 2.3.9
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/alexniko/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8
odbc.ini
[ODBC Data Sources]
uccx
uccx2
[uccx]
DRIVER={IBM INFORMIX ODBC DRIVER (64-bit)}
UID=uccxhruser
PWD=TrebuSHet{}123
DATABASE=db_cra
HOST=uccx-01.fccps.local
SERVER=uccx_01_uccx
SERVICE=1504
PROTOCOL=onsoctcp
CLIENT_LOCALE=en_US.UTF8
DB_LOCALE=en_US.UTF8
[uccx2]
DRIVER={IBM INFORMIX ODBC DRIVER 2 (64-bit)}
UID=uccxhruser
PWD=TrebuShet{}123
DATABASE=db_cra
HOST=uccx-02.fccps.local
SERVER=uccx_02_uccx
SERVICE=1504
PROTOCOL=onsoctcp
CLIENT_LOCALE=en_US.UTF8
DB_LOCALE=en_US.UTF8
odbcinst.ini
[IBM INFORMIX ODBC DRIVER (64-bit)]
Driver=/opt/IBM/Informix_Client-SDK/lib/cli/iclis09b.so
Description=IBM INFORMIX ODBC DRIVER
UID=uccxhruser
PWD=TrebuShet{}123
DATABASE=db_cra
HOST=uccx-01.fccps.local
SERVER=uccx_01_uccx
SERVICE=1504
PROTOCOL=onsoctcp
CLIENT_LOCALE=en_US.UTF8
DB_LOCALE=en_US.UTF8
[IBM INFORMIX ODBC DRIVER 2 (64-bit)]
Driver=/opt/IBM/Informix_Client-SDK/lib/cli/iclis09b.so
Description=IBM INFORMIX ODBC DRIVER
UID=uccxhruser
PWD=TrebuShet{}123
DATABASE=db_cra
HOST=uccx-02.fccps.local
SERVER=uccx_02_uccx
SERVICE=1504
PROTOCOL=onsoctcp
CLIENT_LOCALE=en_US.UTF8
DB_LOCALE=en_US.UTF8
ldd -v /opt/IBM/Informix_Client-SDK/lib/cli/iclis09b.so
Have no error
Connecttion string in python:
SERVICE=1504;PROTOCOL=onsoctcp;
CLIENT_LOCALE=en_US.UTF8;DB_LOCALE=en_US.UTF8;
DRIVER=IBM INFORMIX ODBC DRIVER (64-bit);
UID=uccxhruser;PWD=TrebuShet{}123;
DATABASE=db_cra;
HOST=uccx-01.fccps.local;
SERVER=uccx_01_uccx;
when I try to connect receive error:
Can't open lib '/opt/IBM/Informix_Client-SDK/lib/cli/iclis09b.so' : file not found (0) (SQLDriverConnect)")
Trying to check driver work with isql -v uccx - receive error
[IM002][unixODBC][Driver Manager]Data source name not found and no default driver specified
[ISQL]ERROR: Could not SQLConnect
what am I doing wrong?

Configuring Backup gem in Rails 5.2 - Performing backup of PostgreSQL database

I would like to perform a regular backup of a PostgreSQL database, my current intention is to use the Backup and Whenever gems. I am relatively new to Rails and Postgres, so there is every chance I am making a very simple mistake...
I am currently trying to setup the process on my development machine (MAC), but keep getting an error when trying to connect to the database.
In the terminal window, I have performed the following to check the details of my database and connection:
psql -d my_db_name
my_db_name=# \conninfo
You are connected to database "my_db_name" as user "my_MAC_username" via socket in "/tmp" at port "5432".
\q
I have also manually created a backup of the database:
pg_dump -U my_MAC_username -p 5432 my_db_name > name_of_backup_file
However, when I try to repeat this within db_backup.rb (created by the Backup gem) I get the following error:
[2018/10/03 19:59:00][error] Model::Error: Backup for Description for db_backup (db_backup) Failed!
--- Wrapped Exception ---
Database::PostgreSQL::Error: Dump Failed!
Pipeline STDERR Messages:
(Note: may be interleaved if multiple commands returned error messages)
pg_dump: [archiver (db)] connection to database "my_db_name" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/pg.sock/.s.PGSQL.5432"?
The following system errors were returned:
Errno::EPERM: Operation not permitted - 'pg_dump' returned exit code: 1
The contents of my db_backup.rb:
Model.new(:db_backup, 'Description for db_backup') do
##
# PostgreSQL [Database]
#
database PostgreSQL do |db|
# To dump all databases, set `db.name = :all` (or leave blank)
db.name = "my_db_name"
db.username = "my_MAC_username"
#db.password = ""
db.host = "localhost"
db.port = 5432
db.socket = "/tmp/pg.sock"
# When dumping all databases, `skip_tables` and `only_tables` are ignored.
# db.skip_tables = ["skip", "these", "tables"]
# db.only_tables = ["only", "these", "tables"]
# db.additional_options = ["-xc", "-E=utf8"]
end
end
Please could you suggest what I need to do to resolve this issue and perform the same backup through the db_backup.rb code
In case someone else gets stuck in a similar situation, the key to unlocking this problem was the lines:
psql -d my_db_name
my_db_name=# \conninfo
I realised that I needed to change db.socket = "/tmp/pg.sock" to db.socket = "/tmp", which seems to have resolved the issue.
However, I don't understand why the path on my computer differs to the default as I didn't do anything to customise the installation of any gems or the Postgres App

Using UnixODBC and FreeTDS to connect to Pervasive SQL server in ubuntu?

I am trying to connect to a Pervasive Sql Server which is running on Windows 10 from an Ubuntu 14.04.4 server.
I am using the following services to try connect to the server:
FreeTDS
unixODBC
Before starting I tried to ping the host machine from the vm console with success.
I then run the following command to check FreeTDS has installed correctly;
tsql -C
Which returned:
Compile-time settings (established with the "configure" script)
Version: freetds v0.95.95
freetds.conf directory: /usr/local/etc
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 5.0
iODBC: no
unixodbc: yes
SSPI "trusted" logins: no
Kerberos: no
OpenSSL: no
GnuTLS: no
[freetds.conf] located in [/usr/local/etc] contains:
[PSQLServer]
host = **IP**
port = **PORT**
tds version = 8.0
[odbc.ini] located in [/usr/local/etc] contains:
[PSQLClient]
Description = Pervasive SQL Client Settings
Driver = FreeTDS
ServerName = PSQLServer
Database = **DBNAME**
Trace = No
UID = **USERNAME**
PWD = **PASSWORD**
TDS_Version = 8.0
[odbcinst.ini] located in [/usr/local/etc] contains:
[FreeTDS]
Description = FreeTDS unixODBC Driver
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
TDS_Version = 8.0
I tired using the tsql command adding [TDSVER=*] for each of the possible driver versions resulting in the same error message for each TDSVER.
*[5.0], *[6.0], *[7.0], *[7.1], *[7.2], *[7.3], *[7.4], *[8.0]
When testing the connection using the following command:
TDSVER=8.0 tsql -S PSQLClient -U **USERNAME** -P **PASSWORD**
Which returns the following errors:
Error 20012 (severity 2):
Server name not found in configuration files.
locale is "en_ZA.UTF-8"
locale charset is "UTF-8"
using default charset "UTF-8"
Error 20013 (severity 2):
Unknown host machine name.
There was a problem connecting to the server
After trying the above, I then tried to the isql command:
isql -v PSQLClient **USERNAME** **PASSWORD**
Which returns the following error messages:
[S1000][unixODBC][FreeTDS][SQL Server]Unable to connect to data source
[01000][unixODBC][FreeTDS][SQL Server]Unknown host machine name.
[ISQL]ERROR: Could not SQLConnect
Is it even possible to connect to [Pervasive SQL] via [node-odbc] and if so what am I doing wrong?
Any assistance would be greatly appreciated!
Most recent versions of Pervasive / Actian PSQL support Linux clients and have an ODBC driver for Linux. Since you're using Windows 10, you should probably be using PSQL v12. If you use v12, you can download the Linux client at http://www.pervasive.com/database/Home/Products/PSQLv12.aspx. There is an RPM and TAR available.

RMySQL not working with a cnf file

I am trying to connect to a MySQL server through R and it works perfect with the follwoing line:
con <- dbConnect(MySQL(), user="user", password="password",dbname="dbname", host="localhost", port=3306)
But, I would like to use a cnf file so that my user/apssword credentials donot appear in my code and tried the following:
rmysql.settingsfile<-"mydefault.cnf"
rmysql.db<-"test_db"
drv<-dbDriver("MySQL")
con<-dbConnect(drv,default.file=rmysql.settingsfile,group=rmysql.db)
And this is how my cnf file looks:
[test_db]
user=user
password=password
database=dbname
host=localhost
port=3306
It is in the same folder as in my R script which is my current working directory. But, I run into the following error:
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Access denied for user 'ODBC'#'localhost' (using password: NO)
)
Any suggestions, please?
Thanks so much
I had this problem very recently. RMySQL looks in the root directory for these files so you need to fully qualify the location of the file. i.e.:
rmysql.settingsfile<-"/home/MD-Tech/mydefault.cnf"
or
rmysql.settingsfile<-"c:\Users\MD-Tech\rfiles\mydefault.cnf"
Two things could be going on.
The CNF file should be encrypted, password should say password = ****. The MySQL documentation shows how to create a CNF file. Below would work for your code to create the CNF
shell> mysql_config_editor set --login-path=test_db --host=localhost --user=user --password
press enter without typing password, you will be prompted to enter it
The second thing is that user = NULL and password = NULL are missing as referenced in the src_mysql documentation
rmysql.settingsfile <- "~/.mylogin.cnf"
rmysql.db <- "test_db"
drv <- dbDriver("MySQL")
con <- dbConnect(drv, default.file = rmysql.settingsfile, group = rmysql.db, user = NULL, password = NULL)
When you add these and run the code, you should be set.
Fing something working at: https://www.r-bloggers.com/mysql-and-r/
Not in configuration file... but work.
con <- dbConnect(MySQL(),
user="me", password="nuts2u",
dbname="my_db", host="localhost")
Ya, getting this setup for the first time can be like pulling cats' teeth! Here is what I did while running R on a Droplet (Ubuntu 16.04, MySQL 5.7.16).
First, make sure you can at least login successfully to MySQL through the terminal
mysql -u kevin -p
Next, run R and verify that you can login in directly with dbConnect() using a user name and password
mydb = dbConnect(drv, user='kevin', password='ilovecats', dbname='catnapdb', host='127.0.0.1', port=3306)
Edit your mysql.cnf text file and at the bottom add a new group (exact name of this file and its location will depend on operating system and versions).
[whiskerpatrol]
user = kevin
password = ilovecats
host = 127.0.0.1
port = 3306
database = catnapdb

Lua programming ,error in establishing Database connection

After writing the code to connect to database,lua returns an error as
Error in establishing connection to MySQL ,Can't connect to /var/lib/mysql/mysql.sock
I'm unable to locate the /var/lib/mysql/mysql.sock file (which I haven't created )
My Sock is /tmp/mysql.sock
Below is my code to connect to MySQL database.
mysql = require "luasql.mysql"
local env = mysql.mysql()
local conn = env:connect('test','root','')
print(env,conn)
status,errorString = conn:execute([[CREATE TABLE sample2 (id INTEGER, name TEXT);]])
print(status,errorString )
as seen there: Luasql error: "LuaSQL: error connecting to database", the problem is because luasql defaults to using a default socket path when connecting to localhost without a port. so just set the mysql host and port in env:connect or specify the socket path.
EDIT:
I believe your code should be:
env:connect('test','root','','localhost',3306) -- for tcp
-- or
env:connect('test','root','',':/tmp/mysql.sock') -- for socket

Resources