简体   繁体   中英

MySQL connection fast through mysql cli, but slow using PDO

I have a MySQL database running on Google Cloud. It has SSL enforced, and so I use certificates and keys to connect to it:

  • On the command line, I use: mysql --ssl-ca=/path/to/server-ca.pem --ssl-cert=/path/to/client-cert.pem --ssl-key=/path/to/client-key.pem --host=ip-address --user=username --password
  • With PDO, I use the PDO::MYSQL_ATTR_SSL_CA , PDO::MYSQL_ATTR_SSL_CERT and PDO::MYSQL_ATTR_SSL_KEY options to indicate the required files.

Both connections work fine, except that the PDO connection in PHP is very slow. The CLI method takes milliseconds to connect and execute a queries, while the PDO method takes 5-10 times longer for the same amount of connections. I tried both methods from the same machine, so it doesn't seem to be a hardware/network issue. Could PDO be causing issues here?

I'm using Laravel in case that might be relevant.

Update: things I've tried

  • Run any other PHP script (that doesn't include a MySQL connection) on the same server: perfectly fast.
  • Run a PHP script that connects to a database on 127.0.0.1 / localhost and performs a query: perfectly fast.
  • Connect and query using MySQL CLI (as already mentioned in the question): perfectly fast - although hard to verify how fast so I could be imagining it.
  • Connect and query though PHP/PDO from different machines using all the same settings: very slow, just like the original machine I tried it on.

So the only thing I haven't tried yet is turning off SSL/TLS. Unfortunately, I cannot do that with this instance for security reasons. Also, based on the fact that a SSL/TLS connection using the CLI is very fast, I'm concluding that it must be related to something PHP- or PDO-specific.

I'm going to do some debugging myself and will add any relevant results once I have them.

I ended up opting for a Google Cloud Compute VM to host my PHP and then connect through a private IP. It appears that this is working.

I'm not sure if PDO was actually slower than the MySQL CLI anymore, it may have just seemed so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM