Syntax: driver:host=the-host-name;dbname=your-database-name
Data Source Name (DSN) specifies the host computer for the MySQL database and the name of the database. If the MySQL database is running on the same server as PHP, use the localhost keyword to specify the host computer.
Syntax: new PDO(dsn, username, password);
Assuming that you have already set up your PHP environment on GCP App Engine (this introduction uses Standard App Engine). If you have not set up your PHP environment on GCP App Engine or do not have Cloud SDK available, please refer to PHP deployment on GCP.
Also assume that you have already set up your Cloud SQL. If you have not set up your Cloud SQL, please refer to DB setup on GCP.
your-project-name:us-east4:your-sql-instance-ID
(which is the SQL instance connection name)your-database-name
root
(unless you created other users)password-you-set-for-the-root-user
(unless you created other users)To find the instance connection name, go to the Cloud SQL instances page. Go to the overview page of your SQL instance and locate the Instance connection name.
driver:host=your-project-name:us-east4:your-sql-instance-ID;dbname=your-database-name
.
For example, if your project name is cs4640, your SQL instance ID is db-demo, and the database name is guestbook,
host would be cs4640:us-east4:db-demo
DSN would be mysql:host=cs4640:us-east4:db-demo;dbname=guestbook
(where PDO driver is mysql).
If the above setting results in "getaddrinfo failed: Name or service not known" message, please try the following DSN settting.
mysql:unix_socket=/cloudsql/cs4640:us-east4:db-demo;dbname=guestbook
Assuming that you have already set up your Cloud SQL. If you have not set up your Cloud SQL, please refer to DB setup on GCP.
Enter the IP address you got previously, (this will allow your IP address to make a connection to the SQL instance), click Done and then click Save.
host would be your-cloud-instance-public-IP-address
DSN would be mysql:host=your-cloud-instance-public-IP-address;dbname=your-database-name
Assuming that you have already set up your PHP environment. If you have not set up your PHP environment, please refer to PHP deployment on GCP or PHP deployment using XAMPP.
Also assume that you have already set up your MySQL database using XAMPP. If you have not set up your database, please refer to DB setup using XAMPP.
your-host-name:port-number
your-database-name
root
(unless you create other users)password-you-set-for-the-root-user
(unless you create other users)localhost
SHOW VARIABLES WHERE Variable_name = 'port';
3306
.
driver:host=your-host-name:port-number;dbname=your-database-name
.
For example, assuming the database name is "guestbook", the DSN would be
mysql:host=localhost:3306;dbname=guestbook
(where PDO driver is mysql).
Released under the CC-BY-NC-SA 4.0 license.
Last updated 2022-05-30 18:12