In this article, I have discussed how to connect to mysql database remotely using python. For any application it is very important to store database on a server for easy data access . It is quite complicated to connect to database remotely because every service provider don’t provide remote access to mysql database. Here I am using python’s MySQLdb module for connecting to our database which is at any server that provide remote access.
What is MYSQLdb?
MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 and is built on top of the MySQL C API.
Packages to Install
If using anaconda
conda install -c anaconda mysql-python conda install -c anaconda mysql-connector-python
pip install MySQL-python pip install MySQL-python-connector
How to connect to remote mysql database using python ?
Before we start you should know basics of SQL. Now let us discuss methods used in this code:
- connect() : This method is used for creating a connection to our database it have four arguments:
- Server Name
- Database User Name
- Database Provider
- Database Name
- cursor() : This method creates a cursor object that is capable for executing sql query on database.
- execute() : This method is used for executing sql query on database. It takes a sql query( as string) as an argument.
- fetchone() : This method retrieves the next row of a query result set and returns a single sequence, or None if no more rows are available.
- close() : This method close the database connection.
Connected Today's Date Is 2017-11-14
Thanks to Shaurya Uppal for useful improvements.
- Oracle Database Connection in Python
- Connection Between Eigenvectors and Nullspace
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.