Created
October 30, 2020 11:26
-
-
Save rishabh-jain424/55ed79d7c3fef2c7c2f6a1111631b14a to your computer and use it in GitHub Desktop.
Configuring a secure connection from a PHP Application to Cloud SQL using Cloud SQL Proxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Note: Before start below steps, Cloud SQL Admin API must be enabled in GCP Project. | |
| cd /var/run/; | |
| sudo mkdir cloud_sql_proxy; | |
| #Go to Home directory | |
| mkdir proxy; | |
| cd proxy; | |
| #download Cloud SQL Proxy | |
| wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy; | |
| chmod +x cloud_sql_proxy; | |
| # Change the Cloud SQL instance name. | |
| sudo nano cloud_sql_proxy.service | |
| [Unit] | |
| Descri ption=Google Cloud SQL Proxy | |
| Requires=networking.service | |
| After=network.service | |
| [Service] | |
| Type=simple | |
| WorkingDirectory=/usr/local/bin | |
| ExecStart=/usr/local/bin/cloud_sql_proxy -dir=/var/run/cloud_sql_proxy -instances=<Cloud-SQL instance name>=tcp:3306 | |
| Restart=always | |
| StandardOutput=journal | |
| User=root | |
| [Install] | |
| WantedBy=multi-user.target | |
| sudo cp cloud_sql_proxy /usr/local/bin/.; | |
| sudo cp cloud_sql_proxy.service /lib/systemd/system/.; | |
| sudo systemctl daemon-reload; | |
| #Below command will make the sql service autostart after VM restart. | |
| sudo systemctl enable cloud_sql_proxy.service; | |
| #To start the SQL proxy now, run below command | |
| sudo systemctl start cloud_sql_proxy; | |
| #Restart server: apache2/nginx. | |
| sudo service apache2 restart; | |
| #******Additional/Troubleshoot commands******** | |
| #To check the status of cloud_sql_proxy service. | |
| sudo systemctl status cloud_sql_proxy; | |
| #To check the status of apache2 service. | |
| sudo service apache2 status; | |
| #******Update an application to connect to Cloud SQL using the Proxy****** | |
| #For PHP- PDO & TCP | |
| #Add these lines in config file of application | |
| #Use a Data source name (DSN) to connect to Cloud SQL through the proxy | |
| #$Configuration['Database']['Dsn'] = 'host=127.0.0.1:3306;dbname=<database-name>'; | |
| #$Configuration['Database']['User'] = 'DATABASE_USER'; | |
| #$Configuration['Database']['Password'] = 'PASSWORD'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment