By using Python pymysql, connected to the remote database via sshtunnel module ssh.

Recently a small script to change their own, ready to shelve the document from the local, josn data formats, transfer to MySQL and redis.

Server originally by SHH logged in, but I do not want to open the MySQL external network port to be set, afraid of insecurity, my MySQL password on several numbers.

I checked the information, Python there are third-party modules sshtunnel ssh login

This is my reference link: https: //www.cnblogs.com/luyingfeng/p/6386093.html

My first full-page copied.

Safety reasons, access to the database is mostly restricted to do, so there is a direct problem is often most of the time, in other machines (such as their own local), can not access the database, for everyday use has caused great inconvenient. So a few days ago I made a demand, in the hope that any machine can ssh into the machine on a table in the whitelist database, and then access the database.

Recommended by the people, a query tool called sshtunnel, needs to be installed on the machine you want to log database.

Basic introduction of sshtunnel:  http://sshtunnel.readthedocs.io/en/latest/?badge=latest

This side of Figure 2, is the scene we have just described, a database on a remote server, can only be accessed with another server together, but we need the local ssh to this server

 

But the inside is the code related to the relevant server, remote connection server, what we need is a remote database server, and this, too, only need to change the code in the latter part of it.

This is the original code below:

Copy the code
 1 import paramiko
 2 from sshtunnel import SSHTunnelForwarder  3  4 with SSHTunnelForwarder(  5 (REMOTE_SERVER_IP, 443),  6 ssh_username="",  7 ssh_pkey="/var/ssh/rsa_key",  8 ssh_private_key_password="secret",  9 remote_bind_address=(PRIVATE_SERVER_IP, 22), 10 local_bind_address=('0.0.0.0', 10022) 11 ) as tunnel: 12 client = paramiko.SSHClient() 13  client.load_system_host_keys() 14  client.set_missing_host_key_policy(paramko.AutoAddPolicy()) 15 client.connect('127.0.0.1', 10022) 16 # do some operations with client session 17  client.close() 18 19 print('FINISH!')
Copy the code

Mysql database connection

When connecting mysql database, the Internet has seen a programmer has been achieved:  Python modules are connected using mysqldb mysql through ssh tunnel

code show as below:

Copy the code
. 1 Import MySQLdb
 2 from sshtunnel Import SSHTunnelForwarder . 3 . 4 with SSHTunnelForwarder ( . 5 ( ' sshhost.domain.com ', 22 is), # B configuration of the machine . 6 ssh_password = " sshpasswd " , . 7 = ssh_username " sshusername " , . 8 remote_bind_address = ( ' mysqlhost.domain.com ', mysql.port)) Server AS: # a machine configuration . 9 10 Conn = MySQLdb.connect (Host = ' 127.0.0.1 ', # here must yes 127.0.0.111 port=server.local_bind_port, 12 user='user', 13 passwd='password', 14 db='dbname')
Copy the code

 Then what's next query, write directly with the edge there, and conn align it.

For me there is a problem here, it is because we are connected to this part of the database, often in a separate function, the insert delete update and query other databases are often not together, so, with as there is a feature of leave this scope, the object is destroyed the enemy, the other function is not in use, it will be a case is connected, but the destruction of the object gave off, when the results of the query directly show this error: OperationalError: (2006, 'MySQL server has gone away'), and check online this mistake, many said that because of the time sql operation of your query is too long, or the data transfer is too large, but my because the place is actually out of scope with as a result, the connection is closed and gave out, so this outcome.

On with as, an essay written very detailed. Understanding of Python syntax with ... as ...

After I put the code on top that ssh get rid of, like sshtunnel documents inside view of a corresponding code, as the assignment SSHTunnelForwarder out of the object to the server, and then start the server, and then perform a series of operations, then stop off.

Originally a database connection we write a separate function, changed after direct and also placed in this function just fine, replace the original connect statement.

Copy the code
 1 def connect(self):
 2     '''
 3  self.client = MySQLdb.connect(host=self.server, port=self.port, user=self.user,  4  passwd=self.password, db=self.database,  5  charset=self.charset)  6  # log.info('Connect to MySQL Server: ' + self.server)  7 '''  8  9 server = SSHTunnelForwarder( 10 ('sshhost.domain.com', 22), # B机器的配置 11 ssh_password='ssh_password', 12 ssh_username='ssh_username', Remote_bind_address = 13 is ( ' mysqlhost.domain.com ' , mysql.port) 14 ) 15 server.start () 16 . 17 self.client = MySQLdb.connect (Host = ' 127.0.0.1 ', # here must yes 127.0. 0.1 18 is Port = server.local_bind_port, . 19 = User ' username ' , 20 is the passwd = ' password ' , 21 is DB = ' dbname ')
Copy the code

Then making a query to update or delete operation when the first connection look like a database, with self.client.

Sqlserver database connection

Consistent with mysql, but there should be noted db, SQLServer that database, then pymssql.connect on it, but this place also like to say how the connection after I stepped on a pit, I finished sqlserver Rom database only to discover later version of the problem, the local SQLServer after I updated it. It is always a feeling pit

 

The above is the original author wrote. In fact, this module was originally login to the target server through an intermediate server. But my intermediate server is the target server.

import pymysql
from sshtunnel import SSHTunnelForwarder


class DataBaseHandle:
    '' 'Defines a MySQL operation class' ''

    def __init__(self, host='127.0.01', username='xxx', password='xxx'
                 , database='xxx', port=10022):
        '' 'To initialize the database information and create a database connection' ''
        self.w_server = SSHTunnelForwarder(
                # Intermediate server address
                ("xxx.64.47.xxx", 22),
                ssh_username="xxx",
                ssh_pkey="~/.ssh/id_rsa",
                # ssh_private_key_password="~/.ssh/id_rsa",
                Address and port # target, because the target address is the intermediate address 127.0.0.1 or localhost so write
                remote_bind_address=('127.0.0.1', 3306),
                # Local address and port
                local_bind_address=('0.0.0.0', 10022)
                )
        # Ssh start instance, the follow-up network connection MySQL will run in this environment.
        self.w_server.start()
        # Behind began to MySQL data to initialize
        self.host = host
        self.username = username
        self.password = password
        self.database = database
        self.port = port
        self.db = pymysql.connect(host=self.host,
                                  user=self.username,
                                  password=self.password,
                                  database=self.database,
                                  port=self.port,
                                  charset='utf8')

    # Here annotation method connection to an instance of an object, you create a connection. Allowed to be connected to separate treatment.
    #
    # Def nnDa the taBase (Self):
    # ''' Database Connectivity'''
    #
    #     self.db = pymysql.connect(self.host,self.username,self.password,self.port,self.database)
    #
    #     # self.cursor = self.db.cursor()
    #
    #     return self.db

    def insertDB(self, sql):
        '' 'Into the database operation' ''

        self.cursor = self.db.cursor()

        try:
            # Sql execution
            self.cursor.execute(sql)
            # Tt = self.cursor.execute (sql) # Returns the number of pieces of data may be inserted into the decision process based on the return value
            # print(tt)
            self.db.commit()
        except:
            # Rollback when an error occurs
            self.db.rollback()
        finally:
            self.cursor.close()

    def deleteDB(self, sql):
        '' 'Operation of the database data is deleted' ''
        self.cursor = self.db.cursor()

        try:
            # Sql execution
            self.cursor.execute(sql)
            # Tt = self.cursor.execute (sql) # Returns the number of deleted pieces of data values ​​may be determined according to the processing result returned
            # print(tt)
            self.db.commit()
        except:
            # Rollback when an error occurs
            self.db.rollback()
        finally:
            self.cursor.close()

    def updateDb(self, sql):
        '' 'Operation to update the database' ''

        self.cursor = self.db.cursor()

        try:
            # Sql execution
            self.cursor.execute(sql)
            # Tt = self.cursor.execute (sql) # returns the updated number of data values ​​can be determined in accordance with the processing result returned
            # print(tt)
            self.db.commit()
        except:
            # Rollback when an error occurs
            self.db.rollback()
        finally:
            self.cursor.close()

    def selectDb(self, sql):
        '' 'Database queries' ''
        self.cursor = self.db.cursor()
        try:
            self.cursor.execute (sql) # query that returns the number of pieces of data may be determined based on the return value of the processing result

            data = self.cursor.fetchall () # Returns a list of all the records

            print(data)

            # Traversal results
            for row in data:
                sid = row[0]
                name = row[1]
                # Traversing print results
                print('sid = %s,  name = %s' % (sid, name))
        except:
            print('Error: unable to fecth data')
        finally:
            self.cursor.close()

    def closeDb(self):
        '' 'Database connection is closed' ''
        self.db.close()
        self.w_server.close()



if __name__ == '__main__':
    DbHandle = DataBaseHandle()
    DbHandle.selectDb('SELECT VERSION()')
    DbHandle.closeDb()

 

The operation of the database class is what I got from the Internet, I feel okay to write it directly used to use.

Finally, remember, remember to turn off the MySQL ssh connections on exit.

Guess you like

Origin www.cnblogs.com/sidianok/p/12571798.html