Unable to connect to Google Cloud SQL using PDO - php

I'm a PHP newbie. I'm trying to connect to GAE using PHP and PDO. I have tried connecting using mysql_connect() and mysql_select_db(). I have been successful. However when I try to connect using PDO, I get an error
Connection failed: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
My code is as follows:
define('DBH_SOCKET', '/cloudsql/****:****');
define('DBH_NAME', 'wordpress_db');
define('DBH_USER', 'censored');
define('DBH_PASSWORD', 'censored');
$pdo_conn = "mysql:unix_socket=".DBH_SOCKET.";dbname=".DBH_NAME.";charset=utf8";
try {
$dbconn = new PDO($pdo_conn, DBH_USER, DBH_PASSWORD);
$dbconn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
}
echo "<br> var is ".$pdo_conn;
/*$conn = mysql_connect(DBH_SOCKET, DBH_USER, DBH_PASSWORD);
if (!$conn)
die('Connect error ('.mysql_error());
$db_selected = mysql_select_db(DBH_NAME, $conn);
if(!$db_selected)
die('Cant use db: '.mysql_error());*/
What am I doing wrong? I have looked at some tutorials on the net like, http://webandphp.com/WorkingwithPHPontheGoogleCloudPlatform-166942. They use PHPStorm and a JDBC driver. However, there is no mention of using a JDBC driver in the official google tutorials.

When connecting using PDO locally, you'll need to use a connection string that matches your local MySQL server settings, e.g. "mysql:host=localhost;dbname={db name};charset=utf8".
When running on GAE, your current connection string should work fine. The fact that you're getting the error message "Unable to find the socket transport "unix" - did you forget to enable it when you configured PHP?" suggests that you might have missed the "unix_socket=/cloudsql/{project id}:{instance id}" part.

Related

PHP PDO ODBC CONNECT TO PERVASIVE DATABASE

All 32Bit. Windows 2018 Server.
PVSQL v10 (I can't update this)
PHP 5.3.28 (I might be able update this to 5.6.x) 7.x Would break too much
I've tried searching everywhere for an example on how to connect PHP to Pervasive DB with PDO. I've managed to connect using the following non PDO connector:
$conn = odbc_connect("demodata","","");
if(!$conn) die("Could not connect");
Looking at the examples from https://docs.actian.com/psql/psqlv13/index.html#page/odbc%2Fodbcadm.htm%23ww1213912
I found that I should be using something like:
try {
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerDSN=demodata;");
if(!$conn) die("Could not connect");
}
catch (PDOException $e) {
echo $e->getMessage();
exit;
}
I also tried:
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=localhost;SeverDSN=demodata;");
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=localhost;SeverDSN=DEMODATA;");
When I do though. I get the error:
SQLSTATE[HY000] SQLDriverConnect: -1206 [Pervasive][ODBC Client
Interface][LNA][Pervasive][ODBC Engine Interface][Data Record
Manager]Non-db file or corrupted db.
But I know its not corrupted.I'm able to connect and do queries the other way. Would just really like to be able to use PDO. When I do a test from the Pervasive ODBC Client DSN Setup. I get Connection Successful.
What am I missing?
Thanks to #Mitheil for pointing me in the right direction. The server name does seem to be a must even on local machine. So the string needed is: "odbc:Driver={Pervasive ODBC Client Interface};ServerName=db;ServerDSN=demodata;"
try {
$conn = new PDO("odbc:Driver={Pervasive ODBC Client Interface};ServerName=db;ServerDSN=demodata;");
if(!$conn) die("Could not connect");
}
catch (PDOException $e) {
echo $e->getMessage();
exit;
}

MySQL connection via Unix socket works with mysqli but not with PDO

I'm using MAMP 5.5.42 with PHP 5.6.7 on my Mac OS 10.11.3. The code snippet is as follows :
try{
$db = new PDO('mysql:host=localhost:8889;dbname=lacolshow_database;charset=utf8', 'root', 'root');
}
catch (PDOException $e) {
var_dump($e->getMessage());
}
The code above raises no exception, but produces an empty PDO object.
If localhost:8889 is replaced by just localhost in the code above, an exception is raised with message
SQLSTATE[HY000] [2002] No such file or directory.
If localhost:8889 is replaced by 127.0.0.1 in the code above, an exception is raised with message
SQLSTATE[HY000] [2002] Connection refused.
Any help appreciated.
Update: the connection works if I use mysqli instead of PDO :
$mysqli=mysqli_connect('localhost', 'root', 'root', 'lacolshow_database', NULL, '/Applications/MAMP/tmp/mysql/mysql.sock');
Following the advice given in the second comment at http://php.net/manual/en/pdo.construct.php, I tried the following :
$dsn = 'mysql:dbname=lacolshow_database;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock';
$db = new PDO($dsn,'root', 'root');
but that fails too, producing an empty PDO object.
The port is going into distinct parameter, not into host. So it should be
$db = new PDO('mysql:host=localhost;port=8889;dbname=lacolshow_database;charset=utf8', 'root', 'root');
Note that you don't have to catch and var_dump an exception, PHP will show you it by itself
1) You need to add the port number in the connection string to connect with your DB. Your DB server is running at port no.: 8889 in your local environment, so localhost:8889 used in DB connection string will not raise any exception.
2) If you replace localhost:8889 by localhost then the connection will not establish and an error with message SQLSTATE[HY000] [2002] No such file or directory will occur that indicates a MySQL connection via socket is tried (which is not supported).
3) When you use 127.0.0.1 a connection attempt will be made but will be refused, SQLSTATE[HY000] [2002] Connection refused. It means the SQL database was either offline or being accessed incorrectly. Perhaps an incorrect port or socket path.
Hence, the first point is correct way to establish a connection with DB server.

PDO can't connect to database

Our school let us sue a small database that I am trying to access through the school network.
Connecting using
mysql -D ddddd -h hhhh -u uuuu
works fine (Note that contrary to multiple questions on this site, hhhh is not localhost). However, using
new PDO('mysql:host=hhhh;dbname=dddd, uuuu, pppp)
does not, and brings up an error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
How do I locate this socket? (NB: In case it is relevant, the schools computers run on FreeBSD and I only have a very limited permissions).
the error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
means that the PDO is trying to connect to MySQL via local socket (linux socket is file).
But my guess is that you want to connect via hostname/ip. Your code Has to be wrong. From the example it looks like you are using php. Try double check everything.
Here is an example from php.net:
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Use correct IP address to connect.
If u r using localhost then by default socket will be used on Linux so
Use 127.0.0.1 as host instead of localhost.

Connecting to MSSQL server via php-pdo?

I'm unable to connect to MSSQL database on smarterasp.net domain. I'm using:
PHP Tools for Visual Studio
Here is the code:
<?php
try {
$conn = new PDO("mssql:host=host_name_string;dbname=database_name_string", "username_string", "password_string");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
and the error caught by the PDOexception class:
Connection failed: could not find driver
I've also tested my code online and again getting the same error(online tester url: http://sandbox.onlinephpfunctions.com).
I've searched for the solution and the only thing that could resolve my problem is by uncommenting:
extension=php_pdo_mysql.dll
but the line is already uncommented by default.
EDIT:
The following does not ressolve my problem (instead of mssql:host)
sqlsrv:host
dblib:host
Your problem is that you have not installed either the sql server client or the Microsoft pdo drivers on your machine. Please do that and make sure you can connect via a udp file.
Search SQL server client install and Microsoft pdo drivers

Why php PDO uses a different from hostname to the one used by mysql_connect() when connecting to a remotely hosted mysql database?

I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.

Categories