RMySQL via ssh tunnel - r

I'm using RStudio on OSX and have a local vm running Ubuntu. I'm having issues with RMySQL connecting to mysql running on the local vm via ssh. I've tried forwarding port 3307 via
ssh -L 3307:d.local.internal.com:3306 ubuntu#d.local.internal.com
followed by the following in r
con <- dbConnect(RMySQL::MySQL(), host = "127.0.0.1", user = "root", password = "pass", port=3307)
I'm still getting
Error in .local(drv, ...) : Failed to connect to database: Error: Can't connect to MySQL server on '127.0.0.1' (57)
Any ideas? I am able to successfully connect via SequelPro with the same ssh and mysql credentials.

Try
ssh -L 3307:localhost:3306 ubuntu#d.local.internal.com
I bet d.local.internal.com can't resolve d.local.internal.com hostname, because you may not have DNS entry for d.local.internal.com in the DNS server used by d.local.internal.com or for that matter a /etc/hosts entry.
Also you don't need to setup your local port to 3307, you can use 3306, provided you don't have anything listening on 3306 on your host.

Related

Permission denied message when attempting ssh to Vagrant public network IP from second machine on same network

I've combed the Internet clean for this one and still can't figure it out. I have a Vagrant VM running using VirtualBox, with the following Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.network "public_network"
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.gui = true
end
end
Vagrant launches successfully, and I can run vagrant ssh successfully and get into the box. I can view the IP of the Vagrant public network, and I'm successfully hosting two Nginx sites in the box, which I can view from a different computer on the same network by visiting the IP of the Vagrant public network.
My server is a clunky laptop, so I want to ssh directly into the Vagrant box that's running on it (from a different development machine). I specifically want to edit the files on the server remotely using the VS Code remote dev tool. I can't for the life of me figure out why, when I run
ssh vagrant#[IP of Vagrant public network on the server]
it gives me "Permission denied (publickey)." It throws this both when I try to run it on the server itself and from another machine on the network.
Do I need to modify some ssh config file somewhere?
EDIT: I read this article, which recommended adding something like this to the ssh config file on the client machine:
Host 192.168.100.100 <- Vagrant box network IP
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
IdentitiesOnly yes
User vagrant
IdentityFile /your/user/directory/.vagrant.d/insecure_private_key
PasswordAuthentication no
this didn't work for me; instead of throwing "Permission denied," it just hangs.
Figured it out. I needed a copy of the private key from my [working dir]/.vagrant/machines/default/virtualbox/private_key file on my server. Once I scp'd that onto my secondary machine, I passed it to ssh as the IdentityFile:
ssh -I [location of copied private_key file] vagrant#[IP of Vagrant public network on the server]
and it worked.

reverse tunnel with ssh: channel 0: connection failed: Connection refused

I am trying to set up a reverse ssh tunnel between a local machine behind a router and a machine on the Internet, so that the Internet machine can tunnel back and mount a disk on the local machine.
On the local machine, I type
/usr/bin/ssh -N -f -R *:2222:127.0.0.1:2222 root#ip_of_remote_machine
This causes the remote machine to listen on port 2222. But when I try to mount the sshfs disk on the remote machine, I get "connection refused" on the local machine. Interestingly, port 2222 doesn't show up on the local machine as being bound. However, I'm definitely talking to ssh on the local machine since it complains
debug1: channel 0: connection failed: Connection refused
I have GatewayPort set to Yes on both machines. I also have AllowTcpForwarding yes on both machines as well.
First, the line needs to be
/usr/bin/ssh -N -f -R *:2222:127.0.0.1:22 root#ip_of_remote_machine
Where port 22 represents the ssh server of the local machine.
Second, since I am using sshfs, the following line needs to be in its sshd_config
Subsystem sftp /usr/lib64/misc/sftp-server

Can't connect to local MySQL server through socket error when using SSH tunel

I am trying to use dplyr to connect to a remote database, that I usually query through a ssh tunnel.
I first set up a ssh tunnel like the following:
alias tunnel_ncg='ssh -fNg -L 3307:127.0.0.1:3306 mysqluser#myhost mysql5 -h 127.0.0.1:3306 -P 3307 -u mysqluser -p mypassword'
At this point I can access the database by connecting to localhost:3307. For example:
mysql -h '127.0.0.1' -P 3307 -u mysqluser
If I try to access the same database through dplyr, I get an error complaining that it can't connect to the local MySQL socket:
> conDplyr = src_mysql(dbname = "mydb", user = "mysqluser", password = "mypassword", host = "localhost", port=3307)
Error in .local(drv, ...) :
Failed to connect to database: Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
My understanding is that RMySQL/dplyr are trying to looking for a socket file in the local computer, however they should really be looking for it in the remote server. Is there a way to fix this, or a work-around?
UPDATE:
If I try to connect through dbConnect/RMySQL, the connection works fine:
> dbConnect(dbDriver("MySQL"), user="mysqluser", password="mypassword", dbname="mydb", host="127.0.0.1", port=3307)
<MySQLConnection:0,1>
As silly as it sounds replacing localhost with an IP address (127.0.0.1) solves the problem.
src_mysql(
dbname = "mydb", user = "mysqluser", password = "mypassword",
host = "127.0.0.1", port=3307)
For an explanation take a look at the MySQL documentation:
On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs.
For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number.
To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server.

tsung postgresql ssh: connect to host localhost port 22: Connection refused

I'm trying to do load test postgresql db using tsung.
I used pgsql.xml provided in examples folder in tsung-1.5.0.
Here's my pgsql.xml: https://github.com/processone/tsung/blob/master/examples/pgsql.xml.in
I run tsung using tsung -f test.xml -p pgsql start.
I got the following error:
ssh: connect to host localhost port 22: Connection refused
Could anyone point me out what the problem is and how to solve it?
You are using server monitoring and your SSH is not setup..
<monitoring>
<monitor host="localhost"/>
</monitoring>
Do you really want to monitor server? If not then you can comment the code and it should work fine.. If yes then you will have to set up a password less SSH to your server...

Npgsql failed to establish connection to CentOS PostgreSQL through PgPool

I have a webapplication in Asp.net connecting to a separate database-server running CentOS with PostgreSQL. This setup works fine.
To increase performance on the database-server I'm trying to install and confgure PgPool-II for pooling database-connections on the CentOS server.
After the configuration I can connect to PostgreSQL with the pgpool configured port from the CentOS command line, so I assume pgpool is up and running.
When I try to connect from my webapplication to the database-server with my new pgpool port I get the following NpgdslException in the eventlog:
Failed to establish a connection to '[ip-address of db-server]'
Some configuration files:
pgpool.conf:
listen_address = '*'
port = 6432
pcp_port = 9898
enable_pool_hba = true
socket_dir = '/tmp'
pcp_socket_dir = '/tmp'
backend_hostname = ''
backend_port = 5432
num_init_children = 50
max_pool = 4
connection_life_time = 120
client_idle_limit = 0
debug_level = 0
pool_hba.conf:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# Network access
host all all 192.168.0.0/24 md5
I have all the ports open in my CentOS firewall configured in /etc/sysconfig/iptables.
So when I'm connecting to port 5432 in my webapplication everything is fine, but when I change the port to 6432, the port for pgpool) then I get the mentioned exception.
Can anybody help me?
The first thing to do is to make sure that pgpool is running on the right port. This can be done easily using fuser:
fuser -n tcp 6432
If it doesn't return anything nothing is listening on your port. In that case make sure pgpool is running:
ps -A | grep pgpool
If this only shows your grep, then your pgpool has not been started. Please refer to your rpm information for information on how to start it. This may involve chkconfig and system

Resources