Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 90259|回复: 0

外部连接mysql docker容器异常

[复制链接]
  • TA的每日心情
    奋斗
    2024-11-24 15:47
  • 签到天数: 804 天

    [LV.10]以坛为家III

    2053

    主题

    2111

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    726782
    发表于 2021-4-8 00:23:17 | 显示全部楼层 |阅读模式

    为了方便,使用python测试连接mysql容器

    脚本内容非常简单

    #!/usr/bin/python3
    import pymysql
    
    conn=pymysql.connect(host="localhost",user="root",port=3306,password="12345",database="mydb")
    cursor=conn.cursor()
    cursor.execute("select count(*) from mydb.t")
    cursor.close()
    conn.close()

    执行上述脚本后,发生以下异常

    Traceback (most recent call last):
      File "./p.py", line 6, in <module>
        conn=pymysql.connect(host="localhost",user="root",port=3306,password="12345",database="mydb")
      File "/usr/local/lib64/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
        return Connection(*args, **kwargs)
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
        self.connect()
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 598, in connect
        self._get_server_information()
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 975, in _get_server_information
        packet = self._read_packet()
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 657, in _read_packet
        packet_header = self._read_bytes(4)
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 707, in _read_bytes
        CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
    pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')

    从错误信息看,是不能够正常连接到OMC,刚开始怀疑是pymysql.connect方法中传递的参数不正确,但无论怎么修改,始终出现2013的异常,后来查看mysql配置文件,发现存在一条配置

    bind_address=127.0.0.1

    改参数限制了mysql不能被远程访问,所以在容器外部没法访问到容器内部的mysql,造成2013异常,注释掉改配置,重启mysql docker容器,访问正常

    另外如果将改配置对应的ip地址改为宿主机地址,还会出现如下异常

    Traceback (most recent call last):
      File "./p.py", line 6, in <module>
        conn=pymysql.connect(host="localhost",user="root",port=3306,password="12345",database="mydb")
      File "/usr/local/lib64/python3.6/site-packages/pymysql/__init__.py", line 94, in Connect
        return Connection(*args, **kwargs)
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 325, in __init__
        self.connect()
      File "/usr/local/lib64/python3.6/site-packages/pymysql/connections.py", line 630, in connect
        raise exc
    pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")

    且mysql未正常启动,错误日志中有如下信息

    2019-10-15T02:27:03.769286Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    2019-10-15T02:27:03.791952Z 0 [ERROR] [MY-010262] [Server] Can't start server: Bind on TCP/IP port: Cannot assign requested address
    2019-10-15T02:27:03.791960Z 0 [ERROR] [MY-010257] [Server] Do you already have another mysqld server running on port: 3306 ?
    2019-10-15T02:27:03.792191Z 0 [ERROR] [MY-010119] [Server] Aborting
    2019-10-15T02:27:04.855205Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.17)  MySQL Community Server - GPL.

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-12-22 12:55 , Processed in 0.057754 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表