First connect from Prestashop to Google Cloud SQL always fails

非 Y 不嫁゛ 提交于 2020-01-06 07:01:07

问题


I'm setting up a PrestaShop installation on a development server which is a GCE instance and using Cloud SQL as a database server. Everything works just fine except one thing: whenever there is a long period of inactivity on the site, the first page load after that always gives me this error:

Link to database cannot be established: SQLSTATE[HY000] [2003]

If I refresh the page the error is gone and never appears again until I stop using the site for an hour or so. It almost looks like database instance is going into sleep mode or something like that.

The reason I mentioned Prestashop is the fact that I never get this error when using Adminer or connecting to the database from mysql console client.


回答1:


With the per use billing model, instances are spun down after a 15 minute timeout to save you money. They then take a few seconds to be spun up when next accessed. It may be the Prestashop is timing out on these first requests (though I have no experience with that application).

Try changing your instance to a package billing, which has a 12 hour timeout, to see if this helps

https://developers.google.com/cloud-sql/faq#how_usage_calculated




回答2:


According to GCE documentation,

Once a connection has been established with an instance, traffic is permitted in both directions over that connection, until the connection times out after 10 minutes of inactivity

I suspect that might be the cause. To get around it, you can try to lower the tcp keepalive time.




回答3:


Refer here: https://cloud.google.com/sql/docs/compute-engine-access

To keep long-lived unused connections alive, you can set the TCP keepalive. The following commands set the TCP keepalive value to one minute and make the configuration permanent across instance reboots.

# Display the current tcp_keepalive_time value.
$ cat /proc/sys/net/ipv4/tcp_keepalive_time

# Set tcp_keepalive_time to 60 seconds and make it permanent across reboots.
$ echo 'net.ipv4.tcp_keepalive_time = 60' | sudo tee -a /etc/sysctl.conf

# Apply the change.
$ sudo /sbin/sysctl --load=/etc/sysctl.conf

# Display the tcp_keepalive_time value to verify the change was applied.
$ cat /proc/sys/net/ipv4/tcp_keepalive_time


来源:https://stackoverflow.com/questions/21457437/first-connect-from-prestashop-to-google-cloud-sql-always-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!