Connecting to MySQL over SSH using Python
<p>I need some help adding tables from a desktop PC to an existing MySQL database (db name DB2639162) on a webserver using a python script. I have written the following script (create_db.py):</p>

<pre><code>import MySQLdb
from sshtunnel import SSHTunnelForwarder
ssh_pwd=''
ssh_usr=''
mysql_pwd=''
mysql_usr=''
with SSHTunnelForwarder(('ssh.strato.de', 22),
                        ssh_password=ssh_pwd, ssh_username=ssh_usr,
                        remote_bind_address=('rdbms', 3306)) as server:
    print 'Server connected via SSH!'

    db1 = MySQLdb.connect(host='127.0.0.1',
                      port=server.local_bind_port,
                      user=mysql_usr,
                      passwd=mysql_pwd,
                      db='DB2639162')
    cursor = db1.cursor()
    sql = 'CREATE TABLE mydata'
    cursor.execute(sql)
    db1.close()
</code></pre>

<p>But unfortunately the script does not work and I get the output below. Obviously, the SSH connection can be established successfully, but the access to the database fails.</p>

<pre><code>Server connected via SSH!
2016-09-18 11:02:19,291| ERROR   | Secsh channel 0 open FAILED: open failed: Administratively prohibited
2016-09-18 11:02:19,295| ERROR   | Could not establish connection from ('127.0.0.1', 44017) to remote side of the tunnel
Traceback (most recent call last):
  File "create_db.py", line 18, in &lt;module&gt;
    db='DB2639162')
  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 81, in Connect
  File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 193, in __init__
_mysql_exceptions.OperationalError: (2013, "Lost connection to MySQL server at 'reading initial communication packet', system error: 0")
</code></pre>

<p>Additionally, the usernames, passwords are fine as the terminal commands work well and grant me access to the MySQL database.</p>

<pre><code>ssh ssh_usr@ssh.strato.de
mysql -h rdbms -u mysql_usr -p DB2639162
</code></pre>

<p>Thank you in advance</p>

<p>The answer is right there in the error message:</p>

<blockquote>
  <p>2016-09-18 11:02:19,291| ERROR   | Secsh channel 0 open FAILED: open failed: Administratively prohibited</p>
</blockquote>

<p>Running the MySQL command-line client in a shell session on a server and setting up port forwarding/tunneling are completely different things. The fact that you can do one does not imply that you can do the other.</p>

<p>This server obviously forbids port forwarding/tunneling ("Administratively prohibited"). You'll need a different approach.</p>

python mysql linux ssh