0% found this document useful (0 votes)
13 views4 pages

Connect MySQL with Python Connector

The document explains how to connect to MySQL databases using the MySQL Connector module in Python, which adheres to the Python Database API Specification v2.0. It details the use of the connect() method to establish connections to both local and cloud-based MySQL databases, including example code snippets. Additionally, it describes alternative methods for connection, such as using the MySQLConnection class and passing a dictionary of connection parameters.

Uploaded by

archanaloguraje
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Connect MySQL with Python Connector

The document explains how to connect to MySQL databases using the MySQL Connector module in Python, which adheres to the Python Database API Specification v2.0. It details the use of the connect() method to establish connections to both local and cloud-based MySQL databases, including example code snippets. Additionally, it describes alternative methods for connection, such as using the MySQLConnection class and passing a dictionary of connection parameters.

Uploaded by

archanaloguraje
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Connect MySQL database using MySQL-Connector

Python

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 [Link] library provides
the connect() method, which is used to establish a
connection between the MySQL database and a Python
application. This allows us to connect to
both local and cloud-based MySQL databases effortlessly.
Syntax:
Conn_obj = [Link](
host=<hostname>,
user=<username>,
passwd=<password>,
database=<database_name> # optional
)
connect() function accepts the following arguments.
 Hostname: It represents the server name or IP address

on which MySQL is running.


 Username: It represents the name of the user that we

use to work with the MySQL server. By default, the


username for the MySQL database is root.
 Password: The password is provided at the time of
installing the MySQL database. We don't need to pass a
password if we are using the root.
 Database: It specifies the database name which we want

to connect. This argument is used when we have multiple


databases.
In the following example 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 [Link]() method:
import [Link]

# Connecting from the server


conn = [Link](user = 'username',
host = 'localhost',
database = 'database_name')
print(conn)

# Disconnecting from the server


[Link]()
Output:

Also for the same, we can use


[Link]() class instead of connect():
Example:
from [Link] import connection

# Connecting to the server


conn = [Link](user = 'username',
host = 'localhost',
database = 'database_name')
print(conn)

# Disconnecting from the server


[Link]()
Output:

Another way is to pass the dictionary in


the connect() function using '**' operator:
Example:
from [Link] import connection

dict = {
'user': 'root',
'host': 'localhost',
'database': 'College'
}
# Connecting to the server
conn = [Link](**dict)

print(conn)

# Disconnecting from the server


[Link]()
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
import [Link]

dataBase = [Link](
host="your-cloud-database-host", # Replace with your cloud
database host
user="your-username",
passwd="your-password",
database="your-database-name"
)

[Link]()
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.

You might also like