Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

401
Views
how to connect to mysql via ssh by Python

I have check these answers in StackOverflow but they don't work for me: A1, A2 and A3.

I write my code follows as these answers and got'(1045, "Access denied for user 'root'@'localhost' (using password: YES)")'. Why is that?

my code here:

first try:

from sshtunnel import SSHTunnelForwarder

with SSHTunnelForwarder(
      ('****.ucd.ie', 22),
      ssh_password="***",
      ssh_username="s***",
      remote_bind_address=('127.0.0.1', 3306)) as server:

     con = pymysql.connect(user='root',passwd='***',db='dublinbus',host='127.0.0.1',port=3306)

second try:

from sshtunnel import SSHTunnelForwarder
import pymysql
import pandas as pd

tunnel = SSHTunnelForwarder(('****.ucd.ie', 22), ssh_password='***', ssh_username='s***t',
     remote_bind_address=('127.0.0.1', 3306)) 
tunnel.start()
conn = pymysql.connect(host='127.0.0.1', user='root', passwd='****', port=3306)
data = pd.read_sql_query("SHOW DATABASES;", conn)

third try:

from sshtunnel import SSHTunnelForwarder
import pymysql

with SSHTunnelForwarder(
    ('****.ucd.ie',22),  
    ssh_password='****',
    ssh_username='s****t',
    remote_bind_address=('127.0.0.1', 3306)) as server:  
 
    db_connect = pymysql.connect(host='127.0.0.1',  
                                 port=3306,
                                 user='root',
                                 passwd='****',
                                 db='dublinbus')
 
    cur = db_connect.cursor()
    cur.execute('SELECT stop_num FROM dublinbus.stops limit 10;')
    data=cur.fetchone()
    print(data[0])

All of them give me: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")

So how could I connect to my remote Mysql via SSH?

Moreover, I use the same 'ssh hostname' and 'mysql hostname' 'mysql server port' in my local mysql workbench. I donot know whether it will have some impact or not.

#I use *** to replace some private information.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

this could work:

from sshtunnel import SSHTunnelForwarder
import pymysql
import pandas as pd

server = SSHTunnelForwarder(
ssh_address=('****', 22),
ssh_username="****",
ssh_password="****",
remote_bind_address=("127.0.0.1", 3306))

server.start()

con = pymysql.connect(user='root',passwd='***',db='****',host='127.0.0.1',port=server.local_bind_port)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!