Python MYSQLDB Linux Installation Notes

Source: Internet
Author: User


This article describes the Python mysqldb Linux installation notes, this article explains the rapid installation and manual compilation and installation of two methods, and explained the operation steps, the need for friends can refer to the next

Primarily for centos6.5 64-bit systems


The default Python version is 2.6

Coded installation of python2.7 and python3.4


First, yum Quick Install

Yum Install Mysql-python

Yum Install Python-setuptools

Regular contacts with Python may notice that the Easy_install command may be used when a third-party Python package needs to be installed. Easy_install is a command from the Setuptools package developed by the Peak (Python Enterprise application kit), so use Easy_ The install is actually calling Setuptools to complete the installation of the module.

Perl users are more familiar with CPAN, while Ruby users are more familiar with Gems, Ez_setup tools that guide Setuptools and the accompanying Easy_install and "Cheeseshop" (Python package Inde X, also known as "PyPI") work together to achieve the same functionality. It is easy for you to automatically download, compile, install and manage Python packages.


However, the Yum installation will be installed by default in the appropriate python2.6 directory.


Second, in the python2.7 source package installation

1. Need:
a.gcc
B.setuptools

Download and install Setuptools

Https://pypi.python.org/pypi/setuptools

The recommended-bootstrap setuptools on any system was to downloadez_setup.py and run it using the target Python env Ironment. Differentoperating Systems has different recommended techniques to accomplish thisbasic routine, so below is some exampl Es to get started.

Download ez_setup.py according to its own version of the execution:

Python27 ez_setup.py read python configuration and download Setuptools-17.1.1.zip

After decompression execute:

Python27 setup.py Build

Python27 setup.py Install

Modify according to the error

2. Download and install MySQLdb:

Download http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

After decompression execute:

Python27 setup.py Build

Python27 setup.py Install

Note: This module does not support the python3.4 version.


Third, in the python3.4 source Package Installation

In python3.4 using the original python2.7 MySQLdb can not connect to the MySQL database, the use of pymysql, to complete the task of connecting MySQL

Https://github.com/PyMySQL/PyMySQL


Download to perform after decompression

python27 setup.py Build

Python27 setup.py Install


Example:

The following examples make use of a simple table

CREATE TABLE ' users ' (
' id ' int (one) not NULL auto_increment,
' Email ' varchar (255) COLLATE Utf8_bin not NULL,
' Password ' varchar (255) COLLATE Utf8_bin not NULL,
PRIMARY KEY (' id ')
) Engine=innodb DEFAULT Charset=utf8 Collate=utf8_bin
Auto_increment=1;

Import Pymysql.cursors

# Connect to the database
Connection = pymysql.connect (host= ' localhost ',
user= ' user ',
Passwd= ' passwd ',
db= ' db ',
Charset= ' Utf8mb4 ',
Cursorclass=pymysql.cursors.dictcursor)

Try
With Connection.cursor () as cursor:
# Create a new record
sql = "INSERT into ' users ' (' email ', ' password ') VALUES (%s,%s)"
Cursor.execute (SQL, (' [email protected] ', ' Very-secret '))

# connection is no autocommit by default. So-must commit to save
# your changes.
Connection.commit ()

With Connection.cursor () as cursor:
# Read a single record
sql = "Select ' id ', ' password ' from ' users ' WHERE ' email ' =%s"
Cursor.execute (SQL, (' [email protected] ',))
result = Cursor.fetchone ()
Print (Result)
Finally
Connection.close ()

This example would print:

{' Password ': ' Very-secret ', ' ID ': 1}










This article is from the "Autumn Fairy tale" blog, please be sure to keep this source http://wushank.blog.51cto.com/3489095/1663900

Python MYSQLDB Linux Installation Notes

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.