I am trying to connect a Google Compute Engine instance with a MySQL database to Google App Engine using Laravel. I actually have connected my Google App Engine to a Cloud SQL instance, I don't have problem with this, but I need an additional database connection with the database located on Google Compute Engine.
Google Compute Engine instance is on a different project. This is my scheme:
Project A -> Compute Engine -> Instance -> MySQL database
Project B -> App Engine -> Laravel
Project B -> Cloud SQL -> DB-instance -> MySQL database
This is my app.yaml file:
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: "[KEY]"
STORAGE_DIR: "/tmp"
CACHE_DRIVER: "database"
SESSION_DRIVER: "database"
APP_DEBUG: "true"
#CLOUD SQL database connection
DB_CONNECTION: "[DATABASE_1_NAME]"
DB_HOST: "[CLOUD_SQL_INSTANCE_NAME]"
DB_DATABASE: "[DATABASE_1_NAME]"
DB_USERNAME: "root"
DB_PASSWORD: "[PASSWORD]"
DB_SOCKET: "/cloudsql/[PROJECTB]:[REGION]:[CLOUD_SQL_INSTANCE_NAME]"
# COMPUTE ENGINE database connection
DB_HOST_2: "[COMPUTE_ENGINE_INSTANCE_NAME]"
DB_DATABASE_2: "[DATABASE_2_NAME]"
DB_USERNAME_2: "root"
DB_PASSWORD_2: "[PASSWORD]"
beta_settings:
cloud_sql_instances: "[PROJECTB]:[REGION]:[CLOUD_SQL_INSTANCE_NAME]"
I consider you're using Google App Engine Standard environment with PHP.
When you connect a GAE Application to a DB hosted on a Google Compute Engine instance, it is treated as a connection to an "external DB". Thus, you need to consider GAE as an external client of your GCE instance.
Therefore, you must configure your VPC firewall in order to accept connections to the port 3306 from outside and you must specify the external GCE instance IP address as the host of your connection string. Be sure that your firewall rule accepts connections from everywhere (IP mask: 0.0.0.0/0).
Hope this helps you.
Bye
Related
I an using Docker Compose with my app and database. My app connects to my database using the hostname db as shown in my docker-compose.yml:
db:
image: mysql
# container_name: mysql_db
environment:
- MYSQL_USER=test
- MYSQL_ROOT_PASSWORD=test
- MYSQL_PASSWORD=test
- MYSQL_DATABASE=test
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
volumes:
- ./db:/docker-entrypoint-initdb.d
restart: always
ports:
- 3306:3306`
But when I open my app on Heroku I get a connection error:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'db' ([Errno -2] Name or service not known)")
I supposed that this error appear because Heroku has dynamic hosts.
How can I set my database container up on Heroku?
How can I set my database container up on Heroku?
Don't.
One database container and one code container might make sense in development, but in production these things shouldn't be coupled. Use a hosted database provider instead; there are several options, including MySQL services, most of which have a free starter tier.
This matches Heroku's recommendation:
Use Heroku add-ons in production
For local development: use official Docker images, such as Postgres and Redis.
For staging and production: use Heroku add-ons, such as Heroku Postgres and Heroku Redis.
Using official Docker images locally and Heroku add-ons in production provides you with the best of both worlds:
Parity: You get parity by using the same services on your local machine as you do in production
Reduced ops burden: By using add-ons, Heroku – or the add-on provider – takes the ops burden of replication, availability, and backup.
I followed the two following tutorials:
https://cloud.google.com/go/docs/tutorials/bookshelf-on-compute-engine
https://cloud.google.com/memorystore/docs/redis/creating-managing-instances?authuser=1
The only thing I did different is that I deployed the code of Redis from the second tutorial, not from the first link.
As mentioned "Connecting to a Redis instance" You can connect to the Redis instance from any Compute Engine VM instance located within the same project, region and network as the Redis instance.
my-service: 10.162.0.17 (nic0)
redis: 10.169.12.195:6379
Same project, same region, same zone (a)
However, when I do:
user#my-service:~$ telnet 10.0.0.27 6379
Trying 10.0.0.27...
Nothing happens..
The documentation specifies the command telnet 10.0.0.27 6379. This is an example that you need to modify for your environment.
Change the command to:
telnet 10.169.12.195 6379
I use Laravel Forge to deploy 2 web apps to the same VPC in AWS like following:
A server ( with MySQL installed ):
- domain: test-erp.foo.com
- ip: 3.112.70.152
B server:
- domain: test.foo.com
- ip: 13.230.27.11
I can access MySQL in A server, but I can not access the MySQL from B server. I need both two web apps to access the same DB and how can I config it in Laravel forge ?
You will need to create a security group for that VPC and a rule that allows the traffic that is needed. For reference.
I’ve now tried to create a serverless Aurora (MySQL compatible) database and connect to it for two days, and I just can’t seem to get it to work. Supposedly I should have been able to get it up and running in five minutes.
In any case, I created am Aurora Serverless database in the US East (N. Virginia) region (us-east-1), and have been able to connect to it with the AWS Query Editor. I also have an EC2 server in the same region, and have given the Aurora database the same security group (under RDS > Security Group), and in the security group I have opened for MYSQL/Aurora (TCP, 3306) from all sources. When I click the modify button on the database, there is also another (VPC) Security Group listed (rds-launch-wizard-4), which was created automatically. This one I also located under my EC2 dashboard and gave access to all ports from all sources (inbound), and to all ports (outbound). And there is a networking VPC & subnet group, which I don’t know what to do with, if anything.
I try to connect to the database, using this command line command:
mysql -h hest2.cluster-xxxxx.us-east-1.rds.amazonaws.com -P 3306 -u root –p
It generates an error “ERROR 2003 (HY000): Can't connect to MySQL server on” on both my EC2 instance, my local computer and on other online servers.
From the EC2 instance, try doing a telnet on the DB port to test if all your security group settings are applied correctly.
telnet hest2.cluster-xxxxx.us-east-1.rds.amazonaws.com 3306
If the connection does go through, then the issue is with your client code. Cross check that you have wired the right endpoint in your code.
If the telnet connection does not group (I'm guessing that it would not), then it is guaranteed that your security group settings are not set correctly. In order to debug this further, we would need more details on:
The list of vpc security groups associated with your cluster.
The details of each of these vpc security groups (You've mentioned that
you've opened up everything, but I'd like to see the exact rules in
place)
As for laptop and other servers - If they are outside the VPC, then it would not work. Aurora Serverless is accessible only from within the VPC as of now.
I followed every tutorial from Amazon to set up the RDS Database then to set up the VPC, the subnet, and the Security Groups but I still can't connect to the Database using Laravel command.
However, I can connect to the Database using MySQLWorkbench...
This is the error I got when I use the command "php artisan migrate"
Every help will be more than appreciate, thank you Guys.
I'm taking these settings right out of my RDS instance and the Laravel application that uses it.
The first thing you have to do after setting up your RDS database is to allow the database instance to be accessed publicly. This can be done from your AWS console by going to RDS > DB Instances > your database instance > Modify (top left corner). Then scroll down to the Network and Security section and select 'Yes' for public accessibility.
Also double check your Security groups to make sure there is a rule allowing incoming connections to port 3306.
In your Laravel application's .env file and update the database values.
DB_HOST should contain the endpoint specified on the Connectivity and security section of the RDS dashboard. DB_PORT should remain at 3306. Then specify the database name in DB_DATABASE and the database credentials in DB_USERNAME and DB_PASSWORD.
These are the settings I'm using to connect my Laravel application to an RDS MySQL database.
It's difficult to say but it looks like you have the incorrect DB name.
Double check your RDS db name and the environment variable DB_DATABASE