Reputation: 1978
I just came across with this
PDOException: SQLSTATE[HY000] [2002] Connection refused in /var/www/html/inc/config/db.php on line 11
at beginning i thought it was something to do with mysql server, but after hours try and error, i found that error is from PDO, if using mysqli_connect no error at all
$conn = new PDO("mysql:host=127.0.0.1;dbname=docker", "root", "tiger");
$conn = mysqli_connect("mysql", "root", "tiger", null);
I also tried with different databases and even with newly created database
does anyone know why one is working the other does not ? ?
EDIT: I'm using docker-compose to set up environment. here is docker-compose.yaml
version: "3"
services:
webserver:
build:
context: ./bin/webserver
container_name: '7.1.x-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ./config/php/php.ini:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
mysql:
build: ./bin/mysql
container_name: '5.7-mysql'
restart: 'always'
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: tiger
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'sc-phpmyadmin'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- '8080:80'
volumes:
- /sessions
redis:
container_name: 'sc-redis'
image: redis:latest
ports:
- "6379:6379"
mongo:
container_name: 'mongodb'
image: mongo:4.0.12-xenial
restart: 'always'
ports:
- "${HOST_MACHINE_MONGO_PORT}:27017"
volumes:
- ${MONGO_DATA_DIR-./data/mongodb}:/data/db
- ${MONGO_LOG_DIR-./logs/mongodb/mongod.log}:/var/log/mongodb/mongod.log
- ${MONGO_CONF-./config/mongodb/mongod.conf}:/etc/mongod.conf
Upvotes: 1
Views: 9996
Reputation: 15296
I found the reason why the connection was not working, it was because the connection was trying to connect to port 8888. when it needed to connect to port 8889.
$conn = new PDO("mysql:host=127.0.0.1;port=8889;dbname=docker", "root", "tiger");
To find a listener on a port, do this:
netstat -tln
You should see a line that looks like this if MySQL is indeed listening on that port.
tcp 0 0 127.0.0.1:3306 0.0.0.0:*
Port 3306 is MySql's default port.
If you want to know the port number of your local host on which Mysql is running you can use this query on MySQL Command line client --
SHOW VARIABLES WHERE Variable_name = 'port';
mysql> SHOW VARIABLES WHERE Variable_name = 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
It will give you the port number on which MySQL is running.
Upvotes: 1