【问题标题】:Connecting to remote MySql instance with Google Co-lab使用 Google Co-lab 连接到远程 MySql 实例
【发布时间】:2019-10-02 12:52:33
【问题描述】:

按照指南:

https://pythondata.com/quick-tip-sqlalchemy-for-mysql-and-pandas/

我正在尝试使用 Google co-lab 连接到远程 MySql 数据库。这是连接代码:

import pandas as pd
import sqlalchemy as sql

connect_string = 'my_connection_string'

sql_engine = sql.create_engine(connect_string)

但是返回错误:

ModuleNotFoundError                       Traceback (most recent call last)

<ipython-input-2-8e070e8c6206> in <module>()
      1 connect_string = 'my_connection_string'
----> 2 sql_engine = sql.create_engine(connect_string)
      3 

2 frames

/usr/local/lib/python3.6/dist-packages/sqlalchemy/dialects/mysql/mysqldb.py in dbapi(cls)
    116     @classmethod
    117     def dbapi(cls):
--> 118         return __import__("MySQLdb")
    119 
    120     def on_connect(self):

ModuleNotFoundError: No module named 'MySQLdb'


-----------------------------------------------------

来自No module named MySQLdb建议的答案

我尝试使用 pip install mysql-pythonpip install mysqlclient 安装 MySQLdb

但是两者都收到错误:

Collecting mysqlclient
  Using cached https://files.pythonhosted.org/packages/4d/38/c5f8bac9c50f3042c8f05615f84206f77f03db79781db841898fde1bb284/mysqlclient-1.4.4.tar.gz
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

和:

Collecting mysql-python
  Downloading https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip (108kB)
     |████████████████████████████████| 112kB 4.8MB/s 
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

如何使用 SQLAlchemy 连接到远程 MySql 数据库?

【问题讨论】:

  • @Poojan 谢谢,但我不想使用谷歌云 SQL。我正在尝试连接一个 MySql 实例。

标签: python mysql jupyter-notebook google-colaboratory


【解决方案1】:

这是有效的

!pip install PyMySQL

并使用像"mysql+pymysql://scott:tiger@localhost/foo"这样的连接字符串

【讨论】:

    【解决方案2】:
    • 它似乎失败了,因为缺少一些 lib
    • 首先运行以下命令,然后执行pip install mysqlclient
    sudo apt-get install python3-dev default-libmysqlclient-dev
    pip install mysqlclient
    

    来源:https://github.com/PyMySQL/mysqlclient-python

    • 在 colab 中使用 python3 测试

    【讨论】:

      【解决方案3】:

      这个过程对我有用:

      1. 在 Collab 上安装以下两个包。

         !pip install PyMySQL
         !pip install mysql-connector-python
        
      2. 按照以下格式发起连接:

         from sqlalchemy import  create_engine
         engine = create_engine("mysql+pymysql://<dbuser>:<pwd>@<server_ip>/<db_instance>")
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 2014-09-18
        • 1970-01-01
        • 1970-01-01
        • 2023-03-20
        相关资源
        最近更新 更多