This document records the installation of the python for MySQL driver in PyCharm2016.3.3 based on the Anaconda3 Python3.6 version. An attempt was made to install mysql-connector successfully, but the driver has a bug while connecting to the database, and attempting to install MYSQLDB fails due to a Python version compatibility issue. Final installation Pymysql successful. The detailed installation process is now documented below.
If you do not want to toss, and the system environment and my consistent, it is recommended to install Pymysql directly.
One, the system environment
WINDOWS10 64-bit, with Anaconda3 and PyCharm2016.3.3 installed. In addition, WINDOWS10 also installed its own Python3.4 version (environment variables are configured), such as:
The Python interpreter configured in Pycharm is a python environment that comes with Anaconda3, such as:
The MySQL version installed is as follows:
MySQL Ver 14.14 distrib 5.5.53, for Debian-linux-gnu (x86_64) using ReadLine 6.2
Two, Python for Mysql driver Introduction
For Python, there are many drivers for accessing MySQL, mainly MySQLdb, Mysqlconnector, and Pymysql (refer to this article)
mysqldb module, which is written in C.
Mysql.connector
It is designed to be a drop-in replacement for the Mysql-python package.
Three, local environment install MySQL Connector/python
Download on MySQL Connector/python website: mysql-connector-python-2.1.7-py3.4-windows-x86-64bit.msi Double click to install successfully.
The cmd command line executes the import Mysql.connector successfully, such as:
However, I want to use the Python environment that comes with Anaconda3 instead of using the local Python3.4 environment. Because Anaconda3 has helped us bring in a lot of third-party dependencies, such as the ORM framework in Python: SQLAlchemy. The following will also test the MySQL database connection via "From SQLAlchemy import Create_engine"
So, open Anaconda Prompt and execute the command:
Installation Mysql-connector-python successful. reference: How does I get MySQL to work with the Python 3.6 in Anaconda IDE (Spyder)?
As a result, we begin to test the success of connecting to the database with joy.
Open Pycharm and open the Python console in the menu bar "Tools"---> "Python console":
Execute the following command:
From sqlalchemy Import create_engineengine = Create_engine (' Mysql+mysqlconnector://username:[email protected]_ip: Port/db_name?charset=utf8 ') conn = Engine.connect ()
The error is as follows:
Sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) Failed parsing handshake; End byte not present in buffer
Online search, said is a bug, error can not be solved, refer to here. If can solve, please leave a message to inform, grateful.
then, try installing MYSQLDB. Refer to this article, execute:conda install Mysql-python, result error: The following specifications were found to being in conflict
says that MYSQLDB does not support the Python3.6 version. Specific reference to this article, and then had to give up.
You can try creating a Python 2.7 environment and install Mysql-python to that.
Try again: The goal of the PYMYSQL,PYMYSQL project is to replace the MYSQLDB
The goal of Pymysql is to being a drop-in replacement for mysqldb and work on CPython, PyPy and IronPython.
Performed in Anaconda prompt:conda install -c anaconda pymysql ,参考:这里
After the installation is successful, in Pycharm, ctrl+alt+s searches for "python" and opens the project interpreter to see that the installation was successful Pymysql
You can then use Pymysql to test the connection to the MySQL database. Execute the following command successfully. (Be careful to replace your own database name). At this point, Anaconda3-based Python3.6 in Pycharm can finally access the MySQL database.
From SQLAlchemy import create_engineengine=create_engine (' mysql+pymysql://username:[email protected]_ip: Port/db_name?charset=utf8 ') conn = Engine.connect () R = Conn.execute (' SELECT field1,field2 from Db_nmae limit 1 ')
In addition, by the way, for different drivers, the statement that connects MySQL is different: Refer to SQLAlchemy official documentation for details.
#MySQL-pythonmysql+mysqldb://<User>:<Password>@<Host>[:<Port>]/<dbname>#pymysqlmysql +pymysql://<username>:<Password>@<Host>/<dbname>[?<Options>] #mysqlconnectormysql +mysqlconnector://<User>:<Password>@<Host>[:<Port>]/<dbname>
Original: http://www.cnblogs.com/hapjin/p/7800959.html
WINDOWS10 using Pycharm2016 to install MySQL driver summary based on Anaconda3 Python3.6