Python configuration MySQL tutorial (Must SEE)

Source: Internet
Author: User
Tags localhost mysql mysql tutorial python mysql
Here's a small piece to bring you a Python configuration MySQL tutorial (recommended). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

Linux system comes with Python, and according to the system comes with resources to configure the Python MySQL, installation needs to be configured with the correct Yum source;

In the case of Python not configuring MySQL, the direct import MySQLdb prompt is as follows


>>> Import MySQLdb Traceback (most recent): File "<stdin>", line 1, in <module> importerror : No module named MySQLdb

The Linux system does not have a mysql-python RPM installation package, this resource needs to be downloaded from the Internet:

Https://sourceforge.net/projects/mysql-python

Current mainstream use Python2.6 or Python2.7 version, download as mysql-python-1.2.3c1.tar.gz

Upload to a Linux machine and put in a non-Chinese directory after download

tar-xf mysql-python-1.2.3c1.tar.gz , unzip the directory as follows


[root@localhost home]# CD mysql-python-1.2.3c1/[root@localhost mysql-python-1.2.3c1]# ll total dosage 240drwxr-xr-x. 5 root root 89 October 12:27 build-rw-r--r--. 1 tianf enosoft 59580 March changelogdrwxr-xr-x. 2 root root 57 October 12:27 distdrwxr-xr-x. 2 tianf enosoft 58 March doc-rw-r--r--. 1 tianf enosoft 9716 February 6 ez_setup.py-rw-r--r--. 1 tianf enosoft 17989 February gpl-rw-r--r--. 1 tianf enosoft 2935 March 4 history-rw-r--r--. 1 tianf enosoft 605 February manifest-rw-r--r--. 1 tianf enosoft 272 March 9 Manifest.in-rw-r--r--. 1 tianf enosoft 2098 March metadata.cfg-rw-r--r--. 1 tianf enosoft 75431 March _mysql.cdrwxr-xr-x. 3 tianf enosoft 211 October 12:28 mysqldb-rw-r--r--. 1 tianf enosoft 2306 April 5 2006 _mysql_exceptions.py-rw-r--r--. 1 root root 3791 October 12:28 _mysql_exceptions.pycdrwxr-xr-x. 2 tianf enosoft 90 March mysql_python.egg-info-rw-r--r--. 1 tianf enosoft 1755 March pkg-info-rw-r--r--. 1 tianf enosoft 3203 April 5 2006 Pymemcompat.h-rw-r--r--. 1 tianf enosoft 6696 October readme-rw-r--r--. 1 tianf enosoft 380 March setup.cfg-rw-r--r--. 1 tianf enosoft 951 March 8 Setup_common.py-rw-r--r--. 1 root root 1520 October 12:27 setup_common.pyc-rw-r--r--. 1 tianf enosoft 2947 March 8 setup_posix.py-rw-r--r--. 1 root root 2977 October 12:27 setup_posix.pyc-rw-r--r--. 1 tianf enosoft 495 October setup.py-rw-r--r--. 1 tianf enosoft 1547 March 4 setup_windows.py-rw-r--r--. 1 tianf enosoft 592 October-Site.cfgdrwxr-xr-x. 2 tianf enosoft 149 March tests

You also need to install some dependencies before you configure Python-mysql, otherwise there will be a variety of errors missing

Name Source Installation method
Python-devel System comes with

Yum Whatprovides python*

Install a query to a package that matches the keyword name individually
Mysql-server
Mysql-devel
Setuptools System comes with Http://pypi.python.org/pypi/setuptools Download, select the corresponding Setuptools version according to the Python version or use the package yum install Python-setuptools
Mysql-python Network download

You can use the Yum whatprovides mysql-devel command to see if it is installed (the package name is Mysql-dev for older Linux systems, and if the mysql-devel hint is not found, use dev overrides):

If the command error indicates that the Yum source configuration is incorrect, or the installation disc does not match the system, please refer to the Linux Yum Source Configuration tutorial


[Root@localhost mysql-python]# <strong>yum whatprovides mysql-devel</strong>loaded Plugins:product-id, Refresh-packagekit, security, subscription-managerthis 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 Applicationsrepo  : basematched from:mysql-devel-5.1.66-2.el6_3.i686:files for development of MySQL Applicationsrepo  : basematched from:mysql-devel-5.1.66-2.el6_3.x86_64:files for development of MySQL Applica Tionsrepo  : installedmatched from:other  : Provides-match:mysql-devel mysql-devel-5.1.66-2.el6_3.i686:files For development of MySQL Applicationsrepo  : installedmatched from:other  : Provides-match:mysql-devel

As shown above, the repo value of installed indicates that the package is installed, that the main observation of the MYSQL-DEVEL keyword is installed, and that if it is not installed, enter the Yum install mysql-devel command installation

Install Mysql-devel, Python-devel, python-setuptools in turn, the installation process will continue without error;

After the installation of the above dependencies, go back to the Mysql-python extracted mysql-python-1.2.3c1/directory;

>> python setup.py Build

>> python setup.py Install

The above two commands are executed correctly, which indicates that the Python configuration mysqldb successfully, and again verify the import mysqldb error


[Root@localhost mysql-python]#[root@localhost mysql-python]# Pythonpython 2.7.5 (default, 4, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on Linux2type ' help ', ' copyright ', ' credits ' or ' license ' for more information.>> > Import mysqldb>>>

As indicated above, configuration MySQLdb succeeded

Common error: Python setup.py build hint could not find mysql_config

This problem occurs because the Mysql_config configured in the Site.cfg file in the mysql-python-1.2.3c1/directory is used when building mysqldb;


[Root@localhost mysql-python-1.2.3c1]# lsbuild doc history metadata.cfg _mysql_exceptions.py PKG-INFO setup.cfg Set up_posix.py setup_windows.pychangelog ez_setup.py MANIFEST _mysql.c _mysql_exceptions.pyc pymemcompat.h setup_ common.py setup_posix.pyc site.cfgdist GPL manifest.in mysqldb mysql_python.egg-info README setup_common.pyc setup.p Y 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 = Falsethreadsafe = Truestatic = False # The path to mysql_config.# only with the If Mysql_config is isn't on your path, or y The OU has some weird# setup that requires it. #mysql_config =/usr/local/bin/mysql_config # The Windows registry key for MYSQ L.# this had to is set for Windows builds to work.# only the change this if you have a different Version.registry_key = Softwa Re\mysql Ab\mysql Server 5.0[root@localhost mysql-python-1.2.3c1]# 

If the installation location of MySQL does not match the location configured in Site.cfg, you need to modify the #mysql_config configuration of the Site.cfg file, cancel the previous comment, and configure it as the correct address. For example

mysql_config =/usr/bin/mysql_config # (unspecified case, mysql_config location by default in the/usr/bin directory, different system differences, specifically by searching for files to get the actual location)

Verifying the Python-mysql feature

Configure the MySQL database as needed, and modify the user name and password;

View python-mysql basic syntax, link MySQL database MySQL library, get user table information, code as follows;


#!/usr/bin/python#encoding=utf8 Import mysqldbconn=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:", Getdataconn.close ()

The results of the implementation are as follows:


[root@localhost python]# python mysql-conn.pythe 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 (9, 2, A, a, a-N, +-), None [Root@localhost python]#

At this point, Python configures MySQL authentication to pass!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.