Python - SSH Tunnel Setup and MySQL DB Access












2















I am trying to connect to my server from my local(windows) and access the MySQL DB



With the below code setting up the SSH tunnel through putty, I am not able to access the
MySQL DB.



con = None
con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
cur = con.cursor()


With the below code, I am using paramiko to setup SSH tunnel which is successful but I am not able to connect to MySQL DB



ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='username', password='password')

con = None
con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
cur = con.cursor()

Error:
Error 2003: Can't connect to MySQL server on '127.0.0.1' (10061)


Do I have change the MySQL connecting string settings to access MySQL DB using paramiko If not I need to add anymore parameter for paramiko to simulate SSH tunnel setup like putty.










share|improve this question



























    2















    I am trying to connect to my server from my local(windows) and access the MySQL DB



    With the below code setting up the SSH tunnel through putty, I am not able to access the
    MySQL DB.



    con = None
    con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
    cur = con.cursor()


    With the below code, I am using paramiko to setup SSH tunnel which is successful but I am not able to connect to MySQL DB



    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('host', username='username', password='password')

    con = None
    con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
    cur = con.cursor()

    Error:
    Error 2003: Can't connect to MySQL server on '127.0.0.1' (10061)


    Do I have change the MySQL connecting string settings to access MySQL DB using paramiko If not I need to add anymore parameter for paramiko to simulate SSH tunnel setup like putty.










    share|improve this question

























      2












      2








      2








      I am trying to connect to my server from my local(windows) and access the MySQL DB



      With the below code setting up the SSH tunnel through putty, I am not able to access the
      MySQL DB.



      con = None
      con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
      cur = con.cursor()


      With the below code, I am using paramiko to setup SSH tunnel which is successful but I am not able to connect to MySQL DB



      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('host', username='username', password='password')

      con = None
      con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
      cur = con.cursor()

      Error:
      Error 2003: Can't connect to MySQL server on '127.0.0.1' (10061)


      Do I have change the MySQL connecting string settings to access MySQL DB using paramiko If not I need to add anymore parameter for paramiko to simulate SSH tunnel setup like putty.










      share|improve this question














      I am trying to connect to my server from my local(windows) and access the MySQL DB



      With the below code setting up the SSH tunnel through putty, I am not able to access the
      MySQL DB.



      con = None
      con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
      cur = con.cursor()


      With the below code, I am using paramiko to setup SSH tunnel which is successful but I am not able to connect to MySQL DB



      ssh = paramiko.SSHClient()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect('host', username='username', password='password')

      con = None
      con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308)
      cur = con.cursor()

      Error:
      Error 2003: Can't connect to MySQL server on '127.0.0.1' (10061)


      Do I have change the MySQL connecting string settings to access MySQL DB using paramiko If not I need to add anymore parameter for paramiko to simulate SSH tunnel setup like putty.







      mysql-python paramiko






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 20 '12 at 15:24









      Earnest JasonEarnest Jason

      2114




      2114
























          1 Answer
          1






          active

          oldest

          votes


















          5














          You can use sshtunnel wrapper for paramiko and save you headaches ;)



          from sshtunnel import SSHTunnelForwarder
          import MySQLdb

          with SSHTunnelForwarder(
          ('host', 22),
          ssh_password="password",
          ssh_username="username",
          remote_bind_address=('127.0.0.1', 3308)) as server:

          con = None
          con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=server.local_bind_port)
          cur = con.cursor()





          share|improve this answer



















          • 3





            yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

            – Manuel G
            Jul 29 '15 at 23:16











          • Yep! that's another option ;)

            – jsjc
            Jul 30 '15 at 2:25











          • what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

            – 62mkv
            Aug 4 '15 at 10:45













          • Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

            – 62mkv
            Aug 4 '15 at 11:00






          • 1





            This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

            – jsjc
            Aug 4 '15 at 16:25











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12989866%2fpython-ssh-tunnel-setup-and-mysql-db-access%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          You can use sshtunnel wrapper for paramiko and save you headaches ;)



          from sshtunnel import SSHTunnelForwarder
          import MySQLdb

          with SSHTunnelForwarder(
          ('host', 22),
          ssh_password="password",
          ssh_username="username",
          remote_bind_address=('127.0.0.1', 3308)) as server:

          con = None
          con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=server.local_bind_port)
          cur = con.cursor()





          share|improve this answer



















          • 3





            yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

            – Manuel G
            Jul 29 '15 at 23:16











          • Yep! that's another option ;)

            – jsjc
            Jul 30 '15 at 2:25











          • what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

            – 62mkv
            Aug 4 '15 at 10:45













          • Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

            – 62mkv
            Aug 4 '15 at 11:00






          • 1





            This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

            – jsjc
            Aug 4 '15 at 16:25
















          5














          You can use sshtunnel wrapper for paramiko and save you headaches ;)



          from sshtunnel import SSHTunnelForwarder
          import MySQLdb

          with SSHTunnelForwarder(
          ('host', 22),
          ssh_password="password",
          ssh_username="username",
          remote_bind_address=('127.0.0.1', 3308)) as server:

          con = None
          con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=server.local_bind_port)
          cur = con.cursor()





          share|improve this answer



















          • 3





            yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

            – Manuel G
            Jul 29 '15 at 23:16











          • Yep! that's another option ;)

            – jsjc
            Jul 30 '15 at 2:25











          • what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

            – 62mkv
            Aug 4 '15 at 10:45













          • Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

            – 62mkv
            Aug 4 '15 at 11:00






          • 1





            This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

            – jsjc
            Aug 4 '15 at 16:25














          5












          5








          5







          You can use sshtunnel wrapper for paramiko and save you headaches ;)



          from sshtunnel import SSHTunnelForwarder
          import MySQLdb

          with SSHTunnelForwarder(
          ('host', 22),
          ssh_password="password",
          ssh_username="username",
          remote_bind_address=('127.0.0.1', 3308)) as server:

          con = None
          con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=server.local_bind_port)
          cur = con.cursor()





          share|improve this answer













          You can use sshtunnel wrapper for paramiko and save you headaches ;)



          from sshtunnel import SSHTunnelForwarder
          import MySQLdb

          with SSHTunnelForwarder(
          ('host', 22),
          ssh_password="password",
          ssh_username="username",
          remote_bind_address=('127.0.0.1', 3308)) as server:

          con = None
          con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=server.local_bind_port)
          cur = con.cursor()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 26 '15 at 16:13









          jsjcjsjc

          65821022




          65821022








          • 3





            yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

            – Manuel G
            Jul 29 '15 at 23:16











          • Yep! that's another option ;)

            – jsjc
            Jul 30 '15 at 2:25











          • what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

            – 62mkv
            Aug 4 '15 at 10:45













          • Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

            – 62mkv
            Aug 4 '15 at 11:00






          • 1





            This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

            – jsjc
            Aug 4 '15 at 16:25














          • 3





            yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

            – Manuel G
            Jul 29 '15 at 23:16











          • Yep! that's another option ;)

            – jsjc
            Jul 30 '15 at 2:25











          • what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

            – 62mkv
            Aug 4 '15 at 10:45













          • Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

            – 62mkv
            Aug 4 '15 at 11:00






          • 1





            This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

            – jsjc
            Aug 4 '15 at 16:25








          3




          3





          yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

          – Manuel G
          Jul 29 '15 at 23:16





          yup, this one worked. In case you are using id_rsa just replace ssh_password parameter with ssh_private_key=PATH_TO_id_rsa

          – Manuel G
          Jul 29 '15 at 23:16













          Yep! that's another option ;)

          – jsjc
          Jul 30 '15 at 2:25





          Yep! that's another option ;)

          – jsjc
          Jul 30 '15 at 2:25













          what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

          – 62mkv
          Aug 4 '15 at 10:45







          what is mdb ? I have an error which says "mdb not defined". after replacing it with "MySQLdb" (just a guess), I know have an error: File "C:Python27libsite-packagesMySQLdbcursors.py", line 202, in execute self.errorhandler(self, exc, value) File "C:Python27libsite-packagesMySQLdbconnections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')

          – 62mkv
          Aug 4 '15 at 10:45















          Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

          – 62mkv
          Aug 4 '15 at 11:00





          Ok, I got it - I was working with cur beyond the scope of "with .. server:" construction ! 4 spaces did the magic (as usual in Python !)

          – 62mkv
          Aug 4 '15 at 11:00




          1




          1





          This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

          – jsjc
          Aug 4 '15 at 16:25





          This example was made directly for Earnests question thats why kept it as mdb, so I left things as per his sample. Glad you got it sorted!

          – jsjc
          Aug 4 '15 at 16:25


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12989866%2fpython-ssh-tunnel-setup-and-mysql-db-access%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy