I have created a docker container with MySQL:
docker run --name mysqlfordocker -p3307:3306 -e MYSQL_ROOT_PASSWORD=password_db-d mysql
It works as expected.
It is used by MySQLWorckbech and PHP application which are not in the container with MySQL:
//CONNECTION BY PHP APPLICATION NOT IN CONTAINER
$servername = "127.0.0.1:3307";
$username_db = "username_db";
$password_db = "password_db";
$db = "db";
The Dockerfile for my PHP application is:
FROM php:7.0-apache
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
COPY / /var/www/html
EXPOSE 80
Then I created a container with my PHP application:
docker run –d –p 5000:80 myphpapplication
The PHP application runs, but it has issues with database connection to MySQL container.
I tried to configure a connection servername in a different way but without any luck:
$servername = "127.0.0.1:3307";
$servername = "locahosto:3307";
$servername = "10.0.75.1:3307"; //DockerNAT
$servername = "172.17.0.2:3307"; //Ip by docker inspect mysql
Could you please help me on this?
Ps.
There are two parts to this, one is getting the right IP address of the container. Either the actual IP address should work- 172.17.0.2, but you should also be able to use 172.17.0.1.
Also you normally put the port number as a separate parameter...
$servername = "172.17.0.1";
$username_db = "username_db";
$password_db = "password_db";
$db = "db";
$port= = 3307;
$db = mysqli_connect($servername, $username_db, $password_db, $db, $port);
Related
I ran into this Problem with the PHP Data Object.
I cannot connect to my Database. First here is my PHP Script:
<?php
$serverName = "127.0.0.1";
$port = "3306";
$dbName = "callitTime";
$userName = "root";
$password = "superstrongPassword";
try {
$db = new PDO("mysql:host=$serverName;dbname=$dbName;port=$port;",
$userName, $password);
} catch (PDOException $pdoE) {
echo 'An Error occurred: ' . $pdoE->getMessage();
}
?>
I am using:
PHP 7.1.16
Nginx 1.13.12-1
MySQL 8.0.11-1debian9
All as Docker Containers.
A phpinfo() tells me that PDO Drivers are loaded as follows:
PDO Drivers:
sqlite
mysql
Driver Versions:
mysqlnd 5.0.12-dev
SQLite Library 3.15.1
I get the Error:
An Error occurred: SQLSTATE[HY000] [2002] Connection refused
I can connect to the same Database via Phpstorm with the same Password and Username.
It seems that this is not a problem related to the PHP Configuration.
The error Connection refused shows, from my point of view, that you can't establish a connection from the php container to the mysql container.
You need to expose the 3306 Port to the Webserver Container so that the PHP Container can establish a connection to the database. If you already bridged the containers you need to use the containers IP address and not your loopback 127.0.0.1.
Please see this answer for more information how to connect both containers and how to make a connection from a php to a mysql container:
https://stackoverflow.com/a/43128428/1118905
To narrow down the problem, you can try to establish a connection from within the PHP Container via Netcat to your given DB Host.
For example you can try to establish a connection with the following commands:
Get into the container from which you want to test the connection. docker exec -it <name_of_container> bash
Test to open up a connection via netcat (If not all ready available install it via f.e. apt) nc -vz 127.0.0.1:3306
If you are inside container, 127.0.0.1 is not known as a service that hosts the database. You can use the mysql container name instead. For example $serverName = "mysql"; depending on the name of your container.
sample service definition
services:
mysql:
image: 'mysql:5.5'
container_name: mysql
ports:
- '33066:3306'
...
Also note that the port to be used is the internal one.
This question already has answers here:
From inside of a Docker container, how do I connect to the localhost of the machine?
(41 answers)
Closed 5 years ago.
I have a project developed with PHP and MySql.
When I Use the connection to DB I set the connection as a follow:
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
/* -------------------------------- CONNESSIONE -------------------------------------- */
$servername = "localhosto";
$username_db = "xxxxxx";
$password_db = "yyyyyy";
$db = "zzzzzz";
try {
$conn = new PDO("mysql:host=$servername;dbname=$db", $username_db, $password_db);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//echo "Connected successfully<br/><br/>";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage()."<br/><br/>";
}
the project run correctly and connect to MySql without problems.
Next step I was created a Docker Image with a follow docker file:
FROM php:7.0-apache
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
COPY / /var/www/html
EXPOSE 80
I have create the image and container:
docker build -t mysoftware C:\Users\mysoftware
docker run -d -p 5000:80 mysoftware
but the Php application don't connect to MySql.
I need to change the connection with the name of server:
$servername = "SERVER_NAME";
Why I have an error if the app run correcly without container ??
When running inside the container, localhost will not resolve to the host machine, rather it will refer to the container IP address.
You need to connect to the host machine from within the container. For that check: From inside of a Docker container, how do I connect to the localhost of the machine?
(Assuming *nix) Once you have the mysql docker container running...
docker ps
and find the name of the mysql container that is running. Then to find the IP address...
docker inspect <name_of_container> | grep IPAddress
Then use this IP address for the server to connect to.
i have installed Linux Mint 18.1, then LAMP server using the command
sudo apt-get install lamp-server^ , for testing i tried to create a script connecting to MYSQL database, just like the following.
<?php
$h = "localhost";
$u = "root";
$p = "password";
$conn = mysql_connect($h,$u,$p);
echo "test";
?>
when i remove the line $conn = mysql_connect($h,$u,$p);
the script works fine, otherwise it isn't running,
php5.6-mysql is already installed , and i also tried to connect using
mysqli_connect() instead of mysql_connect()
i don't know what's wrong ?!
Paste this at the top of your file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
It will help you to see the exact error.
Alternatively, you can try:
$conn = mysql_connect($h,$u,$p) or die('Connection Error');
If connection is not getting established then it will show Connection Error
I am trying to connect a database from php script.
<?php
$database = '****';
$user = '*****';
$password = '******';
$hostname = '********';
$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.";
db2_close($conn);
}
else {
echo "Connection failed.";
}
?>
I am geting the following error.
Call to undefined function db2_connect() in /home/s1.php on line 10
Can somebody Help me out how to resolve the following.
I think, you need to install DB2 extension.
Here is the link
http://php.net/manual/en/ibm-db2.installation.php
In addition to installing the db2 extension through PECL, you will need development header files and libraries downloaded on the system before running pecl install.
Here is a shell script that creates 3 files:
Dockerfile
docker-compose.yml
html/index.php
Obviously, please read script before copy/pasting to ensure you're comfortable
with what it does, but in short, it uses cat and the heredoc syntax to output
text into files:
#! /bin/sh
cat << 'EOF' > docker-compose.yml
version: '3'
services:
odb-web:
build: .
ports:
- 3030:80
volumes:
- ./html:/var/www/html
mydb2:
image: ibmcom/db2
ports:
- 50000:50000
volumes:
- ./data:/database
environment:
LICENSE: accept
DB2INST1_PASSWORD: ChangeMe!
DBNAME: testdb
privileged: true
EOF
cat << 'EOF' > Dockerfile
FROM php:7.3-apache
# To build the ibm_db2 extension, the DB2 application development header files and libraries must be installed on your system.
# DB2 does not install these by default, so you may have to return to your DB2 installer and add this option.
# The header files are included with the DB2 Application Development Client freely available for download from
# the IBM DB2 Universal Database » support site: https://www.ibm.com/developerworks/downloads/im/db2express/index.html
# https://github.com/php/pecl-database-ibm_db2
# Download linuxx64_odbc_cli.tar.gz from https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/
## add header files and libraries
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:
# ADD odbc_cli/linuxx64_odbc_cli.tar.gz /opt/ibm/
## set env vars needed for PECL install
ENV IBM_DB_HOME=/opt/ibm/clidriver
ENV LD_LIBRARY_PATH=/opt/ibm/clidriver/lib
## install ibm_db2 drivers
RUN pecl install ibm_db2
RUN echo "extension=ibm_db2.so" > /usr/local/etc/php/conf.d/ibm_db2.ini
EOF
mkdir html && cat << 'EOF' > html/index.php
<?php
$database = 'testdb';
$user = 'db2inst1';
$password = 'ChangeMe!';
$hostname = 'host.docker.internal';
$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 "Connection failed.\n";
}
?>
EOF
upon running this script (or if on windows, copy pasting the file contents manually), you will have 3 files, and can run docker-compose up -d to build a php 7.3 image with apache and then it brings up two containers -- the php apache server and the ibm db2 database.
Browsing to http://localhost:3030 you should see "Connection succeeded".
You can modify the html/index.php file as desired and refresh the page to test things out.
just download a suitable version from PECL
I have a php script (machine_db.php) which makes a connection to mysql database as follows:
<?php
//phpinfo();
// 1 connect to mysql
$servername = "localhost";
$username = "root";
$password = "abcd";
$conn = mysqli_connect($servername, $username, $password);
...
?>
I am using:
Server version: Apache/2.4.12 (Ubuntu)
PHP 5.6.11
mysql Ver 14.14 Distrib 5.6.28
I am using Eclipse Mars and I installed PHP packages (addin or whatever it is called) for Eclipse. Then I created a php project and added my php file to the project and did run as php cli application but I get the following error in Eclipse console,
PHP Fatal error: Call to undefined function mysqli_connect()
However I don't get any error when I run the exact same script from terminal like:
php machine_db.php
I also see that in my Eclipse project explorer there is a PHP Language Library which contains mysqli class.
I think you forget the db name:
$servername = "localhost";
$username = "root";
$password = "abcd";
$db_name = "my-db";
$conn = mysqli_connect($servername, $username, $password, $db_name);
You can also use
$conn = new mysqli($servername, $username, $password, $db_name);
Documentation: http://php.net/manual/fr/mysqli.construct.php
Also try to install php5-mysqli
sudo apt-get update
sudo apt-get install php5-mysql php5-mysqli
sudo service apache2 restart
dont forget to uncomment this line in your /etc/php5/apache2/php.ini
extension=php_mysql.so
extension=php_mysqli.so
Dont forget to restart apache server
sudo service apache2 restart
I just realized that if in eclipse i check "use system default php.ini" under window->preferences->php->phpexecutables it will magically work.
Does anyone know what is "system default php.ini"?
I do a a quick $php --ini and it returns:
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File: /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed: /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-mysqlnd.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-mysql.ini,
/etc/php5/cli/conf.d/20-mysqli.ini,
/etc/php5/cli/conf.d/20-pdo_mysql.ini,
/etc/php5/cli/conf.d/20-readline.ini