pdo_mysql unable to create mysqld.sock file - php

I'm adding PHP (php-fpm) to an existing Nginx Ubuntu 10.04 server and can't get pdo_mysql to work. I'm trying to connect to a MySQL server someplace else, but all the Googling answers I found are in regards to MySQL not working on the local server, so I'm not sure how to proceed.
I don't have mysqld installed, so I'm not sure if I need it, or if there's a way around this? Also I'm wondering if it can't create mysqld.sock because there is mysql user on the server?
Error:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) Fatal error: Call to a member function query() on a non-object
My PHP connection code:
$this->PDO = new PDO('mysql:host=' . $this->config->getValue('db_host') .
';dbname=' . $this->config->getValue('db_name'),
$this->config->getValue('db_user'),
$this->config->getValue('db_pass'));
My Query line it fails on:
$PDOStatement = $this->PDO->query($query);
Appreciate any help, thanks.

Couple of things you should check
D you have mysql-client installed? if you are connecting to remote machine. you don't need mysql running on your box but I think you will still need the client.
Make sure the host value is correct. Double & triple check
Lastly, Try connecting manually using command line, by the following command.
$ mysql -u <INSERT_USER_HERE> -p -h <INSERT_IP_OF_REMOTE_MACHINE_HERE> <INSERT_DB_NAME_HERE>
If it does not work. come back with exact error message.

Your server is attempting to connect to a local MySQL instance. If your database is on a different remote server, make sure that the the db_host value of your configuration file points to the correct hostname of the MySQL server.
You do not need mysqld installed, but you do need MySQL client libraries.

Are you sure that $this->config->getValue('db_host') is returning the correct value? The behavior you're describing sounds a lot like it's returning an empty or null value.

Related

MariaDB with PHP: SQLSTATE[HY000] [2002] Connection refused

I have had a lot of reading on this issue, but I have not found an answer.
I have a PDO connection to mariadb 10.5 through php7.4
My problem: If I go to the url directly on browser, the connection goes through. If I try to access from another php file through file_get_contents or cUrl, the error ocurrs.
$this->conn = new PDO("mysql:host=".$this->host.";dbname=".$dbname, $this->username, $this->password);
I have tried to use IP address, localhost, 127.0.0.1 as host to no avail.
I've also tried setting port=8889 like advised elsewhere, binding mariadb to 0.0.0.0 or even IP
Repeat: Problem happens only when called though another php script, or when my companion android app hits the php page. (Volley error) It doesn't occur when I load it through the browser. I feel like screaming.
Yeah, CentOS 8, Openlitespeed, Php 7.4, MariaDb 10.5
some of my reading n trials...
PHP Connection failed: SQLSTATE[HY000] [2002] Connection refused
I have successfully solved the problem by dumping the database and re-importing it. Maybe 'something' had happened as I exported or imported the database through phpMyadmin.
Using ssh, I did:
mysqldump -u root database > database.sql;
Then login to mysql
mysql -u root; drop database..; create database...; \q;
Then restore the dump
mysqldump -u root database < database.sql;
Voila! error gone. Whatever devil that was, it kept me busy reinstalling the vps, configuring the server and checking my code for a whole week.

LOAD DATA LOCAL INFILE does not work from php 5.5 using PDO

I've recently moved web servers, and the new web server has a different version of PHP.
New Server: PHP 5.5.3-1ubuntu2 (cli)
Old Server: PHP 5.3.10 (cli)
On the database server, my.cnf is set to allow local-infile.
I can use load data local infile from:
- HeidiSQL
- The command line client running on the new web server using --local-infile=1
- PHP, using PDO, from the old web server
However, when I try to connect from the new web server, using PHP with PDO, I get:
A database problem has occurred: SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this MySQL version
php.ini:
[MySQL]
; Allow accessing, from PHP's perspective, local files with LOAD DATA statements
; http://php.net/mysql.allow_local_infile
mysql.allow_local_infile = On
PDO constructor:
$this->db_conn = new PDO("mysql:host=$host;dbname=$dbname;port=$port", $user, $pass, array(PDO::MYSQL_ATTR_LOCAL_INFILE => 1));
I've also tried ini_set('mysql.allow_local_infile', 1) and ini_set('mysql.allow_local_infile', true) in the PHP script.
The LOCAL keyword is needed because the files are hosted on the web server, not the database server.
What else does PHP 5.5 want from me?
You need to configure both the client and the server to allow this command:
Make sure the server allows LOAD DATA LOCAL INFILE. Edit /etc/my.cnf on the MySQL server:
[server]
local-infile=1
Set the PDO attribute in your PHP script:
<?php
$dsn = "mysql:host=localhost;dbname=test";
$user = "root";
$password = "root";
$pdo = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_LOCAL_INFILE=>1));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("LOAD DATA LOCAL INFILE 'foo.csv' INTO TABLE foo FIELDS TERMINATED BY ','");
This needs to be set in the driver options argument to the constructor, not in a subsequent call to setAttribute().
I just tested the above code example successfully with PHP 5.5.8 and Percona Server 5.6.15 on CentOS Linux 6.5.
If you still have trouble, you may have a build of the MySQL client or PDO that does not permit local-infile. The requirements are described in http://dev.mysql.com/doc/refman/5.6/en/load-data-local.html
The mysql.allow_local_infile option in your php.ini is not relevant to PDO.
Your code is absolutelty correct no need to change as the error lies in your database file.
error cause
The reason of your error caused as you have created the database backup through commandline and trying to upload data through custom php script.
solution
There are 2 solutions available to resolved the issue are:-
Try to load database through commandline and if you dont have access to commandline then you can ask your service provider to do upload for you. As he has to just run simple command.
Open the databse file in text editor and check the headers which encoding is used. Then read the data and de-encode it then run the insert command.
If the problem doesnt resolve then let me know..

db2_connect to as400 with php and wamp server

I have been trying to connect to a ibm db2 database but it seems impposible. The as400 is in a different server than the one running php.
Everytime I do a db2_connect I get the following error:
Fatal error: Call to undefined function db2_connect()
How can I make this function work?
Tip: I've already tried with odbc and it was a lost of time, but I'm open to suggestions on that path as well.
[EDIT]
I finally changed to java... it was impossible with php...
Check your php.ini file and make sure it has the DB2 extension enabled. http://www.php.net/manual/en/install.pecl.php
Try this link:
http://www.theregister.co.uk/2006/08/09/db2_udb_part2/
Deals with:
Installing the PHP DB2 extension,
Creating a connection,
Obtaining a result set
Also gives as an alternative the PDO option.
But as for DB2 on the AS400, am not sure if something still needs to be installed on the
AS400 for this to work?
Anyway, have used the ODBC Client Access with no problems at all. What difficulty did you run into?

Confusing PDO-only problem : Can't connect through socket/Access denied/Can't connect to server (shared host)

So the problem changed from what it was, i'll leave the original question below to prevent bad reviews on answers like I had after someone editing his question I answered :
So I am working on a (really lame) shared hosting which has PDO installed, but it doesn't work.
With default parameters
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
it throws this message :
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
With a simple mysql_connect, it works.
And the socket path seems correct (both phpinfo and this query :
show variables like 'socket';
confirm.
Localhost redirects to 10.103.0.14 (this data comes from mysql_get_host_info() and in phpMyAdmin)
In the PDO, if i replace localhost by 127.0.0.1 i will get
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)
And if i replace localhost by 10.103.0.14 :
Access denied for user 'USER_NAME'#'10.103.0.14' (using password: YES
Both IP adress (127.0.0.1 and 10.103.0.14) work with mysql_connect.
So apparently the problem comes from the PDO connection.
Does somebody knows where this could come from, or/and any way to fix it ?
Some server datas :
The PHP Version : 5.2.10
You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5
No command line possible.
(i know it should be the tech suport's job, but they're reaaaaaly slow)
Thanks
Previous question :
How to find the mysql.sock on a shared host (tricky way needed...)
So today's problem is : The PDO connection doesn't work on a shared host, and it's supposed to (it's installed on the server).
Just a basic PDO connection :
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=THE_DB_NAME', 'THE_USER', 'THE_PASSWORD');
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
throws this message :
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
A regular mysql connection :
mysql_connect("localhost", "THE_USER", "THE_PWD") or die(mysql_error());
mysql_select_db("24DLJLRR1") or die(mysql_error());;
echo 'Connected to database <br/>';
works fine.
So apparently it cannot find the .sock.
I think specifying the correct address should work, i tried some "classic" mysql path that I found on internet, without success.
The phpinfo says it is at this adress (/var/lib/mysql/mysql.sock)
(The PHP Version is 5.2.10)
You can see the server's phpinfo : http://web.lerelaisinternet.com/abcd.php?v=5
So i am trying to figure out where the hell it is !!!
I tried to look in the phpMyAdmin interface, but i couldn't find the info, plus it seems that phpMyAdmin connects to a different server (it has a different IP adress, and trying to connect to it with php gives a "Wrong password" error). The mysql_connect also connects to this adress, i think it redirects to a different server with some internal password/login.
Well if you have any idea of how to obtain this info (the provider's technical support is "fixing the problem"... it's been 1 month...).
Also maybe the problem comes from somewhere else, but the same stuff works on other shared hosts...
The need of PDO is because I use the Symfony framework with Doctrine for this website, and the Doctrine plugin needs PDO... I don't want to redo the website from scratch !
Thanks for your help !
This was already marked as answered, but not really solved (without changing databases).
So, just in case someone like me also experiences this problem...
The easiest way to fix this is to first get the socket path (either by looking in the php.ini file or by using: phpmyadmin or the console (or construct it in mysql or mysqli)
...to run the following query (anything but PDO):
show variables like 'socket'; //as mentioned by symcbean
THEN, in the PDO connection string, change it to use the socket instead of a hostname:
$dbc = new
PDO("mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=$DBName",
$User, $Password, array(PDO::ATTR_PERSISTENT => true)); // using
persistent connections
This worked for me.
FWIW, I had this issue and changed my host from 'localhost' to '127.0.0.1'.
I have no clue why localhost wasn't working, but that did the trick.
Odd thing is, we have tons of servers and it works on almost every one using 'localhost'
Is your server running with SeLinux enabled (enforcing)? If it is, try running as root:
# setsebool -P httpd_can_network_connect on
Can you try 127.0.0.1 as the server name instead of localhost?
IIRC, with some mySQL drivers / adapters, this decides whether the socket is used for establishing the connection or not.
Using the connection which works, run the query:
show variables like 'socket';
(this behaves just like a select statement)...and you'll get the path of the running socket.
Then check the file permissions.
I had the problem that production version worked just fine and a test version wasn't able to connect PDO :/
both versions was located at same servers, test in a sub directory.
The fix was replacing in DSN the localhost for ip.
'mysql:host=localhost;dbname=db'
became
'mysql:host=127.0.0.1;dbname=db'
try:
exec('`which mysql_config` --socket');
this should show you the configured socket.
I found the reason for the strange behaviour. If bind-address is different to 127.0.0.1 or 0.0.0.0 (all addresses) PDO can't connect to 127.0.0.1.
For what it's worth, I found this page after having the exact same issue. I am on a server running Apache & PHP only - MySQL is installed on another machine. I tried both the DNS name of the server and its IP and confirmed I could ping it. A PHP app on the same machine is talking to the database fine, using old syntax mysql_connect( ). But PDO from the CLI was throwing this error.
The solution for me was to check my DSN. Any typo in the DSN itself is ignored silently, and PDO assumes you mean localhost. My issue was I had "name=" instead of "dbname=" in the DSN.
The Issue In the Mysql configuration It you need to disable the option of skip-networking
in my.conf configuration file this should work fine
reference
http://www.wolfcms.org/forum/post7098.html#p7098
I just solved a similar issue. My guess is you probably replaced your mysql_connect() statement with the PDO equivalent. Don't forget you still have lots of other code dependent on that old connection statement. Try keeping the mysql_connect in place while writing in the PDO code.
What worked for me was specifying the port number like so:
mysql:hostname;port=3306;dbname=dbname;
This got it to work when connecting to a local database. Now I'm working on getting it to work with a remote db.
My problem may be different to the OP, but I thought it was worth posting. I did a software upgrade on a VM, then rebooted and got the OP's error message. It turned out to be an out-of-memory problem preventing mysql from starting. Deleting a few large files made the problem go away.
One year later, I found a solution for this issue : using a SQLite database. PDO worked fine, but not with MySQL
** EDIT ** as everyone is downvoting this: This solved my issue (I'm the OP). I was using Doctrine, so switching RDBMS was easy and quick. Also the website was some a home made CMS, with very few trafic, so SQLite was fine.
I know it's not a real "Answer" to the problem, but if someone is in the same context: a crappy shared hosting which you can't change with this weird PDO-MySQL bug AND is using doctrine. This IS a solution. I can delete this answer, but if I had thought of this at the time of the OP, I would have saved a lot of time.

Warning: mysql_connect(): Can't connect to local MySQL server

Warning: mysql_connect(): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (11) in /home/aa/public_html/bb/db.php on line 2
Could not connect:
iam getting this error but the problem is that this error comes irregularly,sometimes it didnt come all the day and sometimes it comes countless times.
what could be the possible reason for this?????
That could mean that MYSQL is down or you use the wrong host name while connect. One more possible reason for that could be difference in socket configuration of php against mysql, you can check it in by looking at entry socket in mysql config. file and by looking at output of phpinfo(), you need just to compare it. Or it could that someone else on your machine also using that socket.
PS. As well my wild guess, go through your code and check you always get your connection close right and all your queries as well.
Make sure that your MySQL is running.
OR
Create a file called: phpinfo.php with the following inside it:
<?php phpinfo(); ?>
Load the file in your browser: http://localhost/phpinfo.php and scroll down for mysql. Look for the MYSQL_SOCKET and make sure it matches your entry in your my.cnf file.
FYI, my my.cnf file is located in: /etc/my.cnf and it contains something like this:
# The MySQL server
[mysqld]
port = 3306
socket = /tmp/mysql.sock

Categories