9951 explained code solutions for 126 technologies


python-mysqlHow do I check the version of MySQL I am using with Python?


To check the version of MySQL you are using with Python, you can use the mysql-connector-python library. This library provides an interface to connect to MySQL databases.

The following example code block shows how to connect to a MySQL database and retrieve the version information:

import mysql.connector

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

# Create a cursor
my_cursor = mydb.cursor()

# Execute a query to retrieve the version
my_cursor.execute("SELECT VERSION()")

# Fetch the result
version = my_cursor.fetchone()

# Print the version
print(version)

The output of the above code is:

('8.0.19',)

The code consists of the following parts:

  1. import mysql.connector: This imports the MySQL Connector/Python library.
  2. mydb = mysql.connector.connect(...): This creates a connection to the MySQL database.
  3. my_cursor = mydb.cursor(): This creates a cursor object to execute queries.
  4. my_cursor.execute("SELECT VERSION()"): This executes the query to retrieve the version information from the database.
  5. version = my_cursor.fetchone(): This fetches the result of the query.
  6. print(version): This prints the version information.

For more information about the MySQL Connector/Python library, see the documentation.

Edit this code on GitHub