SupimpaAllTheWay
SupimpaAllTheWay

Reputation: 1478

GAE Python - OperationalError: (2013, 'Lost connection to MySQL server during query')

I've been trying to connect to ClouSQL using Flexible Environments (vm:true)

but when I upload my app using:

gcloud preview app deploy --version MYVERSION

An error is thrown:

OperationalError: (2013, 'Lost connection to MySQL server during query')

I found out that it might be because the query is too large but I think that's not the case because it works locally and on production when I wans't using flexible environments with MySQLdb.

My code:

import os
import logging
import pymysql

class MySQL(object):
    '''
    classdocs
    '''
    # TO INSTALL LOCAL DB: http://stackoverflow.com/questions/30893734/no-module-named-mysql-google-app-engine-django


    @classmethod
    def getConnection(cls):
        # When running on Google App Engine, use the special unix socket
        # to connect to Cloud SQL.
        if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
            logging.debug('PROJECT [%s], INSTANCE[%s] - USER [%s] - PASS [%s], SCHEMA [%s]',
                          os.getenv('CLOUDSQL_PROJECT'),
                          os.getenv('CLOUDSQL_INSTANCE'),
                          os.getenv('CLOUDSQL_USER'),
                          os.getenv('CLOUDSQL_PASS'),
                          os.getenv('CLOUDSQL_SCHEMA'))

            db = pymysql.connect(unix_socket='/cloudsql/APP:REGION:INSTANCENAME')
                    #os.getenv('CLOUDSQL_PROJECT'),
                    #os.getenv('CLOUDSQL_INSTANCE')), 
                    #user=os.getenv('CLOUDSQL_USER'),
                    #passwd=os.getenv('CLOUDSQL_PASS'),  
                    #db=os.getenv('CLOUDSQL_SCHEMA'))
        # When running locally, you can either connect to a local running
        # MySQL instance, or connect to your Cloud SQL instance over TCP.
        else:
            db = pymysql.connect(host=os.getenv('DBDEV_HOST'), user=os.getenv('DBDEV_USER'), 
                                 passwd=os.getenv('DBDEV_PASS', ''), db=os.getenv('DBDEV_SCHEMA'))

        return db

Any thoughts on this?

Thanks!

Upvotes: 4

Views: 1141

Answers (1)

Bernd Buffen
Bernd Buffen

Reputation: 15057

take a look in you my.cnf in the /etc/mysql/ directory and change the parameter max_allowed_packet and set the value higher. then you must restart the Database

you can also change this value via SQL like this:

MariaDB [yourSchema]> show GLOBAL variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 2097152 |
+--------------------+---------+
1 row in set (0.00 sec)

MariaDB [yourSchema]> SET GLOBAL max_allowed_packet=2*2097152;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yourSchema]> show GLOBAL variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 4194304 |
+--------------------+---------+
1 row in set (0.00 sec)

MariaDB [yourSchema]>

MariaDB Manual:

max_allowed_packet

Description:

Maximum size in bytes of a packet or a generated/intermediate string. The packet message buffer is initialized with the value from net_buffer_length, but can grow up to max_allowed_packet bytes. Set as large as the largest BLOB, in multiples of 1024. If this value is changed, it should be changed on the client side as well. See slave_max_allowed_packet for a specific limit for replication purposes.

Commandline: --max-allowed-packet=#

Scope: Global

Dynamic: Yes

Data Type: numeric Default Value: 1048576 (1MB) <= MariaDB 10.1.6, 4M >= MariaDB 10.1.7, 1073741824 (1GB) (client-side)

Range: 1024 to 1073741824

Upvotes: 1

Related Questions