<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: PHP, MySQL and SSH Tunneling (Port Forwarding)</title>
	<atom:link href="http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/feed/" rel="self" type="application/rss+xml" />
	<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/</link>
	<description>A blog about data by RJMetrics</description>
	<lastBuildDate>Wed, 03 Mar 2010 23:03:16 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Ming</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1524</link>
		<dc:creator>Ming</dc:creator>
		<pubDate>Thu, 07 Jan 2010 20:52:54 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1524</guid>
		<description>Thanks for sharing your solution. I was having a heck of a time connecting to mysql via ssh. The examples most readily found on the net suggest using the remote ip between the local and remote ports.  Changing it to 127.0.0.1 fixed the problem.  Good thing I kept googling the various error terms, else I would still be puzzling.</description>
		<content:encoded><![CDATA[<p>Thanks for sharing your solution. I was having a heck of a time connecting to mysql via ssh. The examples most readily found on the net suggest using the remote ip between the local and remote ports.  Changing it to 127.0.0.1 fixed the problem.  Good thing I kept googling the various error terms, else I would still be puzzling.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1409</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Fri, 25 Sep 2009 00:02:16 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1409</guid>
		<description>Robert, now I have connected to the remote database through ssh tunnel in php script by following your solution. 

Now I need to select from remote database and insert the result into local database.  I can do it in two steps, select first, and then insert second.  But it seems to be slow for large set of records.

I would like to use subquery, something like 

//get the data from remotedatabase
    $sql_select_records_from_remotedatabse=&quot;SELECT field_1, field_2 FROM remotedatabase.table1&quot;;

//insert into localsdatabase using subquery

    $sql_insert_records_to_localdatabase=&quot;INSERT INTO table_a (field_a, field_b) $sql_select_records_from_remotedatabse&quot;; 

some threads suggest to make it work, I need to use sp_addlinkedserver.

solution for remote server
Submitted by Anonymous (not verified) on Tue, 06/02/2009 - 11:56.

1. first add the server to your local sql server (from where you want to run the query)

exec sp_addlinkedserver [192.168.1.130]

2. now run the query as

select * from [192.168.1.130].[pubs].[dbo].[authors] 

Did you run into the same situation, if so, what is your solution?  

Thanks!</description>
		<content:encoded><![CDATA[<p>Robert, now I have connected to the remote database through ssh tunnel in php script by following your solution. </p>
<p>Now I need to select from remote database and insert the result into local database.  I can do it in two steps, select first, and then insert second.  But it seems to be slow for large set of records.</p>
<p>I would like to use subquery, something like </p>
<p>//get the data from remotedatabase<br />
    $sql_select_records_from_remotedatabse=&#8221;SELECT field_1, field_2 FROM remotedatabase.table1&#8243;;</p>
<p>//insert into localsdatabase using subquery</p>
<p>    $sql_insert_records_to_localdatabase=&#8221;INSERT INTO table_a (field_a, field_b) $sql_select_records_from_remotedatabse&#8221;; </p>
<p>some threads suggest to make it work, I need to use sp_addlinkedserver.</p>
<p>solution for remote server<br />
Submitted by Anonymous (not verified) on Tue, 06/02/2009 &#8211; 11:56.</p>
<p>1. first add the server to your local sql server (from where you want to run the query)</p>
<p>exec sp_addlinkedserver [192.168.1.130]</p>
<p>2. now run the query as</p>
<p>select * from [192.168.1.130].[pubs].[dbo].[authors] </p>
<p>Did you run into the same situation, if so, what is your solution?  </p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1408</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Thu, 24 Sep 2009 19:47:44 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1408</guid>
		<description>It works!

Thanks Robert.  I got it works now!

A few points to answer my own questions:

1) &gt;&gt; logfile is needed.  (as robert&#039;s post)
2) I use regular mysql_connect and set up the host to be &quot;127.0.0.1:3307&quot;, so I get the localport too, but somehow &quot;localhost:3307&quot; is not working, it has to be &quot;127.0.0.1:3307&quot;.
3) and if I run web page due to user is &quot;apache&quot;, I will need to set key pairs for user apache.  But I will run my php from shell command, and the &quot;user&quot; will be just my user account.

Thanks again!</description>
		<content:encoded><![CDATA[<p>It works!</p>
<p>Thanks Robert.  I got it works now!</p>
<p>A few points to answer my own questions:</p>
<p>1) &gt;&gt; logfile is needed.  (as robert&#8217;s post)<br />
2) I use regular mysql_connect and set up the host to be &#8220;127.0.0.1:3307&#8243;, so I get the localport too, but somehow &#8220;localhost:3307&#8243; is not working, it has to be &#8220;127.0.0.1:3307&#8243;.<br />
3) and if I run web page due to user is &#8220;apache&#8221;, I will need to set key pairs for user apache.  But I will run my php from shell command, and the &#8220;user&#8221; will be just my user account.</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1407</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Thu, 24 Sep 2009 18:46:19 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1407</guid>
		<description>Robert, following your instruction.

ssh -f -L 3307:127.0.0.1:3306 user@remotehost sleep 60
mysql -u mysqluser -p -h 127.0.0.1 -P 3307

Works fine! I think that I am in the right direction.

But I wrote test.php with following php codes:

shell_exec(”ssh -f -L 3307:127.0.0.1:3306 user@remotehost sleep 60”);
$db = mysqli_connect(’127.0.0.1′, ’sqluser’, ’sqlpassword’, ‘sqldatabase’, 3307);

I then run command line
php test.php 

It gave me Error: Can&#039;t connect to MySQL server on &#039;127.0.0.1&#039; (111)

So I change test.php to the following line:

shell_exec(&#039;ssh -f -L 3307:127.0.0.1:3306 user@remotehost sleep 60&#039;);
shell_exec(&#039;mysql -u mysqluser -p -h 127.0.0.1 -P 3307&#039;);

Run command line
php test.php

I would be prompted for password, but after I keyed in password, I still got Error: Can&#039;t connect to MySQL server on &#039;127.0.0.1&#039; (111)

Any one has any advices, some set ups I would need?  Still thanks for your help, at least I am close to get it working.</description>
		<content:encoded><![CDATA[<p>Robert, following your instruction.</p>
<p>ssh -f -L 3307:127.0.0.1:3306 user@remotehost sleep 60<br />
mysql -u mysqluser -p -h 127.0.0.1 -P 3307</p>
<p>Works fine! I think that I am in the right direction.</p>
<p>But I wrote test.php with following php codes:</p>
<p>shell_exec(”ssh -f -L 3307:127.0.0.1:3306 user@remotehost sleep 60”);<br />
$db = mysqli_connect(’127.0.0.1′, ’sqluser’, ’sqlpassword’, ‘sqldatabase’, 3307);</p>
<p>I then run command line<br />
php test.php </p>
<p>It gave me Error: Can&#8217;t connect to MySQL server on &#8216;127.0.0.1&#8242; (111)</p>
<p>So I change test.php to the following line:</p>
<p>shell_exec(&#8217;ssh -f -L 3307:127.0.0.1:3306 user@remotehost sleep 60&#8242;);<br />
shell_exec(&#8216;mysql -u mysqluser -p -h 127.0.0.1 -P 3307&#8242;);</p>
<p>Run command line<br />
php test.php</p>
<p>I would be prompted for password, but after I keyed in password, I still got Error: Can&#8217;t connect to MySQL server on &#8216;127.0.0.1&#8242; (111)</p>
<p>Any one has any advices, some set ups I would need?  Still thanks for your help, at least I am close to get it working.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robertjmoore</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1403</link>
		<dc:creator>robertjmoore</dc:creator>
		<pubDate>Wed, 23 Sep 2009 20:40:54 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1403</guid>
		<description>Hui, #1 and #2 have to do with your own application and its needs-- my notes in this blog post were only made for simplicity&#039;s sake.  For #3, you might consider writing a script that uses netstat to detect if the tunnel is still open and respond accordingly.  Good luck.</description>
		<content:encoded><![CDATA[<p>Hui, #1 and #2 have to do with your own application and its needs&#8211; my notes in this blog post were only made for simplicity&#8217;s sake.  For #3, you might consider writing a script that uses netstat to detect if the tunnel is still open and respond accordingly.  Good luck.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1400</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Wed, 23 Sep 2009 06:29:17 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1400</guid>
		<description>I made a mistake in my previous post. correction: &quot;Like you pointed out, ssh2_tunnel won’t work due to they WOULDN&#039;T allow specifying the local port.&quot;</description>
		<content:encoded><![CDATA[<p>I made a mistake in my previous post. correction: &#8220;Like you pointed out, ssh2_tunnel won’t work due to they WOULDN&#8217;T allow specifying the local port.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1399</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Wed, 23 Sep 2009 06:27:28 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1399</guid>
		<description>Robert, I was struggling with using ssh2_tunnel php function to build a ssh tunnel to access remote mysql.  Like you pointed out, ssh2_tunnel won&#039;t work due to they would allow specifying the local port.  (Googled, many people fell in this ssh2_tunnel trap.)

So use php to connect mysql through ssh that is the only way, right?

thanks for this instruction! Due to I will be doing many of same kind works, get data from remote mysql, like you did, a few quick questions.

1) so you run this php script through command line not web page to get the remote database data, due to it is command line, there are several types scripts you can use, why use php, any other programming scripts will do the command line mysql job better? php is on my top to use list too, only because it is the script I am more familiar with than other scripts.

2) if you already have mysql access through ssh to remote host, why not just run one mysqldump command to get the mysql data from remote host?  will that be simpler and safer? (or usually you would not be granted the &#039;lock&quot; rights?

3) about the ssh sleep 60, when the ssh tunnel will be closed, after the php script executed.  Then if I am running second php script right after the first, will that port 3307 be already released after first page finished, or will the 3307 still in use, and my second php page will get error?

Thanks again!</description>
		<content:encoded><![CDATA[<p>Robert, I was struggling with using ssh2_tunnel php function to build a ssh tunnel to access remote mysql.  Like you pointed out, ssh2_tunnel won&#8217;t work due to they would allow specifying the local port.  (Googled, many people fell in this ssh2_tunnel trap.)</p>
<p>So use php to connect mysql through ssh that is the only way, right?</p>
<p>thanks for this instruction! Due to I will be doing many of same kind works, get data from remote mysql, like you did, a few quick questions.</p>
<p>1) so you run this php script through command line not web page to get the remote database data, due to it is command line, there are several types scripts you can use, why use php, any other programming scripts will do the command line mysql job better? php is on my top to use list too, only because it is the script I am more familiar with than other scripts.</p>
<p>2) if you already have mysql access through ssh to remote host, why not just run one mysqldump command to get the mysql data from remote host?  will that be simpler and safer? (or usually you would not be granted the &#8216;lock&#8221; rights?</p>
<p>3) about the ssh sleep 60, when the ssh tunnel will be closed, after the php script executed.  Then if I am running second php script right after the first, will that port 3307 be already released after first page finished, or will the 3307 still in use, and my second php page will get error?</p>
<p>Thanks again!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1398</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Mon, 21 Sep 2009 18:53:47 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1398</guid>
		<description>I checked the sshd_config and I see the following line
RSAAuthentication yes 

But I didn&#039;t see the following line
PubkeyAuthentication yes 

Will this line be required &quot;PubkeyAuthentication yes&quot; for the passwordless to work.

Thanks!</description>
		<content:encoded><![CDATA[<p>I checked the sshd_config and I see the following line<br />
RSAAuthentication yes </p>
<p>But I didn&#8217;t see the following line<br />
PubkeyAuthentication yes </p>
<p>Will this line be required &#8220;PubkeyAuthentication yes&#8221; for the passwordless to work.</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: hui</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1397</link>
		<dc:creator>hui</dc:creator>
		<pubDate>Mon, 21 Sep 2009 17:52:22 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1397</guid>
		<description>I got the user as &#039;apache&quot;, so I think I am out of luck and I have to use apache account to generate the key and set up the authorized_key files.

One more question:

I have set up my account keys pair and paste my public key to one remote account, and the passwordless authentication is working fine. 

But on another remote account, I did the same thing, but I cannot get the passwordless access to that account.  I double checked the authorized_keys file, ssh folder and their permissions.  They are all set up right.  But somehow it works on one machine but not on another.

Both remote accounts have open ssh, and I can generate keys at both remote accounts.

You have to access many remote accounts from your clients.  Any issues that you think may stop me to passwordless access to that remote account (it always ask for the password.)</description>
		<content:encoded><![CDATA[<p>I got the user as &#8216;apache&#8221;, so I think I am out of luck and I have to use apache account to generate the key and set up the authorized_key files.</p>
<p>One more question:</p>
<p>I have set up my account keys pair and paste my public key to one remote account, and the passwordless authentication is working fine. </p>
<p>But on another remote account, I did the same thing, but I cannot get the passwordless access to that account.  I double checked the authorized_keys file, ssh folder and their permissions.  They are all set up right.  But somehow it works on one machine but not on another.</p>
<p>Both remote accounts have open ssh, and I can generate keys at both remote accounts.</p>
<p>You have to access many remote accounts from your clients.  Any issues that you think may stop me to passwordless access to that remote account (it always ask for the password.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robertjmoore</title>
		<link>http://themetricsystem.rjmetrics.com/2009/01/06/php-mysql-and-ssh-tunneling-port-forwarding/#comment-1396</link>
		<dc:creator>robertjmoore</dc:creator>
		<pubDate>Mon, 21 Sep 2009 17:25:45 +0000</pubDate>
		<guid isPermaLink="false">http://themetricsystem.rjmetrics.com/?p=206#comment-1396</guid>
		<description>try this:
print shell_exec(&quot;whoami&quot;);

It should output the linux user that is actually executing commands when using the shell_exec() function.  Configure passwordless authentication for this user.  Also, as a general rule, I would make sure everything is working from the command line first before doing it from PHP.

Good luck.</description>
		<content:encoded><![CDATA[<p>try this:<br />
print shell_exec(&#8220;whoami&#8221;);</p>
<p>It should output the linux user that is actually executing commands when using the shell_exec() function.  Configure passwordless authentication for this user.  Also, as a general rule, I would make sure everything is working from the command line first before doing it from PHP.</p>
<p>Good luck.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
