snower / TorMySQL

The highest performance asynchronous MySQL driver by PyMySQL
MIT License
308 stars 63 forks source link

Error while trying to connect to a Google Cloud SQL instance using ssl. #32

Closed c11z closed 6 years ago

c11z commented 6 years ago

We are hosting our MySQL instance in Google Cloud SQL and we use ssl. Our pool is configured like this:

pool = tormysql.ConnectionPool(
    max_connections = 20, #max open connections
    idle_seconds = 7200, #conntion idle timeout time, 0 is not timeout
    wait_connection_timeout = 3, #wait connection timeout
    host = "{GCP_SQL_IP}",
    user = "user",
    passwd = "pass",
    db = "production",
    charset = "utf8",
    ssl = {
        "ca": "/path/to/server-ca.pem",
        "key": "/path/to/client-key.pem",
        "cert": "/path/to/client-cert.pem",
        "check_hostname": False
    }
)

The error out of tornado is:

Traceback (most recent call last):
  File "app.py", line 34, in <module>
    ioloop.run_sync(test)
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/ioloop.py", line 582, in run_sync
    return future_cell[0].result()
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/concurrent.py", line 260, in result
    raise_exc_info(self._exc_info)
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/gen.py", line 1107, in run
    yielded = self.gen.throw(*exc_info)
  File "app.py", line 24, in test
    tx = yield pool.begin()
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/gen.py", line 1099, in run
    value = future.result()
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/concurrent.py", line 260, in result
    raise_exc_info(self._exc_info)
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/gen.py", line 1107, in run
    yielded = self.gen.throw(*exc_info)
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tormysql/helpers.py", line 203, in begin
    connection = yield self.Connection()
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/gen.py", line 1099, in run
    value = future.result()
  File "/home/cory/.local/share/virtualenvs/tortest-C-kyhIye/lib/python2.7/site-packages/tornado/concurrent.py", line 260, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on {GCP_SQL_IP}:3306 (OperationalError(2013, 'Lost connection to MySQL server during query'))")

And the error in the SQL logs is:

{
    severity:  "ERROR"  
    textPayload:  "2018-04-03T18:33:14.500739Z 166871 [Note] Aborted connection 166871 to db: 'production' user: 'cory' host: '67.188.16.74' (Got an error reading communication packets)"  
    timestamp:  "2018-04-03T18:33:14.500917Z"
} 

So my guess is that there is a bug with how tormysql uses ssl when it creates a connection pool of pymysql connections.

snower commented 6 years ago

Thanks, I will test it.

snower commented 6 years ago

Already fixed, you can follow the new version and thanks for feedback.

c11z commented 6 years ago

Thank you so much for the quick turn around. Currently we are still on Tornado 4.5. If you wanted to back patch this to 3.6.1 we would love it. But we have a plan to get to 5.0 soon so we'll get there eventually.