Home > Backend Development > Python Tutorial > Tutorial on configuring mysql in Python (must read)

Tutorial on configuring mysql in Python (must read)

黄舟
Release: 2017-10-13 10:57:58
Original
2083 people have browsed it

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
Copy after login

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
Copy after login

Before configuring python-mysql, also Some dependencies need to be installed; otherwise various missing errors will be reported

python-develThe system comes with itmysql-servermysql-develsetuptoolsThe system comes with ithttp://pypi.python.org/pypi/setuptoolsMySQL-pythonDownload from the Internet
NameSource Installation method
yum whatprovides python*

Install the queried packages that match the keyword names one by one

Download, select the corresponding setuptools according to the python version version or use the built-in package yum install python-setuptools

You can use the yum whatprovides mysql-devel command to check whether it has been installed (the package name under the old version of Linux system is mysql-dev, if mysql-devel prompts that it cannot be found, use dev instead):

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
Copy after login

As shown above, if the Repo value is installed, it means it has been installed; mainly observe whether the package of the mysql-devel keyword has been installed; if not, enter the yum install mysql-devel command to install it

Install mysql-devel, python-devel, python-setuptools in sequence, and the installation process will continue if no errors are reported;

After the installation of the above dependencies is completed, return to MySQL-python-1.2.3c1 extracted from MySQL-python /directory;

>> python setup.py build

>> python setup.py install

If the above two commands are executed correctly, it means python configuration mysqldb is successful, verify again whether an error is reported when importing MySQLdb


[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
>>>
Copy after login

The above indicates that the configuration of MySQLdb is successful

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]#
Copy after login

If the installation location of mysql does not match the location configured in site.cfg, you need to modify the site.cfg file

#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()
Copy after login

The execution results are as follows :


[root@localhost python]# python mysql-conn.py
the user table content is: (&#39;%&#39;, &#39;root&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;Y&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, &#39;&#39;, 0L, 0L, 0L, 0L, <br>&#39;mysql_native_password&#39;, &#39;123456&#39;, &#39;Y&#39;, datetime.datetime(2017, 9, 14, 14, 40, 2), None, &#39;N&#39;)
[root@localhost python]#
Copy after login
At this point, the Python configuration Mysql verification has passed!

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template