PHP Connection Erreur : SQLSTATE[HY000] [2002] A connection attempt failed - php

It's the first time I ask a question on a forum.
I'm making a quizz with a basic html form and need to save the answers on my server (using lcn.com company for hosting it).
Server details:
Host: database3.lcn.com
User: xxx
pwd: xxx
Alright, I'm simply trying to connect to my MySQL database using PHP language.
I've pasted here my php file:
<?php
$dsn='mysql:host=database3.lcn.com;dbname=test_table;charset=utf8';
$user="***";
$pwd="***";
try {
$linkpdo = new PDO ($dsn,$user,$pwd);
echo "Success!";
$linkpdo=null;
} catch(Exception $e) {
die('Erreur : '.$e->getMessage());
}
?>
But I have this problem :
Erreur : SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
And don't know why it doesn't work...
Could someone please give me a hand?
Thanks

mysql --version
sudo service mysql restart
sudo mysql

Related

Mysql Remote Connection Failed

Im trying with php to connect my remote server's mysql im getting this error:
"Database Connection Error: SQLSTATE[HY000] [2002] connection refused"
I made what said in that page : https://www.configserverfirewall.com/ubuntu-linux/enable-mysql-remote-access-ubuntu/
this is my setting now:
and ofcourse restarted mysql.
ANd its still saying same error "Database Connection Error: SQLSTATE[HY000] [2002] connection refused"
What can i do else?
this is my connection setting:
When i try port 3306 same error. What can cause that?
ps: also this is a remote connection from a hosting shared to my dedicated server
it was about cloudflare or other proxies. Fixed.

Connecting to mysql within Glassfish

My goal is to run php on glassfish server. I installed Quercus and deployed it as an application within glassfish under (C:\glassfish4\glassfish\domains\domain1\applications\Quercus).
Now I try to connect to mysql. The mysql server is running and connection is verified through command (mysql -u root -p).
next test.php is created:
<?php
$servername="localhost";
$username="root";
$password="whatever";
$conn = new mysqli($servername, $username, $password);
if (!$conn) {
die("Failed : " . mysqli.connect_error());
}
echo "Connection Successful!";
?>
Running test.php gives the following message:
Warning: A link to the server could not be established.
url=jdbc:mysql://localhost/?
jdbcCompliantTruncation=false&characterSetResults=ISO885_1&characterEncoding=ISO8859_1&userServerPrepStmts=true
driver=com.mysql.jdbc.Driver com.caucho.quercus.QuercusModuleException:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Connection Successful!
From the message, it is probable that the mysql jdbc driver is not installed.
I added the driver to C:\glassfish4\glassfish\domains\domain1\lib\mysql-connector-java-8.0.11\mysql-connectior-java-8.0.11.jar. I am not sure how to add it from glassfish admin console. It is not shown under jdbc>connection pool.
2nd EDIT (based on comments):
The jar files are moved into ...\domain1\lib and ...\glassfish\lib as well. A full system restart was done.
To add a connection pool, I Followed the instructions on the following link:
https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-usagenotes-glassfish-config.html
Then I get the following error message:
HTTP Status 500 - Internal Server Error:
type Exception report
message Internal Server Error
description The server encountered an internal error that prevented it from fulfilling this request.
exception java.lang.IllegalStateException: getoutputStream() has already been called for this response.
...

Mongodb:Failed to connect

I am new to MongoDB.I installed mongodb and used with Laravel framework.Its had been worked for a long time without any issues.But currently while i try to acess my website it shows:
Failed to connect to: localhost:27017: Connection refused
when i try to acces mongodb via command line,it shows:
warning: Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146 exception: connect failed
when i try to access it via rock tool,it shows:
Unable to connect MongoDB, please check your configurations. MongoDB said:Failed to connect to: 127.0.0.1:27017: Connection refused.
what i will do? I can't figure what's wrong from my part since its working after the past months.I do nothing since the last days.Thanks in advance..
After a short break, I can find out the solution.
The origin of that issue is the unexpected shutdown of the system.So i run the below code in command prompt:
sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
then after the above code run, its working fine :)

MAMP on Yosemite - connection error : SQLSTATE[HY000] [2002] No such file or directory

I know that I have seen this question before but have not seen an answer that has been able to fix the issue on my machine.
I'm relatively new to PHP and have no experience with Apache settings.
I am using the latest version of MAMP (not pro) with all the default settings on the latest version of OS X (10.3.3).
When I try to connect via PDO I receive the following error:
Unable to connect to the database server.exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/resources/config.php:7 Stack trace: #0 /Applications/MAMP/htdocs/resources/config.php(7): PDO->__construct('mysql:host=loca...', 'USERNAME', 'PASSWORD') #1 /Applications/MAMP/htdocs/WEBSITE/index.php(2): include_once('/Applications/M...') #2 {main}
The connection code is from Kevin Yank's Novice to Ninja book, modified to apply to my database, username, etc.
try
{
$pdo = new PDO('mysql:host=localhost;dbname=db', $DBUser,$DBPass);
}
catch (PDOException $e)
{
$error = 'Unable to connect to the database server.' . $e;
include 'error.html.php';
exit();
}
I have tried localhost and 127.0.0.1 and get the same error regardless.
I don't see anything in Console that could help but I may not be looking for the right thing.
Any help is appreciated. Thanks
try this command in mac
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
This could happen if mysqld is not running (stuff happens). It could also happen if you have a configuration error.
For example, on my installation, i reproduce the error with this DSN :
mysql:unix_socket=/tmp/mysqle.sock;dbname=notifications;charset=utf8
the config calls for /tmp/mysql.sock
Check the config to see what method is configured (port or socket) and which (port or socket). If you have the wrong port or socket in your DSN, you will produce the same error report in the question.

I want to access another server database in PHP

I am really a beginner in PHP and Mysql. I made a database on static IP 192.168.1.211 which is based on CentOS(only Command prompt) and on this IP there is no other software like easyPHP, and I am working on static IP 192.168.1.20 based on Windows 7. I also installed easyPHP and Dreamweaver... using Dreamweaver I made one .php file and I tried to use the database which is on 192.168.1.20..
using below code
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect(server2,'root','password','vvani');
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
but I get an error as shown below
Notice: Use of undefined constant server2 - assumed 'server2' in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
Failed to connect to MySQL: php_network_getaddresses: getaddrinfo failed: No such host is known.
How can I access the database on 192.168.1.211 from 192.168.1.20 in PHP code?
I also tried hard to find a solution from Google, but I am not getting a perfect solution.
Just read errors and correct it..
<?php
$server2 = '192.168.1.211';
$con = mysqli_connect($server2,'root','password','vvani'); // here, $server2 is variable, not constant
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Change server2 to $server2. If that still doesn't work then it's possible the other server does not allow requests in such a way so you will have to update the MySQL settings on that server to allow external access to the port (or otherwise forward it).
Thanks Elon Than and Explosion Pills,
I changed server2 to $server2, but I get new warning
Warning: mysqli_connect(): (HY000/1130): Host '192.168.1.20' is not allowed to connect to this MySQL server in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\test\welcome.php on line 4
And I research on it and I try to give remotely access to database on another server.
Follow some few steps and solved problem.
# mysql -u root -p
mysql> GRANT ALL ON Database.* TO <un>#'localhost' IDENTIFIED BY 'password';
Thanks again.

Categories