spraff
spraff

Reputation: 33385

Connect to Informix without an entry in /etc/services (matching sqlhosts)

I have Informix databases running in Docker containers. They have unpredictable published port numbers.

I am trying to connect from outside the container. Informix seems to want an entry in the sqlhosts file. I can create an ad hoc /tmp/sqlhosts.xxx containing

 mydbservername onsoctcp <container ip address> <servicename>

and use it with

INFORMIXSQLHOSTS=/tmp/sqlhosts.xxx /my.program

and my.program connects to dbname@mydbservername, I have verified this temporary sqlhosts file is being read and the mydbservername is being discovered.

The error is

Cannot locate <servicename> service in /etc/services.

So far, so hacky, but it seems to work apart from the <servicename> part. In static environments, this would match an entry in /etc/services but I don't have write privileges for that file (and in any case, the dynamic port numbers would make it messy).

Ideally I would be able to connect to the database without going through a sqlhosts file at all -- the IP address and port number are known when I executemy.program -- but there is a layer I don't control between my.program and the database connection itself: I can only control the dbname@server string, the userspace filesystem, and the environment variables.

How can I connect to an Informix database which manifests with an unpredictable IP/port?

Upvotes: 1

Views: 574

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

Use a port number instead of a service name in the INFORMIXSQLHOSTS file. I do that most of the time. For example, one sqlhosts file I currently use on a machine lxdev04 contains:

# lxdev04_23 on lxdev04 added 2019-04-24 14:59:32
lxdev04_23      olsoctcp  lxdev04  10040
lxdev04_23_tcp  olsoctcp  lxdev04  10041
lxdev04_23_shm  olipcshm  lxdev04  lxdev04_23
lxdev04_23_str  olipcstr  lxdev04  lxdev04_23
lxdev04_23_ssl  olsocssl  lxdev04  10042
lxdev04_23_enc  olsoctcp  lxdev04  10043         csm=(lxdev04_23_enc)
lxdev04_23_pwd  olsoctcp  lxdev04  10044         csm=(lxdev04_23_pwd)

The shared memory and stream connections use a name (but those aren't in /etc/services anyway), but the rest use just a port number. I name my Informix servers with the machine name (lxdev04) and a 2-digit prime number, which is also the server number, plus suffixes for different connection types.

The advantage of a service name (which is simply a way to map between a name string and a port number) is that it can be used across machines if you use an appropriate service (NIS+, etc), and they can be changed if necessary. It is harder to mess with numbers.

Upvotes: 1

Related Questions