windows10 下使用Pycharm2016 基于Anaconda3 Python3.6 安装Mysql驱动总结

本文记录:在PyCharm2016.3.3 中基于Anaconda3 Python3.6版本安装Python for Mysql驱动。尝试了安装Mysql-Connector成功,但是连接数据库时驱动有bug而失败;尝试安装MySQLdb,因Python版本兼容问题 失败。最终安装PyMysql成功。现将详细安装过程记录如下。

若不想折腾,且系统环境与我的一致,建议直接就安装PyMysql吧。

一,系统环境

windows10 64位,安装了Anaconda3 和PyCharm2016.3.3。此外,windows10下还自己安装了Python3.4版本(已配置好环境变量),如下图:

 

PyCharm中配置的Python解释器为Anaconda3自带的Python环境,如下图:

安装的Mysql版本如下:

mysql  Ver 14.14 Distrib 5.5.53, for debian-linux-gnu (x86_64) using readline 6.2

 

二,Python for Mysql driver 介绍

对于Python而言,有很多访问Mysql的驱动,主要是Mysqldb、mysqlconnector 和 pymysql(参考这篇文章)

MySQL-python: This package contains the MySQLdb module, which is written in C.
It is one of the most commonly used Python packages for MySQL. mysql-connector-python: This package contains the mysql.connector module, which is written entirely in Python. PyMySQL: This package contains the pymysql module, which is written entirely in Python.
It is designed to be a drop-in replacement for the MySQL-python package.

 

三,本地环境安装MySQL Connector/Python

MySQL Connector/Python官网下载:mysql-connector-python-2.1.7-py3.4-windows-x86-64bit.msi 双击安装成功。

cmd命令行下执行 import mysql.connector 成功,如下图:

 

但是,我想使用Anaconda3下自带的Python环境,而不想使用本地的Python3.4环境。因为Anaconda3中已经帮我们自带了很多第三方的依赖包,比如Python中的ORM框架:SQLAlchemy 。而下面也将 通过 “from sqlalchemy import create_engine”来测试Mysql数据库连接

于是,打开Anaconda Prompt,执行命令:

conda install -c anaconda mysql-connector-python 

安装 mysql-connector-python 成功。参考:how do i get mysql to work with python 3.6 in anaconda ide (spyder)? 

于是,开始欢喜满满地去测试是否能成功地连接数据库。

打开PyCharm,在菜单栏“Tools”--->“Python console”打开Python Console:

执行如下命令:

from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://username:password.@HOST_IP:PORT/db_name?charset=utf8')
conn = engine.connect()

报错如下:

sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) Failed parsing handshake; end byte not present in buffer

网上搜了一下,说是Bug,错误无法解决,参考这里。如能解决,请留言告知,感激不尽。

于是,尝试安装MySQLdb。参考了这篇文章,执行:conda install mysql-python,结果报错:The following specifications were found to be in conflict

说是MySQLdb不支持Python3.6版本。具体可参考这篇文章,于是又只得放弃。

The mysql-python conda package hasn't been built for python 3 . 
You can try creating a python 2.7 environment and install mysql-python into that.

再尝试:PyMysql,PyMysql项目的目标是替换MySQLdb

This package contains a pure-Python MySQL client library. 
The goal of PyMySQL is to be a drop-in replacement for MySQLdb and work on CPython, PyPy and IronPython.

在Anaconda Prompt中执行:conda install -c anaconda pymysql ,参考:这里

 

安装成功后,在PyCharm中,Ctrl+Alt+S 搜索“python”,打开project 解释器,可看到已成功安装 pymysql

于是可以使用PyMySQL 来测试连接Mysql数据库了。执行如下命令成功。(注意替换成你自己的数据库名)。至此,在PyCharm中基于 Anaconda3的Python3.6 终于可以访问Mysql数据库了。

from sqlalchemy import create_engine
engine=create_engine('mysql+pymysql://username:password.@HOST_IP:PORT/db_name?charset=utf8')
conn = engine.connect()
r = conn.execute('SELECT field1,field2 FROM DB_NMAE limit 1 ')

 

另外,顺便说一下,对于不同的驱动,连接Mysql的语句是不同的:具体可参考 sqlalchemy 官方文档

#MySQL-Python
mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>

#pymysql
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]

#mysqlconnector
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>

 

另外,还可以在Python Console中使用 %run 直接运行编写好的 .py 文件:如下图所示,在 audit包下,新建了一个my_file.py测试文件

#audit/my_file.py
s = "Hello World"
print(s)

 

原文:http://www.cnblogs.com/hapjin/p/7800959.html

posted @ 2017-11-07 20:43  大熊猫同学  阅读(5529)  评论(1编辑  收藏  举报