Devdit
 

TimeoutError: [WinError 10060]: Established connection failed because connected host has failed to respond Python คือ วิธีแก้ไข

873

ติดตั้ง Python และโปรแกรม MySQL ไว้ที่เครื่องเดียวกัน และลองเขียนโปรแกรมเพื่อเชื่อมต่อแต่พอรันแล้วไม่สามารถทำงานได้ขึ้นข้อความผิดพลาดว่า TimeoutError: [WinError 10060]: Established connection failed because connected host has failed to respond แบบนี้ต้องแก้ไขอย่างไร

cnx = mysql.connector.connect(user='root', password='01234', host='192.168.1.1', database='my_db', port=3306)

 

วิธีแก้ไข

กรณีถ้ามีการติดตั้ง Python และ MySQL ไว้ที่เครื่องเดียวกันในส่วนของโค้ดการเชื่อมต่อ ตัวแปร host='192.168.1.1' ต้องระบุเป็น 127.0.0.1 หรือ localhost แก้ไขโค้ดได้ดังนี้

cnx = mysql.connector.connect(user='root', password='01234', host='127.0.0.1', database='my_db', port=3306)
หรือ
cnx = mysql.connector.connect(user='root', password='01234', host='localhost', database='my_db', port=3306)
แก้ไข 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ