{"id":3856,"date":"2019-03-20T21:09:10","date_gmt":"2019-03-20T15:39:10","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=3856"},"modified":"2026-10-13T15:43:36","modified_gmt":"2026-10-13T10:13:36","slug":"connect-python-to-mysql","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/connect-python-to-mysql\/","title":{"rendered":"How to Connect Python to MySQL Database"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Post Date: March 20, 2019<br>Last Updated: October 13, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Learn how to <strong>connect Python to MySQL<\/strong> and perform essential database operations such as creating tables, inserting records, and retrieving data. In this tutorial, we will explain how to establish a database connection using the <code>MySQLdb<\/code> module and execute basic SQL queries in a Linux environment. The examples are based on CentOS 7 and cover the installation of Python packages, database connectivity, and common MySQL operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding with this tutorial, ensure that you have the following:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Linux operating system:<\/strong> A CentOS 7 or RHEL-based environment.<\/li>\n\n\n\n<li><strong>Python:<\/strong> Python must be installed on the server.<\/li>\n\n\n\n<li><strong>Pip:<\/strong> The Python package installer is required to install the necessary database connector packages.<\/li>\n\n\n\n<li><strong>MySQL server:<\/strong> MySQL must be installed and running, with a database and a user configured with the required privileges.<\/li>\n\n\n\n<li><strong>MySQLdb module:<\/strong> Install the <code>MySQL-python<\/code> package for compatible Python 2 environments. For Python 3, use a compatible alternative such as <code>mysqlclient<\/code>.<\/li>\n\n\n\n<li><strong>Basic knowledge:<\/strong> Familiarity with Python programming, MySQL, and SQL queries will help you follow the examples.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article helps you in  connect python with MySQldb and insert queries  in DB.\u00a0 This example is explained based on Linux environment in Centos 7.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>For centOS and RHEL  based environment:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># sudo yum install python<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> 2. Pip installation is most important for installing python packages<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># curl -O https:\/\/bootstrap.pypa.io\/get-pip.py<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Confirm the installed PIP version so that we will proceed our work further:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># pip \u2013version<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"> <br><strong>Importing Modules and their Pip Installation:<\/strong> <\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">While Importing, if the packaging Modules of&nbsp; any one among the above missed, will arise&nbsp;<strong>&nbsp;Import error.<\/strong> To eradicate the error follow these step by step modules to import&nbsp; using pip Installation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. Importing Mysqldb:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Yum install MYSQL-python<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4.png\"><img fetchpriority=\"high\" decoding=\"async\" width=\"470\" height=\"178\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4.png\" alt=\"\" class=\"wp-image-2989\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4.png 470w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/blog-4-300x114.png 300w\" sizes=\"(max-width: 470px) 100vw, 470px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">2. Importing mysql.connector:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install mysql-connector-python<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1.png\"><img decoding=\"async\" width=\"796\" height=\"169\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1.png\" alt=\"\" class=\"wp-image-2991\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1.png 796w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1-300x64.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2018\/11\/Blog1-768x163.png 768w\" sizes=\"(max-width: 796px) 100vw, 796px\" \/><\/a><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Create a table and insert into it<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n\n# Connect to db and create a table called blog that each have a name field and insert a couple blogs\n\nimport MySQLdb as mysqldb\n\nconnection = mysqldb.connect('localhost', 'bloguser', 'blogpw', 'blogdb');\n\nwith connection:\n    cursor = connection.cursor()\n    cursor.execute(\"create table blogs(id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(25))\")\n    cursor.execute(\"insert into blogs(name) values('author')\")\n cursor.execute(\"insert into blogs(name) values('editor')\")\n cursor.execute(\"insert into blogs(name) values('admin')\")\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>We can verify this on the MySQL side after running<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>mysql&gt; select * from blogs;\n+----+-----------+\n| id | name      |\n+----+-----------+\n| 2 | author    |\n| 3 | editor    |\n| 4 | admin     |\n+----+-----------+\n3 rows in set (0.00 sec)\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Select data from a table<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/usr\/bin\/env python\n\n# Connect to db and create a table called blogs that each have a name field and insert a couple blogs\n\nimport MySQLdb as mysqldb\n\nconnection = mysqldb.connect('localhost', 'bloguser', 'blogpw', 'blogdb');\n\nwith connection:\n    cursor = connection.cursor()\n    cursor.execute(\"select * from blogs\")\nblogs = cursor.fetchall()\n    for blog in blogs:\n        print(blog)\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\n\nThat&#8217;s it! Hope it&#8217;s useful.\n\n<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Connecting Python with MySQL enables developers to automate database operations and manage data efficiently through Python scripts. In this tutorial, we covered the basic steps to establish a database connection using the <code>MySQLdb<\/code> module, create a table, insert records, and retrieve data using SQL queries.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These fundamentals can be extended to build database-driven applications and automate routine database tasks. For production environments, use supported software versions, secure database credentials, parameterised queries for user-supplied data, and appropriate transaction handling to improve security and reliability. For more information about connecting Python applications to MySQL, refer to the official documentation <a href=\"https:\/\/dev.mysql.com\/doc\/connector-python\/en\" target=\"_blank\" rel=\"noopener\">Link<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. How can Python connect to a MySQL database?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Python can connect to MySQL using database connector libraries such as <code>MySQLdb<\/code>, <code>mysqlclient<\/code>, or <code>mysql-connector-python<\/code>. These libraries allow Python scripts to execute SQL queries and manage database operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. What is the MySQLdb module in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>MySQLdb<\/code> is a Python interface for connecting to MySQL databases. It is commonly associated with the <code>mysqlclient<\/code> library in Python 3 environments.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. How do I install the MySQLdb module?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For compatible Python 2 environments, you can install <code>MySQL-python<\/code> using the package manager. For Python 3, install <code>mysqlclient<\/code> using pip:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">pip install mysqlclient<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/configuring-mysql-to-listen-on-a-private-ip-address\/\">Configuring MySQL to Listen on a Private IP Address<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-copy-a-table-from-one-database-to-another-in-mysql\/\">How to Copy a Table from One Database to Another in MySQL?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Need Help with Python and MySQL Integration?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with Python and MySQL database connectivity, configuration, query execution, or database troubleshooting? Our experts can assist with Python application development, MySQL database administration, server optimization, Linux server management, DevOps, cloud solutions, and other infrastructure requirements. Get in touch with our team <a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: March 20, 2019Last Updated: October 13, 2026 Introduction Learn how to connect Python to MySQL and perform essential [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[586,179,585,292],"class_list":["post-3856","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-import-mysqldb","tag-mysql-2","tag-pip-installation","tag-python","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-10c","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=3856"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3856\/revisions"}],"predecessor-version":[{"id":12274,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/3856\/revisions\/12274"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=3856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=3856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=3856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}