1
votes

I am using ruby on the flexible app engine. I need to connect to a generation two cloud SQL server on the same project. I have tried directly specifying the socket from within database.yml as well as connecting via localhost, and the IPv4 address for my cloud sql instance, but nothing seems to work. I have looked at all the similar threads for this question on StackOverflow but nothing worked. I would try using a service account, but I can't find a working example of this with ruby.

The closest I have been able to get is #<Mysql2::Error: Lost connection to MySQL server at 'reading initial communication packet', system error: 0>. Please advise.

1
Can you show your code somewhere? stackoverflow.com/help/mcve - Herman

1 Answers

2
votes

This is less than ideal, but from what I can find elsewhere, the only supported way right now of connecting to a gen 2 cloud SQL server from flexible app environment is as follows. I'll accept any answer that can do this but without having to white-list IP all incoming connections. Preferably there is a way of doing this via localhost, but from what I gather, this is not yet the case.

  1. Disable non-SSL connections and download the SSL certificate bundle for your Cloud SQL generation 2 server enter image description here
  2. Allow all incoming IPv4 addresses on your Cloud SQL server enter image description here
  3. Set the root password for your Cloud SQL server (though technically unneeded since we are already using an SSL certificate bundle?)
  4. Copy the IPv4 address for your Cloud SQL server and the root password
  5. Paste the IP and password directly into your database.yml file, as below, and include the certificate bundle, as bellow:

    # config/database.yml
    
    production:
      adapter: mysql2
      encoding: utf8
      pool: 5
      host: <IP ADDRESS HERE>
      database: <DATABASE NAME HERE>
      username: root
      password: <PASSWORD HERE>
      sslca: config/ssl/server-ca.pem
      sslcert: config/ssl/client-cert.pem
      sslkey: config/ssl/client-key.pem