How to connect MYSQL Database with python using pycharm IDE

In this blog, we will see about how to connect a database to python using PyCharm IDE.

Here we are going to connect mysql database to python. In the similar fashion we can connect any database software to python.

Here are the pre-requisite needed for establishing connection between python and mysql or any database

PRE-REQUISITE:

  1. Adding database plugin in Pycharm
  2. Install mysql-connector packages

Adding database Plugin:

Database plugin supports addition of packages to support the connection between database and python.Below are the steps to add the database plugin.

i) Select PyCharm-> Preferences or select Settings options from the IDE

 ii) Select Plugins sections on the left pane of pop up dialog box.The list of plugins available from Market place will be displayed like csv , UI themes etc. As per our needs we can add the appropriate plugins.

iii) Type database in search box and install the database navigator plugin.

Installing MySQL Python connector packages:

MySql connector packages need to be installed to establish connection between mysql and python. Follow the below steps to install the same using Pycharm.

i) Go to File -> New Preferences of the project. Select Project Interpreter to add the package.

 ii) Select the “+” icon to install the package. Search the mysql python connector packages and install them.

iii)Install mysql-connector and mysql-connector-python packages.

Adding new connection to connect MySQL Database

This step is essential to setup a connection to access the mysql database from python.

In this process we will need the database details we are going to connect like hostname, port, username and password.

i) Go to View -> DB Browser . In the left pane you will be able to view the DB Browser section. 

ii) Select “+” icon to Add new connection. Then select the database going to be accessed.

Here we are going to connect mysql database server. The DB – Navigator settings dialog box will pop up .

iii)Provide the mysql db connection details as below

   * Hostname

   * Port

   * Username and Password

Then select Test Connection for success status  and select OK. 

iv) After the connection is set up . The database schema and corresponding table details will be shown in the DB Browser section in the left pane.