Python OperationalError: (OperationalError) (2003, "无法连接到 MySQL 服务器上的 '192.168.129.139' (111)") 无 无

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16472175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:44:20  来源:igfitidea点击:

OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on '192.168.129.139' (111)") None None

pythonmysqlsqlsqlalchemy

提问by liv2hak

I am trying to create a remote database using mysql on an Ubuntu machine running 12.04.

我正在尝试在运行 12.04 的 Ubuntu 机器上使用 mysql 创建远程数据库。

It has a root user with remote login enabled and no password.I have started the server.

它有一个启用了远程登录且没有密码的 root 用户。我已经启动了服务器。

output of

输出

sudo netstat -tap | grep mysql

shows

显示

tcp        0      0 localhost:mysql         *:*                     LISTEN      13246/mysqld

I have created a database called nwtopology using (as mentioned root doesn't have a password yet.)

我已经使用创建了一个名为 nwtopology 的数据库(如前所述,root 还没有密码。)

 create database nwtopology
 grant all privileges on *.* to [email protected]
 FLUSH PRIVILEGES;

From the client machine that also runs Ubuntu 12.04 I use a python script to connect to the remote mysql database using sqlalchemy.

在同样运行 Ubuntu 12.04 的客户端机器上,我使用 python 脚本通过 sqlalchemy 连接到远程 mysql 数据库。

from pox.core import core
import pox.openflow.libopenflow_01 as of
import re
import datetime
import time
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import exists

log = core.getLogger()
engine = create_engine('mysql://[email protected]/nwtopology', echo=False)
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()

class SourcetoPort(Base):
    """"""
    __tablename__ = 'source_to_port'
    id = Column(Integer, primary_key=True)
    port_no        = Column(Integer)
    src_address    = Column(String,index=True)

    #-----------------------------------------
    def __init__(self, src_address,port_no):
        """"""
        self.src_address = src_address
        self.port_no     = port_no

The create_engine() call is failing with the following error.

create_engine() 调用失败并出现以下错误。

POX 0.1.0 (betta) / Copyright 2011-2013 James McCauley, et al.
Traceback (most recent call last):
  File "/home/karthik/ms_thesis/pox/pox/boot.py", line 89, in do_import2
    __import__(name, globals(), locals())
  File "/home/karthik/ms_thesis/pox/custom/tutorial.py", line 39, in <module>
    Base.metadata.create_all(engine)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/schema.py", line 2515, in create_all
    tables=tables)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2230, in _run_visitor
    conn = self.contextual_connect(close_with_result=False)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 2340, in contextual_connect
    self.pool.connect(),
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 210, in connect
    return _ConnectionFairy(self).checkout()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 371, in __init__
    rec = self._connection_record = pool._do_get()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 697, in _do_get
    con = self._create_connection()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 174, in _create_connection
    return _ConnectionRecord(self)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 256, in __init__
    self.connection = self.__connect()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/pool.py", line 316, in __connect
    connection = self.__pool._creator()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 80, in connect
    return dialect.connect(*cargs, **cparams)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 280, in connect
    return self.dbapi.connect(*cargs, **cparams)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (OperationalError) (2003, "Can't connect to MySQL server on '192.168.129.139' (111)") None None

I cannot figure out why this is happening?Any help is greatly appreciated?

我不明白为什么会这样?非常感谢任何帮助?

采纳答案by vvladymyrov

Looks like mysql is configured to listen only on localhost.

看起来 mysql 配置为仅在 localhost 上侦听。

You can test this by running telnet 192.168.129.139 3306from your client machine.

您可以通过telnet 192.168.129.139 3306从您的客户端机器运行来测试它。

Most probable reason - mysqld is configured to do so.

最可能的原因 - mysqld 被配置为这样做。

Please try to follow Configuration step described here:

请尝试按照此处描述的配置步骤进行操作

Edit the /etc/mysql/my.cnffile to configure MySQL to listen for connections from network hosts, change the bind-address directive to the server's IP address. For example Replace 192.168.0.5 with the appropriate address. If there are no such entry - uncomment it or create new line.

编辑/etc/mysql/my.cnf文件以配置 MySQL 以侦听来自网络主机的连接,将 bind-address 指令更改为服务器的 IP 地址。例如,将 192.168.0.5 替换为适当的地址。如果没有这样的条目 - 取消注释或创建新行。

bind-address            = 192.168.0.5

Then restart mysqld

然后重启mysqld

sudo service mysql restart

Then test with telnet or by running your application once again. Also netstat would have second entry for mysqld.

然后使用 telnet 或再次运行您的应用程序进行测试。netstat 也会有 mysqld 的第二个条目。