php - Why can't I run a working PDO connection through terminal?

Solution:
I think, your unix_socket changed and you didn't change your php client php.ini
First of all, get your unix_socket location.
$ mysql -uroot -p
Enter your mysql password and login your mysql server from command line.
mysql> show variables like '%sock%';
+
Your unix_soket could be diffrent.
Then change your php.ini, find your php.ini file from
{-code-3}
You maybe install many php with different version, so please don't assume your php.ini file location, get it from your 'phpinfo';
Change your php.ini:
{-code-4}
Then restart your apache or php-fpm.
Answer
Answer
Solution:
It looks like you're usinglocalhost
as the connection string for your MySQL database. When MySQL seeslocalhost
, it interprets that as "use a mysql.sock" file. PHP is telling you it can't find that mysql.sock file.
The most likely cause for this is your web server and command line PHP are configured to use differentphp.ini
files, and thosephp.ini
files have differently configuredmysql.sock
files. You can find your web serverphp.ini
file(s) by running
phpinfo();
from a PHP file loaded via the web server. You can find your command linephp.ini
file(s) by running the following
$ php --ini
If you can't fix your command linephp.ini
file, consider connecting to your MySQL server via127.0.0.1
instead oflocalhost
. When you use the IP address, PHP should attempt to connect via the network instead of a socket file.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the payload is invalid.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.