Python connection to mysql error: Cant connect to MySQL server on 127.0.0.1

Source: Internet
Author: User

Python does a program that needs to read data frequently and write data.

After a few days of running, I found that a MySQL link error occurred after a few hours of running, and the key error message is as follows:

2003, "Can" T connect to the MySQL server on ' 127.0.0.1 ' (10048)

Read some information, MySQL 10048 error is because the port is not enough to create a new link! The problem is the original link I have closed ah! The key is that the closed link, whose port is not immediately available, has a Windows default port cycle of 240 seconds.

Solution, from two aspects:

1. Increase available ports

Locate the Hkey_local_machine/system/currentcontrolset/services/tcpip/parameters registry subkey
and create a new REG_DWORD value named MaxUserPort
Set this value to decimal minimum 32768
The wait time for this value will be 30 seconds.
Reboot the Windows system.
Default value for this entry: 5000 (decimal)

2, shorten the port available time

Locate the Hkey_local_machine/system/currentcontrolset/services/tcpip/parameters registry subkey
and create a new REG_DWORD value named TcpTimedWaitDelay
Set this value to decimal 30, hexadecimal to 0x0000001e
The wait time for this value will be 30 seconds.
Default value for this entry: 0xF0 (16), wait time set to 240 seconds

and test the connection to MySQL.

  code is as follows copy code

Import MYSQLDB
db = MySQLdb.connect (host= "localhost", user= "root", passwd= "root", db= "Slopeone")
cursor = Db.cursor ()
F = open ("Sample.data", "R")
for line in F:
    content = Line.rstrip (' \ n '). Split ("\ T")
&N bsp;   user_id = content[0]
    item_id = content[1]
    rating = content [2]
    print user_id,item_id,rating
    cursor.execute (insert INTO Oso_user_ Ratings VALUES (%s,%s,%s) ", (user_id,item_id,rating))
F.close ()
print" over! "

The

is successful, and Reiki has been saved successfully.

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.