The editor below will bring you a tutorial on configuring mysql in Python (recommended). The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look.
Linux system comes with Python, and configure mysql for python according to the system’s own resources; installation requires that the correct yum source has been configured;
In python When mysql is not configured, the prompt for directly importing MySQLdb is as follows
>>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named MySQLdb
There is no rpm installation package for mysql-python in the Linux system. This resource needs to be downloaded from the Internet:
https://sourceforge.net/projects/mysql-python
Currently the mainstream uses Python2.6 or Python2.7 version, download it as MySQL-python-1.2.3c1 .tar.gz
After downloading, upload it to the Linux machine and put it in the non-Chinese directory
##tar -xf MySQL -python-1.2.3c1.tar.gz, the decompression directory is as follows
[root@localhost home]# cd MySQL-python-1.2.3c1/ [root@localhost MySQL-python-1.2.3c1]# ll 总用量 240 drwxr-xr-x. 5 root root 89 10月 12 12:27 build -rw-r--r--. 1 tianF enosoft 59580 3月 31 2009 ChangeLog drwxr-xr-x. 2 root root 57 10月 12 12:27 dist drwxr-xr-x. 2 tianF enosoft 58 3月 31 2009 doc -rw-r--r--. 1 tianF enosoft 9716 2月 6 2009 ez_setup.py -rw-r--r--. 1 tianF enosoft 17989 2月 25 2007 GPL -rw-r--r--. 1 tianF enosoft 2935 3月 4 2007 HISTORY -rw-r--r--. 1 tianF enosoft 605 2月 11 2007 MANIFEST -rw-r--r--. 1 tianF enosoft 272 3月 9 2009 MANIFEST.in -rw-r--r--. 1 tianF enosoft 2098 3月 31 2009 metadata.cfg -rw-r--r--. 1 tianF enosoft 75431 3月 31 2009 _mysql.c drwxr-xr-x. 3 tianF enosoft 211 10月 12 12:28 MySQLdb -rw-r--r--. 1 tianF enosoft 2306 4月 5 2006 _mysql_exceptions.py -rw-r--r--. 1 root root 3791 10月 12 12:28 _mysql_exceptions.pyc drwxr-xr-x. 2 tianF enosoft 90 3月 31 2009 MySQL_python.egg-info -rw-r--r--. 1 tianF enosoft 1755 3月 31 2009 PKG-INFO -rw-r--r--. 1 tianF enosoft 3203 4月 5 2006 pymemcompat.h -rw-r--r--. 1 tianF enosoft 6696 10月 17 2008 README -rw-r--r--. 1 tianF enosoft 380 3月 31 2009 setup.cfg -rw-r--r--. 1 tianF enosoft 951 3月 8 2009 setup_common.py -rw-r--r--. 1 root root 1520 10月 12 12:27 setup_common.pyc -rw-r--r--. 1 tianF enosoft 2947 3月 8 2009 setup_posix.py -rw-r--r--. 1 root root 2977 10月 12 12:27 setup_posix.pyc -rw-r--r--. 1 tianF enosoft 495 10月 18 2008 setup.py -rw-r--r--. 1 tianF enosoft 1547 3月 4 2007 setup_windows.py -rw-r--r--. 1 tianF enosoft 592 10月 17 2008 site.cfg drwxr-xr-x. 2 tianF enosoft 149 3月 31 2009 tests
Name | Source | Installation method |
The system comes with it | yum whatprovides python* Install the queried packages that match the keyword names one by one | |
The system comes with it | http://pypi.python.org/pypi/setuptools | Download, select the corresponding setuptools according to the python version version or use the built-in package yum install python-setuptools |
Download from the Internet |
If the command reports an error, it means that the yum source configuration is incorrect, or the installation CD does not match the system, etc. For details, please refer to the yum source configuration tutorial under Linux
[root@localhost mysql-python]# <strong>yum whatprovides mysql-devel</strong> Loaded plugins: product-id, refresh-packagekit, security, subscription-manager This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register. base | 3.9 kB 00:00 ... mysql-devel-5.1.66-2.el6_3.x86_64 : Files for development of MySQL applications Repo : base Matched from: mysql-devel-5.1.66-2.el6_3.i686 : Files for development of MySQL applications Repo : base Matched from: mysql-devel-5.1.66-2.el6_3.x86_64 : Files for development of MySQL applications Repo : installed Matched from: Other : Provides-match: mysql-devel mysql-devel-5.1.66-2.el6_3.i686 : Files for development of MySQL applications Repo : installed Matched from: Other : Provides-match: mysql-devel
[root@localhost mysql-python]# [root@localhost mysql-python]# 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 >>>
Common errors: python setup.py build Prompt that mysql_config cannot be found
This problem is because when building mysqldb, the mysql_config configured in the site.cfg file in the MySQL-python-1.2.3c1/ directory is used;
[root@localhost MySQL-python-1.2.3c1]# ls build doc HISTORY metadata.cfg _mysql_exceptions.py PKG-INFO setup.cfg setup_posix.py setup_windows.py ChangeLog ez_setup.py MANIFEST _mysql.c _mysql_exceptions.pyc pymemcompat.h setup_common.py setup_posix.pyc site.cfg dist GPL MANIFEST.in MySQLdb MySQL_python.egg-info README setup_common.pyc setup.py tests [root@localhost MySQL-python-1.2.3c1]# more site.cfg [options] # embedded: link against the embedded server library # threadsafe: use the threadsafe client # static: link against a static library (probably required for embedded) embedded = False threadsafe = True static = False # The path to mysql_config. # Only use this if mysql_config is not on your PATH, or you have some weird # setup that requires it. #mysql_config = /usr/local/bin/mysql_config # The Windows registry key for MySQL. # This has to be set for Windows builds to work. # Only change this if you have a different version. registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0 [root@localhost MySQL-python-1.2.3c1]#
#mysql_config Configure, uncomment the previous comment, and configure it to the correct address. For example
mysql_config = /usr/bin/mysql_config #(If not specified, the location of mysql_config defaults to the /usr/bin directory, and there are differences in different systems. , the actual location can be obtained by searching the file)
Verify the python-Mysql function
Configure the Mysql database as needed and modify the user name and password;View the basic syntax of python-mysql, link the mysql library of the mysql database, and obtain the user table information. The code is as follows;#!/usr/bin/python #encoding=utf8 import MySQLdb conn=MySQLdb.connect("127.0.0.1","root","123456","mysql") cursor=conn.cursor() cursor.execute("select * from user") getdata=cursor.fetchone() print "the user table content is:",getdata conn.close()
[root@localhost python]# python mysql-conn.py the user table content is: ('%', 'root', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '', '', '', 0L, 0L, 0L, 0L, <br>'mysql_native_password', '123456', 'Y', datetime.datetime(2017, 9, 14, 14, 40, 2), None, 'N') [root@localhost python]#
The above is the detailed content of Tutorial on configuring mysql in Python (must read). For more information, please follow other related articles on the PHP Chinese website!