Python configuration mysql tutorial (recommended), pythonmysql

Source: Internet
Author: User
Tags localhost mysql mysql tutorial python mysql

Python configuration mysql tutorial (recommended), pythonmysql

Python is provided in Linux and mysql is configured for python based on the system's built-in resources. The correct yum source must be configured for installation;

If mysql is not configured in python, the following prompt is displayed when you directly import MySQLdb:

 >>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named MySQLdb

The rpm installation package for mysql-python is not available in Linux. You need to download this resource from the Internet:

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

Currently, Python2.6 or Python2.7 is widely used.MySQL-python-1.2.3c1.tar.gz

Download the package and upload it to the Linux machine. Put it in a non-Chinese directory.

Tar-xf MySQL-python-1.2.3c1.tar.gz, Decompress the directory as follows:

[Root @ localhost home] # cd MySQL-python-1.2.3c1/[root @ localhost MySQL-python-1.2.3c1] # ll total usage 240drwxr-xr-x. 5 root 89 October 12 12:27 build-rw-r --. 1 tianF enosoft 59580 March 31 2009 ChangeLogdrwxr-xr-x. 2 root 57 October 12 12:27 distdrwxr-xr-x. 2 tianF enosoft 58 August 2009 doc-rw-r --. 1 tianF enosoft 9716 2009 ez_setup.py-rw-r -- r --. 1 tianF enosoft 17989 February 25 2007 GPL-rw-r --. 1 tianF enosoft 2935 March 4 2007 HISTORY-rw-r --. 1 tianF enosoft 605 February 11 2007 MANIFEST-rw-r --. 1 tianF enosoft 272 March 9 2009 MANIFEST. in-rw-r --. 1 tianF enosoft 2098 March 31 2009 metadata. cfg-rw-r --. 1 tianF enosoft 75431 2009 _ mysql. cdrwxr-xr-x. 3 tianF enosoft 211 12:28 MySQLdb-rw-r --. 1 tianF enosoft 2306 April 5 2006 _ mysql_exceptions.py-rw-r -- r --. 1 root 3791 October 12 12:28 _ mysql_exceptions.pycdrwxr-xr-x. 2 tianF enosoft 90 March 31 2009 MySQL_python.egg-info-rw-r -- r --. 1 tianF enosoft 1755 March 31 2009 PKG-INFO-rw-r --. 1 tianF enosoft 3203 April 5 2006 pymemcompat. h-rw-r --. 1 tianF enosoft 6696 October 17 2008 README-rw-r --. 1 tianF enosoft 380 March 31 2009 setup. cfg-rw-r --. 1 tianF enosoft 951 2009 setup_common.py-rw-r -- r --. 1 root 1520 October 12 12:27 setup_common.pyc-rw-r -- r --. 1 tianF enosoft 2947 2009 setup_posix.py-rw-r -- r --. 1 root 2977 October 12 12:27 setup_posix.pyc-rw-r -- r --. 1 tianF enosoft 495 October 18 2008 setup. py-rw-r --. 1 tianF enosoft 1547 2007 setup_windows.py-rw-r -- r --. 1 tianF enosoft 592 October 17 2008 site. optional drwxr-xr-x. 2 tianF enosoft 149 March 31 2009 tests

Before configuring python-mysql, you also need to install some dependencies; otherwise, various errors may be missing.

Name Source Installation Method
Python-devel Built-in System

Yum whatprovides python *

Install the packages that match the keyword names one by one
Mysql-server
Mysql-devel
Setuptools Built-in System Download the http://pypi.python.org/pypi/setuptools, select the corresponding setuptools version based on the python version or use the built-in package yum install python-setuptools
MySQL-python Network download

You can run the yum whatprovides mysql-devel command to check whether the package is installed. (The package name in the old Linux system is mysql-dev. If mysql-devel cannot be found, use dev instead ):

If the command reports an error, it indicates that the yum source configuration is incorrect, or the installation disc does not match the system. For details, refer to the yum source configuration tutorial in Linux.

[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 applicationsRepo  : 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, if the Repo value is installed, it indicates that the package has been installed. Check whether the mysql-devel keyword package has been installed. If not, enter the yum install mysql-devel command to install the package.

Install mysql-devel, python-devel, and python-setuptools in sequence. If no error is reported during installation, continue;

After the preceding dependencies are installed, return to the MySQL-python-1.2.3c1/directory decompressed by MySQL-python;

> Python setup. py build

> Python setup. py install

If the preceding two commands are correctly executed, mysqldb is successfully configured in python, and an error is returned after MySQLdb is imported.

[root@localhost mysql-python]#[root@localhost mysql-python]# pythonPython 2.7.5 (default, Aug 4 2017, 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>>>

The above indicates that MySQLdb is successfully configured.

Common error: python setup. py build prompts that mysql_config cannot be found

This problem is because when building mysqldb, use the mysql_config configured in the site. cfg file in the MySQL-python-1.2.3c1/directory;

[root@localhost MySQL-python-1.2.3c1]# lsbuild  doc   HISTORY  metadata.cfg _mysql_exceptions.py PKG-INFO  setup.cfg   setup_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.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 = Falsethreadsafe = Truestatic = 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]#

If the installation location of mysql is inconsistent with that configured in site. cfg, you must modify# Mysql_configConfigure, cancel the preceding comments, and set the address to the correct one. For example

Mysql_config =/usr/bin/mysql_config# (If not specified, the location of mysql_config is in the/usr/bin directory by default. Different systems may have differences. You can find the actual location by searching for files)

Verify the python-Mysql Function

Configure the Mysql database as needed and modify the user name and password;

View the basic python-mysql syntax and link to the mysql database of the mysql database to obtain the user table information. The Code is 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 execution result is 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(2017, 9, 14, 14, 40, 2), None, 'N')[root@localhost python]#

So far, Python has passed Mysql configuration verification!

The above Python configuration mysql tutorial (recommended) is a small series of Content shared to everyone, I hope to give you a reference, but also hope you can support a lot of homes.

Related Article

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.