4

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!

SupimpaAllTheWay
  • 1,308
  • 3
  • 16
  • 22

1 Answers1

1

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

Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
  • Hi @Bernd Buffen and thanks for your help, the main problem is not running it locally, I'm using CloudSQL (gae service) and this error occurs on production when I'm using this service, so I don't have this directory to look at :\ . Sorry if I didn't understand what you mean. – SupimpaAllTheWay Jun 26 '16 at 19:39
  • @Dyego - you can also change it via SQL. i have add it in my answer – Bernd Buffen Jun 26 '16 at 19:44
  • Sorry for being so late, I just connected to mysql and it's already setted "max_allowed_packet | 4194304 |". I upvoted your answer because it helped me to investigate the problem. If you think of anything else that could be happening I'd appreciate the help. :) Thanks. – SupimpaAllTheWay Jun 28 '16 at 19:56
  • @Dyego - have you tested with a bigger size ? the size is not absolute, its depends on the a single sql-line in your SQL file. so its possible that you must set it to 1 GB – Bernd Buffen Jun 28 '16 at 21:08