使用python连接到远程数据库

时间:2015-03-27 10:44:00

标签: python mysql

在终端中使用正确的值运行此命令我已连接并可以读取数据库:

mysql -h mysql.lyon...fr -u user --password=passworld

我必须在python脚本中执行此操作,我尝试了几个函数,如:

import MySQLdb as mysql

print '======================================================================================'
print 'Connextion to the database'
print '======================================================================================'
# mysql -h mysql.lyon....fr -u XXuser --password=XXpasswd

import MySQLdb as mdb

con = mdb.connect(host='mysql....grid5000.fr'  , user='XXuser', passwd='XXpasswdread oar2')
======================================================================================
Connextion to the database
======================================================================================

    Traceback (most recent call last):
      File "process.py", line 14, in <module>
        con = mdb.connect(host='mysql.lyon......fr'  , user='user name ', passwd='......')
      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)
    _mysql_exceptions.OperationalError: (1045, "Access denied for user 'oarreader'@'flyon.lyon.grid5000.fr' (using password: YES)")

如何定义connect函数中的参数以连接到知道其地址的服务器?

1 个答案:

答案 0 :(得分:0)

您没有使用终端中的同一用户,在终端中使用-u user并使用python使用oarreader