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
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

-10%

CPU
CPU
10 Epyc Cores
RAM
RAM
64GB
Space
Space
400 GB NVMe
Bandwidth
Bandwidth
Unlimited
Keitaro KVM 65536
OS
CentOS
Software
Software
Keitaro

149.04 /mo

/mo

Billed annually

-13.1%

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

7 /mo

/mo

Billed annually

-21.4%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
500 GB
wKVM-SSD 8192 HK Windows

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
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

-5.6%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
60 GB HDD
Bandwidth
Bandwidth
Unlimited
wKVM-HDD 2048 Windows

13.7 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
MT5 KVM 4096 Windows

19.99 /mo

/mo

Billed annually

-9.2%

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

72 /mo

/mo

Billed annually

-10%

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

8.8 /mo

/mo

Billed annually

Other articles on this topic

KVM VPS SSD-powered now
KVM VPS SSD-powered now
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.