错误:SQLSTATE[HY000] [2002] 没有那个文件或目录 [英] Error: SQLSTATE[HY000] [2002] No such file or directory
问题描述
我知道这种类型的问题可能被问过几次,但它们都与 Laravel 有关,在我的情况下,这是没有框架的 vanilla php.
I know this type of question as probably been asked a couple of times, but they all have something to do with laravel, in my case this is vanilla php with no framework.
我有一个表单可以从用户那里收集表单并将其保存到数据库中,但我不断收到关于 MYSQL 的错误 错误:SQLSTATE[HY000] [2002] 没有这样的文件或目录代码>.这是我的数据库配置文件和包含它的文件:
I have a form that that will collect form from users and save it into the database as it should, but i keep getting this error about MYSQL Error: SQLSTATE[HY000] [2002] No such file or directory
. Here is my database config file and the file that includes it:
// Database config
<?php
$host = 'localhost';
$user = 'admin';
$password = '123456';
$db_name = 'nairobi';
$dsn = 'mysql:host='.$host.';dbname='.$db_name;
$options = [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
// Create a new PDO instance
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
print "Error: " . $e->getMessage() . "</br>";
die();
}
?>
<?php
require "./library/Database.php";
if (isset($_POST['submit'])) {
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$sql = 'INSERT INTO TABLE members(first_name, last_name, email)
VALUES (:first_name, :last_name, :email)';
$stmt = $dbh->prepare($sql);
$stmt_value = [
':first_name' => $first_name,
':last_name' => $last_name,
':email'=>$email
];
$stmt->execute($stmt_value);
}
?>
是的,我知道处理表单的脚本不安全.
Yes, I know the script that process the form is not secure.
另外,当我访问页面时出现以下错误
Also, I get the following error when I access the page
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] No such file or directory in /var/www/html/library/Database.php:19 Stack trace: #0 /var/www/html/index.php(2): require() #1 {main} thrown in /var/www/html/library/Database.php on line 19
如果需要,这是我使用的 sql
If needed, this is the sql I have gone with
CREATE TABLE IF NOT EXISTS members(
member_id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
reference_code INT NOT NULL
);
整个项目也在一个docker容器中,这里是docker-compose.yml
文件
This entire project is also inside a docker container, here is the docker-compose.yml
file
version: '3.7'
services:
php:
container_name: nairobi_php
build:
context: ./
volumes:
- './src:/var/www/html'
depends_on:
- mysql
ports:
- 80:80
mysql:
container_name: nairobi_mysql
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: CUeHpADRmZCtnTFGctxp
MYSQL_DATABASE: nairobi
MYSQL_USER: admin
MYSQL_PASSWORD: 123456
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
推荐答案
我终于解决了问题,感谢@Dlk 的帮助.
I finally solved the problem, thanks @Dlk for your help.
问题的原因是因为在 database.php
中,我将 mysql 的主机称为 localhost
而不是 database.php
中的 MYSQL 服务名称代码>docker-compose.yml 文件.因此 database.php
文件应该是这样的:
The cause of the problem was because in database.php
, I was referring to the host for mysql as localhost
instead of the name of the MYSQL service in the docker-compose.yml
file. Hence database.php
file should look like so :
<?php
$host = 'nairobi_mysql'; // Must be the service name of the database in `docker-compose.yml`
$db_name = 'nairobi';
$user = 'admin';
$pass = '123456';
$charset = 'utf8mb4'; // Always set charset for database
$port = '3306';
$dsn = "mysql:host=$host;dbname=$db_name;port=$port;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>
与我的 docker-compose.yml
文件相关
version: '3.7'
services:
php:
container_name: nairobi_php
build:
context: ./
volumes:
- './src:/var/www/html'
depends_on:
- mysql
ports:
- 80:80
mysql:
container_name: nairobi_mysql
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: CUeHpADRmZCtnTFGctxp
MYSQL_DATABASE: nairobi
MYSQL_USER: admin
MYSQL_PASSWORD: 123456
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
这篇关于错误:SQLSTATE[HY000] [2002] 没有那个文件或目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!