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

-4.5%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
300 Gb
wKVM-HDD HK 4096 Windows

€ 17.67 /mo

€
/mo

Billed annually

-9.5%

CPU
CPU
8 Epyc Cores
RAM
RAM
32 GB
Space
Space
200 GB NVMe
Bandwidth
Bandwidth
Unlimited
wKVM-NVMe 32768 Windows

€ 74.49 /mo

€
/mo

Billed annually

-10%

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

€ 17.45 /mo

€
/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
wKVM-SSD 4096 Windows

€ 18.65 /mo

€
/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
KVM-SSD 8192 Linux

€ 25.85 /mo

€
/mo

Billed annually

-20.4%

CPU
CPU
2 Xeon Cores
RAM
RAM
2 GB
Space
Space
30 GB SSD
Bandwidth
Bandwidth
300 GB
KVM-SSD 2048 HK Linux

€ 18 /mo

€
/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
30 GB SSD
Bandwidth
Bandwidth
Unlimited
10Ge-KVM-SSD 2048 Linux

€ 30.3 /mo

€
/mo

Billed annually

-13.3%

CPU
CPU
2 Xeon Cores
RAM
RAM
1 GB
Space
Space
20 GB SSD
Bandwidth
Bandwidth
300 GB
KVM-SSD 1024 HK Linux

€ 11.33 /mo

€
/mo

Billed annually

-10%

CPU
CPU
3 Epyc Cores
RAM
RAM
2 GB
Space
Space
20 GB NVMe
Bandwidth
Bandwidth
Unlimited
aiKVM-NVMe 2048 Linux

€ 9.33 /mo

€
/mo

Billed annually

-9.3%

CPU
CPU
6 Epyc Cores
RAM
RAM
16 GB
Space
Space
150 GB NVMe
Bandwidth
Bandwidth
Unlimited
wKVM-NVMe 16384 Windows

€ 54.49 /mo

€
/mo

Billed annually

Other articles on this topic

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.