Solved

Connect Apache Ant remotely to MySQL over SSH tunneling

Posted on 2011-09-28
2
1,102 Views
Last Modified: 2013-11-10
A hosting provider I'm working with has port 3306 blocked for public access on their firewall, they recommend connecting to MySQL over ssh tunneling. This works fine for MySQL Workbench as it provides such functionality, but I'm trying to get a similar thing going with Apache Ant so I can automate restoring databases. I'm trying to figure out if I'm doing this correctly, from what I've read <sshsession> in Apache Ant 1.8.x provides tunneling. But I'm not sure whether to use localtunnel or remote tunnel and I'm not sure if I'll be able to even do what I want over a tunnel. Any suggestions?

<sshsession host="example.com" username="user" keyfile="${sshKeyLocation}" passphrase="password">
    <remotetunnel rport="3306" lhost="example.com" lport="3306"/>
    <sequential>
        <sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://example.com:3306/${databaseName}" userid="dbuser" password="dbpass">
            <transaction src="temp.sql"/>
        </sql>
    </sequential>
</sshsession>
0
Comment
Question by:level9wizard
2 Comments
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 36813808
Maybe you can try it in two steps:

1.  Set up tunnel:

ssh -f example.com -L 3305:127.0.0.1:3306 -N

2.  Use 127.0.0.1:3305 in your connection string.

(Replace 3305 with whatever you want as the local port).

0
 
LVL 11

Author Closing Comment

by:level9wizard
ID: 36814530
Thanks, this lead me in the right direction. The actual code used with Ant (in case anyone else stumbles on this Q/A):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="Release Builder" default="release" basedir=".">
    <description><![CDATA[Release Builder]]></description>
   
    <!-- REQUIREMENTS: MySQL JDBC Driver, DatabaseNameParser, DomainFileName, jsch -->
   
   
    <!-- Configuration -->
    <property name="mysqlUser" value="root"/>
    <property name="mysqlPassword" value="xxxxxxx"/>
    <property name="sshKeyLocation" value="private.ossh"/>
   
    <!-- End Configuration -->
    <basename property="basedir.name" file="${basedir}"/>
   
    <target name="release" description="Release to the live server">
        <!-- Create the time stamp -->
        <tstamp/>
       
        <sshsession host="example.com" username="root" keyfile="${sshKeyLocation}" passphrase="xxxxxxxxxxxx" trust="true">
            <localtunnel rport="3306" rhost="example.com" lport="3305"/>
            <sequential>
                <sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3305/project_name" userid="zzzzzzz" password="xxxxxxx">
                    SELECT * FROM SiteTree LIMIT 1
                </sql>
            </sequential>
        </sshsession>
    </target>
</project>
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now