9951 explained code solutions for 126 technologies


python-mysqlHow do I connect to a MySQL database using Python?


To connect to a MySQL database using Python, you need to use a database connector such as MySQL Connector/Python. This connector provides Python with access to the MySQL server.

The following example code connects to a MySQL database using MySQL Connector/Python:

import mysql.connector

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

print(mydb)
# Output: <mysql.connector.connection_cext.CMySQLConnection object at 0x7f8d5f8f8e48>

Code explanation

  1. import mysql.connector - import the MySQL Connector/Python module.
  2. mydb = mysql.connector.connect(host="localhost", user="user", passwd="password", database="mydatabase") - Connect to the MySQL database using the connect() method. Replace the parameters with the correct values for your database.
  3. print(mydb) - Print out the connection object.

For further information, please refer to the MySQL Connector/Python documentation: https://dev.mysql.com/doc/connector-python/en/

Edit this code on GitHub