Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python (1040, 'Too many connections')

im newbie in Python. I have Item table with 3 attributes ( name, price, image) I want to get html from link and save to table. So i do that

from books.models import Book, Author, Publisher, Item
import urllib
import gzip
from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
import MySQLdb as mdb

def updateItems(request): 
    con = mdb.connect('localhost', 'anhnt', '12', 'db_django');
    cur = con.cursor()
    link = "http://pandagift.vn/i263/price1/qua-tang-doc-dao.htm"
    request = urllib.urlopen(link)
    output1 = open('web.txt', 'wb')
    html = ""
    html = request.read()
    output1.write(html)
    output1.close()
    beautifulSoup = BeautifulSoup(html)
    list_item = beautifulSoup.findAll("div", {"class":"Item_ProcNews"})
    if list_item:
    for item in list_item :
        #lay anh san pham
        image = item.findAll("img")[0]      
        st = str(image)
        beginindex = st.find('src') + 5
            endindex = st.find('jpg') + 3
        if(endindex == 2):
                endindex = st.find('png') + 3
            if(endindex == 2):
            endindex = st.find('jpeg') + 4
            if(endindex == 3):
                endindex = st.find('gif') + 3
            if(endindex == 2):
                        endindex = st.find('bmp') + 3
                if(endindex == 2):
                        endindex = st.find('JPG') + 3
        itemimage = str(image)[beginindex:endindex].encode('utf-8')
        #lay ten san pham
            name = item.findAll("span", {"class":"item-name"})[0]
        temp = name.findAll("a")[0]
        #itemname = temp.get('alt').encode('utf-8')
        itemname = temp.string.encode('utf-8')
            #lay gia san pham
            price = item.findAll("span", {"class":"price"})[0]
        #temp1 = str(price)
        #beginindex1 = temp1.find('price') + 7
            #endindex1 = temp1.find('/span') -1
        #itemprice = str(temp1)[beginindex1:endindex1]
        itemprice = str(price.string)
        #luu vao csdl
        query = "INSERT INTO books_item(name, price, image) VALUES('"+itemname+"', '"+itemprice+"', '"+itemimage+"')"
        cur.execute(query)
        #print query
    else:
        cur.execute("INSERT INTO books_item(name, price) VALUES('Hat', '10000vnd')")
    updateItems(request)

Its return

  Exception Type:   OperationalError
  Exception Value:  (1040, 'Too many connections')

Please tell me why its happen and can i do fix it. Thanks many :)

like image 382
lex_luther_9x Avatar asked Dec 08 '22 08:12

lex_luther_9x


1 Answers

I solved this problem by increasing the maximum number of connections allowed by MySQL in my django app settings.py file. More information in MySQL docs

DATABASES = { 
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': '',
    'USER': '',
    'PASSWORD': '',
    'HOST': 'localhost',
    'PORT': '3306',
    'OPTIONS': {
           "init_command": "SET GLOBAL max_connections = 100000", #<-- The fix
    }
  }
}
like image 162
endur Avatar answered Dec 11 '22 09:12

endur