Not able to connect PHP application to db2 database on remote server - php

Been struggling connecting a PHP site to a remote DB2 database for the last three days in a docker-compose project I'm working on. I keep getting Connection failed:
Error: 58031
[IBM][CLI Driver] SQL1031N The database directory cannot be found on the indicated file system. SQLSTATE=58031 SQLCODE=-1031
Here's my docker-compose setup:
# /docker-compose.yml
---
version: '3.8'
services:
web:
container_name: nginx
depends_on:
- db2
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/conf.d/nginx.conf
- ./app:/app
links:
- php
- db2
php:
container_name: php
build:
context: .
dockerfile: PHP.Dockerfile
volumes:
- ./app:/app
links:
- db2
db2:
container_name: db2.rn.dk
image: ibmcom/db2:latest
hostname: db2.rn.dk
privileged: true
ports:
- "50000:50000"
environment:
- DB_NAME=testdb
- DB_USER=db2inst1
- DB_PASSWORD=ChangeMe!
- LICENSE=accept
## Creates the database DB_NAME, if its not exists
- STARTUP_MODE=createIfNotExists
- DB_CREATE_SCRIPT=create_wdemo.sql
## loads an old backup of DB_NAME,
## if the dabase DB_NAME does not exist
# - STARTUP_MODE=restoreIfNotExists
# - DB_BACKUP=wdemo.tar.gz
volumes:
- './data/db2:/database'
And the PHP.dockerfile:
FROM php:fpm
RUN apt-get update -qq > /dev/null && \
apt-get install unzip
RUN pecl install xdebug && docker-php-ext-enable xdebug
# Documentation
# https://www.ibm.com/docs/en/db2/11.5?topic=dsd-installing-data-server-driver-odbc-cli-software-linux-unix-operating-systems
# https://www.ibm.com/docs/en/db2/11.5?topic=environment-configuring
# https://stackoverflow.com/questions/37066985/php-connection-to-db2
# https://github.com/nagstaku/php_db2
# https://github.com/php/pecl-database-ibm_db2
# https://github.com/php/pecl-database-pdo_ibm
# Install DB2 php-extensions
RUN mkdir -p /opt/ibm/ && curl https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/linuxx64_odbc_cli.tar.gz | tar -xz -C /opt/ibm/
# if you prefer to keep the file locally, download it and use:
## set env vars needed for PECL install
ENV IBM_DB_HOME=/opt/ibm/clidriver
ENV LD_LIBRARY_PATH=/opt/ibm/clidriver/lib
# ENV IBM_DB_HOME=/opt/ibm/dsdriver
# ENV LD_LIBRARY_PATH=IBM_DB_HOME/lib
## install ibm_db2 drivers
RUN pecl install ibm_db2 pdo_ibm
RUN echo "extension=ibm_db2.so" > /usr/local/etc/php/conf.d/ibm_db2.ini
RUN echo "extension=ibm_db2.so" >> /usr/local/etc/php/php.ini
RUN echo "extension=pdo.so" >> /usr/local/etc/php/php.ini
RUN echo "extension=pdo_ibm.so" >> /usr/local/etc/php/php.ini
#
# RUN echo "ibm_db2.instance_name=db2inst1" >> /usr/local/etc/php/php.ini
index.php
<?php
...
$database = 'testdb';
$user = 'db2inst1';
$password = 'ChangeMe!';
$hostname = 'db2.rn.dk';
$port = 50000;
$conn_string =
"DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;" .
"HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;";
$conn = db2_connect($conn_string, '', '');
if ($conn) {
echo "Connection succeeded.\n";
db2_close($conn);
} else {
echo "<p><b>Using ibm_db2 extension:</b></p>";
echo "<p><u>Connection failed:</u></p>";
echo "<p>Error: ".db2_conn_error()."<br />";
echo db2_conn_errormsg()."</p>";
}
?>
Is there something I'm missing regarding the setup, that isn't documented anywhere?
Best regards,
Thomas

Related

PHP socket connect socket_connect(): unable to connect [111

I have created below docker-composer yml file to create environment for connect php to socket server.
services:
websocket-server:
build: .
command: php -S 127.0.0.1:1000 -t /var/www/html/websocket
volumes:
- ./src:/var/www/html
ports:
- 1000:1000
php-tutorial:
build: .
volumes:
- ./src:/var/www/html
ports:
- 7000:80
My Dockerfile looks like
FROM php:8.0-apache
WORKDIR /var/www/html
RUN apt-get update -y && apt-get install -y telnet libmariadb-dev
RUN docker-php-ext-install mysqli sockets
After buid docker container, both container running well , in websocket-server I'm getting below log
[Fri Feb 3 05:12:43 2023] PHP 8.0.27 Development Server (http://127.0.0.1:1000) started
Now I have written a php code to connect socket
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if (socket_connect($socket, '127.0.0.1', 1000) === false) {
echo "Unable to connect to server\n";
exit;
}
socket_write($socket, "Hello, server!");
$response = socket_read($socket, 1024);
echo "Response from server: $response\n";
socket_close($socket);
After run in browser , I'm getting below Warning
Warning: socket_connect(): unable to connect [111]: Connection refused in /var/www/html/index.php on line 4
Unable to connect to server
I am unable to find the solution.
You need to connect two containers to same network, forward port is just connect container port your host machine, it won't help you connect each containers, so your php container can touch socket container.
Read document: https://docs.docker.com/network/
I'm not sure if this example work, but you can try it:
services:
websocket-server:
build: .
command: php -S 127.0.0.1:1000 -t /var/www/html/websocket
volumes:
- ./src:/var/www/html
ports:
- 1000:1000
networks:
- dev
php-tutorial:
build: .
volumes:
- ./src:/var/www/html
ports:
- 7000:80
networks:
- dev
networks:
dev:
driver: bridge
And even if you connect to same network, you still need to change your code:
if (socket_connect($socket, '127.0.0.1', 1000) === false)
// change to below
if (socket_connect($socket, 'websocket-server', 1000) === false)

SQLSTATE[HY000] [2002] Connection refused when trying to Dockerize Laravel App

I was able to access the mysql database from phpmyadmin using user: admin and password: root and the url: 127.0.0.1:3310 and I was able to load the website on 127.0.0.1:8008 but when i try to login or interact with the database i get the error below:
SQLSTATE[HY000] [2002] Connection refused
select * from users where email = boyiajas#gmail.com limit 1
and I also try to do a migration from within the app docker container but failed as well
root#58a709f18668:/var/www/html# php artisan migrate
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravelvueblog_db and table_name = migrations and table_type = 'BASE TABLE')
below is my .env file
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3309
DB_DATABASE=laravelvueblog_db
DB_USERNAME=admin
DB_PASSWORD=root
below is my vhost.conf file
<VirtualHost *:8008>
DocumentRoot /var/www/html/public
<Directory "/var/www/html/public">
AllowOverride all
Require all granted
</Directory>
#ErrorLog ${APACHE_LOG_DIR}/error.log
#CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
below is my Docker file
FROM php:8-apache
USER root
RUN apt-get update -y && apt-get install -y openssl curl zip unzip git nano
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install mysqli pdo pdo_mysql opcache
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
COPY vhost.conf /etc/apache2/sites-available/000-default.conf
RUN chown -R www-data:www-data /app && a2enmod rewrite
RUN rm -rf /var/www/html && ln -s /app /var/www/html
RUN composer install
RUN php artisan optimize:clear
CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000
below is my docker-compose.yaml file
version: '3.8'
services:
db:
image: mariadb:latest
container_name: db
ports:
- 3309:3306
environment:
MYSQL_DATABASE: laravelvueblog_db
MYSQL_ROOT_PASSWORD: root
MYSQL_PASSWORD: root
MYSQL_USER: admin
volumes:
- mysql_file:/docker-entrypoint-initdb.d
networks:
- appnetwork
main:
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
ports:
- 8008:8000
environment:
# MYSQL_DATABASE: laravelvueblog_db
# MYSQL_ROOT_PASSWORD: root
# MYSQL_PASSWORD: root
# MYSQL_USER: admin
DB_HOST: db
DB_USER: admin
DB_PASSWORD: root
DB_NAME: laravelvueblog_db
WAIT_HOSTS: db:3306
depends_on:
- db
links:
- db
networks:
- appnetwork
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 3310:80
links:
- mysql
environment:
PMA_HOSTS: db
PMA_PORT: 3306
depends_on:
- db
networks:
- appnetwork
volumes:
mysql_file:
driver: local
networks:
appnetwork:
driver: bridge
After several attempt I found the problem, since all the services are on the same network (appnework)
I used the internal port in my laravel .env file like below
DB_PORT=3306
And now it works fine

Can't connect to Postgres database using PHP PDO and Docker

I am running a Docker app that is built on three images: php:7.4-fpm, nginx:stable-alpine and postgres:alpine.
I was previously attempting to use the image php:7.4-fpm-alpine, but apparently it does not come with the Postgres PDO driver, which was causing a could not find driver error. This post explains more about that, and how to fix it. I followed the steps in that post, including creating a Dockerfile for my PHP FPM service (though I did not create a PHP CLI service), and that remedied my driver issues. However, I am now getting the following error when I try to connect to my PSQL database using PDO:
SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 6432?
I know my database is running (on port 6432) because I can access it from my terminal and from Positco, and have created tables after my Docker app was created. I also tried to connect to a database on port 5432 (through which my Postgres desktop app is running), but that did not work either. I also tried to restart my Postgres container, but that did not help.
Here is my PHP:
class Database {
private $connection;
private static $options = array(
PDO::ATTR_EMULATE_PREPARES => FALSE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
public function __construct() {
try {
$dsn = 'pgsql:host=localhost;port=6432;dbname=db_my_test_app';
$username = 'root';
$password = 'secret';
$connection = new PDO($dsn, $username, $password, self::$options);
$this->connection = $connection;
return $connection;
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
$database = new Database();
...and my docker-compose.yml file:
version: '3'
networks:
my_test_app:
services:
# nginx
nginx-service:
image: nginx:stable-alpine
container_name: nginx-container
ports:
- "8080:80"
volumes:
- ./app:/var/www/project
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php-fpm-service
- postgres-service
networks:
- my_test_app
# php
php-fpm-service:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
container_name: php-fpm-container
ports:
- "9000:9000"
working_dir: /var/www/project
volumes:
- ./app:/var/www/project
networks:
- my_test_app
# postgres
postgres-service:
image: postgres:alpine
container_name: postgres-container
ports:
- "6432:5432"
volumes:
- ./postgres:/var/lib/postgresql/data
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: secret
POSTGRES_DB: db_my_test_app
networks:
- my_test_app
...and the PHP service Dockerfile (from the aforementioned site):
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y libpq-dev
RUN docker-php-ext-install pdo pdo_pgsql pgsql
RUN ln -s /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini
RUN sed -i -e 's/;extension=pgsql/extension=pgsql/' /usr/local/etc/php/php.ini
RUN sed -i -e 's/;extension=pdo_pgsql/extension=pdo_pgsql/' /usr/local/etc/php/php.ini
You are configuring docker under the default network mode: bridge. In order for the php-fpm-service to be able to access the postgres-service you need to make the following modifications:
In the docker-compose.yml file you'll need to add depends_on to php-fpm-service
# php
php-fpm-service:
build:
context: .
dockerfile: ./php-fpm/Dockerfile
container_name: php-fpm-container
ports:
- "9000:9000"
working_dir: /var/www/project
volumes:
- ./app:/var/www/project
networks:
- my_test_app
depends_on:
- postgres-service
In your PHP config, you need modify localhost to postgres-service and using port 5432:
$dsn = 'pgsql:host=postgres-service;port=5432;dbname=db_my_test_app';

docker-compose mysql_pdo connection fail

I have 3 running containers, all is fine (containers are running, database is setted up) except that the PDO connection does not work. There is the error report
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection
refused in /var/www/html/lib/OCFram/PDOFactory.php:9 Stack trace: #0 /var/www
/html/lib/OCFram/PDOFactory.php(9): PDO->__construct('mysql:host=mysq...',
'root', 'root') #1 /var/www/html/lib/OCFram/BackController.php(17):
OCFram\PDOFactory::getMysqlConnexion() #2 /var/www/html/lib/OCFram
/Application.php(69): OCFram\BackController->__construct(Object(App\Frontend
\FrontendApplication), 'Welcome', 'index') #3 /var/www/html/App/Frontend
/FrontendApplication.php(17): OCFram\Application->getController() #4 /var/www
/html/bootstrap.php(30): App\Frontend\FrontendApplication->run() #5 {main}
thrown in /var/www/html/lib/OCFram/PDOFactory.php on line 9
the docker-compose.yml
version: "3.2"
services:
php:
build: './php/'
volumes:
- ./MediterPourGrandir/:/var/www/html/
apache:
build: './apache/'
depends_on:
- php
- mysql
ports:
- "8080:80"
volumes:
- ./MediterPourGrandir/:/var/www/html/
mysql:
image: mysql:5.6.40
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_USER=root
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
the php docker file
FROM php:7.2.7-fpm-alpine3.7
# RUN apk update; \
# apk upgrade;
# RUN docker-php-ext-install pdo pdo_mysql
# RUN docker-php-ext-install mysqli
RUN apk update --no-cache \
&& apk add --no-cache $PHPIZE_DEPS \
&& apk add --no-cache mysql-dev \
&& docker-php-ext-install pdo pdo_mysql
the pdo connection class
<?php
namespace OCFram;
class PDOFactory
{
public static function getMysqlConnexion()
{
$db = new \PDO('mysql:host=mysql;port=3306;dbname=monsupersite', 'root', 'root');
// $db = new \PDO('mysql:host=mysql;port=3306;charset=utf8', 'root', 'rootpassword');
$db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
echo "Connected succesfully";
return $db;
}
}
I have done so many unsuccessful try. I'am missing something, but can not find out..... If someone got an idea, it would be great.
Thank you.
I finally can find out.
I did not notice it but after few seconds the mysql container were shutting down. The reason is: by default a mysql container name the MYSQL_USER as 'root', so who wants to use 'root' as MYSQL_USER must not declare it. See the github solved issue https://github.com/docker-library/mysql/issues/129
With these settings it works.
mysql:
image: mysql:5.6.40
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=monsupersite
- MYSQL_PASSWORD=root
ports:
- "3306:3306"
Yes. Or you can check php_info() information does php have enable extension or not.

docker php-fpm: the connection between services by port 9000 fails

I'm setting up a config to run my environment. I've a mysql, redis, nginx and php-fpm.
The services mysql and redis are ok, but when I start nginx and php my environment don't run.
The nginx config call to fastcgi_pass: php:9000
The error is:
[error] 6#6: *1 connect() failed (113: No route to host) while connecting to upstream, client: 192.168.224.1, server: ~.*, request: "POST /sellers HTTP/1.1", upstream: "fastcgi://192.168.224.4:9000", host: "127.0.0.1:8080"
My Dockerfile is:
FROM php:7.2-fpm-alpine
WORKDIR /app
# Install composer
RUN apk --update add curl && rm /var/cache/apk/*
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN docker-php-ext-install pdo pdo_mysql
# Copy files
COPY . /app/
## THE LIFE SAVER
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.2.1/wait /wait
RUN chmod +x /wait
RUN composer up
CMD /wait && bin/console doctrine:migrations:migrate
and my docker-compose is:
version: '3'
services:
mysql:
image: mysql:5.7
restart: unless-stopped
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: database
MYSQL_DATABASE: cart
redis:
image: redis:alpine
ports:
- '6379:6379'
web:
image: nginx:latest
depends_on:
- php
ports:
- '8080:80'
volumes:
- .:/app
- ./docker/site.conf:/etc/nginx/conf.d/site.conf
php:
build:
context: .
depends_on:
- mysql
volumes:
- .:/app
environment:
- APP_ENV=prod
- APP_SECRET=aaaa
- DATABASE_URL=mysql://root:database#mysql:3306/cart
- REDIS_HOST=redis
- REDIS_PORT=6379
- WAIT_HOSTS= mysql:3306

Categories