Python (1040, 'Too many connections')

linux, mysql, mysql-python, python

Solution

http://dev.mysql.com/doc/refman/5.0/en/too-many-connections.html

If you get a Too many connections error when you try to connect to the mysqld server, this means that all available connections are in use by other clients.

The number of connections permitted is controlled by the max_connections system variable. Its default value is 100. If you need to support more connections, you should set a larger value for this variable.

This means that you have opened more connections to the database than permitted by the server without closing them. I never see calls to `close` the connection `con` in your code.

Problem

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 :)

Original source