Skip to content

connecting RMySQL to and external server

14 messages · Brian Ripley, Gabor Grothendieck, Patrick Connolly +3 more

#
Dear list,

I learned how to connect R to a local MySQL server, using:
drv <- dbDriver("MySQL")
con <- dbConnect(drv, user="root", password="mypass", dbname="mydb")

Is it possible to connect R in this way to an external server (on a different 
machine, with a different IP)?

I read the documentation on ?dbConnect (and everything I could find on the 
internet), but I failed to find some other relevant arguments. For example, 
one needs to first connect to the external machine and only after that to the 
MySQL server on that machine. Is this possible from within R?

Thank you in advance,
Adrian
#
It is trivial with RODBC (I know that is not what you asked, but it is the 
solution we found first).


For RMySQL, note first that the MySQL configuration file is consulted, so 
the default host is specified in the client section, which is like

[client]

port=3306

[mysql]

default-character-set=latin1

Add host=foo under [client] to change the default host.

And ?dbConnect mentions a 'host' argument under '...'.  That seems to work 
for me (provided of course the server allows connections from other 
machines): on my home network from Windows laptop to Linux server
Loading required package: DBI
This mixture of using the *local* configuration file overridden by 
arguments is a bit dangerous: RMySQL seems not really designed for 
client-server operation and there are some things that definitely do not 
work.  (As I recall, that included dbWrite as that imports a file which is 
on the local machine.)
On Fri, 14 Dec 2007, Adrian Dusa wrote:

            

  
    
#
Indeed, I noticed the "host" argument but the server demands an username and a 
password for the machine first, and only after that for the MySQL server. 
Those were the arguments I was looking for.
I will study the RODBC package then, if it solves the problem.

Thank you very much,
Adrian
On Saturday 15 December 2007, Prof Brian Ripley wrote:

  
    
#
On Sat, 15 Dec 2007, Adrian Dusa wrote:

            
But you said 'connect to', not 'log in to', so how were we to know that?
I am afraid I don't understand your setup. MySQL works by listening on 
port 3306: user accounts don't come into that.  With our bastion servers 
all such ports are blocked and can only be accessed via tunnels 
(implemented by stunnel).  I think you need to discuss this with your 
sysadmins: if it works under mysql or for ODBC (isql) it will work with 
the corresponding R packages.

  
    
#
Use ssh forwarding to forward local port 3307 to remote port 3306
specifying the remote account and password.  Then if you use local port
3306 you can access your local version of MySQL and if you
use port 3307 you can access the remote version.   There is some
info on the MySQL site.  First test it out by running the mysql command
line program accessing the remote data base via port 3307 and once
that works you know its ok and you can try RMySQL or RODBC packages.
On Dec 15, 2007 11:33 AM, Adrian Dusa <dusa.adrian at gmail.com> wrote:
#
On Saturday 15 December 2007, Prof Brian Ripley wrote:
Of course, most definitely. I usually log in to the external machine using 
ssh, then access the MySQL server using the MySQL username and password.
I probably asked for too much from R, since logging to an external machine 
needs a secure connection.
I'll talk to our sysadmin for local advice, thanks again.

Adrian
#
On Saturday 15 December 2007, you wrote:
Thanks Gabor, it is a little bit of a foreign language for me (at the moment) 
but I'm sure your hints will be relevant to our sysadmin.
I do want to understand this stuff myself, just need more digging in the 
manuals.

Cheers,
Adrian
#
On Fri, 14-Dec-2007 at 08:55PM +0200, Adrian Dusa wrote:
|> 
|> Dear list,
|> 
|> I learned how to connect R to a local MySQL server, using:
|> drv <- dbDriver("MySQL")
|> con <- dbConnect(drv, user="root", password="mypass", dbname="mydb")
|> 
|> Is it possible to connect R in this way to an external server (on a different 
|> machine, with a different IP)?

Do you use a ~/.my.cnf file?

There are many settings that can be made there.  Talk to your sysadmin
about those.

HTH
3 days later
#
On Saturday 15 December 2007, Gabor Grothendieck wrote:
For those interested in using R with a remote MySQL database, here's the most 
simple and straightforward solution (thanks to Gabor):

On the console, forward local port 3307 to remote port 3306
ssh -L 3307:xxx.xxx.xxx.xxx:3306 myuser at xxx.xxx.xxx.xxx

(where xxx.xxx.xxx.xxx is the IP of the remote server and myuser is the login 
name). After entering the password, keep that console open.

In R:

library(RMySQL)
drv <- dbDriver("MySQL")
dbConnect(drv, user="mysqluser", password="mysqlpassword", 
dbname="anydatabase", host="127.0.0.1", port="3307")

(where mysqluser and mysqlpassword are the remote machine's MySQL username and 
password).

Another possible solution is suggested by Prof. Ripley, using stunnel to 
create a secure tunnel between the local and the remote machines, but I 
haven't explored that in detail.


Best wishes,
Adrian
#
On Wed, Dec 19, 2007 at 04:34:26PM +0200, Adrian Dusa wrote:
Err, I am late to this but

 dbConnect(drv, user="mysqluser", password="mysqlpassword", dbname="anydatabase", host="xxx.xxx.xxx")

works fine for me (eg on Ubuntu with a remote MySQL on some other
box).  What's the problem you were seeing?

Dirk
#
On Wednesday 19 December 2007, Dirk Eddelbuettel wrote:
The remote host expects a login username and password (different from the 
MySQL username and password), which I cannot provide in the dbConnect 
function.
The solution is to first login to the external machine (a secure login using 
ssh), forwarding the MySQL port 3306 to the local port 3307 (not to interfere 
with the local MySQL server), and connecting R to the remote MySQL server 
using the ssh tunneling binded to local port 3307.

Best,
Adi
#
Hi,

I'm sorry I'm also coming late to this discussion, but  like Dirk, I
fail to understand what's wrong with using dbConnect() the way
the documentation, (see ?MySQL) suggests.

RMySQL was developed in a fully distributed client/server
environment, and it uses the MySQL-provided client API.  The
options that the MySQL client library allows (user, password, host,
port or socket file, compression, etc.) should work from RMySQL
(RMySQL just passes those directly to the MySQL client library).
If you have a specific (e.g., security-related) need that the MySQL client
API does not address, then the use of ssh may be the proper way
to go.  But for simple, vanilla style of client-server communications
the underlying MySQL client should be sufficient.

Regards,

--
David
On Dec 19, 2007 11:04 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
#
Hi James,
On Wednesday 19 December 2007, David James wrote:
Normally yes, you are right, but this particular MySQL server does not allow 
remote connections (it does not directly listens to port 3306) but only local 
connections using a socket (this terminology is unfamiliar to me, I may talk 
stupid).
So I have to first create a secure login and only after that connect to the 
remote MySQL server.

I hope my situation is more clear now,
Adrian
#
On Wed, Dec 19, 2007 at 07:51:47PM +0200, Adrian Dusa wrote:
Yes, that case is sometimes used on internet-facing or otherwise
widely visible machines where the number of open ports is to be
minimised.

In fact, this may be worth adding as a hint in the DBI or RMySQL
docs... 

Dirk