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')
Related
I want to connect a database on remotemysql.com too my website (ddu.nu/3/weatherstation). When i try to run the connection to the database on the website the connection fails. But when i try the exact same code on the localhsot the connection is created. Here is the code that i am using.
$connect = mysqli_connect("remotemysql.com:3306", "dbuser", "dbpass", "dbname");
if(!$connect ) {
die('Connection failed: ' . mysqli_error());
}
echo 'Connected successfully';
The website doesn't give any error.
Here is the website
And here is the localhost
My quetion is what I need to do in order for the real website to function.
You should try to disable ssl or the ssl have to have ssl cert, mine is work pefectly with ssl disable:
String dbURL = "jdbc:mysql://remotemysql.com:3306/";
Connection con = DriverManager.getConnection(dbURL + dbName + "?useSSL=false",
dbUsername,
dbPassword);
This question already has answers here:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES) (SQL: select * from `table`)
(2 answers)
Closed 2 years ago.
I tried connecting my PHP file to MySQli database but when i ran the code it displays
But when i logon to phpmyadmin it works fine no errors i can login to my account with no problems
Credentials in the database:
Connection Code:
<?php
$con = mysqli_connect('localhost','sotomango', '1234', 'mysql');
if(!$con) {
echo 'noooo';
}
?>
I double checked the database usernames, passwords, and privileges. Everything seems in place and correct, but the php file wont allow me to connect to the database.
#Dharman had a nice post but I couldnt find it.
Try this for debug :
$con = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$con) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
echo "Success: A proper connection to MySQL was made! The my_db database is
great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($con) . PHP_EOL;
mysqli_close($con);
mysqli_connect
if its a new instalation or new upgrade, You need to include your port into connection, mariadb comes as default port so, when you try to connect mysql its redirecting to mariadb.
you need to include your correct port into connection it might be 3306, 3307 or 3308, and use ip instead of localhost.
your final codes should look like this :
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$port = '3308';
$charset = 'utf8mb4';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
$mysqli = mysqli_connect($host, $user, $pass, $port, $db);
mysqli_set_charset($mysqli, $charset);
} catch (\mysqli_sql_exception $e) {
throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}
For The People Wondering
I solved this by just adding a port to the host name like localhost:3308
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.
I have working mssql connecting codes and them working in my another company php host but dont work when i moved new host. All codes same i dont know why connect from new host ? In new host using cpanel and php ( Hostgator ) and im looking php.ini in cpanel mssql settings looking good. my codes below under.
$usernameb ="myuser";
$passwordb = "mypass";
$databaseb = "mydb";
$host ="myhostip"
$connection = mssql_connect($host, $usernameb, $passwordb);
if (!$connection) { die('Not connected : ' . mssql_get_last_message());}
$db_selected = mssql_select_db($databaseb, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mssql_get_last_message());
} else{
}
And gives that error on my new hosting
Warning: mssql_connect(): Unable to connect to server: ip here in
Any idea ?
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;
}