什么是PyMySQL,它与MySQLdb有什么不同?它会影响Django的部署吗?

时间:2022-09-22 12:30:20

I just solved some problems in my Django 1.3 app by using PyMySQL instead of MySQLdb. I followed this tutorial on how to make the switch: http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html

我刚刚用PyMySQL代替MySQLdb解决了Django 1.3应用中的一些问题。我跟随本教程学习了如何进行切换:http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for python-26-and.html

Now I want to know what PyMySQL actually is and how it is different from MySQLdb.

现在我想知道PyMySQL到底是什么以及它与MySQLdb的区别。

I am using it on localhost and will then upload it to some hosting.

我在localhost上使用它,然后将它上传到一些主机。

Is it fine to use PyMySQL on localhost and on hosting whatever they provide? Since I have changed "MySQLdb" in base.py and introspection.py to "PyMySQL", will I need to upload it to the server after changing these files? Or as it is Django's files, since Django will be uploaded there already, does it not matter much?

在本地主机上和主机上使用PyMySQL可以吗?因为我在base中更改了“MySQLdb”。py和自省。py到“PyMySQL”,修改这些文件后需要上传到服务器吗?或者因为是Django的文件,因为Django已经在那里上传了,这不是很重要吗?

4 个解决方案

#1


14  

PyMySQL and MySQLdb are both database connectors for Python, libraries to enable Python programs to talk to a MySQL server.

PyMySQL和MySQLdb都是Python的数据库连接器,它们可以让Python程序与MySQL服务器对话。

You would normally never upload core Django files when deploying an app. If Django is working fine on your deployment server, you definitely don't need to change anything there. The DB driver is a step or two below the ORM even, and certainly none of the code you have written depends on which of these is in use.

在部署应用程序时,通常不会上传核心的Django文件。DB驱动程序甚至比ORM还要低一两步,当然,您所编写的任何代码都不依赖于使用的代码。

#2


54  

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python.

PyMySQL和MySQLdb提供了相同的功能——它们都是数据库连接器。不同之处在于实现中,MySQLdb是C扩展,PyMySQL是纯Python。

There are a few reasons to try PyMySQL:

有几个理由可以尝试PyMySQL:

  • it might be easier to get running on some systems
  • 在某些系统上运行可能更容易
  • it works with PyPy
  • 它的工作原理与PyPy
  • it can be "greened" and works with gevent
  • 它可以被“绿化”,并与gevent一起工作

The proper way to use it with Django is to import it and tell it to impersonate MySQLdb in your top-level file, usually manage.py. Put the following code at the very top of your manage.py (or whatever file you call when starting your server):

与Django一起使用它的正确方法是导入它,并告诉它在顶层文件中模拟MySQLdb(通常是manageed .py)。把下面的代码放在你管理的最上面。py(或启动服务器时调用的任何文件):

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass

#3


9  

Your first point:

你的第一点:

According to pymysql wiki page:

根据pymysql wiki页面:

MySQLdb, is a C extension module that has a reputation of being difficult to compile, especially if you're on a Mac. Additionally, end-users need to wait for new binaries to be compiled for each new release of Python, and MySQLdb will never run on Jython, IronPython, or PyPy (without something like cpyext or IronClad). We also maintain 100% compatibility between Python 2 and Python 3, so all advancements made on the 2.x trunk will be immediately available on Python 3.

MySQLdb,是一个C扩展模块,它有一个编译的困难,尤其是如果你在Mac。此外,最终用户需要等待新的二进制文件被编译为每个新版本的Python,MySQLdb永远不会运行在Jython,IronPython或PyPy(没有cpyext或铁的)。我们还在Python 2和Python 3之间保持100%的兼容性,因此所有的改进都是基于2。在Python 3上可以立即使用x trunk。

Your second point:

你的第二点:

If django is working fine on your localhost, then it should be fine on your development.

如果django在本地主机上工作得很好,那么在开发上应该也可以。

#4


0  

As per my experience I had difficulty in installing "MySQL-python" - (MySQLdb). It made me search for more alternatives so I found pymysql, and it also got installed easily and worked in first go like a charm. So I would suggest using pymysql only instead of MySQLdb.

根据我的经验,我在安装“sql -python”(MySQLdb)时遇到了困难。它让我寻找更多的替代方案,所以我找到了pymysql,而且它也很容易安装,而且在go first go就像一个魅力。所以我建议只使用pymysql而不是MySQLdb。

#1


14  

PyMySQL and MySQLdb are both database connectors for Python, libraries to enable Python programs to talk to a MySQL server.

PyMySQL和MySQLdb都是Python的数据库连接器,它们可以让Python程序与MySQL服务器对话。

You would normally never upload core Django files when deploying an app. If Django is working fine on your deployment server, you definitely don't need to change anything there. The DB driver is a step or two below the ORM even, and certainly none of the code you have written depends on which of these is in use.

在部署应用程序时,通常不会上传核心的Django文件。DB驱动程序甚至比ORM还要低一两步,当然,您所编写的任何代码都不依赖于使用的代码。

#2


54  

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python.

PyMySQL和MySQLdb提供了相同的功能——它们都是数据库连接器。不同之处在于实现中,MySQLdb是C扩展,PyMySQL是纯Python。

There are a few reasons to try PyMySQL:

有几个理由可以尝试PyMySQL:

  • it might be easier to get running on some systems
  • 在某些系统上运行可能更容易
  • it works with PyPy
  • 它的工作原理与PyPy
  • it can be "greened" and works with gevent
  • 它可以被“绿化”,并与gevent一起工作

The proper way to use it with Django is to import it and tell it to impersonate MySQLdb in your top-level file, usually manage.py. Put the following code at the very top of your manage.py (or whatever file you call when starting your server):

与Django一起使用它的正确方法是导入它,并告诉它在顶层文件中模拟MySQLdb(通常是manageed .py)。把下面的代码放在你管理的最上面。py(或启动服务器时调用的任何文件):

try:
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass

#3


9  

Your first point:

你的第一点:

According to pymysql wiki page:

根据pymysql wiki页面:

MySQLdb, is a C extension module that has a reputation of being difficult to compile, especially if you're on a Mac. Additionally, end-users need to wait for new binaries to be compiled for each new release of Python, and MySQLdb will never run on Jython, IronPython, or PyPy (without something like cpyext or IronClad). We also maintain 100% compatibility between Python 2 and Python 3, so all advancements made on the 2.x trunk will be immediately available on Python 3.

MySQLdb,是一个C扩展模块,它有一个编译的困难,尤其是如果你在Mac。此外,最终用户需要等待新的二进制文件被编译为每个新版本的Python,MySQLdb永远不会运行在Jython,IronPython或PyPy(没有cpyext或铁的)。我们还在Python 2和Python 3之间保持100%的兼容性,因此所有的改进都是基于2。在Python 3上可以立即使用x trunk。

Your second point:

你的第二点:

If django is working fine on your localhost, then it should be fine on your development.

如果django在本地主机上工作得很好,那么在开发上应该也可以。

#4


0  

As per my experience I had difficulty in installing "MySQL-python" - (MySQLdb). It made me search for more alternatives so I found pymysql, and it also got installed easily and worked in first go like a charm. So I would suggest using pymysql only instead of MySQLdb.

根据我的经验,我在安装“sql -python”(MySQLdb)时遇到了困难。它让我寻找更多的替代方案,所以我找到了pymysql,而且它也很容易安装,而且在go first go就像一个魅力。所以我建议只使用pymysql而不是MySQLdb。