Learn Python3 MySQL database syntax encountered problems: "Can ' t connect to MySQL server on ' localhost '

Source: Internet
Author: User

  Run the learning tutorial inside the MySQL database in accordance with the steps to install pymysql, learning the contents, there will be some doubts, I also met, found that this is a recognition error, the example is as follows: 

Run the following code according to the basic tutorial, and you will get an error:

#!/usr/bin/python3ImportPymysql# Open Database connectionDb=Pymysql.Connect("LocalHost","Test","Test","DB" )# Create a Cursor object using the cursor () method cursorCursor=Db.Cursor()# Execute SQL Query using the Execute () methodCursor.Execute("Select VERSION ()")# Use the Fetchone () method to get a single piece of data.Data=Cursor.Fetchone()Print ("Database Version:%s" %Data)# Close the database connectionDb.Close()

Error content:
Traceback (most recent):
File "C:\Python34\lib\site-packages\pymysql-0.7.10-py3.4.egg\pymysql\connections.py", line 916, in Connect
**kwargs)
File "C:\Python34\lib\socket.py", line +, in Create_connection
Raise Err
File "C:\Python34\lib\socket.py", line 503, in Create_connection
Sock.connect (SA)
Connectionrefusederror: [Winerror 10061] Unable to connect because the target computer was actively rejected.

During handling of the above exception, another exception occurred:

Traceback (most recent):
File "c:/users/*/pycharmprojects/untitled/.idea/mysql.py", line 6, <module>
db = Pymysql.connect (host= "localhost", user= "root", password= "", db= "Test_pymysql", port=3306)
File "C:\Python34\lib\site-packages\pymysql-0.7.10-py3.4.egg\pymysql\__init__.py", line +, in Connect
Return Connection (*args, **kwargs)
File "C:\Python34\lib\site-packages\pymysql-0.7.10-py3.4.egg\pymysql\connections.py", line 706, in __init__
Self.connect ()
File "C:\Python34\lib\site-packages\pymysql-0.7.10-py3.4.egg\pymysql\connections.py", line 963, in Connect
Raise EXC
Pymysql.err.OperationalError: (2003, "can ' t connect to MySQL server on ' localhost ' ([winerror 10061] cannot connect because the target computer is actively rejecting. )")

Process has ended, exit code 1

Encounter this problem, very uncomfortable, do not know why this, I specifically to see the various aspects of the place, spent 2 days, such as some official api:http://pymysql.readthedocs.io/en/latest/, English content, not easy to see, But did not find the reason, did not understand, blocked.
Can't think of reason, later look for information, online data A lot, later saw a piece of information, very reference value, perhaps you read the wake up: http://bbs.csdn.net/topics/391931225?page=1
Summarize the causes and consequences:
1, recognize the error: First look at the installation steps have installed Pymysql, thought is a database, in fact, Pymysql is not a database, just between Python and MySQL play a bridge between the role, the example provided in the account password is generally not useful, is a prerequisite
2, did not install MySQL database: Some tutorials only said install Pymysql, did not say to install MySQL. Did not say installation, we do not know the installation, not installed on the error. Installing MySQL is the MySQL premise for learning python.
about how to install MySQL, not available here, it is recommended to install the compression package version, need to manually match something, but it will be more useful, especially to uninstall MySQL.

Learn Python3 MySQL database syntax encountered problems: "Can ' t connect to MySQL server on ' localhost '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.