Connect MySQL database using MySQL-Connector Python

While working with Python we need to work with databases, they may be of different types like MySQL, SQLite, NoSQL, etc. In this article, we will be looking forward to how to connect MySQL databases 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 mysql.connector provides the connect() method used to create a connection between the MySQL database and the Python application. The syntax is given below.

Conn_obj= mysql.connector.connect(host = , user = , passwd = )

The 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.
DatabaseIt specifies the database name which we want to connect. This argument is used when we have multiple databases.

In the following example we will be connecting to MySQL database using connect()
Example: