I'm setting up a PDO connection in a test script:
use app\SomeDAO;
class SomeTest extends \PHPUnit_Framework_TestCase
{
protected $db;
public function setUp()
{
$dsn = "mysql:host=localhost;dbname=baseball;user=root;password=root";
$this->db = new PDO($dsn);
}
I'm getting an error:
PDOException: SQLSTATE[HY000] [2002] No such file or directory.
How can I use PDO here?
In Unix based (like linux, bsd, or OS X) systems, with MySQL localhost is secret code for try-to-use-a-socket, unless a you force it via a protocol flag to not do this (and no one ever does this). Just remember localhost usually equals socket file.
If Mysql in your MAMP is running in non-socket mode, you can try replacing the host value with 127.0.0.1 which connects via TCP on via port to the local machine--you'll need to figure out which port it's running on if it's not the default port.
If you look at the MAMP start script
less /Applications/MAMP/bin/startMysql.sh
You can see if it's starting in socket mode, and what file it's using if it has a config param like:
--socket=/Applications/MAMP/tmp/mysql/mysql.sock
You can also investigate what open socket mysql might be using by following the answer in this question: error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
If you're running in socket mode, you need to make sure PDO knows the path to the socket file. One way to do this is specify the unix_socket instead of host in the dsn:
$dsn = "mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=baseball;user=root;password=root";
I had problem like that at MAMP. My decided it, when I connected with PDO I used next line code:
$this->pdo = new \PDO("mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;port=8889;dbname=mydatabase;charset=utf8", 'root', 'root');
Related
I have a .php file which should receive and show data from a remote database. I run my program from PHPStorm (which is connected to the remote database through the "Database" right-hand pane) and a browser. Both ways I get an error which depends on the number of arguments I pass to pg_connect() function.
If I use
$dbconn = pg_connect("host=pg hostaddr=server.address.ru port=5432 dbname=studs user=... password=...")
than the error is
Unable to connect to PostgreSQL server: could not parse network address "server.address.ru": Unknown host in...
But I am sure that I wrote the address correctly (there are no typos in it). This way I am not sure about the correctness of the format of the passed arguments.
If I use
$dbconn = pg_connect("host=server.address.ru dbname=studs user=... password=...")
command, the error is
pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection timed out
I found a lot of information about this errors, but it mostly refers to localhosts and doesn't solve my problem. I guess, the problem can be in the way this connection is set in the function, but I do not know why it doesn't work properly. How can I solve it?
Thanks to #TangentiallyPerpendicular, I got on a right way of setting the connection. But since I have PostgreSQL remote connection, it wasn't just up to this answer.
What I did and how I set the connection (I work from PHPStorm so all the actions are based on this platform):
Open PuTTY and set an SSH connection (an SSH tunnel) between the server's DB port (for PostgreSQL it's usually 5432) and your local computer's PostgreSQL port (5432 most often too). You can do the same from a command line.
Open PHPStorm and in "Database" section (an icon on the right-hand side of the environment or "Data Sources and Drivers" in Settings) set general information ("General" section) and set another SSH tunnel ("SSH/SSL"). In SSH Configurations (the same "SSH/SSL" section) set a local port - it will be important in our connection! Let's say, this port is 20000. The port of the server you're connecting to is a usual one (usually 22 or 2222).
In the program the right use of function is $dbconn = pg_connect("host=localhost port=20000 dbname=studs user=... password=...") or die('Error: ' . pg_last_error());
The connection is set.
For those who has troubles setting an SSH tunnel with a remote PostgreSQL from PHP this can be useful too.
I am trying to connect via ssh from php, and send a file. When the machines are different, it works. When I connect to the same machine, it doesn't work.
$this->conn = ssh2_connect($this->ci['host'],$this->ci['ssh_port']);
ssh2_scp_send($this->conn,$ssh_source,$ssh_destination);
The connection $this->conn is valid in both cases.
So is there any reasons why ssh2_scp_send() will not work when $this->conn connects to the same machine? When trying on the same machine I get: Warning: ssh2_scp_send(): Failure creating remote file: (null)
As per this
1: http://php.net/manual/en/function.ssh2-scp-send.php Documentation this function is use for Copy a file from the local filesystem to the remote server using the SCP protocol.
So for same system you can use this function - copy
I am running PHP inside of the official PHP Docker container found here: https://github.com/docker-library/php
I am trying to get my application to connect to an external MySQL server using the old mysql_connect function as that's what our application uses. When trying a very simple script it yields the error: Warning: mysql_connect(): No route to host.
I have verified that using the standard MySQL CLI that I can connect to the intended server from inside the docker container so know that my container can speak to the outside world with no issues. Similarly things like file_get_contents() work with no issues indicating no DNS issues.
Both the host and the container have their DNS set to 8.8.8.8 and the container was launched with the --net host flag.
For reference here is the script I tried:
<?php
mysql_connect("my-external-rds.amazonaws.com", "user", "password")
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
Any ideas on how to resolve this issue? I can provide any information necessary.
Probably just because I'm not experienced with this sort of thing, but I downloaded MySQL with Apache running, and I'm working with the following code in a PHP file:
public static $connection = NULL;
private static $host = MYSQL_HOST;
private static $username = MYSQL_USER;
private static $passwd = MYSQL_PASS;
private static $db = MYSQL_DBNAME;
public static function init() {
if(!self::$connection) {
self::$connection = new mysqli(self::$host, self::$username, self::$passwd, self::$db);
}
}
It comes up with this when I open it in Firefox:
trying to connect via unix:///var/mysql/mysql.sock (says this doesn't exist—which, it doesn't)
I replaced MYSQL_HOST with 'localhost', MYSQL_USER with both 'mysql' and 'root' (stupid, yes), MYSQL_PASS with both my system password and NULL, and MYSQL_DBNAME with a few more things (I am having trouble finding out what my database name is called, even with MySQLWorkbench...I started learning this entire field of computing two days ago). It does say that a MySQL server is running on my machine, just not sure how to put the legos together here. Most of the settings are default (port 3306 and such). A test database migration over MySQLWorkBench failed (something to do with not reading the number of rows correctly), but otherwise it was fine and dandy, from what I saw.
Any help would be very welcome!
When you specify localhost as the host name, your computer will try to access the MySQL server using sockets in /var/mysql/mysql.sock. Since that file does not exist, this won't work. However, when you specify 127.0.0.1 as host name, the MySQL connection is set up over TCP/IP.
See also this question.
So the answer is to either find where MYSQL_HOST is defined and change it to be 127.0.0.1, or forget about MYSQL_HOST and just enter 127.0.0.1 instead. The latter is harder to maintain in case you would want to move your site to some other location (server).
Try restarting SQL server. This may recreate the missing .sock file. See here for info on restarting: http://theos.in/desktop-linux/tip-that-matters/how-do-i-restart-mysql-server/
I am attempting to use PHP's PDO_DBLIB driver to connect to a remote database and am having some issues.
The database is connectable via the same environment using telnet and a SQL client. However, connecting using the following code in PHP does not work:
<?php
$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');
Running this code, whether it be from the command line or Apache, yields the following error:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)'
I am able to connect to the database using the same code on a different web server, which leads me to believe that it's a configuration issue. The php.ini files on the two servers look relatively the same. They each have the same PDO libraries enabled with the same options configured.
Does anyone have any idea why this could be happening?
Turns out that it was a much simpler issue than I thought. For whatever reason, the development server was not using Port 1433 as the default port in the connection and was instead using Port 4000.
I discovered this by enabling the logs in the freetds.conf file and monitoring them as I was making the request.
Also, something to note: The DBLIB extension uses a colon (:) as a separator between the host and the port instead of the comma. Unfortunately, the error that you receive when you use a comma isn't very descriptive, so hopefully someone benefits from this discovery.
Write port into freetds.conf directly for this host:
[RemoteServer]
host = RemoteServer
port = 1433
And leave php-code us is:
$conn = new PDO('dblib:dbname=TestDB;host=RemoteServer;charset=utf8', 'my_user', 'my_pass');