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

时间:2013-05-09 22:26:57

标签: python mysql sql sqlalchemy

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

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

的输出
sudo netstat -tap | grep mysql

显示

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

我创建了一个名为nwtopology的数据库(如上所述root还没有密码。)

 create database nwtopology
 grant all privileges on *.* to root@192.168.129.221
 FLUSH PRIVILEGES;

从运行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://root@192.168.129.139/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

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

我无法弄清楚为什么会发生这种情况?非常感谢任何帮助?

1 个答案:

答案 0 :(得分:13)

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

您可以通过从客户端计算机运行telnet 192.168.129.139 3306来对此进行测试。

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

请尝试关注Configuration step described here

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

bind-address            = 192.168.0.5

然后重启mysqld

sudo service mysql restart

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