简体   繁体   English

PDOException: SQLSTATE[HY000] [2002] 连接被拒绝

[英]PDOException: SQLSTATE[HY000] [2002] Connection refused

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一开始我认为这与 mysql 服务器有关,但经过数小时的尝试和错误,我发现错误来自 PDO,如果使用 mysqli_connect 根本没有错误

$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.编辑:我正在使用 docker-compose 来设置环境。 here is docker-compose.yaml这里是 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

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.我找到了连接不工作的原因,是因为连接试图连接到端口 8888。当它需要连接到端口 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.如果 MySQL 确实在该端口上侦听,您应该会看到如下所示的行。

tcp        0      0 127.0.0.1:3306              0.0.0.0:* 

Port 3306 is MySql's default port.端口 3306 是 MySql 的默认端口。

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 --如果您想知道Mysql 正在运行的本地主机的端口号,您可以在 MySQL 命令行客户端上使用此查询——

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.它将为您提供 MySQL 正在运行的端口号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM