I’m not sure if this will work for you; but I use CakePHP, and I get this error whenever I forget to put the port number at the end of the ‘host’.
Hope this helps!
Before
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
After
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost:8080',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
I’m not sure if this will work for you; but I use CakePHP, and I get this error whenever I forget to put the port number at the end of the ‘host’.
Hope this helps!
Before
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
After
'test' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost:8080',
'username' => 'root',
'password' => 'root',
'database' => 'mylogin',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetadata' => true,
'quoteIdentifiers' => false,
'log' => false,
//'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
'url' => env('DATABASE_TEST_URL', null),
]
I’m trying to open a file and IMPORT it to PSQL
$sql ="COPY r_vehicle_submodel FROM '".$filePath."' DELIMITER ',' CSV";
However it display me error:
Fatal error: Uncaught exception ‘PDOException’ with message
‘SQLSTATE[58P01]: Undefined file: 7 ERROR: could not open file
«C:\xampp…\uploads\SearchTreeCZ.csv» for reading: No such file or
directory’ in
I try to copy this path to clipboard and open file and i open it correctly so file is there for sure.
After that I was thinking if just PHP don’t have permission to this file/ folder so I write something really simple:
$file = fopen($filePath,'r');
echo fread($file,filesize($filePath));
Which open me my file correctly, I also try to scandir this path successfully.
echo $filepath display:
C:\xampp\htdocs\xampp\utilities\searchTreeImport\uploads\SearchTreeCZ.csv
Guys do you have any idea where can be a problem and WHY I can’t open this file?
Thanks
EDIT:
This QUERY works (MySQL)
$sql = "LOAD DATA INFILE '".$filePath."'
INTO TABLE r_vehicle_submodel
CHARACTER SET UTF8
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
IGNORE 1 LINES ";
This QUERY doesn’t work (PSQL)
$sql ="COPY r_vehicle_submodel FROM '".$filePath."' DELIMITER ',' CSV";
So i expect problem can be in my $sql query
If you are using PDO (PHP Data Objects) for database connections and encountered the error message “PDOException: SQLSTATE[HY000] [2002] No such file or directory”, this guide will help you to fix the issue.
This error occurs when the PHP script cannot connect to the MySQL server. The most common reason is that the MySQL server is not running or the connection details are incorrect.
Check MySQL Server Status
First, check whether the MySQL server is running or not. To do this, open the terminal and run the following command:
systemctl status mysql.service
If the MySQL server is not running, start it using the following command:
sudo systemctl start mysql.service
Check Connection Details
Check whether the connection details in the PDO connection string are correct. Make sure that the server name, username, password, and database name are correct. Here’s an example of a PDO connection string:
$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "myusername";
$password = "mypassword";
try {
$pdo = new PDO($dsn, $username, $password);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Check MySQL Server Configuration
If the MySQL server is running and the connection details are correct, then the issue might be with the MySQL server configuration. Check the MySQL configuration file (`/etc/mysql/mysql.conf.d/mysqld.cnf`) and make sure that the `bind-address` is set to `127.0.0.1`. If it is set to `0.0.0.0` or any other IP address, change it to `127.0.0.1` and restart the MySQL server.
bind-address = 127.0.0.1
Conclusion
In this guide, we have discussed how to fix the “PDOException: SQLSTATE[HY000] [2002] No such file or directory” error in PDO while connecting to the MySQL server. Make sure the MySQL server is running, the connection details are correct, and the MySQL configuration is set properly. If you have any further issues, consult the PDO documentation or seek help from the PHP community.
Работаю с PDO классом , новенький в PHP ..
Получаю ошибку при попытке подключится к базе данных ..
При использовании команды
php -i | grep pdo // Я получаю pdo_mysql.default_socket => /var/mysql/mysql.sock =>
// /var/mysql/mysql.sock
При использовании команды в mySql shell ->
mysql> show variables like '%sock%' ->Я не получаю ни какой информации .
В php.ini файле я просмотрел pdo_mysql.default_socket , которое было в начале пустое после чего я заменил значение на pdo_mysql.default_socket=/var/mysql/mysql.sock
В моем XAMP/xamppfiles/etc/my.cnf в разделе [mysqld] я получаю это информацию :
user = mysql
port = 3306
socket = /Applications/XAMP/xamppfiles/var/mysql/mysql.sock
Нужна помощь , как решить эту проблему с сокетом ? Использую MAC Sierra 10.12.6 .
Не использую Laravel .
Мой код :
Test:
<?php
require_once '../classes/DBConnector.php';
class ConnectionTesting extends TestCase
{
public static function connect($dbdriver,$host,$port,$dbname,$username,$password){
$connection = new DBConnector($dbdriver,$host,$port,$dbname,$username,$password);
print_r($connection->getConnection());
return $connection;
}
}
ConnectionTesting::connect(
'mysql:','127.0.0.1',':3306',
'conlineltd','root','plemia19880@@'
);
The Connection:
<?php
class DBConnector
{
private $user_name;
private $password;
private $db_driver;
private $host;
private $port;
private $db_name;
function __construct($dbdriver,$host,$port,$dbname,$username,$password)
{
$this->checkValidation($dbdriver, $host, $port, $dbname, $username, $password);
$this->host = $host;
$this->user_name = $username;
$this->password = $password;
$this->port = $port;
$this->db_name = $dbname;
$this->db_driver = $dbdriver;
}
private function checkValidation($dbdriver,$host,$port,$dbname,$username,$password)
{
$arr = array(
"name" => $username,
"password" => $password,
"host" => $host,
"port" => $port,
"dbname" => $dbname,
"driver" => $dbdriver
);
foreach ($arr as $key => $value) {
if (empty($value)) {
throw new BadMethodCallException($key . " Can't be empty!");
}
}
}
public function getConnection():PDO{
$connection = null;
try {
$connection = new PDO($this->db_driver . $this->host . $this->port . $this->db_name, $this->user_name, $this->password);
$connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $exception) {
echo "Problems with connection: ".$exception->getMessage();
}
return $connection;
}
}
Error:
Problems with connection: SQLSTATE[HY000] [2002] No such file or directory
Fatal error: Uncaught TypeError: Return value of DBConnector::getConnection() must be an instance of PDO, null returned in /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/classes/DBConnector.php:49 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/tests/ConnectionTesting.php(9): DBConnector->getConnection() #1 /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/tests/ConnectionTesting.php(24): ConnectionTesting::connect('mysql:', '127.0.0.1', ':3306', 'conlineltd', 'root', 'plemia19880@@') #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/conlineltd/src/php/classes/DBConnector.php on line 49
