0

I am able to connect to my database with sql workbench. But when I am trying to connect with python code, I am getting confused. I have found some similar codes on internet and stack over flow but I am not able to connect with code.

How can I connect to the DB with the following details? Using python code.

mySQL workbench connection

EDIT:: I also want to know how to connect to TCP/IP over ssh. details shown in below image.

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44
Ashutosh Yadav
  • 333
  • 1
  • 12

1 Answers1

2

You can use the python-mysql driver

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  port="2121",
  user="yourusername",
  password="yourpassword"
)

print(mydb)

This doc explains it in details.

Connecting VIA SSH is descibed here - Enable Python to Connect to MySQL via SSH Tunnelling

vega77
  • 52
  • 3
  • its not working – Ashutosh Yadav Nov 02 '22 at 12:47
  • @AshutoshYadav what is the error you are seeing? – vega77 Nov 02 '22 at 12:48
  • 1
    The port (and maybe database) is missing. It should be something like: mysql.connect.connect(host="123.45.67.89", port=2121, username="my_user_name", password="...", database="su") – Nuno Mariz Nov 02 '22 at 13:17
  • @AshutoshYadav yes.. Like mentioned above, include the port as well if needed. Also post the errors which will be useful. – vega77 Nov 02 '22 at 13:55
  • Thanks @Nuno It worked. I have edited the question. I am also looking to connect to TCP/IP over ssh as shown in image. please look into it and help if possible. Thanks. – Ashutosh Yadav Nov 02 '22 at 14:28
  • @AshutoshYadav I think this is what you are looking for - https://stackoverflow.com/questions/21903411/enable-python-to-connect-to-mysql-via-ssh-tunnelling – vega77 Nov 02 '22 at 15:56