Can not connect to DB while using Docker compose

2,513 views
Skip to first unread message

Sunny

unread,
Feb 4, 2021, 2:17:02 PM2/4/21
to redbeanphp
Having troubles to connect php with mysql while using docker compose. I saw there is a similar post on reddit, but not solved 

docker-compose.yml: 

``` 
version: "3.3"

services:
php:
build:
context: ./webapp
dockerfile: Dockerfile
image: php:7.2-apache
volumes:
- ./webapp:/var/www/html/
depends_on:
- db
ports:
- 80:80
environment:
DB_HOST: db:3306
DB_USER: dbuser
DB_PASSWORD: dbpassword
stdin_open: true
tty: true
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
ports:
- "9906:3306"
environment:
MYSQL_ROOT_PASSWORD: testpw
MYSQL_DATABASE: testdb
MYSQL_USER: devuser
MYSQL_PASSWORD: devpass

volumes:
db_data:
``` 

index.php:

```
<?php
require 'rb.php';
print("<h1>hi</h1>");
R::setup( 'mysql:host=172.17.0.2_9906;dbname=testdb', 'devuser', 'devpass' );
$post = R::dispense( 'post' );
$post->title = 'My holiday';
$id = R::store( $post );
$post = R::load( 'post', $id );
print_r($post);
?>
```

Error message:

```Fatal error: Uncaught PDOException: Could not connect to database (testdb). in /var/www/html/rb.php:1101 Stack trace: #0 /var/www/html/rb.php(798): RedBeanPHP\Driver\RPDO->connect() #1 /var/www/html/rb.php(1171): RedBeanPHP\Driver\RPDO->runQuery('show tables', Array) #2 /var/www/html/rb.php(1192): RedBeanPHP\Driver\RPDO->GetAll('show tables', Array) #3 /var/www/html/rb.php(4318): RedBeanPHP\Driver\RPDO->GetCol('show tables', Array) #4 /var/www/html/rb.php(7193): RedBeanPHP\Adapter\DBAdapter->getCol('show tables') #5 /var/www/html/rb.php(6138): RedBeanPHP\QueryWriter\MySQL->getTables() #6 /var/www/html/rb.php(9539): RedBeanPHP\QueryWriter\AQueryWriter->tableExists('post') #7 /var/www/html/rb.php(9641): RedBeanPHP\Repository->tableExists('post') #8 /var/www/html/rb.php(9773): RedBeanPHP\Repository\Fluid->createTableIfNotExists(Object(RedBeanPHP\OODBBean), 'post') #9 /var/www/html/rb.php(9344): RedBeanPHP\Repository\Fluid->storeBean(Object(RedBeanPHP\OODBBean)) #10 /var/www/html/rb.php(10454): RedBeanPHP\Repository->store(Ob in /var/www/html/rb.php on line 1101 ```


Fatal error: Uncaught PDOException: Could not connect to database (testdb). in /var/www/html/rb.php:1101 Stack trace: #0 /var/www/html/rb.php(798): RedBeanPHP\Driver\RPDO->connect() #1 /var/www/html/rb.php(1171): RedBeanPHP\Driver\RPDO->runQuery('show tables', Array) #2 /var/www/html/rb.php(1192): RedBeanPHP\Driver\RPDO->GetAll('show tables', Array) #3 /var/www/html/rb.php(4318): RedBeanPHP\Driver\RPDO->GetCol('show tables', Array) #4 /var/www/html/rb.php(7193): RedBeanPHP\Adapter\DBAdapter->getCol('show tables') #5 /var/www/html/rb.php(6138): RedBeanPHP\QueryWriter\MySQL->getTables() #6 /var/www/html/rb.php(9539): RedBeanPHP\QueryWriter\AQueryWriter->tableExists('post') #7 /var/www/html/rb.php(9641): RedBeanPHP\Repository->tableExists('post') #8 /var/www/html/rb.php(9773): RedBeanPHP\Repository\Fluid->createTableIfNotExists(Object(RedBeanPHP\OODBBean), 'post') #9 /var/www/html/rb.php(9344): RedBeanPHP\Repository\Fluid->storeBean(Object(RedBeanPHP\OODBBean)) #10 /var/www/html/rb.php(10454): RedBeanPHP\Repository->store(Ob in /var/www/html/rb.php on line 1101

Matthew Frederico

unread,
Feb 4, 2021, 9:21:17 PM2/4/21
to redbe...@googlegroups.com
It may just be me, but:

R::setup( 'mysql:host=172.17.0.2_9906;dbname=testdb', 'devuser', 'devpass' );

Should have a ':' before the 9906 e.g.:

R::setup( 'mysql:host=172.17.0.2:9906;dbname=testdb', 'devuser', 'devpass' );

--
You received this message because you are subscribed to the Google Groups "redbeanphp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redbeanorm+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/redbeanorm/0f8fa150-b043-4633-81cf-d73997c79a19n%40googlegroups.com.


--
-`;'- Matthew Frederico

Sunny

unread,
Feb 8, 2021, 4:23:04 AM2/8/21
to redbeanphp
Hi MattZilla
That is a typo in the post, in my code it's actually a colon. It's still not working. 

Marios K

unread,
Feb 8, 2021, 4:19:38 PM2/8/21
to redbe...@googlegroups.com
since you are using docker-compose you should probably use the internal DNS system instead of IPs

try "docker exec -ti {{container_name}} /bin/bash"
then inside the container you should be able to ping the corresponding services with "ping db" or "ping php"
---
You can also use the ENV variables from your compose "getenv('DB_HOST');"
---
Side notes
  - My personal preference is not to use docker volumes, its much easier to mount directories "db_data:/var/lib/mysql" --> "/home/username/mysql:/var/lib/mysql"
  - Don't expose the mysql port, communication of PHP<->SQL is internal to docker
  - There is no need to change the default mysql port
  - If you absolutely need a direct connection from another machine to the database, setup an ssh tunnel
  - If you want to quickly test redbean and your db schema you can try using SQLite with Adminer



Sunny

unread,
Feb 9, 2021, 8:53:23 AM2/9/21
to redbeanphp
Thank you Marios,

So I changed in docker-compose.yaml the Volumes to:

db:
   image: mysql:5.7
   volumes:
      - .:/var/lib/mysql

=>Error: 

2021-02-09 13:47:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.

2021-02-09 13:47:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'

2021-02-09 13:47:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.33-1debian10 started.

2021-02-09 13:47:58+00:00 [Note] [Entrypoint]: Initializing database files

2021-02-09T13:47:58.421410Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2021-02-09T13:47:58.426808Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.

2021-02-09T13:47:58.426877Z 0 [ERROR] Aborting



Could you provide me a really simple docker-compose file, where php and mysql can talk over redbean? I tried like ever possible combination..


"You can also use the ENV variables from your compose "getenv('DB_HOST');" => yeah but it will just use what I've defined in the docker-compose.yaml as DB_HOST.. and that is also not working. 


I could not use ping in the image => 
tried to install ping:
$ apt-get update apt-get install iputils-ping
$ping 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package iputils-pingping


Its such a simple task, but I can't make it run... I don't even want to think about the next step: make this run on azure. But first I want to be able to test this locally.


Sunny

unread,
Feb 9, 2021, 9:06:44 AM2/9/21
to redbeanphp
Just to make it clear.. I changed my PHP file and could connect to Mysql without redbean.. and then I tried to connect to the same DB with redbean:



<?php
require 'rb.php';

// Plain php without redbean
$host = 'mysql';
$user = 'root';
$pass = 'rootpassword';
$conn = new mysqli($host, $user, $pass);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Working well!

// Creating a database named newDB
$sql = "CREATE DATABASE newDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully with the name newDB"; // working well!
} else {
echo "Error creating database: " . $conn->error;
}

// closing connection
$conn->close();

//try to connect to the same Database with redbean
R::setup( 'mysql:host=mysql;dbname=newDB', 'root', 'rootpassword' );
// Error: Fatal error: Uncaught PDOException: Could not connect to database (newDB). in /var/www/html/rb.php:1101 Stack trace: #0 /var/www/html/rb.php(798): RedBeanPHP\Driver\RPDO->connect()
$post = R::dispense( 'post' );
$post->title = 'My holiday';
$id = R::store( $post );
$post = R::load( 'post', $id );
print_r($post);
?>

The problem is in redbean somewhere... nothing todo with docker or docker compose.

Marios K

unread,
Feb 9, 2021, 9:27:38 AM2/9/21
to redbe...@googlegroups.com
Try connecting with vanilla PDO

Sample code


Reply all
Reply to author
Forward
0 new messages