(Pythin,MySQL)数据库错误:2003年(HY000):无法连接到“196.xxx.xxx.xxx:3306”上的MySQL服务器(113)

jxct1oxe  于 2023-01-12  发布在  Mysql
关注(0)|答案(1)|浏览(161)

连接远程mySQL服务器失败,我确保MySQL正在运行,Django可以成功连接,但希望连接python与mySQL失败

  • python代码(在谷歌colab中运行)
import mysql.connector

mydb = mysql.connector.connect(
  host="196.xxx.xxx.xxx",
  user="xxxxx",
  passwd="xxxxxx", 
  database="Unnamed"  # I tried to put "cloud_db" and "Unnamed" is not work
)
                
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM webpage")                       
                                         
myresult = mycursor.fetchall()

for row in myresult:
  print(row)
  • 误差输出
---------------------------------------------------------------------------
MySQLInterfaceError                       Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py in _open_connection(self)
    267         try:
--> 268             self._cmysql.connect(**cnx_kwargs)
    269             self._cmysql.converter_str_fallback = self._converter_str_fallback

MySQLInterfaceError: Can't connect to MySQL server on '196.xxx.xxx.xxx:3306' (113)

The above exception was the direct cause of the following exception:

DatabaseError                             Traceback (most recent call last)
4 frames
/usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py in _open_connection(self)
    271                 self.converter.str_fallback = self._converter_str_fallback
    272         except MySQLInterfaceError as err:
--> 273             raise get_mysql_exception(
    274                 msg=err.msg, errno=err.errno, sqlstate=err.sqlstate
    275             ) from err

DatabaseError: 2003 (HY000): Can't connect to MySQL server on '196.xxx.xxx.xxx:3306' (113)

当我连接Django和mySQL时,下面的信息可以正常工作,但是python到MySQL的连接仍然有效

q3qa4bjr

q3qa4bjr1#

我建议您先使用SQL工具连接,如果连接成功,您就可以知道hostpassword ..是否正确,然后是编码问题

import mysql.connector

mydb = mysql.connector.connect(
  host="1xx.xxx.xxx.xxx",
  user="root", # defult user name is root
  password="password", 
  database="your_database",  
  auth_plugin='mysql_native_password' # In case plugging password is not working
)
                
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM your_kist")                       
                                         
myresult = mycursor.fetchall()

for row in myresult:
  print("row_01 = ", row[0], )
  print("row_02 = ", row[1])
  print("row_03  = ", row[2])
  print("row_04  = ", row[3], "\n")

相关问题