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

1
  • Can you share the Python code you are using? Commented Nov 2, 2022 at 14:12

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

its not working
@AshutoshYadav what is the error you are seeing?
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")
@AshutoshYadav yes.. Like mentioned above, include the port as well if needed. Also post the errors which will be useful.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.