Connecting Laravel to GAE Cloud SQL Database with PostgreSQL - laravel

I am deploying a new application to Google App Engine Flex environment. The app is powered by Laravel.
I am able to connect to the database on my local machine, however, I am unable to connect to the database once I deploy my application.
What information do I need to include in the app.yaml file to connect to the database? Do I require any other information in a different file?
"message": "SQLSTATE[08006] [7] could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (::1) and accepting\n\tTCP/IP connections on port 5432?\ncould not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432? (SQL: select * from \"users\" where \"email\" = mitchell#efficialtec.com and \"users\".\"deleted_at\" is null limit 1)",
This is the contents of my .ENV file which allows me to connect locally
APP_ENV=development
APP_KEY=base64:B0G3Yr82fWO7xw8LrvcOC19DGUAEd32loJlPHCfP2sg=
APP_DEBUG=true
LOG_CHANNEL=stack
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_POST=5432
DB_DATABASE=DATABASE
DB_USERNAME=USERNAME
DB_PASSWORD=PASSWORD
DB_SOCKET: "/cloudsql/CONNECTION_NAME"
This is the contents of my app.yaml file:
env: flex # let app engine know we use flexible environment
service: SERVICENAME
automatic_scaling:
min_num_instances: 1
max_num_instances: 2
cpu_utilization:
target_utilization: 0.8
runtime_config:
document_root: public
skip_files:
- .env #we want to skip this to make sure we don’t mess stuff up on the server
env_variables:
# Put production environment variables here.
APP_ENV: development
APP_DEBUG : true # or false
APP_KEY: base64:B0G3Yr82fWO7xw8LrvcOC19DGUAEd32loJlPHCfP2sg=
APP_LOG: daily
APP_TIMEZONE: UTC #your timezone of choice
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
# values obtained when configuring your Cloud SQL instance.
POSTGRES_USER: USERNAME
POSTGRES_PASSWORD: PASSWORD
POSTGRES_DSN: pgsql:dbname=bnsw;host=/cloudsql/CONNECTION_NAME"
DB_HOST: localhost
DB_DATABASE: DATABASE
DB_USERNAME: USERNAME
DB_PASSWORD: PASSWORD
DB_SOCKET: "/cloudsql/CONNECTION_NAME"
beta_settings:
cloud_sql_instances: "CONNECTION_NAME"
Any help on this would be greatly appreciated, I have been looking on Google for almost a week with no answers yet!

By default, Laravel does not have DB_SOCKET under pgsql connection.
Thus, in order to connect to cloud SQL connection from app engine, you env show look like :
DB_CONNECTION=pgsql
DB_HOST=/cloudsql/CONNECTION_NAME
DB_DATABASE=DATABASE
DB_USERNAME=USERNAME
DB_PASSWORD=PASSWORD
Also please, don't include DB_PORT in the database env variables.

Related

How do i setup websocket on GCP Flex environment?

I am using laravel 6 and I want to deploy laravel based websocket server using supervisor on google cloud Flex app engine platform.
I am using this package: beyondcode/laravel-websockets
I have setup everything and everything is working fine on local machine! What is the exact procedure to follow to deploy it on GCP?
My additional-supervisord.conf
[program:websockets]
command = php %(ENV_APP_DIR)s/artisan websockets:serve
enviroment = PORT="6001"
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes=0
user = root
autostart = true
autorestart = true
priority = 10
stopwaitsecs = 20
App.yaml
runtime: php
env: flex
runtime_config:
document_root: public
service: orders
skip_files:
- .env
#only for testing purpose.
manual_scaling:
instances: 1
env_variables:
APP_NAME: "Havn"
BROADCAST_DRIVER: pusher
PUSHER_APP_ID: SOMEID
PUSHER_APP_KEY: SOMEKEY
PUSHER_APP_SECRET: SOMESECRET
PUSHER_APP_CLUSTER: mt1
LARAVEL_WEBSOCKETS_PORT: 6001
PUSHER_SCHEME: https
PUSHER_HOST: 127.0.0.1
PUSHER_PORT: 6001
QUEUE_DRIVER: database
beta_settings:
cloud_sql_instances: "INSTANCE ID"
You can find in the official documentation how to use WebSockets to create a persistent connection from a client (such as a mobile device or a computer) to an App Engine instance.
Please consider that WebSockets connection will time out after one hour.
Creating Persistent Connections with WebSockets PHP

gcloud app SQLSTATE[HY000] [2002] Connection timed out

I have created laravel application with database it run on localhost so i decide to deploy on GCP appengine but when i click on deploy this happen
SQLSTATE[HY000] [2002] Connection timed out
i connecting to my GCP sql Instance below are my app.yaml
1st i use local host that uses sql_proxy it show no files directory then i remove the DB_Socket then this error come out SQLSTATE[HY000] [2002] Connection timed out
runtime: php
env: flex
runtime_config:
document_root: public
Ensure we skip ".env", which is only for local development
skip_files:
- .env
env_variables:
# Put production environment variables here.
APP_LOG: errorlog
APP_KEY: base64:G9druqluKMZ6TugSgmM1OJVwWSzfstc08acMRBAqxwg=
CACHE_DRIVER: database
SESSION_DRIVER: database
## Set these environment variables according to your CloudSQL configuration.
DB_HOST: 35.231.242.40
DB_DATABASE: lkcfes
DB_USERNAME: root
DB_PASSWORD:
This question is more appropriate for a support case so that a Google Cloud Platform support agent can inspect your app.yaml and Cloud SQL instance.
Having said that, there is a guide here on running Laravel on GAE Flex. You'll find it includes using a "beta_settings" section on your yaml, the Cloud SQL instance's public IP is not used.

Laravel Homestead could not connect to server 127.0.0.1

I have installed laravel homestead and vagrant. After vagrant up my local site started work.
My migrations worked well and i could connect to DB with PhpStorm ( http://joxi.ru/a2X45M1S1x3Vw2 )
my .env file contains the same code as written in doc
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=54320
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
But when I try to register new user I got an error
SQLSTATE[08006] [7] could not connect to server: Connection refused Is
the server running on host "127.0.0.1" and accepting TCP/IP
connections on port 54320?
( http://joxi.ru/nAynW7asYOD39r )
How it possible when migrations run well but connection refused?
Your screenshot says that Laravel is having trouble connecting to 192.168.10.10, which isn't the same as 127.0.0.1. Check that the correct DB_HOST is set in your .env file.
In case the config from the .env file isn't being used, also check the config/database.php file and verify the settings there too.
You have to change .env -> DB_PORT
for pgsql use DB_PORT=5432
I was having this same issue, and what worked for me was to change my DB_HOST=127.0.0.1 to ip: "192.168.56.56"(the same id that is on my Homestead.yaml file).

Laravel CI/CD in Gitlab, Docker set my IP MySQL

I followed this Gitlab tutorial to test and deploy Laravel applications with GitLab CI/CD and Envoy.
All works well, but when I run a pipeline it shows this error:
PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'user'#'172.17.0.4' (using password: YES)")
My IP server is 172.0.0.1 but the pipeline set the IP by 172.17.0.4.
How can I set the IP to make work it?
Finally, it works. I added only this values to the yml file:
variables:
MYSQL_DATABASE: mydatabase
MYSQL_ROOT_PASSWORD: 123456
Obviously the same values to the .env file.
(I also uninstalled and re-installed MySQL, but I don't know if that helped to solve it)

How to use database on Vagrant for testing with Codeception

I use Vagrant and Codception, but when i want to test application localy, I must do it on LAMP (or something like that) server. How can I use database on Vagrant?
All you need is just to setup access to your mysql server that runs on the guest machine (vagrant box) from the host and then to set appropriate database DNS in your codeception.yml config.
Here is some general instructions:
1) Allow your vagrant's mysql server listen all interfaces ssh to your box by setting 'bind-address' option in mysql's my.cnf config and change it's value to 0.0.0.0
2) Grant appropriate privileges to mysql user that will be used to connect to database from the host.
You can do this running following SQL commands using mysql client
USE mysql;
GRANT ALL ON *.* to root#'192.168.0.1' IDENTIFIED BY 'mypass';
FLUSH PRIVILEGES;
where root and mypass - your database user which will be used from codeception to connect to database and its password and 192.168.0.1 - the ip of the host (read how get host's ip for your gest here)
3) Restart vagrant's mysql server
4) To test connection to vagrant's mysql from the host run
mysql -h 192.168.33.10 -P 3306 -u root -p
(Here 192.168.33.10 - ip of my running vagrant box)
5) Set up DNS in codeception.yml file, e.g.
modules:
config:
Db:
dsn: 'mysql:host=192.168.33.10;dbname=MyDB'
user: 'root'
password: 'mypass'

Resources