我一直在尝试使用灵活环境连接到ClouSQL(vm:true)
但是当我上传我的应用时使用:
gcloud preview app deploy --version MYVERSION
抛出错误:
OperationalError: (2013, 'Lost connection to MySQL server during query')
我发现这可能是因为查询太大但我认为情况并非如此,因为当我不使用MySQLdb的灵活环境时,它可以在本地和生产中使用。
我的代码:
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
对此有何想法?
谢谢!
答案 0 :(得分:1)
在/ etc / mysql /目录中查看my.cnf并更改参数 max_allowed_packet 并将值设置得更高。那么你必须重新启动数据库
您也可以通过SQL更改此值:
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手册:
<强> max_allowed_packet的强>
说明强>
数据包或a的最大字节数 生成/中间字符串。包消息缓冲区是 使用net_buffer_length中的值初始化,但可以长大到 max_allowed_packet个字节。设置与最大的BLOB一样大 1024的倍数。如果更改此值,则应更改此值 客户端也是如此。有关具体内容,请参阅slave_max_allowed_packet 限制复制目的。
命令行: --max-allowed-packet =#
范围:全球
动态:是
数据类型:数字默认值:1048576 (1MB)&lt; = MariaDB 10.1.6,4M&gt; = MariaDB 10.1.7,1073741824(1GB) (客户端)
范围: 1024到1073741824