9951 explained code solutions for 126 technologies


python-mysqlHow can I establish a connection between Python and MySQL?


Establishing a connection between Python and MySQL can be done using the MySQL Connector/Python package. This package allows for Python to communicate with a MySQL database. Here is an example of a code block to connect to a MySQL database:

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="user",
  passwd="passwd"
)

print(mydb)

This code will output a MySQL connection object if the connection is successful.

The code is broken down as follows:

  1. import mysql.connector: imports the MySQL Connector/Python package
  2. mydb = mysql.connector.connect(...): creates a connection object, mydb, using the connect() method
  3. host="localhost": specifies the host as localhost
  4. user="user": specifies the username
  5. passwd="passwd": specifies the password
  6. print(mydb): prints the MySQL connection object

For more information and examples of using the MySQL Connector/Python package, please refer to the MySQL Connector/Python documentation.

Edit this code on GitHub