I am trying to run some php scripts that we use at my company to do some data analysis. These scripts do things that require root access, so I have no choice but to run them with sudo
.
I have narrowed down the issues I'm having to connecting to the database as root vs. my own user. I have the following super stripped down code just to show the issue:
<?php
// mydatabasename, myUsername, myPassword are replaced with the real names, everything else is the same
$pdo = new PDO('mysql:host=localhost;dbname=mydatabasename', 'myUsername', 'myPassword', array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
var_dump($pdo);
?>
When I run this script without sudo
, it works just fine. When I run it with sudo
, I get the following error:
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)' in /opt/.../something.php:5
I'm not sure what's going on here, and I don't have a super advanced knowledge of PHP or MySQL. the file /var/run/mysqld/mysqld.sock
does not exist on our server. I'm not sure why root would be trying to access it but my user wouldn't.