MySQLdb连接问题 [英] MySQLdb connection problems

查看:37
本文介绍了MySQLdb连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MySQLdb模块时遇到问题.

I'm having trouble with the MySQLdb module.

db = MySQLdb.connect(
    host = 'localhost', 
    user = 'root', 
    passwd = '', 
    db = 'testdb', 
    port = 3000)

(我正在使用自定义端口)

(I'm using a custom port)

我得到的错误是:

Error 2002: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

这没有什么意义,因为这是my.conf中设置的默认连接.这似乎是在忽略我提供的连接信息.

Which doesn't make much sense since that's the default connection set in my.conf.. it's as though it's ignoring the connection info I give..

mysql服务器肯定在那里:

The mysql server is definitely there:


[root@baster ~]# mysql -uroot -p -P3000
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use testdb;
Database changed
mysql> 

我直接在python提示符下尝试过

I tried directly from the python prompt:


>>> db = MySQLdb.connect(user='root', passwd='', port=3000, host='localhost', db='pyneoform')
Traceback (most recent call last):
File "", line 1, in 
File "/usr/lib64/python2.5/site-packages/MySQLdb/__init__.py", line 74, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.5/site-packages/MySQLdb/connections.py", line 169, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")
>>>

我很困惑...:(

推荐答案

确保mysql服务器正在侦听tcp连接,您可以使用netstat -nlp(在* nix中)进行此操作.这是您尝试建立的连接类型,出于安全原因,默认情况下,db通常不会在网络上侦听.另外,在使用mysql命令时,请尝试指定--host = localhost,这也将尝试通过unix套接字进行连接,除非另行指定.如果未将mysql配置为监听TCP连接,则该命令也会失败.

Make sure that the mysql server is listening for tcp connections, which you can do with netstat -nlp (in *nix). This is the type of connection you are attempting to make, and db's normally don't listen on the network by default for security reasons. Also, try specifying --host=localhost when using the mysql command, this also try to connect via unix sockets unless you specify otherwise. If mysql is not configured to listen for tcp connections, the command will also fail.

这是 mysql 5.1手册和对连接进行故障排除.请注意,所描述的错误(2002)与您得到的错误相同.

Here's a relevant section from the mysql 5.1 manual on unix sockets and troubleshooting connections. Note that the error described (2002) is the same one that you are getting.

或者,检查所使用的模块是否具有通过unix套接字进行连接的选项(如David建议).

Alternatively, check to see if the module you are using has an option to connect via unix sockets (as David Suggests).

这篇关于MySQLdb连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆