google-collab:如何在Google Collab中连接到pymysql?

时间:2019-08-02 06:22:46

标签: python mysql google-colaboratory pymysql

在google-colab中,我尝试了此操作

!pip install pymysql
import pymysql
cnx = {'host':'localhost','username':'root','pwd':'','db':'test'}
connection = pymysql.connect(cnx['host'], cnx['username'], cnx['pwd'], cnx['db'])
connection.close()

面对错误:

---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pymysql/connections.py in connect(self, sock)
    582                                 (self.host, self.port), self.connect_timeout,
--> 583                                 **kwargs)
    584                             break

5 frames
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

OperationalError                          Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pymysql/connections.py in connect(self, sock)
    628                 exc.traceback = traceback.format_exc()
    629                 if DEBUG: print(exc.traceback)
--> 630                 raise exc
    631 
    632             # If e is neither DatabaseError or IOError, It's a bug.

OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

尝试了127.0.0.1的建议,但没有运气,然后看到this,尝试了:

connection = pymysql.connect(cnx['host'], cnx['username'], cnx['pwd'], cnx['db'],port=3306)

仍为connection refused。是否有可能从google-colab连接到本地数据库?如果是这样,错误是什么,我有什么选择?

数据库详细信息(从XAMPP开始运行):

MySQL Version                 : 10.1.13-MariaDB
Host                          : localhost
User                          : root
Port                          : 3306
Server Default Charset        : latin1

my.ini文件的状态:bind-address="127.0.0.1",密码已在文件# password = your_password中注释掉。在SQLYog中,数据库正在运行。

0 个答案:

没有答案