简体   繁体   中英

“Can't connect to MySQL server on 'localhost' (10061)” error in python

Just today I have decided that I wanted to learn python because I personally like the syntax and its feel. I am a pretty young developer and I really wanted to learn a new programming language since I am only fluent in JavaScript (especially javascript) and PHP so I decided on Python . Python seems quite like javascript execpt for the fact that they don't use curly braces to define a new block. And of course its far more powerful.

I've learned that you can connect to MySQL databases using MySQLdb .

But I am having an issue, when I use the following code.

db = MySQLdb.connect(host="localhost", user="methodjs_postes", passwd="********", db="methodjs_postes");

I get this large error:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    db = MySQLdb.connect(host="localhost", user="methodjs_postes", passwd="********", db="methodjs_postes");
  File "C:\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' (10061)")

The main part that I was really focusing on though was the Can't connect to MySQL server on 'localhost' (10061) part. (But it would be nice if you explain the other parts too)

I don't see why i'm getting this issue. I use localhost in my PHP and it works just fine. And I did import the MySQLdb which I don't think is the error because then it wouldn't even attempt to connect.

I am using python 2.7 , I would be using 3.3 (have it downloaded) but MySQLdb only supports up to 2.7.

When using select User,Host,Password from mysql.user where User like '%methodjs_postes%'; I get the following error:

SELECT command denied to user 'methodjs'@'localhost' for table 'user'

只需将localhost替换为127.0.0.1即可

http://chat.stackoverflow.com/rooms/22618/discussion-between-shawn31313-and-suku

As per out chat, the conclusion is :- "Your mysql host is a remote machine and which is not allowing remote mysql connections"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM