Connecting to Cloud SQL Proxy with PDO - google-app-engine

I'm following this tutorial except that I do not wish to use Silex.
I have set up a Cloud SQL Second Generation instance and a local proxy, which I can successfully connect to using SQL Workbench. This is the code I'm using to connect with:
define('DBH_SOCKET', '/cloudsql/project-***:us-east1:instance-****');
define('DBH_NAME', 'testdb');
define('DBH_USER', 'root');
define('DBH_PASSWORD', '****');
$pdo = "mysql:unix_socket=".DBH_SOCKET.";dbname=".DBH_NAME.";charset=utf8";
try {
$db = new PDO($pdo, DBH_USER, DBH_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
echo '<br />';
if($db) echo 'YAY'; else echo 'NAY';
It works absolutely fine when I deploy the app an visit project-***.appspot.com, but the connection fails when I try to work locally.
Connection failed: SQLSTATE[HY000] [2002] No such file or directory.
I know this question has been asked before in various contexts, but none of their solutions are working for me or don't directly apply. I have added this to my app.yaml file with no change in results:
env_variables:
# Replace project, instance, database, user and password with the values obtained
# when configuring your Cloud SQL instance.
MYSQL_DSN: mysql:unix_socket=/cloudsql/project-****:us-east1:instance-****;dbname=testdb
MYSQL_USER: root
MYSQL_PASSWORD: '****'
beta_settings:
cloud_sql_instances: "project-****:us-east1:instance-****"
Why can't I connect locally using PDO when I can connect locally using my SQL client?

Related

NodeJS OracleDB connection string

I am running Oracle XE 21c in a Docker container and I can connect to it with a JDBC thin connection using the JDBC url jdbc:oracle:thin:#localhost:1521:XE using the SYSTEM account. I can also log into the terminal Docker instance and connect to the database fine.
When I try to connect to it using NodeJS OracleDB example code described here:
https://node-oracledb.readthedocs.io/en/latest/user_guide/connection_handling.html, I cannot get the client to connect using either:
connection = await oracledb.getConnection({
user : "hr",
password : mypw
connectString : "localhost/XEPDB1"
});
or
connection = await oracledb.getConnection({
user : "SYSTEM",
password : "MyPassword",
connectString : "localhost/XE"
});
I get an error ORA-01017: invalid username/password; logon denied.
I can connect to Oracle XE using sqlplus either by logging into the Docker container or from the host Mac using:
sqlplus SYSTEM/Password#localhost:1521/XE
if I change the nodejs code to:
const connection = await oracledb.getConnection({
//user: "SYSTEM",
//password: "Password42",
//connectString: connectionURI.url
connectString: "SYSTEM/Password#localhost:1521/XE"
});
I get the error:
ORA-12154: TNS:could not resolve the connect identifier specified
Any ideas why I can connect with sqlplus but not NodeJS OracleDB?

strapi Error connecting to the atlas Mongo database

I want to use mongo atlas with strapi. SO I am reading the values from .env
these are the settings
connections: {
default: {
connector: 'mongoose',
settings: {
host: env('DB_MONGO_URL', '127.0.0.1'),
srv: env.bool('DATABASE_SRV', false),
port: env.int('DB_PORT', 27017),
database: env('DATABASE_NAME', 'mydb'),
username: env('DB_USER_NAME', null),
password: env('DB_PASSWORD', null)
},
options: {
authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
ssl: env.bool('DB_SSL_ENABLE', false),
},
},
and here are the values
DB_MONGO_URL=myproject.random.mongodb.net/mydbt?ssl_cert_reqs=CERT_NONE
DB_USER_NAME=myuser
DB_PASSWORD=mypasswor
but this is showing
[2021-10-12T13:18:25.485Z] debug ⛔️ Server wasn't able to start properly.
[2021-10-12T13:18:25.490Z] error Error connecting to the Mongo database. Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
error Command failed with exit code 1.
In network i have allowed, 0.0.0.0 that includes any IP address.
If i pass this url in MongoDB Compass
mongodb+srv://myproject.random.mongodb.net/mydbt?ssl_cert_reqs=CERT_NONE
then this works
I tried the same as well
DB_MONGO_URL=mongodb+srv://myproject.random.mongodb.net/mydbt?ssl_cert_reqs=CERT_NONE
DB_USER_NAME=myuser
DB_PASSWORD=mypassword
but this shows
[2021-10-13T06:59:01.372Z] debug ⛔️ Server wasn't able to start properly.
[2021-10-13T06:59:01.374Z] error Error connecting to the Mongo database. getaddrinfo ENOTFOUND mongodb+srv
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
If i tried enable srv true
[2021-10-13T07:01:20.065Z] debug ⛔️ Server wasn't able to start properly.
[2021-10-13T07:01:20.067Z] error Error connecting to the Mongo database. URI does not have hostname, domain name and tld
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
how can i solve this?

Error connecting to Google Cloud SQL from App Engine custom environment using TCP

I'm trying to connect to google sql cloud instance from custom runtime environment in App Engine.
When I follow the doc to connect using unix domain socket, it works. The problem is when I try to connect using a TCP connect. It shows:
Warning: mysqli_connect(): (HY000/2002): Connection refused in
/var/www/html/index.php on line 3
Connect error: Connection refused
This is my app.yaml file:
runtime: custom
env: flex
beta_settings:
cloud_sql_instances: testing-mvalcam:europe-west1:testdb=tcp:3306
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
The Dockerfile:
FROM php:7.0-apache
ENV PORT 8080
CMD sed -i "s/80/$PORT/g" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && docker-php-entrypoint apache2-foreground
RUN docker-php-ext-install mysqli
RUN a2enmod rewrite
COPY ./src /var/www/html
EXPOSE $PORT
And index.php:
<?php
$link = mysqli_connect('127.0.0.1', 'root', 'root', 'test');
if (!$link){
die('Connect error: '. mysqli_connect_error());
}
echo 'successfully connected';
mysqli_close($link);
?>
What am I doing Wrong?
The ip address ‘172.17.0.1’ is related with the docker container where the webserver is running, you can get more context on that in this documentation.
The documentation page you’re using might be lacking on adjusting the use case if you’re deploying with a presence of a Dockerfile. In the following documentation you can read more information about App Engine flexible runtimes.
As demonstrated by the documentation you’re using (remember to click on the TCP CONNECTION tab on this page), on the section of the app.yaml related to Cloud SQL instances information about the TCP port in use by the database server is needed.

Lumen App Deployed on GAE is unable to connect to DB in cloud-sql

I developed an API in Lumen and deployed it on GAE. I followed this tutorial for that purpose. It worked fine until the deployment of API but the problem came when I tried to access the DB that is on the cloud-sql instance. I am unable to access the DB in the way described in the above mentioned tutorial. Then I tried the different solution and none of them work. At-last just for test purpose I added the Instances IP address (where the GAE is deployed) in the authorized list of the cloud-sql instance and its working now. But this is a temporary solution as Instances are deployed on VM's and their IP address can change at anytime that will break the connection between GAE and cloud-sql. Any solution or suggestion will be appreciated.
Although I have tried by changing my App.yaml multiple ways but still I have given it below:
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_ENV: production
APP_DEBUG: true
#QUEUE_DRIVER: sync
APP_LOG: errorlog
APP_KEY: 32 char key
STORAGE_DIR: /tmp
CACHE_DRIVER: database
SESSION_DRIVER: database
#APP_TIMEZONE: UTC
## Set these environment variables according to your CloudSQL configuration.
DB_HOST: 127.0.0.1
DB_DATABASE: dbname
DB_USERNAME: root
DB_PASSWORD: pass
DB_SOCKET: "/cloudsql/Instance Connection Name"
MYSQL_DSN: "mysql:unix_socket=/Instance Connection Name;dbname=dbname"
MYSQL_USER: root
MYSQL_PASSWORD: pass
beta_settings:
# for Cloud SQL, set this value to the Cloud SQL connection name,
# e.g. "project:region:cloudsql-instance"
cloud_sql_instances: "Instance Connection Name"

JEE application in Docker accessing local DB port

I am trying to deploy a JEE application in Docker container. The application requires embedded apache derby on port 1527. It works fine when run in regular dev environment. However, when I run inside a Docker container, the connection on port 1527 is refused.
Things to note, I am using 'default' db connection. That means the application is trying to access the database on localhost:1527. Following is the error message:
java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused (Connection refused).
I am wondering what does localhost mean within a Docker container, the host server or the container?
Any suggestions to fix it?

Resources