"Could Not Find " 和 "SQLSTATE[HY000] [2002]" 错误
"Could Not Find " and "SQLSTATE[HY000] [2002]" Errors
我在 Windows 开发了一个项目。我用过 PHP、MySQL(使用 PDO)和 Apache。
我用代码创建了数据库和表。没有问题。
我正试图在我的 Ubuntu 18.04 机器上 docker 化这个项目。相信我,我从早上开始就一直在尝试这个。
项目在本地主机上 CSS、JavaScript 运行顺利。但是添加到数据库时会出错。我收到的错误代码是:找不到驱动程序。
php.ini file is missing
I also don't have my apache2 folder that should be in etc/php/7.2.
我收到 SQLSTATE[HY000] [2002] Cannot assign requested address 错误 我是这样连接的:
$db = new PDO("mysql:host=$this->servername;charset=utf8", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
我收到 找不到驱动程序 错误 我是这样连接的(使用 pdo_mysql):
$db = new PDO("pdo_mysql:host=$this->servername;charset=utf8", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
我已经在 Dockerfile 中安装了 apache2 和 php。
这是我的树命令结果:
.
├── docker-compose.yml
├── Dockerfile
└── src
├── css
│ ├── css
│ │ └── dashboard.css
│ ├── fonts
│ │ └── feather
│ │ ├── feather-webfont6cfa.eot
│ │ ├── feather-webfont6cfa.svg
│ │ ├── feather-webfont6cfa.ttf
│ │ └── feather-webfont6cfa.woff
│ └── style.css
├── docker-compose.yml
├── js
│ ├── guncelle.js
│ └── resimler.js
└── php
├── about.php
├── categories.php
├── create_database.php
├── footer.php
├── header.php
├── index.php
├── info.php
├── login.php
└── logout.php
有 docker-compose.yml 和 Dockerfile 文件:
# DOCKERFILE
FROM php:7.3-apache
RUN apt-get update
RUN apt-get install -y git
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN a2enmod rewrite
#Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=. --filename=composer
RUN mv composer /usr/local/bin/
COPY src/ /var/www/html/
EXPOSE 80
# DOCKER-COMPOSE.YML
version: '3'
services:
mysql:
image: mysql:8.0
container_name: vt_album
command: --default-authentication-plugin=mysql_native_password
volumes:
- .:/application
restart: always
environment:
- MYSQL_ROOT_PASSWORD=2222
- MYSQL_PASSWORD=2222
ports:
- "3306:3306"
website:
container_name: php_album
build:
context: ./
volumes:
- ./src/:/var/www/html
ports:
- 8080:8080
depends_on:
- mysql
好像"This image ships with the default php.ini-development and php.ini-production configuration files."你应该选一个
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
我找到了解决方案。 docker 文件和 docker-compose.yml 文件与问题中的相同。没有变化。但是连接数据库的时候应该写"mysql: host = mysql"而不是"mysql: host = localhost"。这里的第二个"mysql"代表我们在docker-compose.yml文件中定义的服务名
以下代码首先创建数据库。然后,它创建连接对象。然后调用相关方法创建表。
protected $conn;
protected $username = "root";
protected $password = 2222;
protected $dbname = "db_2";
function _construct(){
$db = new PDO("mysql:host=mysql;charset=utf8; port:3306", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database = "CREATE DATABASE IF NOT EXISTS ".$this->dbname.";ALTER DATABASE ".$this->dbname." CHARACTER SET utf8 COLLATE utf8_turkish_ci;";
$db->exec($database);
$this->conn=new PDO("mysql:host=mysql;charset=utf8;port:3306", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->create_table_categories();
$this->create_table_users();
$this->create_table_files();
return "<br>success<br>";
}
我在 Windows 开发了一个项目。我用过 PHP、MySQL(使用 PDO)和 Apache。
我用代码创建了数据库和表。没有问题。
我正试图在我的 Ubuntu 18.04 机器上 docker 化这个项目。相信我,我从早上开始就一直在尝试这个。
项目在本地主机上 CSS、JavaScript 运行顺利。但是添加到数据库时会出错。我收到的错误代码是:找不到驱动程序。 php.ini file is missing
I also don't have my apache2 folder that should be in etc/php/7.2.
我收到 SQLSTATE[HY000] [2002] Cannot assign requested address 错误 我是这样连接的:
$db = new PDO("mysql:host=$this->servername;charset=utf8", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
我收到 找不到驱动程序 错误 我是这样连接的(使用 pdo_mysql):
$db = new PDO("pdo_mysql:host=$this->servername;charset=utf8", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
我已经在 Dockerfile 中安装了 apache2 和 php。
这是我的树命令结果:
.
├── docker-compose.yml
├── Dockerfile
└── src
├── css
│ ├── css
│ │ └── dashboard.css
│ ├── fonts
│ │ └── feather
│ │ ├── feather-webfont6cfa.eot
│ │ ├── feather-webfont6cfa.svg
│ │ ├── feather-webfont6cfa.ttf
│ │ └── feather-webfont6cfa.woff
│ └── style.css
├── docker-compose.yml
├── js
│ ├── guncelle.js
│ └── resimler.js
└── php
├── about.php
├── categories.php
├── create_database.php
├── footer.php
├── header.php
├── index.php
├── info.php
├── login.php
└── logout.php
有 docker-compose.yml 和 Dockerfile 文件:
# DOCKERFILE
FROM php:7.3-apache
RUN apt-get update
RUN apt-get install -y git
RUN docker-php-ext-install pdo pdo_mysql mysqli
RUN a2enmod rewrite
#Install Composer
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
RUN php composer-setup.php --install-dir=. --filename=composer
RUN mv composer /usr/local/bin/
COPY src/ /var/www/html/
EXPOSE 80
# DOCKER-COMPOSE.YML
version: '3'
services:
mysql:
image: mysql:8.0
container_name: vt_album
command: --default-authentication-plugin=mysql_native_password
volumes:
- .:/application
restart: always
environment:
- MYSQL_ROOT_PASSWORD=2222
- MYSQL_PASSWORD=2222
ports:
- "3306:3306"
website:
container_name: php_album
build:
context: ./
volumes:
- ./src/:/var/www/html
ports:
- 8080:8080
depends_on:
- mysql
好像"This image ships with the default php.ini-development and php.ini-production configuration files."你应该选一个
# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
我找到了解决方案。 docker 文件和 docker-compose.yml 文件与问题中的相同。没有变化。但是连接数据库的时候应该写"mysql: host = mysql"而不是"mysql: host = localhost"。这里的第二个"mysql"代表我们在docker-compose.yml文件中定义的服务名
以下代码首先创建数据库。然后,它创建连接对象。然后调用相关方法创建表。
protected $conn;
protected $username = "root";
protected $password = 2222;
protected $dbname = "db_2";
function _construct(){
$db = new PDO("mysql:host=mysql;charset=utf8; port:3306", $this->username, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$database = "CREATE DATABASE IF NOT EXISTS ".$this->dbname.";ALTER DATABASE ".$this->dbname." CHARACTER SET utf8 COLLATE utf8_turkish_ci;";
$db->exec($database);
$this->conn=new PDO("mysql:host=mysql;charset=utf8;port:3306", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->create_table_categories();
$this->create_table_users();
$this->create_table_files();
return "<br>success<br>";
}