Search code examples
phpmysqlpropel

How do I reverse engineer over an SSH tunnel with Propel?


I can only connect to my database through an SSH tunnel. Up until now, I've been dumping the schema from my database and loading it onto my local. How can I connect directly to my database?


Solution

  • This information is entirely lacking on the internet, so I'm answering my own question.

    1. Shut down your local MySQL server instance (if you have one).
    2. Open a terminal and run the following command:

      ssh -L 3307:[database hostname]:3306 [SSH username]@[SSH hostname] [-p[SSH port]]
      

      This might also work with PuTTY, I'm not sure.

    3. In your build.properties, set the following:

      propel.database.url=mysql:host=0.0.0.0;port=3307;dbname=[database name]
      

    It cannot be localhost. If it is, you will receive the following error:

    propel > reverse:
    
         [echo] +-----------------------------------------------+
         [echo] |                                               |
         [echo] | Generating XML from PDO connection !          |
         [echo] |                                               |
         [echo] +-----------------------------------------------+
    [propel-schema-reverse] There was an error building XML from metadata: vendor/propel/propel1/generator/build-propel.xml:296:1: SQLSTATE[HY000] [2002] No such file or directory
    
    BUILD FINISHED
    
    Total time: 0.2293 seconds
    

    This cryptic error means that it cannot find the Unix socket on your computer because, even if you are specifying the host and the port, PDO will still look for one with localhost. Obviously, you can never open a socket for a server that does not reside on your machine, and even if you add unix_socket to the DSN as null, it will complain about being a bad filename. Using 0.0.0.0 fixes this error. I actually figured that out from this question.

    And you're done! You can now reverse engineer with Propel over an SSH tunnel.