python mySQL 连接不上
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)
我需要怎么改
那些xxx都是要替换成你实际的ip的,你直接写xxx能连上才怪
如果你是要模糊地址,纯粹多此一举,局域网ip你放出来别人也不可能用这ip找到你电脑在哪,每个局域网都有这个ip
如果确定ip都没有问题,那么检查
1.服务启动了吗,本地能连接吗
2.端口设置正确吗,是默认的3306吗
3.计算机互相之间能ping通吗,确定是在一个局域网下吗
4.端口开放了吗,是不是防火墙拦截了
如果到这一步都没有问题,就应该能连上,如果是因为用户名密码不正确或者root用户禁止远程连接,应该报其他类型的错误
另:你应该先用mysql的客户端工具来测试,不要一开始就直接上自己写的程序,这样很难定位问题
你是纯粹代码连不上?软件能连上么,要是软件能连上就是代码问题,软件也连不上那就是你服务的问题,看下服务是否启动
我也有用其他方式 (Django )连接mysql 并没有问题,服务有启动,防火墙没拦截 等等的
所以我想说是不是代码的问题,或有其他我没排除到的地方,才能让python 连接 mysql 成功