How to query the INSPIRE database at CERN from you local host

General description

There's extensive email on -dev on how to set this up conveniently.

The basic idea is to use local port forwarding capability of ssh via -L option (man ssh) .
What is needed:

  1. a shell account on a worker node
  2. the address of the mysql host and the port it is listening on (3306)
  3. a mysql account (username and password) which is allowed to connect from the chosen worker node
  4. a (external) host which will connect to mysql
Then use the worker node as a relay to forward local port <x> to remote port 3306 on the mysql host
ssh -L<x> <cern-user>
now connect to mysql via
mysql -u <mysql-username> -h --port=<x> -p
mysql-username is: readonly port <x> can be choosen to be 3306 or anything else

a local invenio instance can be configured to talk to the remote mysql via the tunnel simply
by setting CFG_DATABASE_.. variables, i.e. these
invenio-local.conf-example:#CFG_DATABASE_HOST = localhost invenio-local.conf-example:#CFG_DATABASE_PORT = 3306 invenio-local.conf-example:#CFG_DATABASE_NAME = cdsinvenio invenio-local.conf-example:#CFG_DATABASE_USER = cdsinvenio invenio-local.conf-example:#CFG_DATABASE_PASS = ...............
in the site's invenio-local.conf file.
The above ssh line assumes a ~/.ssh/config convenience to directly ssh
to a worker node via lxplus/netcat (ProxyCommand directive for ssh)
I (TS) use
Host i4 inspire04 HostName
ProxyCommand ssh <cern-user>
exec /usr/bin/nc %h %p Compression yes ControlMaster auto ControlPath ~/.ssh/%r@%h:%p # ControlPersist 120 GSSAPIAuthentication yes GSSAPIDelegateCredentials yes PubkeyAuthentication no ServerAliveInterval 120

Also note that the above works from any external host anywhere.
It doesn't have dependencies on invenio. It simply establishes a channel for connections to mysql db.
With a properly configured remote invenio/inspire installation commands like
$ dbexec -i or python scripts using run_sql() from invenio will run transparently,
and so will ipython sessions.

Worker nodes for which "readonly" mysql account is allowed to connect to mysql
mysql> select Host,Db,User,Select_priv from db where User="readonly";

master db server:
test db server:

See also:

How to set it up

Steps for testing:

  1. log into a worker node and test db connection> mysql -u readonly -h -p Enter password:
    should result in mysql prompt
  2. ensure that the ProxyCommand is working.
    remotehost> ssh -l<cern-user>
    should give login session on
  3. put things together
    remotehost> ssh -L <cern-user>
    remotehost> mysql -u readonly -h --port=3306 -p
  4. check the tunnel via
    remotehost> lsof -i4:3306
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME ssh 17522 thorsten 11u IPv4 2454309 0t0 TCP localhost.localdomain:mysql (LISTEN)
    voila, ssh is listening on standard local mysql port
  5. improve the ssh command to not lead to a login shell and some other refinements: add options
    e.g. remotehost> ssh -C2qfTnN <cern-user>
    this will establish the tunnel and also give you back the command prompt on remotehost.

Keep the tunnel up with autossh

Maintaining the tunnel via autossh from remotehost to <cern-user>

generate public/private key pair: ssh-keygen -t rsa
e.g. with name id_dsa
Make sure the private key id_dsa is not in a public directory.

on lxplus:
Copy the public key to ~/.ssh/ and make it public (if necessary make the directory public).
Append the public key to ~/.ssh/authorized_keys and make it public.
On lxplus if you need private info in .ssh create a private/.ssh, move the corresponding files there
and link to the private file from the public directory.

on remotehost:
Copy the private key to ~/.ssh/id_dsa and make it private (if necessary make the directory private).
If you need public info in .ssh do the same trick as above.
Alternatively use a local, private directory like /home/.../ssh

Create ~/.ssh/config (to be on the save side replace ~ by full path.)

 Host prod4
 ServerAliveInterval 60
 ProxyCommand ssh <cern-user>  -i ~/.ssh/id_dsa -F ~/.ssh/config -o ForwardX11=no exec /usr/bin/nc %h %p
 User <cern-user>
 # GSSAPIAuthentication yes
 # GSSAPIDelegateCredentials yes

Create /etc/init.d/autossh:
 autossh -M 0 -fTnN -i ~/.ssh/id_dsa -F ~/.ssh/config -o ForwardX11=no -L  prod4

You can now start the tunnel manually via
remotehost> sudo ssh -fTnN -i ~/.ssh/id_dsa -F ~/.ssh/config -o ForwardX11=no -L prod4

See also

-- KirstenSachs - 10 Apr 2014

Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r3 - 2014-06-26 - KirstenSachs
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Inspire All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2023 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback