PDOException: SQLSTATE[HY000] [2002] No such file or directory - php

I have put PushChatServer dir in htdocs folder and create database puschat try to run #"http://localhost/PushChatServer/api/test/database.php"
Then I got following exception.
I want do same thing to explain this link http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
I have done all that but I got this exception
Could not connect to the database. Reason: exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/PushChatServer/api/test/database.php:17 Stack trace: #0 /Applications/MAMP/htdocs/PushChatServer/api/test/database.php(17): PDO->__construct('mysql:host=loca...', 'root', 'root', Array) #1 {main}

You need to change host from localhost to 127.0.0.1
Laravel 4: In your app/config/database.php try changing host from localhost to 127.0.0.1
Laravel 5: In the .env file, change DB_HOST from localhost to 127.0.0.1
Source: PDOException SQLSTATE[HY000] [2002] No such file or directory

Quick test (run in shell):
php -r "new PDO('mysql:host=localhost;dbname=test', 'username', 'password');"
SQLSTATE[HY000] [2002] No such file or directory means php cannot find the mysql.default_socket file. Fix it by modifying php.ini file. On Mac it is mysql.default_socket = /tmp/mysql.sock (See MySQL connection not working: 2002 No such file or directory)
SQLSTATE[HY000] [1044] Access denied for user 'username'#'localhost' CONGRATULATION! You have the correct mysql.default_socket setting now. Fix your dbname/username/password.
Also see Error on creating connection to PDO in PHP

I had the same error using PHP 5.6.30 (macOS Sierra) with the simple PDO code like:
$pdo = new PDO('mysql:host=localhost;dbname=db_php', 'root', '');
And I received the same error message. To fix, I changed "localhost" for IP (loopback) "127.0.0.1" in my code:
$pdo = new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');
To test the connection:
$ php -r "new PDO('mysql:host=127.0.0.1;dbname=db_php', 'root', '');"
This work's for me!

Restart your database and local web server:
sudo service mysqld restart
It should work!

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),
]

PDO treats localhost very particularly:
From http://php.net/manual/en/ref.pdo-mysql.connection.php:
Note: Unix only: When the host name is set to "localhost", then the
connection to the server is made thru a domain socket. If PDO_MYSQL is
compiled against libmysqlclient then the location of the socket file
is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled
against mysqlnd a default socket can be set thru the
pdo_mysql.default_socket setting.
That why PDO and mysql_connect will give different behavior for localhost.
So, if you want to use a TCP connection with PDO, never use localhost but 127.0.0.1.

I had the same error for Mysql PDO, this code works for me!
<?php
$dsn = 'mysql:host=127.0.0.1;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
?>
got this code from : http://php.net/manual/en/ref.pdo-mysql.connection.php

Run the following command in shell, I think it will help.
php artisan cache:clear
php artisan config:clear
php artisan view:clear

On Mac OSX/MAMP you may find a mysql.sock.lock file in your /Applications/MAMP/tmp/mysql folder.
You can safely remove the file mysql.sock.lock and you should be in good shape.

Just ran into this same issue and the problem is the password. In the tutorial the author lists the password as:
"d]682\#%yl1nb3"
So as per the suggestion given by #Marki555, I looked in the config file - api_config.php. and the password listed there is:
"d]682\#%yI1nb3"
The upper case 'I' is what caused the issue because the password you set for user on the db has the password with the lowercase l but it really is looking for the uppercase I. After I changed the pushchat user's password to have an uppercase i, it worked!

Related

"Unable to connect to your database server using the provided settings."

how to import mysqldb into heroku postgres?
I am getting an error In codeigniter
these are my settings:
$active_group = 'default';
$query_builder = TRUE;
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'whatsapp',
'dbdriver' => 'mysqli',
'dbprefix' => '',
You should post the error you are getting, this way others can maybe help you.
I found also this solution that, may or may not be helpful:
For me the issue was in the php.ini file. The property mysql.default_socket was pointing to file in a non-existent directory. The property was pointing to /var/mysql/mysql.sock but in OSX, the file was located in /tmp/mysql.sock.
Source: CodeIgniter: Unable to connect to your database server using the provided settings Error Message
You can use mysql-postgresql-converter Dump MySQL database in PostgreSQL-compatible format
mysqldump -u username -p --compatible=postgresql databasename > database.sql
then use the converter to transfer data into *.psql file. then load new dump into a fresh PostgreSQL database

Doctrine fails to connect to MYSQL database in linked docker container

I am using two Docker container, one having ubuntu and an apache webserver running, the other one a mysql server. The containers are linked and i can connect from the ubuntu container onto the mysql server. For the connection I use in the ubuntu container:
mysql -u root -h mysql
where the second 'mysql' is the name of the container. I can connect to it through the container id as well, so the connection works as well as connecting onto the database from the windows environment.
What doesnt work is the connection from doctrine to the database within the PHP application which is in the ubuntu container.
The config looks like this:
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'mysql',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => 'db_name',
'charset' => 'utf8',
)
)
),
)
But I get the Error message
Uncaught PDOException: could not find driver in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
and
Zend\ServiceManager\Exception\ServiceNotCreatedException: An abstract factory could not create an instance of doctrine.entitymanager.ormdefault(alias: doctrine.entitymanager.orm_default). in /var/www/vendor/zendframework/zend-servicemanager/src/ServiceManager.php
Does anyone have any idea how to solve this and where exactly the error comes from?
I have already tried to put in the container ID as 'host' and commented out the 'password' field as it is not used.
Thanks in advance
Jonathan
You need to have pdo_mysql allowed on your system, it doesn't seem a connectivity issue but a php configuration problem.
Can you try to do this command inside your php container
php -i | grep pdo_mysql
Just to understand if php has this module

Laravel setup of migrations using Artisan is failing

I am trying to set up migrations for the first time and when I run
php artisan migrate:install
from within the root folder of my project I get the following SQLSTATE error:
SQLSTATE[HY000] [2002] No such file or directory
I have tested running mysql to make sure it is working and referenced and I have ran php artisan help:commands to make sure artisan is working (it is).
The website itself is functioning and reading from the database fine.
This may be an issue with the sockets if you're using MAMP or XAMPP.
From http://forums.laravel.io/viewtopic.php?id=980...
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'database',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8',
'prefix' => '',
),
),
You can pass an optional "unix_socket" to the array and specify the MAMP socket instead of the default location.
Also, try and change 'host' => 'localhost', to 'host'=> '127.0.0.1', because unix does not recognise 'localhost'

CakePHP: Can't access MySQL database

I'm new to CakePHP and am just running through the configuration process, but am stumped why Cake can't access my MySQL database. The Cake info page says my tmp directory is writable, the FileEngine is being used for caching (don't know what this means), and my database configuration file is present, but that CakePHP cannot connect to the database.
Here are my setup details:
PHP 5.3 (pre-installed on Snow Leopard)
MySQL 5.1.40 64-bit
CakePHP 1.2.4.8284
Here are the steps I went through:
Created a MySQL schema called cake_blog
Created a MySQL user called cake_blog_user
Granted cake_blog_user the appropriate permissions on cake_blog#localhost and cake_blog#%
Copied the database.php.default file to database.php and edited the database connection details as appropriate
Here is the relevant configuration data from database.php:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Am I missing something here? I should also mention that if I insert an echo mysql_error(); into the /cake/libs/view/pages/home.ctp file right before it tests the database connection, the error displayed is "No such file or directory." I have no idea what file or directory it's talking about.
Thanks!
What usually bites me in it's that MySQL thinks of 'localhost' as 'connect thru the unix socket' and '127.0.0.1' 'connect thru TCP port'. With things like XAMPP (at least on mac) the unix socket file isn't there. Just use 127.0.0.1 instead.
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Should work all the time.
If it is the socket, just edit /etc/php.ini to reflect the following
pdo_mysql.default_socket=/tmp/mysql.sock
and
mysql.default_socket = /tmp/mysql.sock
I believe you can also do the following
<?php
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
'port' => '/tmp/mysql.sock',
)
?>
doing this might mean you need to edit the database.php file when you go live on the production server.
Thanks everyone for pointing me in the right direction. The mysql.sock file has been moved to /tmp/mysql.sock instead of its default location at /var/mysql/mysql.sock. Editing the php.ini file to reflect this has fixed the problem.
check your phpinfo and use the socket listed. that worked for me.
On Ubuntu, if you installed both 7.0 and 5.6 versions of PHP this wont work.
Youll need to switch if you have both versions:
Look first if is 7.0 version: the command is php -v.
Next do
sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart

How do I get CakePHP bake to find mysql.sock and recognize MySQL while using MAMP on Mac OSX?

I am currently reading "Beginning CakePHP:From Novice to Professional" by David Golding. At one point I have to use the CLI-command "cake bake", I get the welcome-screen but when I try to bake e.g. a Controller I get the following error messages:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.
I suspect that the error-messages has to do with php trying to access the wrong mysql-socket, namely the default osx mysql-socket - instead of the one that MAMP uses. Hence I change my database configurations to connect to the UNIX mysql-socket (:/Applications/MAMP/tmp/mysql/mysql.sock):
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'connect' => 'mysql_connect',
'persistent' => false,
'host' =>':/Applications/MAMP/tmp/mysql/mysql.sock', // UNIX MySQL-socket
'login' => 'my_user',
'password' => 'my_pass',
'database' => 'blog',
'prefix' => '',
);
}
But I get the same error-messages with the new socket:
Warning: mysql_connect(): Can't connect to local MySQL server through socket '/Applications/MAMP/tmp/mysql/mysql.sock:3306' (2) in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 117
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 122
Warning: mysql_get_server_info(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 130
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /Applications/MAMP/htdocs/blog/cake/libs/model/datasources/dbo/dbo_mysql.php on line 154
Error: Your database does not have any tables.
Also, even though I use the UNIX-socket that MAMP show on it's welcome-screen, CakePHP loses the database-connection, when using this socket instead of localhost.
Any ideas on how I can get bake to work?
-- Edit 1 --
Thank you guys for helping me out! :)
I have a problem figuring out where in my.cnf to edit to get MySQL to listen to TCP/IP request. The only paragraph I can find where TCP/IP is mentioned is the following:
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
That allows me to turn off TCP/IP completely, which is the opposite of my intention. I don't know how to go about what you suggest, if you could be more elaborate it would be great. I am a total n00b on these matters :S
Reg. connecting to a local socket: I removed the leading colon in the host-parameter, same result.
I find the solution to this problem :
Add a socket config in the cakephp app/config/database.php file
class DATABASE_CONFIG {
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock', // here is the key !
'login' => 'you',
'password' => 'yourpass',
'database' => 'yourdb',
'prefix' => '',
);
From the error, it looks like it's trying to connect to an actual IP address and not a UNIX socket, look:
'/Applications/MAMP/tmp/mysql/mysql.sock:3306'
It's appending a port to the socket, which is wrong.
So, I'd first try to configure MySQL to listen to TCP/IP requests (edit the proper section in my.cnf) and try providing 127.0.0.1 instead of the socket.
In case you won't scroll down:
To fix it at CakePHP level, change host on database.php to 'localhost' and add a port directive with its value set to the socket name '/Applications/MAMP/tmp/mysql/mysql.sock'
For anyone running into this problem when using CakePHP 2.0: for me the above database configuration files didn't do the trick. Found the 'unix_socket' property though, that worked for me:
<?php
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'unix_socket' => '/tmp/mysql.sock',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'encoding' => 'UTF8',
'prefix' => ''
);
}
I had the same problem, when using MAMP and the Cake CLI. I'm running CakePHP 1.1xxx and MAMP 1.7.
The problem is, that the MySQL socket can't be found :D
Open Terminal and enter the following:
my-macbook:~ chris$ php -i | grep mysql.default_socket
mysql.default_socket => no value => no value
my-macbook:~ chris$ php -i -c /Applications/MAMP/conf/php5 | grep mysql.default_socket
mysql.default_socket => /Applications/MAMP/tmp/mysql/mysql.sock => /Applications/MAMP/tmp/mysql/mysql.sock
The catch is that without explicitly giving the php binary the path to its (read MAMP's) config file, the mysql.default_socket is not set.
Using that I did not need to change my database configuration whatsoever.
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
I had this exact same problem with mamp, and fixed it with the above command. I think you have to run this command every time you restart your computer. Maybe there is a better way to do it, but I use this with clix.app, so it is usually pretty fast. Also, change your host to localhost.
For me, I forgot to set the port on the host, since I was not using the default MySQL port in MAMP.
i.e. If your MySQL port is 8889, set host to localhost:8889.
Yeah had a similar issue, I think pointing to the the socket:portno as Vinko suggested might work - however if you use an ip address / localhost you'll be fine.
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'account',
'password' => 'password',
'database' => 'database',
'prefix' => '',
'port' => '/var/mysql/mysql.sock'
);
}
This worked for me:
class DATABASE_CONFIG
{
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'account',
'password' => 'password',
'database' => 'database',
'prefix' => '',
'port' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
}
Try configuring your firewall... That was the case for me!
You can also make a symbolic link, bake is looking for mysql.sock in /tmp
like this:
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
Cheers, Ebbot.

Categories