A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/connect-mysql-database-using-mysql-connector-python/ below:

Connect MySQL database using MySQL-Connector Python

Connect MySQL database using MySQL-Connector Python

Last Updated : 19 Jul, 2025

MySQL Connector module of Python is used to connect MySQL databases with the Python programs, it does that using the Python Database API Specification v2.0 (PEP 249). It uses the Python standard library and has no dependencies.

Connecting to the Database

The mysql.connector library provides the connect() method, which is used to establish a connection between the MySQL database and a Python application. This allows you to connect to both local and cloud-based MySQL databases effortlessly.

Syntax:

Conn_obj = mysql.connector.connect(
host=<hostname>,
user=<username>,
passwd=<password>,
database=<database_name> # optional

)

connect() function accepts the following arguments.

In the following example we will be connecting to MySQL database using connect() method.

1. Connecting to a Local MySQL Database

Here’s how to connect to a local MySQL database using the mysql.connector.connect() method:

Python
import mysql.connector

# Connecting from the server
conn = mysql.connector.connect(user = 'username',
                               host = 'localhost',
                              database = 'database_name')
print(conn)

# Disconnecting from the server
conn.close()

Output:

Also for the same, we can use connection.MySQLConnection() class instead of connect():

Example:

Python
from mysql.connector import connection

# Connecting to the server
conn = connection.MySQLConnection(user = 'username', 
                              host = 'localhost',
                              database = 'database_name')

print(conn)

# Disconnecting from the server
conn.close()

Output:

Another way is to pass the dictionary in the connect() function using '**' operator:
Example:

Python
from mysql.connector import connection

dict = {
  'user': 'root',
  'host': 'localhost',
  'database': 'College'
}
# Connecting to the server
conn = connection.MySQLConnection(**dict)

print(conn)

# Disconnecting from the server
conn.close()

Output:

Connecting to an Online MySQL Database

You can easily switch from connecting to a local MySQL database to an online MySQL database. All you need to do is modify the host parameter to the IP address or hostname of your online database server.

Example

Python
import mysql.connector

dataBase = mysql.connector.connect(
  host="your-cloud-database-host",  # Replace with your cloud database host
  user="your-username",         
  passwd="your-password",          
  database="your-database-name"    
)

dataBase.close()

By changing the host value to point to the IP address or hostname of the cloud database, you can switch easily between a local and a cloud MySQL database without changing much of your code.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4