博客
关于我
python 连接 mysql 错误
阅读量:357 次
发布时间:2019-03-04

本文共 1766 字,大约阅读时间需要 5 分钟。

MySQL 连接错误解决指南

一、错误一:无法连接到本地 MySQL 服务器

错误信息:

File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connectreturn Connection(*args, **kwargs)File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init__super(Connection, self).__init__(*args, **kwargs2)_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'xxx.xxx.xxx.xxx' (111)")

解决方法:

  • 打开 my.cnf 文件,通常位于 /etc/my.cnf~/.my.cnf
    • 注释或修改 bind-address 参数,确保允许外部访问。
      # bind-address = 127.0.0.1# 修改为:bind-address = 0.0.0.0
  • 重新启动 MySQL 服务。
    sudo /etc/init.d/mysql restart
  • 说明:

    默认情况下,MySQL 只绑定本地地址。修改 bind-address0.0.0.0 后,MySQL 会监听所有 IP 地址,确保外部可以连接。


    二、错误二:IP 地址被禁止访问

    错误信息:

    File "/usr/lib/pymodules/python2.6/MySQLdb/__init__.py", line 81, in Connectreturn Connection(*args, **kwargs)File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 170, in __init__super(Connection, self).__init__(*args, **kwargs2)_mysql_exceptions.OperationalError: (1130, "Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server")

    解决方法:

  • 使用 MySQL 语句授予访问权限。
    mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
    • root@% 表示允许所有主机使用 root 用户登录。
    • 如果需要限制访问,替换 % 为具体主机名。
  • 说明:

    默认情况下,MySQL 只允许本地 IP 访问。通过 GRANT 语句,允许来自所有 IP 的访问,确保外部程序可以连接。


    三、错误三:连接过多

    错误信息:

    (1040, 'Too many connections')The reason is: 'NoneType' object has no attribute 'cursor'

    上下文:

    网络爬虫改为多线程后,数据库连接超过服务器限制。

    解决方法:

  • 增加 MySQL 的最大连接数。
    • 打开 my.cnf,修改 max_connections
      [mysql]max_connections = 10000

    -,默认值为 100,建议改为 16384 以支持更多连接。

  • 使用数据库连接池。
    • 在程序中,避免直接使用连接池,改用 DBPool 或类似工具管理连接。
  • 优化程序结构。
    • 将爬虫逻辑分为多个线程或进程,每个线程/进程独立处理 SQL 语句。
    • 使用队列存储 SQL 语句,单独一个进程负责批量插入数据库。
  • 说明:

    MySQL 的连接池机制可以显著提高性能,避免因连接过多导致的错误。通过优化程序结构,减少数据库压力,确保长时间运行稳定。


    总结

    以上错误均与 MySQL 连接配置或程序逻辑相关。通过合理优化配置文件和调整程序结构,可以有效解决连接问题,确保数据库运行稳定。

    转载地址:http://jmbr.baihongyu.com/

    你可能感兴趣的文章
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>