Aiomysql (2003, "Can't connect to MySQL server on '127.0.0.1'")

430 views Asked by At

I have a python script that spawns processes with the concurrent.futures package, and all these processes send INSERT queries to my database hosted on the same machine (I connect to it by using 127.0.0.1)

Every time it works for a few hours, then those queries start returning : (2003, "Can't connect to MySQL server on '127.0.0.1'")

Upon failure my program tries to send a new query 2 seconds later.(They will all fail too)

The database is not down because I can still send requests to it on MySQL Workbench, that systemctl status indicates it's active, and that I even restarted mysql and mariadb through systemctl restart (while the script was failing to send queries) and it still wouldn't work.

But, if I simply killed all the processes my script made, and started the script again, everything work.

I have no idea where to even begin to search, I tried to look on google and in this forum but it's never the same as my issue and I'm kind of lost here

Thank you for your help

Below is the code responsible for sending requests, and handling failures. I know it doesn't look great.

    async def request_wrapper(query_type, request, dict=False, retry=0, charset='', collation=None, db='twitch_chat'):
        try:
            conn = await Connect(db)
            if query_type == 'INSERT' and len(charset) > 0:
                try:
                    await conn.set_charset_collation(charset=charset)
                except Exception as e:
                    print(e,"[#994]")
            cursor = await conn.cursor()
            await cursor.execute('SET NAMES utf8mb4')
            await cursor.execute("SET CHARACTER SET utf8mb4")
            await cursor.execute("SET character_set_connection=utf8mb4")
            await cursor.execute(request)
            if query_type == "SELECT":
                result = await cursor.fetchall()
                await cursor.close()
                conn.close()
                return result
            await cursor.close()
            conn.close()
        except Exception as e:
            try:
                if e[0] == 1062:
                    return
            except:
                print("======================")
                print(e, "#00.5")
                print("======================")
                return
            if retry < 3:
                print("======================")
                print(f"({datetime.datetime.now().strftime('%H:%M')}) [MYSQL ERROR]", request)
                print(e)
                print("RETRY IN 2 SECONDS.")
                print("======================")
                sleep(2)
                await request_wrapper(query_type, request, dict, retry+1, charset, collation)
                try:
                    conn.close()
                except Exception as e:
                    print(e, "#993")
    
    async def Connect(db):
        try:
            conn = await aiomysql.connect(
                host = credentials.host,
                db = db,
                user = credentials.user,
                password = credentials.password,
                autocommit=True
            )
        except Exception as err:
            print(f"({datetime.datetime.now().strftime('%H:%M')})",err, "\n Retrying in 2 seconds", "#992")
            sleep(2)
            return await Connect(db)
        return conn

The script log file indicate it passes through where you'll see "#992" in the code(in the Connect function in the exception handler)

0

There are 0 answers