简体   繁体   English

不能将Flask-SqlAlchemy与Mamp一起使用

[英]Can't use Flask-SqlAlchemy with mamp

I'm learning flask development & just a beginner. 我正在学习Flask开发,并且只是一个初学者。 I'm trying to use flask-sqlalchemy ORM to connect with mysql database which is running on mamp server. 我正在尝试使用flask-sqlalchemy ORM连接到在mamp服务器上运行的mysql数据库。 However when I tried to run the application it throw this error: 但是,当我尝试运行该应用程序时,它将引发以下错误:

ModuleNotFoundError: No module named 'MySQLdb'

After doing little research I thought I should install mysqldb. 经过很少的研究,我认为我应该安装mysqldb。 Then I tried running this command: 然后,我尝试运行此命令:

pip install flask-mysqldb

and I see a new error & here is the full trace: 我看到一个新错误,这是完整的跟踪信息:

  Collecting flask-mysqldb
    Using cached Flask-MySQLdb-0.2.0.tar.gz
  Requirement already satisfied: Flask>=0.10 in ./env/lib/python3.6/site-packages (from flask-mysqldb)
  Collecting mysqlclient (from flask-mysqldb)
    Using cached mysqlclient-1.3.10.tar.gz
      Complete output from command python setup.py egg_info:
      /bin/sh: mysql_config: command not found
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
        File "/private/var/folders/qy/81pz3q7n2l310x2r6nrjp9nr0000gn/T/pip-build-rdep43c8/mysqlclient/setup.py", line 17, in <module>
          metadata, options = get_config()
        File "/private/var/folders/qy/81pz3q7n2l310x2r6nrjp9nr0000gn/T/pip-build-rdep43c8/mysqlclient/setup_posix.py", line 44, in get_config
          libs = mysql_config("libs_r")
        File "/private/var/folders/qy/81pz3q7n2l310x2r6nrjp9nr0000gn/T/pip-build-rdep43c8/mysqlclient/setup_posix.py", line 26, in mysql_config
          raise EnvironmentError("%s not found" % (mysql_config.path,))
      OSError: mysql_config not found

I read some article regarding this issue and most of them guide to change the config on site.cfg. 我读了一些有关此问题的文章,其中大多数指导更改了site.cfg上的配置。 But unfortunately I couldn't find any file named site.cfg. 但是很遗憾,我找不到任何名为site.cfg的文件。 I read many article regarding this issue and tried to follow their guide but nothing worked for me. 我阅读了许多有关此问题的文章,并试图按照他们的指南进行操作,但对我没有任何帮助。 Don't know but may be I'm doing some silly mistake or something. 不知道,但可能是我在做一些愚蠢的错误或其他事情。 Someone please help me to get rid of this... 有人请帮我摆脱这个...

Thanks, Robin 谢谢罗宾

I found the solution after doing some research here are the steps: 经过研究后,我发现解决方案如下:

  1. Install the latest version of XCode & XCode developer tools. 安装最新版本的XCode和XCode开发人员工具。
  2. Install 'brew' package manager 安装“ brew”软件包管理器
  3. install MySQL using this command: brew install mysql 使用以下命令安装MySQL: brew install mysql
  4. Run this command to make a async link between MySQL socket: sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock (your socket url could be different if you using are xampp or any other local server. 运行以下命令以在MySQL套接字之间建立异步链接: sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock (如果使用的是xampp或任何其他本地地址,则套接字URL可能会不同服务器。
  5. Start MAMP/XAMMP MySQL server 启动MAMP / XAMMP MySQL服务器
  6. Install pip install flask-mysqldb then sqlalchemy etc. 安装pip install flask-mysqldb然后pip install flask-mysqldb

I think all above steps explain everything & After that sqlAlchemy should connect with your database without any problem. 我认为上述所有步骤都可以解释所有内容,之后sqlAlchemy应该可以毫无问题地与数据库连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM