Answer by Deepthink
To migrate WordPress from a local MySQL server to Google Cloud SQL, update the following in the wp-config.php file:
-
Database Host (
DB_HOST)
Replace'localhost'with the Cloud SQL instance's IP address or connection name (if using the Cloud SQL Proxy).- Public IP: Use the instance's public IP (e.g.,
'123.456.789.0'). - Private IP (GCP VPC): Use the private IP.
- Cloud SQL Proxy: Use the socket path (e.g.,
'localhost:/cloudsql/project-id:region:instance-id').
- Public IP: Use the instance's public IP (e.g.,
-
Database Name (
DB_NAME)
Set to the database name created in Cloud SQL (e.g.,'wordpress_db'). -
Database User (
DB_USER)
Set to the Cloud SQL username (e.g.,'wp_user'). -
Database Password (
DB_PASSWORD)
Set to the Cloud SQL user's password (e.g.,'secure_password'). -
(Optional) SSL Configuration
If enforcing SSL, add these lines (adjust paths to your certificate files):define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL ); define( 'MYSQL_SSL_CA', '/path/to/server-ca.pem' ); define( 'MYSQL_SSL_CERT', '/path/to/client-cert.pem' ); define( 'MYSQL_SSL_KEY', '/path/to/client-key.pem' );
Example Configuration:
// Before
define( 'DB_HOST', 'localhost' );
// After (using public IP)
define( 'DB_NAME', 'wordpress_db' );
define( 'DB_USER', 'wp_cloud_user' );
define( 'DB_PASSWORD', 'your_secure_password' );
define( 'DB_HOST', '203.0.113.10' ); // Cloud SQL public IP
// Optional SSL (uncomment if needed)
// define( 'MYSQL_CLIENT_FLAGS', MYSQLI_CLIENT_SSL );
// define( 'MYSQL_SSL_CA', '/var/www/ssl/server-ca.pem' );
// define( 'MYSQL_SSL_CERT', '/var/www/ssl/client-cert.pem' );
// define( 'MYSQL_SSL_KEY', '/var/www/ssl/client-key.pem' );Additional Steps:
- Authorize WordPress Server IP: In Cloud SQL, whitelist the server's IP under Connections > Networking > Authorized Networks (for public IP).
- Use Cloud SQL Proxy: For enhanced security, deploy the proxy and configure
DB_HOSTto use the proxy socket. - Test Connectivity: Verify the WordPress server can connect to Cloud SQL (e.g., using
mysql -h [IP] -u [user] -p).
This ensures WordPress connects to Cloud SQL instead of the local MySQL instance.