in Education by
I'm using MySqldb with Python 2.7 to allow Python to make connections to another MySQL server import MySQLdb db = MySQLdb.connect(host="sql.domain.com", user="dev", passwd="*******", db="appdb") Instead of connecting normally like this, how can the connection be made through a SSH tunnel using SSH key pairs? The SSH tunnel should ideally be opened by Python. The SSH tunnel host and the MySQL server are the same machine. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Only this worked for me import pymysql import paramiko import pandas as pd from paramiko import SSHClient from sshtunnel import SSHTunnelForwarder from os.path import expanduser home = expanduser('~') mypkey = paramiko.RSAKey.from_private_key_file(home + pkeyfilepath) # if you want to use ssh password use - ssh_password='your ssh password', bellow sql_hostname = 'sql_hostname' sql_username = 'sql_username' sql_password = 'sql_password' sql_main_database = 'db_name' sql_port = 3306 ssh_host = 'ssh_hostname' ssh_user = 'ssh_username' ssh_port = 22 sql_ip = '1.1.1.1.1' with SSHTunnelForwarder( (ssh_host, ssh_port), ssh_username=ssh_user, ssh_pkey=mypkey, remote_bind_address=(sql_hostname, sql_port)) as tunnel: conn = pymysql.connect(host='127.0.0.1', user=sql_username, passwd=sql_password, db=sql_main_database, port=tunnel.local_bind_port) query = '''SELECT VERSION();''' data = pd.read_sql_query(query, conn) conn.close()

Related questions

0 votes
    Our core application changed from Python 2.6 to Python 2.7 maybe to Python 3 in later release. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I need to use environment variable "PATH" in yaml file which needs to be parsed with a script. ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    My task is connected to the container via ssh from tests. I have dockefile: (almost from https:// ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am using Pycharm 2018.2.4 community edition on Ubuntu 18.04 and I have started python 2.7 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I am using Pycharm 2018.2.4 community edition on Ubuntu 18.04 and I have started python 2.7 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    I want to use python to connect to a MySQL database, how can I do that? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I am trying to connect to MySQL. I have defined the db connection vars in a .env file in my root ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to connect to db on EC2 from my local machine, I am not able to do and have tried everything- I ... to connect to the database Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Which of the following package is used to connect MySQL RDBMS with R? (a) RMySQL vignette (b) MySQL ... questions and answers pdf, Data Science interview questions for beginners...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    How to enable inno-db support on installed instance of MySql? I have installed mysql-5.0.67-win32. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    How to enable slow query log MySQL?...
asked Aug 20, 2021 in Technology by JackTerrance
0 votes
    Convert mysql timestamp to epoch time in python - is there an easy way to do this? JavaScript questions ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 18, 2022 in Education by JackTerrance
0 votes
    How can you lessen the number of SSH connections required? How can you lessen the number of SSH connections required? 1. become_method 2. forks 3. pipelining 4. accelerate_port...
asked Jan 26, 2023 in Education by JackTerrance
0 votes
    How can you reduce the number of SSH connections required? (i)forks (ii)pipelining (iii)accelerate_port (iv)become_method...
asked Jan 25, 2023 in Technology by JackTerrance
0 votes
    Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
...