Python + MySQL: How to Connect Using MySQL Connector

watch 2m, 46s
views 2

16:19, 31.08.2026

Article Content
arrow

  • Establishing a Connection to the Database
  • Establishing connection to a local database
  • Establishing connection to online database

Here is the article, where we will explain how to connect MySQL databases with Python programs. This task can be easily done with the help of the Connector module, which uses only the Python standard library, and there is no need for any specific dependencies.

Establishing a Connection to the Database

MySQL Connector can be used for the connection of cloud-based as well as local databases in the most intuitive and simple way you can ever imagine. In mysql.connector library, there is a method which we will be using called connect().

Here is the standard syntax for the connect() function:  

conn_obj = mysql.connector.connect(

     host="localhost", # Change to your hostname or IP

     user="your_username", # Replace with your MySQL username

     password="your_password", # Replace with your MySQL password

     database="your_database" # Optional: use only if you're connecting to a specific DB

)

Let’s explain some of the next arguments that you should change in the code above:

  • localhost: should be changed to IP or server name where the database is running.
  • your_username: by default, it is usually root.
  • your_password: in case you are using root, there is no necessity to use a password.

Establishing connection to a local database

Here is an example of how a connection can be made to a local MySQL database:

# Connecting to the MySQL server

conn = mysql.connector.connect(

     user='username',

     host='localhost',

     password='your_password',   # Add password if required

     database='database_name'

)

 

if conn.is_connected():

     print("Successfully connected to the database.")

     print("Connection object:", conn)

Also, it is possible to use MySQLConnection() instead of the above-mentioned variant. In such a scenario, the code will be as follows:

# Connecting to the MySQL server

conn = connection.MySQLConnection(

     user='username',

     host='localhost',

     password='your_password',   # Add password if required

     database='database_name'

)

 

if conn.is_connected():

     print("Successfully connected to the database.")

     print("Connection object:", conn)

One more possible variant is the usage of the ‘**’ operator, as in here:

db_config = {

'user': 'root',

'host': 'localhost',

'password': 'your_password',  # Add password if needed

'database': 'College'

}

 

try:

# Connecting to the server

conn = connection.MySQLConnection(**db_config)

 

if conn.is_connected():

     print("Connected to the MySQL database.")

     print("Connection object:", conn)

Establishing connection to online database

Connection to the online database is almost identical to the local connections. Here, you should just change the parameter so that the online server will be mentioned here.

dataBase = mysql.connector.connect(

     host="your-cloud-database-host",   # e.g., "db.example.com"

     user="your-username",

     passwd="your-password",

     database="your-database-name"

)

 

if dataBase.is_connected():

     print("Successfully connected to the cloud database.")

By just changing the value, it is possible to easily switch between cloud and local database, and there is no need to write new code all the time.

Share

Was this article helpful to you?

VPS popular offers

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-KVM-SSD 4096 Linux

60.5 /mo

/mo

Billed annually

-5.3%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
1 TB
wKVM-SSD 1024 Metered Windows

15.67 /mo

/mo

Billed annually

-22.2%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
50 GB SSD
Bandwidth
Bandwidth
300 GB
KVM-SSD 4096 HK Linux

33 /mo

/mo

Billed annually

-10%

CPU
CPU
2 Xeon Cores
RAM
RAM
512 MB
Space
Space
10 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 512 Linux

5.2 /mo

/mo

Billed annually

-8.9%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
400 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 16384 Windows

56 /mo

/mo

Billed annually

-12.3%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-wKVM-SSD 16384 Windows

237 /mo

/mo

Billed annually

-5.3%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
60 GB HDD
Bandwidth
Bandwidth
300 Gb
wKVM-HDD HK 2048 Windows

11.64 /mo

/mo

Billed annually

-9.9%

CPU
CPU
4 Epyc Cores
RAM
RAM
4 GB
Space
Space
50 GB NVMe
Bandwidth
Bandwidth
Unlimited
aiKVM-NVMe 4096 Linux

16.8 /mo

/mo

Billed annually

-21%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
8 TB
wKVM-SSD 8192 Metered Windows

65 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Epyc Cores
RAM
RAM
8 GB
Space
Space
100 GB NVMe
Bandwidth
Bandwidth
Unlimited
wKVM-NVMe 8192 Windows

28.99 /mo

/mo

Billed annually

Other articles on this topic

CloudFlare for your website
CloudFlare for your website
cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.