问题遇到的现象和发生背景
用pycharm 连接mysql数据库
发出警告
遇到的现象和发生背景,请写出第一个错误信息
socket.gaierror: [Errno 11001] getaddrinfo failed
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
import numpy as np
import pymysql #导入pymysql库
coon = pymysql.connect( #创建连接对象
host = '192.168.1.9 ', #MySQL的服务器地址,一般为本地一般写127.0.0.1或者localhost
user = 'root', #数据库账户
passwd = '123456', #数据库密码
port = 3306, #MySQL的端口号
db = 'ku', #你所要操作的数据库名称
charset = 'utf8') #字符集 ,charset必须写utf8,不能写utf-8
print('MySQL数据库连接成功.....\n')
cur = coon.cursor() #建立游标
cur.execute("select * from test1") #查询数据 ,这里的test1是我数据库ku下的表
res = cur.fetchall() #获取结果
from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import AgglomerativeClustering
data = np.array(res)
# 画点
plt.scatter(x=data[:, 0:1], y=data[:, 1:2], marker='.', color='red')
n = np.arange(data.shape[0])
for i, txt in enumerate(n):
plt.annotate(txt, (data[i:i + 1, 0:1], data[i:i + 1, 1:2]))
plt.show()
运行结果及详细报错内容
D:\pythonProject2\venv\Scripts\python.exe D:\pythonProject2\venv\ggb.py
Traceback (most recent call last):
File "D:\pythonProject2\venv\lib\site-packages\pymysql\connections.py", line 613, in connect
sock = socket.create_connection(
File "D:\python\lib\socket.py", line 822, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "D:\python\lib\socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\pythonProject2\venv\ggb.py", line 3, in
coon = pymysql.connect( #创建连接对象
File "D:\pythonProject2\venv\lib\site-packages\pymysql\connections.py", line 353, in init
self.connect()
File "D:\pythonProject2\venv\lib\site-packages\pymysql\connections.py", line 664, in connect
raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.9 ' ([Errno 11001] getaddrinfo failed)")
我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
换端口,不行