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

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

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

  • python代码(在谷歌colab中运行)
  1. import mysql.connector
  2. mydb = mysql.connector.connect(
  3. host="196.xxx.xxx.xxx",
  4. user="xxxxx",
  5. passwd="xxxxxx",
  6. database="Unnamed" # I tried to put "cloud_db" and "Unnamed" is not work
  7. )
  8. mycursor = mydb.cursor()
  9. mycursor.execute("SELECT * FROM webpage")
  10. myresult = mycursor.fetchall()
  11. for row in myresult:
  12. print(row)
  • 误差输出
  1. ---------------------------------------------------------------------------
  2. MySQLInterfaceError Traceback (most recent call last)
  3. /usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py in _open_connection(self)
  4. 267 try:
  5. --> 268 self._cmysql.connect(**cnx_kwargs)
  6. 269 self._cmysql.converter_str_fallback = self._converter_str_fallback
  7. MySQLInterfaceError: Can't connect to MySQL server on '196.xxx.xxx.xxx:3306' (113)
  8. The above exception was the direct cause of the following exception:
  9. DatabaseError Traceback (most recent call last)
  10. 4 frames
  11. /usr/local/lib/python3.8/dist-packages/mysql/connector/connection_cext.py in _open_connection(self)
  12. 271 self.converter.str_fallback = self._converter_str_fallback
  13. 272 except MySQLInterfaceError as err:
  14. --> 273 raise get_mysql_exception(
  15. 274 msg=err.msg, errno=err.errno, sqlstate=err.sqlstate
  16. 275 ) from err
  17. DatabaseError: 2003 (HY000): Can't connect to MySQL server on '196.xxx.xxx.xxx:3306' (113)

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

q3qa4bjr

q3qa4bjr1#

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

  1. import mysql.connector
  2. mydb = mysql.connector.connect(
  3. host="1xx.xxx.xxx.xxx",
  4. user="root", # defult user name is root
  5. password="password",
  6. database="your_database",
  7. auth_plugin='mysql_native_password' # In case plugging password is not working
  8. )
  9. mycursor = mydb.cursor()
  10. mycursor.execute("SELECT * FROM your_kist")
  11. myresult = mycursor.fetchall()
  12. for row in myresult:
  13. print("row_01 = ", row[0], )
  14. print("row_02 = ", row[1])
  15. print("row_03 = ", row[2])
  16. print("row_04 = ", row[3], "\n")
展开查看全部

相关问题