简体   繁体   English

Jupyter Notebook 无法连接到本地 MySQL 数据库

[英]Jupyter Notebook unable to connect to local MySQL database

I'm trying to connect my Jupyter Notebook, I use Google Colab, with my local MySQL database.我正在尝试将我使用 Google Colab 的 Jupyter Notebook 与我的本地 MySQL 数据库连接起来。 When I run this script in PyCharm, my preferred IDE, it works no problem.当我在 PyCharm(我的首选 IDE)中运行此脚本时,它没有问题。 Only when I run it in Google Colab or Project Jupyter, both get the same error, does this happen.只有当我在 Google Colab 或 Project Jupyter 中运行它时,都会出现相同的错误,才会发生这种情况。

import pymysql
import pandas as pd

mySQLuser = 'username'
mySQLpasswd = 'password'
mySQLhost = '127.0.0.1'
mySQLport = '3306'
mySQLdatabase = 'databasename'
  
connection = pymysql.connect(user = mySQLuser, passwd = mySQLpasswd, host = mySQLhost, port = mySQLport, db = mySQLdatabase)

I've also tried the same with sqlalchemy(create_engine) and mysql.connector, but get the same error.我也对 sqlalchemy(create_engine) 和 mysql.connector 进行了同样的尝试,但得到了同样的错误。

OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' ([Errno 111] Connection refused)") OperationalError:(2003,“无法连接到 '127.0.0.1' 上的 MySQL 服务器([Errno 111] 连接被拒绝)”)

I've also tried granting all privileges to the user and that didn't change anything.我还尝试向用户授予所有权限,但这并没有改变任何东西。 Another suggestion I've seen online is to null or change the bind-address in the configuration file (my.ini), but that didn't affect anything either.我在网上看到的另一个建议是 null 或更改配置文件(my.ini)中的绑定地址,但这也没有任何影响。

Are you sure your colab/jupyter instances are run locally?您确定您的 colab/jupyter 实例在本地运行吗?
If that's not the case, you will not be able to access the SQL database on localhost (127.0.0.1) until you make your it accessible remotely (which implies several steps: making it accessible, changing the mySQLhost address in your code, authorizing connections from the host server in your SQL settings).如果不是这种情况,您将无法访问 localhost (127.0.0.1) 上的 SQL 数据库,直到您使其可远程访问(这意味着几个步骤:使其可访问,更改代码中的mySQLhost地址,授权连接从您的 SQL 设置中的主机服务器)。

Got it to work.让它工作。 The service provider was rejecting the requests despite me configuring the network to allow it, but I sorted it out.尽管我将网络配置为允许,但服务提供商拒绝了请求,但我将其整理出来。

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

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