为了账号安全,请及时绑定邮箱和手机立即绑定

Python无法连接MySQL数据库是什么原因?

Python无法连接MySQL数据库是什么原因?

尚方宝剑之说 2019-02-16 22:33:36
我想要使用Python来连接MySQL数据库,但是报了如下错误 File "/usr/local/lib/python3.5/dist-packages/pymysql/__init__.py", line 94, in Connect return Connection(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 325, in __init__ self.connect() File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 599, in connect self._request_authentication() File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 861, in _request_authentication auth_packet = self._read_packet() File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 684, in _read_packet packet.check_error() File "/usr/local/lib/python3.5/dist-packages/pymysql/protocol.py", line 220, in check_error err.raise_mysql_exception(self._data) File "/usr/local/lib/python3.5/dist-packages/pymysql/err.py", line 109, in raise_mysql_exception raise errorclass(errno, errval) pymysql.err.InternalError: (1049, "Unknown database 'expert'") 百度结果均告知为未创建相应的数据库导致本错误,但是我已经创建了expert数据库,但仍然提示数据库不存在。此外,本服务器下的数据库均能正常连接。 这是我的数据库连接代码: config = { 'host': cf.get("mySql", "host"), 'port': int(cf.get("mySql", "port")), 'user': cf.get("mySql", "user"), 'password': cf.get("mySql", "password"), 'db': cf.get("mySql", "db"), 'charset': cf.get("mySql", "charset") } table_to_save = cf.get("mySql", "save_table") try: con = pymysql.connect(**config) except Exception: con = Exception log_info = "Connection to MySQL has been rejected" print(log_info) os._exit(0) 配置文件为: [mySql] host=127.0.0.1 port=3306 user=root password=123456 db=expert charset=utf8 有大佬可以帮忙解决一下吗?
查看完整描述

1 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

从报错信息中看到, 这个数据库expert不存在

查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 765 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信