Can't access database server with remote connection - php

I'm a beginner and I'm trying to connect remotely to my MySQL database server, but still don't succeed.
I have a dedicated server and my provider doesn't want to help me.
-CentOS 6 with Parallels Plesk 12 (64-bit)-
I'm accessing my server trough Parallels Plesk. I set up a administrator and database user with "allow remote connection from any host".
I'm able to access my server with FTP and the MySQL database server locally, but not from my computer.
I have this error displaying when trying to connect with php:
Warning: PDO::__construct(): MySQL server has gone away in /Users/X/Sites/connexionBDD.php on line 31
Here is the way I connect:
define("DB_SERVER","mywebsite.com:8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';
For DB_SERVER, if using IP address, do I need to give V4 + port?

The host and port should not be specified in the same variable, split them up and define your DSN like below. Additionally, check the port is the correct one, MySQL by default uses port 3306.
define("DB_SERVER","mywebsite.com");
define("DB_PORT","8443");
define("DB_NAME","databasName");
define("DB_USER","MyUser");
define("DB_PWD","MyPassword");
try {
//line 31
$bdd = new PDO('mysql:host='.DB_SERVER.';port='.DB_PORT.';dbname='.DB_NAME, DB_USER, DB_PWD);
}
catch (Exception $e) {
die($e->getMessage());
}
echo 'You made it';

Related

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.

PDO via SSH2 in PHP?

I'm trying to access a remote MySQL Database which is only reachable locally (localhost).
My Code looks like this:
$connection = ssh2_connect('IP to Server', 22);
if (ssh2_auth_password($connection, 'User', 'Password')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
$tunnel = ssh2_tunnel($connection, '127.0.0.1', 3306);
try {
$this->_connection = new PDO($dsn, $config['login'], $config['password'], $flags);
$this->connected = true;
if (! empty($config['settings'])) {
foreach ($config['settings'] as $key => $value) {
$this->_execute("SET $key=$value");
}
}
} catch (PDOException $e) {
throw new MissingConnectionException(array(
'class' => get_class($this),
'message' => $e->getMessage()
));
}
The script is successful till $tunnel. The PDO connection fails with "Connection refused". I think PDO is trying to connect to MAMP? The $dsn variable looks like "mysql:host=127.0.0.1;port=3306;dbname=mydb".
How can I tell MAMP, that 127.0.0.1 is on the remote server connected through SSH?
Thank you!
As you already noted, your script is attempting to connect to your local MySQL server instead of the remote one. In order for PDO to establish a connection with the remote server, you could establish an SSH tunnel and forward a local port to the server port. As shown here https://brettrawlins.com/blog/mysql-ssh-tunnel.
To run the ssh command in php: shell_exec(ssh code here)
You might want to see: Connect to a MySQL server over SSH in PHP
However as noted in there as well, this method is relatively slow as you will have to create a new tunnel each time you query the database, making your queries take quite a bit longer. I suppose for a development environment it's a valid option since you don't have a static IP, but I don't see this working in production.
If you are trying to do this for the sake of encryption, I would recommend connecting to your database with SSL. However note that SSL for PDO might not be supported by all PHP versions.

Connection to the database fails

I'm using PDO's as PHP extension. My project works fine in local server, but when I upload it on a live server... it throws a connection error..
Here goes my connection.php file
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=cms','root','');
}
catch(PDOException $e) {
exit('Database Error');
}
?>
Check if the MySQL database ist hosted on the same server as the script is. Otherwise you cannot use localhost.
In addition you may add a password for the root user, which you should have configured already.
$pdo = new PDO('mysql:host=localhost;dbname=cms', 'root', 'yourpwhere');

How do I connect to external mysql database on heroku?

I created a FB app hosted on Heroku.
Instead of Heroku's database I use a mysql database. I made the chage using this code:
heroku config:add DATABASE_URL=mysql://user:pass#server:port/database_name
So far everything is ok but now I have an issue connecting to the database in my index.php file.
I don't know ho to do.
I tried to connect to the DB as I do on my website
try {
$bdd = new PDO('mysql:host=host;dbname=database_name', 'user', 'pass');
}
catch (Exception $e) {
die('Erreur : ' . $e->getMessage());
}
But I have an error in my app:
Application Error
An error occurred in the application and your page could not be served. Please try again in a few moments.
Thank you
Check whether your remote database port is opened for external connection. Look for bind-address configuration of your remote mysql server.

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