9951 explained code solutions for 126 technologies


python-mysqlHow can I reconnect to a MySQL database using Python?


To reconnect to a MySQL database using Python, you can use the MySQL Connector/Python library. This library provides an API for connecting to a MySQL database. The following example code will connect to a MySQL database and print out the version of the database:

import mysql.connector

# Connect to the database
db = mysql.connector.connect(
    host="localhost",
    user="yourusername",
    passwd="yourpassword"
)

# Print out the version of the database
print(db.get_server_info())

Output example

8.0.20

The code above consists of the following parts:

  1. Import the MySQL Connector/Python library: import mysql.connector
  2. Connect to the database: db = mysql.connector.connect(host="localhost", user="yourusername", passwd="yourpassword")
  3. Print out the version of the database: print(db.get_server_info())

For more information about MySQL Connector/Python, see the following links:

Edit this code on GitHub