Accessing the MySQL database from Python
The Python developer creates the MySQLdb
module, which provides a utility to interact and manage the database from a Python script. This module can be installed using Python's pip
, or with an operating system package manager, such as yum
or apt
.
To install the package, use the following command:
yum install MySQL-python
Verify the installation as follows:
[root@AutomationServer ~]# python Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
Since the module has imported without any errors, we know that the Python module has successfully installed.
We will now access the database through the console and create a simple database called TestingPython
, with one table inside it. We will then connect to it from Python:
[root@AutomationServer ~]# mysql -u root -p Enter password: EnterpriseAutomation Welcome...