如何在Mac OS X上安装MySQLdb(MySQL的Python数据访问库)?

问题:如何在Mac OS X上安装MySQLdb(MySQL的Python数据访问库)?

我是Python的新手,但是我只花了一天的时间来研究如何使MySQLdb正常工作,而根据Google的说法,Universe包含了许多有关PITA是什么的参考,并且似乎有大量的指南。过时的。鉴于此站点旨在解决此类问题,并且我知道将来需要该解决方案的参考,因此我将提出问题,提供答案并查看还有哪些问题表面。

所以问题是如何使MySQLdb在Mac OS X上运行?

I’m a Python newbie, but I’ve just spent a day working out how to get MySQLdb working properly, and the universe according to google includes numerous references to what a PITA it is, and an inordinate number of guides that seem to be outdated. Given that this site is intended to address these sorts of problems, and I know that I’m going to need a reference to the solution in future, I’m going to ask the question, provide my answer and see what else floats to the surface.

So the question is how to get MySQLdb working on Mac OS X?


回答 0

使用Python3的用户的更新: 您可以简单地conda install mysqlclient用来安装使用MySQLdb所需的库,因为它目前存在。以下SO问题是一个有用的线索:Python 3 ImportError:没有名为’ConfigParser’的模块。安装mysqlclient将安装mysqlclient,mysql-connector和llvmdev(至少在我的机器上安装了这3个库)。

这是我关于这个问题的漫漫经历的故事。如果您对问题有更好的经验,不妨看一下它的编辑或概括…应用一下这种SO魔术。

注意:下一段中的注释适用于Snow Leopard,但不适用于Lion,后者似乎需要64位MySQL

首先,MySQLdb的作者(还是?)在这里说,最有害的问题之一是OS X附带了32位版本的Python,但是大多数普通人(包括我自己)都可能安装了64位版本。 MySQL。不好动…删除64位版本,如果你已经安装了它(这个繁琐的任务指令适用于SO 这里),然后下载并(包安装32位版本在这里

关于如何构建和安装MySQLdb库,有许多分步说明。它们通常有细微的差异。 对我来说似乎最受欢迎,并提供了可行的解决方案。我在下面进行了一些修改,复制了它

步骤0: 开始之前,我假设您在Mac上安装了MySQL,Python和GCC

步骤1:从SourceForge 下载最新的MySQL for Python适配器

步骤2:解 压缩下载的软件包:

tar xzvf MySQL-python-1.2.2.tar.gz

步骤3: 在文件夹内,清洁包装:

sudo python setup.py clean

额外步骤,(来自此评论

步骤3b: 删除MySQL-python-1.2.2 / build / *目录下的所有内容-不要相信“ python setup.py clean”为您完成此操作

步骤3c: 删除Users / $ USER / .python-eggs下的鸡蛋

步骤4: 最初需要编辑_mysql.c,但现在不再需要。MySQLdb社区似乎已经修复了此错误。

步骤5: 在lib下创建一个符号链接,以指向一个名为mysql的子目录。这是编译期间要查找的位置。

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

步骤6: 编辑setup_posix.py并更改以下内容

mysql_config.path =“ mysql_config”

mysql_config.path =“ / usr / local / mysql / bin / mysql_config”

步骤7: 在同一个目录中,重建您的软件包(忽略软件包附带的警告)

sudo python setup.py build

步骤8: 安装软件包,您已完成。

sudo python setup.py install

第9步: 测试它是否正常工作。如果可以导入MySQLdb,则可以使用。

python

>>> import MySQLdb

步骤10: 如果在尝试导入时收到错误消息,提示Library not loaded: libmysqlclient.18.dylib以以下结尾:Reason: image not found您需要创建一个附加的符号链接,即:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

然后,您应该能够import MySQLdb没有任何错误。

最后一个麻烦是,如果您从构建目录启动Python,则会出现此错误:

/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3:用户警告:模块_mysql已从/Library/Python/2.5/中导入site-packages / MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg / _mysql.pyc,但XXXX / MySQL-python-1.2.3c1已添加到sys.path

这对Google来说很容易,但是要为您省去麻烦,您将最终在这里(或者可能不是……不是特别适合未来的URL),并发现您需要cd ..脱离构建目录,并且错误应该消失。

正如我在顶部写的那样,我很乐意看到这个答案具有普遍性,因为对于这个可怕的问题还有很多其他的具体经验。编辑掉,或者提供自己的更好的答案。

Update for those using Python3: You can simply use conda install mysqlclient to install the libraries required to use MySQLdb as it currently exists. The following SO question was a helpful clue: Python 3 ImportError: No module named ‘ConfigParser’ . Installing mysqlclient will install mysqlclient, mysql-connector, and llvmdev (at least, it installed these 3 libraries on my machine).

Here is the tale of my rambling experience with this problem. Would love to see it edited or generalised if you have better experience of the issue… apply a bit of that SO magic.

Note: Comments in next paragraph applied to Snow Leopard, but not to Lion, which appears to require 64-bit MySQL

First off, the author (still?) of MySQLdb says here that one of the most pernicious problems is that OS X comes installed with a 32 bit version of Python, but most average joes (myself included) probably jump to install the 64 bit version of MySQL. Bad move… remove the 64 bit version if you have installed it (instructions on this fiddly task are available on SO here), then download and install the 32 bit version (package here)

There are numerous step-by-steps on how to build and install the MySQLdb libraries. They often have subtle differences. This seemed the most popular to me, and provided the working solution. I’ve reproduced it with a couple of edits below

Step 0: Before I start, I assume that you have MySQL, Python, and GCC installed on the mac.

Step 1: Download the latest MySQL for Python adapter from SourceForge.

Step 2: Extract your downloaded package:

tar xzvf MySQL-python-1.2.2.tar.gz

Step 3: Inside the folder, clean the package:

sudo python setup.py clean

COUPLE OF EXTRA STEPS, (from this comment)

Step 3b: Remove everything under your MySQL-python-1.2.2/build/* directory — don’t trust the “python setup.py clean” to do it for you

Step 3c: Remove the egg under Users/$USER/.python-eggs

Step 4: Originally required editing _mysql.c, but is now NO LONGER NECESSARY. MySQLdb community seem to have fixed this bug now.

Step 5: Create a symbolic link under lib to point to a sub-directory called mysql. This is where it looks for during compilation.

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

Step 6: Edit the setup_posix.py and change the following

mysql_config.path = “mysql_config”

to

mysql_config.path = “/usr/local/mysql/bin/mysql_config”

Step 7: In the same directory, rebuild your package (ignore the warnings that comes with it)

sudo python setup.py build

Step 8: Install the package and you are done.

sudo python setup.py install

Step 9: Test if it’s working. It works if you can import MySQLdb.

python

>>> import MySQLdb

Step 10: If upon trying to import you receive an error complaining that Library not loaded: libmysqlclient.18.dylib ending with: Reason: image not found you need to create one additional symlink which is:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

You should then be able to import MySQLdb without any errors.

One final hiccup though is that if you start Python from the build directory you will get this error:

/Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.3c1-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but XXXX/MySQL-python-1.2.3c1 is being added to sys.path

This is pretty easy to Google, but to save you the trouble you will end up here (or maybe not… not a particularly future-proof URL) and figure out that you need to cd .. out of build directory and the error should disappear.

As I wrote at the top, I’d love to see this answer generalised, as there are numerous other specific experiences of this horrible problem out there. Edit away, or provide your own, better answer.


回答 1

适用于Mac OS X 10.8(Mountain Lion),10.9(Mavericks),10.10(Yosemite)和10.11(El Capitan)的快速简便的方法:

我假设您已经安装了XCode,其命令行工具,Python和MySQL。

  1. 安装PIP:

    sudo easy_install pip
  2. 编辑〜/ .profile :(在Mac OS X 10.10中可能不需要)

    nano ~/.profile

    复制并粘贴以下两行

    export PATH=/usr/local/mysql/bin:$PATH
    export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

    保存并退出。后记执行以下命令:

    source  ~/.profile
  3. 安装MySQLdb

    sudo pip install MySQL-python

    要测试一切是否正常,请尝试

    python -c "import MySQLdb"

对我来说,它就像是一种魅力。希望对您有所帮助。

如果遇到有关缺少库的错误:库未加载:libmysqlclient.18.dylib,则必须将其符号链接为/usr/lib

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib 

A quick and easy way for Mac OS X 10.8 (Mountain Lion), 10.9 (Mavericks), 10.10 (Yosemite), and 10.11 (El Capitan):

I assume you have XCode, its command line tools, Python, and MySQL installed.

  1. Install PIP:

    sudo easy_install pip
    
  2. Edit ~/.profile: (Might not be necessary in Mac OS X 10.10)

    nano ~/.profile
    

    Copy and paste the following two line

    export PATH=/usr/local/mysql/bin:$PATH
    export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
    

    Save and exit. Afterwords execute the following command:

    source  ~/.profile
    
  3. Install MySQLdb

    sudo pip install MySQL-python
    

    To test if everything works fine just try

    python -c "import MySQLdb"
    

It worked like a charm for me. I hope it helps.

If you encounter an error regarding a missing library: Library not loaded: libmysqlclient.18.dylib then you have to symlink it to /usr/lib like so:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib 

回答 2

通过Macports安装mysql和python 搬运工完成了所有困难的工作。

sudo port install py26-mysql 
sudo port install mysql5-server

应该安装您需要的东西。(请参阅堆栈溢出以获取有关mysql服务器的注释

如果只需要连接到mysql而不运行服务器,那么第一行就足够了。

Macports现在(2013年初)将为操作系统的常见组合和可执行体系结构提供二进制下载,而对于其他操作系统(如果需要),它将从源代码构建。

通常,当有复杂的库等需要安装时,macports(或fink)会提供帮助。

仅使用Python的代码,如果可以通过setuptools等设置简单的C依赖关系,但是如果将两者混合使用,它就会变得复杂起来。

Install mysql and python via Macports The porters have done all the difficult work.

sudo port install py26-mysql 
sudo port install mysql5-server

should install what you need. (see Stack overflow for comments re mysql server)

If you only need to connect to mysql and not run a server then the first line is sufficient.

Macports now (early 2013) will provide binary downloads for common combinations of OS a executable architecture, for others (and if you request it) it will build from source.

In general macports (or fink) help when there are complex libraries etc that need to be installed.

Python only code and if simple C dependencies can be set up via setuptools etc, but it begins to get complex if you mix the two.


回答 3

安装点子:

sudo easy_install pip

安装brew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

安装mysql:

brew install mysql

安装MySQLdb

sudo pip install MySQL-python

如果您遇到编译问题,请尝试编辑〜/ .profile文件,如此处的答案之一所示。

Install pip:

sudo easy_install pip

Install brew:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Install mysql:

brew install mysql

Install MySQLdb

sudo pip install MySQL-python

If you have compilation problems, try editing the ~/.profile file like in one of the answers here.


回答 4

在完成第9步时收到错误消息后,我还必须完成另一个步骤:

ImportError: dlopen(/Users/rick/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

参考:谢谢!http://ageekstory.blogspot.com/2011_04_01_archive.html

Here’s another step I had to go through, after receiving an error on completing Step 9:

ImportError: dlopen(/Users/rick/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

Reference: Thanks! http://ageekstory.blogspot.com/2011_04_01_archive.html


回答 5

在Mac安装MySQL-python所述

pip uninstall MySQL-python
brew install mysql
pip install MySQL-python

然后测试一下:

python -c "import MySQLdb"

As stated on Installing MySQL-python on mac :

pip uninstall MySQL-python
brew install mysql
pip install MySQL-python

Then test it :

python -c "import MySQLdb"

回答 6

刚得到一个新的Lion盒之后,又遇到了这个问题(再次!)。

我发现的最佳解决方案(仍然不是100%最佳,但可以工作):

可以通过从Apple下载XCode / Dev工具获得它-这是一个很大的下载-

…但是我推荐这个github,它具有您所需要的(并且没有XCode):https : //github.com/kennethreitz/osx-gcc-installer

我下载了他们为狮子预装的PKG,https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg

  • 确保您已下载64位版本的MYSQL社区。(DMG安装是一条简单的路线)http://dev.mysql.com/downloads/mysql/

  • 设置路径如下:

    导出PATH = $ PATH:/ usr / local / mysql-XXXX

    导出DYLD_LIBRARY_PATH = / usr / local / mysql / lib /

    出口ARCHFLAGS =’-arch x86_64′

注意:

在上面的mysql-XXXX中为1,XXX是您下载的特定版本。(也许/ usr / local / mysql /也可以工作,因为这很可能是相同的别名,但是我不会假装知道您的设置)

2我已经看到它建议将ARCHFLAGS设置为’-arch i386 -arch x86_64’,但是仅指定x86_64似乎对我来说更好。(我可以想到一些原因,但是它们并不严格相关)。

  • 安装野兽!

    easy_install MySQL-python

  • 最后一步:

永久添加DYLD_LIBRARY_PATH!

您可以将其添加到您的bash_profile或类似文件中。这是我所缺少的步骤,没有它,我的系统将继续坚持各种错误来查找_mysql.so等。

导出DYLD_LIBRARY_PATH = / usr / local / mysql / lib /

@ richard-boardman,刚注意到您的软链接解决方案,实际上可能是在执行我的PATH解决方案所做的相同工作……民间,无论哪种方法最适合您。

最佳参考:http : //activeintelligence.org/blog/archive/mysql-python-aka-mysqldb-on-osx-lion/

Just had this problem (again!) after getting a new Lion box.

Best solution I’ve found (still not 100% optimal, but working):

you can get it by downloading XCode/Dev Tools from Apple – this is a big download –

… but instead I recommend this github which has what you need (and does not have XCode): https://github.com/kennethreitz/osx-gcc-installer

I downloaded their prebuilt PKG for lion, https://github.com/downloads/kennethreitz/osx-gcc-installer/GCC-10.7-v2.pkg

  • make sure you have downloaded a 64-BIT version of MYSQL Community. (The DMG install is an easy route) http://dev.mysql.com/downloads/mysql/

  • Set paths as follows:

    export PATH=$PATH:/usr/local/mysql-XXXX

    export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/

    export ARCHFLAGS=’-arch x86_64′

NOTE THAT:

1 in mysql-XXXX above, XXX is the specific version you downloaded. (Probably /usr/local/mysql/ would also work since this is most likely an alias to the same, but I won’t pretend to know your setup)

2 I have seen it suggested that ARCHFLAGS be set to ‘-arch i386 -arch x86_64’ but specifying only x86_64 seemed to work better for me. (I can think of some reasons for this but they are not strictly relevant).

  • Install the beast!

    easy_install MySQL-python

  • LAST STEP:

Permanently add the DYLD_LIBRARY_PATH!

You can add it to your bash_profile or similar. This was the missing step for me, without which my system continued to insist on various errors finding _mysql.so and so on.

export DYLD_LIBRARY_PATH = /usr/local/mysql/lib/

@richard-boardman, just noticed your soft link solution, which may in effect be doing the same thing my PATH solution does…folks, whatever works best for you.

Best reference: http://activeintelligence.org/blog/archive/mysql-python-aka-mysqldb-on-osx-lion/


回答 7

您可以尝试使用pure-python pymysql

sudo easy_install pymysql

(或者使用pip是否已安装。)然后,import MySQLdb在代码中添加以下代码:

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

You could try using the pure-python pymysql:

sudo easy_install pymysql

(Or use pip if you have it installed.) Then, add this before you import MySQLdb in your code:

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

回答 8

或简单尝试:

> sudo easy_install MySQL-python

如果出现如下错误:

EnvironmentError:找不到mysql_config

,然后运行

> export PATH=$PATH:/usr/local/mysql/bin

Or simple try:

> sudo easy_install MySQL-python

If it gives a error like below:

EnvironmentError: mysql_config not found

, then just run this

> export PATH=$PATH:/usr/local/mysql/bin

回答 9

如果您使用的是64位MySQL,则在构建mysql-python库时使用ARCHFLAGS指定您的cpu体系结构将达到目的:

ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install

If you are using 64 bit MySQL, using ARCHFLAGS to specify your cpu architecture while building mysql-python libraries would do the trick:

ARCHFLAGS='-arch x86_64' python setup.py build
ARCHFLAGS='-arch x86_64' python setup.py install

回答 10

export PATH=$PATH:/usr/local/mysql/bin/

应该会为您解决该问题,因为系统无法找到mysql_config文件。

export PATH=$PATH:/usr/local/mysql/bin/

should fix the issue for you as the system is not able to find the mysql_config file.


回答 11

在macos Sierra上,这对我有用,其中Python由anaconda管理:

anaconda search -t conda mysql-python

anaconda show CEFCA/mysql-python

conda install --channel https://conda.anaconda.org/CEFCA mysql-python

与SQLAlchemy一起使用:

Python 2.7.13 | Continuum Analytics,Inc. | (默认值,2016年12月20日,23:05:08)darwin上的[GCC 4.2.1兼容Apple LLVM 6.0(clang-600.0.57)]有关更多信息,请键入“ help”,“ copyright”,“ credits”或“ license”信息。Anaconda由Continuum Analytics带给您。请检出:http : //continuum.io/thankshttps://anaconda.org

>>>从sqlalchemy导入*

>>> dbengine = create_engine(’mysql:// ….’)

On macos Sierra this work for me, where python is managed by anaconda:

anaconda search -t conda mysql-python

anaconda show CEFCA/mysql-python

conda install --channel https://conda.anaconda.org/CEFCA mysql-python

The to use with SQLAlchemy:

Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org

>>> from sqlalchemy import *

>>>dbengine = create_engine(‘mysql://….’)


回答 12

这个答案是大约在2019年11月的更新。没有一个流行的pip安装程序可以在MacOS 10.13(以及其他版本)上为您提供有效的安装程序。这是使工作正常的一种简单方法:

brew install mysql
pip install mysqlclient

如果需要安装brew的帮助,请访问以下站点:https : //brew.sh/

This answer is an update, circa November 2019. None of the popular pip installs will give you a working setup on MacOS 10.13 (and likely other versions as well). Here is a simple way that I got things working:

brew install mysql
pip install mysqlclient

If you need help installing brew, see this site: https://brew.sh/


回答 13

我遇到了这个问题,发现mysqlclient需要知道在哪里可以找到openssl,而OSX默认将其隐藏。

使用找到“ openssl” brew info openssl,然后将opensslbin 的路径添加到PATH:

export PATH="/usr/local/opt/openssl/bin:$PATH"

我建议将其添加到您的.zshrc或.bashrc中,这样以后就不必担心了。然后,导出该变量(可能需要关闭并重新打开bash会话),再添加两个env变量:

# in terminal
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

然后,最后运行:

pipenv install mysqlclient

它应该安装得很好。

来源:https//medium.com/@shandou/pipenv-install-mysqlclient-on-macosx-7c253b0112f2

I ran into this issue and found that mysqlclient needs to know where to find openssl, and OSX hides this by default.

Locate openssl with brew info openssl, and then add the path to your openssl bin to your PATH:

export PATH="/usr/local/opt/openssl/bin:$PATH"

I recommend adding that to your .zshrc or .bashrc so you don’t need to worry about it in the future. Then, with that variable exported (which may meed closing and re-opening your bash session), add two more env variables:

# in terminal
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Then, finally, run:

pipenv install mysqlclient

and it should install just fine.

Source: https://medium.com/@shandou/pipenv-install-mysqlclient-on-macosx-7c253b0112f2