I want to connect to Oracle database, which requires port forwarding, using PHP.
Here is the setup.
Currently i use PuTTy to connect to Oracle Db. There i feed
Host ip: 172.XX.XX.111 port:22
Tunnel setting
Source port : L19005
Destination : 172.XX.XX.40:1521
After entering userid/password,i am able to connect sqldeveloper which needs TNS config.
Now i want to perform same activity using PHP. Here's what i have done so far
set_include_path(get_include_path().'\phpseclib');
include('Net/SSH2.php');
$ssh = new Net_SSH2('172.XX.XX.111');
if (!$ssh->login('userid', 'password')) {
exit('Login Failed');
}
$ssh->write("ssh -L19005:172.XX.XX.40:1521 172.XX.XX.111\n");
$ssh->write("password\n");
$connect='(DESCRIPTION=(ADDRESS= (PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=19005))(CONNECT_DATA = (SID = connect)))';
$conn = oci_connect('Db_username', 'Db_password', $connect);
if ($conn)
{echo 'True';}
I also used ssh2_connect function.
$connection = ssh2_connect('172.XX.XX.111', 22);
if (ssh2_auth_password($connection, 'userid', 'password'))
{
echo 'true';
}
ssh2_exec($connection,"ssh -L19005:172.XX.XX.40:1521 172.XX.XX.111","\n");
ssh2_exec($connection,"password","\n");
$connect='(DESCRIPTION=(ADDRESS= (PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=19005))(CONNECT_DATA = (SID = connect)))';
$conn = oci_connect('Db_username', 'Db_password', $connect);
if ($conn)
{echo 'True';}
In both the cases, i am getting the following error.
oci_connect(): ORA-12541: TNS:no listener in C:\xampp\htdocs\USAGE\index.php
I am using XAMPP3.2.2 with PHP5.5.
Please help. I have searched a lot but could not find anything that relates to my issue.
phpseclib doesn't do tunneling. What it looks like you're expecting it to do is for PHP to create a server on port 127.0.0.1:19005 but at that point you'd need an SSH server - not an SSH client.
My recommendation: use autossh.
Related
I am trying to connect to (remote) AWS MYSQL database using php and ssl.
I created a database user and could connect with it from php and also on console.
Then I modified the user to use SSL with "require ssl" in the database.
I can stil connect on the console by:
mysql -h host -u username -p --ssl
But in php I get an ERROR 2002.
$con=mysqli_init();
mysqli_ssl_set($con,NULL,NULL,"cacert.pem",NULL,NULL);
$link = mysqli_real_connect($con, "host", "username", "password");
As far as I know the ssl_set parameters doesn't matter with AWS databases.
How to connect to Amazon RDS via SSL?
I think it has to do with the mysqli_ssl_set method, without it a connection is made and access is denied by database and this is the correct behaviour. It might be some configuration error on client side, but I have no idea what I shoud check.
The error is fired by mysqli_real_connect, as mysqli_ssl_set always returns true, just after it tries to connect will it receive an error.
I noticed it did not work for me until I specified the port. The following worked:
$db = mysqli_init();
$db->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set(NULL, NULL, 'rds-combined-ca-bundle.pem', NULL, NULL);
$db->connect($host, $user, $pass, $db, 3306);
$servername = "only-ssl-db.ct5b4uz1gops.eu-central-1.rds.amazonaws.com";
$username = "username";
$password = "password";
$dbname = "dbname";
$con = mysqli_init();
if (!$con){
die("mysqli_init failed");
}
mysqli_ssl_set($con,NULL,NULL,'rds-combined-ca-bundle.pem',NULL,'DHE-RSA-AES256-SHA');
if (!mysqli_real_connect($con,$servername, $username, $password, $dbname))
{
die("Connect Error: " . mysqli_connect_error());
}
// Some queries...
echo "Database connected";
printf("Client version: %d\n", mysqli_get_client_version());
mysqli_close($con);
I currently have a virtual machine setup to be connected to via ssh and contains a mariadb database. I am trying to run a php script to connect to it and query the database, but I cannot get past the connection without it saying
Warning: mysqli::mysqli(): (HY000/2002): Connection refused
My php code that deals with this connection looks like the following:
set_include_path(':/Users/self/pear/share/pear' . PATH_SEPARATOR . 'phpseclib/');
include('Net/SSH2.php');
$ssh = new Net_SSH2('IP_Address');
if (!$ssh->login('user', 'password')) {
exit('Login Failed');
}
$mysqli = new mysqli("IP_Address", "root", "password", "test_db");
$connect_errno = array();
$connect_error = array();
if ($mysqli->connect_errno) {
$connect_errno[] = ($mysqli->errno);
$connect_error[] = ($mysqli->error);
}
I have checked multiple times and the password/username/ip address above are all correct so it can't be that (Also I believe it would say 'Access Denied' rather than 'Connection Refused')
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I do not believe the question to be a duplicate as I believe the issue is related to Shared Hosting environments getting outbound to other cloud mysql servers.
Error message
PDO Exception: SQLSTATE[HY000] [2002] Connection timed out.
I am simply just trying to connect to my instance of AWS RDS and run a query on one of my tables.
SOLUTION: GODADDY BLOCKS ALL PORTS EXCEPT 21 AND 22 (FTP) ON A STANDARD ACCOUNT. THIS IS WHAT I WAS TOLD BY GODADDY TECHNICAL SUPPORT. HENCE, IT WILL BLOCK PORT 1234 AND EVEN 3306 FOR A DATABASE CONNECTION.
UPDATE:
#Drew has validated that this php code works for the connection:
try {
$db = new PDO('mysql:host=uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com;port=1234;dbname=uwm_parking;charset=utf8', 'fbgrecojr', 'test1234');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
echo "Connected and Happy<br>";
$sql="select id,thing from thingsCoveted where id in (1,3)";
foreach ($db->query($sql) as $row) {
print $row['id'] . "\t";
print $row['thing'] . "<br>";
}
} catch (PDOException $e) {
echo 'PDO Exception: ' . $e->getMessage();
exit();
}
I am getting this error however on my end:
PDO Exception: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com' (110)
Hence, there is either a config issue on the Godaddy side (where the .php is hosted) or the AWS RDS side (where the database is hosted);
Things I have tried:
1) checked to make sure all PDO options are selected in GoDaddy's php settings
2) added an inbound rule for my AWS RDS security group to allow all connections using my port (1234).
The problem is you've just copied and pasted your connection info into the example given by Amazon inside the $_SERVER array.
// Example
$link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);
// What yours should be
mysqli_connect(
'***********.******.us-east-1.rds.amazonaws.com',
'*******',
'********',
'dbname',
'1234'
);
You also need to use mysqli functions since that's how you're connecting, e.g. http://php.net/manual/en/mysqli.query.php
EDIT:
Now you're using PDO you will need to take a different approach to getting your data.
$dbhost = '***********.******.us-east-1.rds.amazonaws.com';
$dbport = 3306;
$dbname = '*****';
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
$username = '*****';
$password = '*****';
try {
$dbh = new PDO($dsn, $username, $password);
$query = $dbh->query("SELECT num_spaces FROM uwm_parking.lots");
while($row = $query->fetch(PDO::FETCH_OBJ)) {
var_dump( $row );
}
} catch(PDOException $e ){
echo "Error: ".$e;
}
I am attempting to connect to a mysql database using the c9.io development environment. I have followed their documentation and have seen multiple links, 1, 2 and 3.
I verified the mysql service is running. I also verified the PDO extension was installed via phpinfo(). Here is my current code:
$ip = getenv("REMOTE_ADDR");
$port = '3306';
$user = "username";
$db = "c9";
try{
$con = new PDO("mysql:host=$ip;port=$port;dbname=$db;charset=utf8",$user,"");
}
catch(Exception $e){
echo $e->getMessage();
}
I get the error Can't connect to MySQL server on '10.240.x.x' (111)
If i try localhost as host, I get the error Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I also followed a comment from the second link above: echo $IP in the terminal which returns 0.0.0.0
Any assistance appreciated.
You were on the right track. On https://docs.c9.io/setting_up_mysql.html it says use $IP for host. You can use getenv("IP") instead or use its value: 0.0.0.0. That should work.
Please try something like:
$dbname = 'c9';
$ip = getenv('IP');
$user = getenv('C9_USER');
mysql_connect($ip, $user, '') or die('Could not connect to mysql');
I'm having trouble connecting to a MySQL database on my host OS (Win 7) from my Oracle VirtualBox running Ubuntu. I wrote this php code in order to connect but I keep receiving an error connecting:
<?php
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'sdb_user';
$db_password = 'password';
try {
$db = new PDO($dsn, $db_username, $db_password);
}catch (PDOException $e) {
$error_message = $e->getMessage();
include('../errors/database_error.php');
exit();
}
$query = 'SELECT * FROM books';
$books = $db->query($query);
foreach ($books as $book) {
echo $book['title'];
}
?>
I know the code works, because I can get it to run from my Windows 7 host OS using localhost instead of the IP address, and I know that both Linux on VirtualBox and Windows 7 are on the same subnet because I can successfully ping the other from each side.
I'm fairly certain that I opened port 3306 correctly on the windows side also, but I have no idea how to check if that's the problem or not.
What else should I be doing to get the code to connect to the MySQL db?
EDIT: The problem was that the port was not open for remote connections. After I opened the port I got the error message stating I was not allowed to connect to the sql server. From there I created a new user on the Windows MySQL with the username "home" and password "home" and was able to connect to it using:
$ mysql -h 192.168.1.9 -u home -p
However, when I run my file, which now reads:
$dsn = 'mysql:host=192.168.1.9;dbname=finance';
$db_username = 'home';
$db_password = 'home';
I still get the 'database error'
Any suggestions?