when my database connection wrong or my connection lost then get error like this
QueryException in Connection.php line 647: SQLSTATE[HY000] [2002] No
connection could be made because the target machine actively refused
it.
How to handle this error in laravel 5.4
Thank You
According to your question, I think you need Try-Catch Block, and your Problem will be Solved, or you Need to be Clear about what Problem you are facing. Here is what you can try to solve your Problem.
try
{
// Code that is Throwing Exception
}
catch(\QueryException $e)
{
return redirect("some_page")->with("error","user friendly message");
}
Let me know if it helps or Let me know if you are looking for something else!
Related
I'm using Laravel-7 and MySQL SERVER for my project. How can I get the status of DB connection?
I have already tried few methods, but it did not work. I want to get the status and display the custom success or error status.
My code is below:
try{
DB::connection()->getDatabaseName();
return redirect('/registration')->with('db_con_status','Database connected!');
}catch(Illuminate\Database\QueryException $dbexp){
return redirect('/registration')->with('db_con_error',$dbexp->getMessage());
}
but it still shown Laravel in-built ignition error like...
Illuminate\Database\QueryException
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. (SQL: select count(*) as aggregate from users where email = abc#xyz.com)
How to catch that exception and handle it?
I think your xamp is running in background. Open task manager and stop it from there (if you use windows).
I'm currently developing an application using NodeJS.
However, often the server throws this error, and I can't interact with mysql.
[Error: read ETIMEDOUT]
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'read',
fatal: true }
{ [Error: Cannot enqueue Query after fatal error.] code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR', fatal: false }
Does someone have a solution to fix that?
Thanks
From your question, I assume that you can work with your database perfectly but this error happens often after a given time of that connection being up...
Supposing you are using node-mysql
Source : https://github.com/felixge/node-mysql#error-handling
Your error terminates your connection to the database :
err.fatal: Boolean, indicating if this error is terminal to the connection object.
If the error is not from a MySQL protocol operation, this property will not be defined.
Note: 'error' events are special in node. If they occur without an
attached listener, a stack trace is printed and your process is
killed.
tl;dr: This module does not want you to deal with silent failures. You
should always provide callbacks to your method calls. If you want to
ignore this advice and suppress unhandled errors, you can do this:
// I am Chuck Norris:
connection.on('error', function() {});
From this you could check connection status and perform a reconnect if needed.
You could also try to connect manually to your mysql service and change request timeout :
wait_timeout” : the amount of seconds during inactivity that MySQL will wait before it will close a connection on a non-interactive connection in seconds.
http://www.serveridol.com/2012/04/13/mysql-interactive_timeout-vs-wait_timeout/
https://support.rackspace.com/how-to/how-to-change-the-mysql-timeout-on-a-server/
Try to use mysql2 over mysql npm pakcage. it will solve your problem.
full explanation here
i have a problem with my website,
i get a Database connection error (2): Could not connect to MySQL. error.
the tech support told that: "A script is not closing the MySQL connection after accessing the database. Please make sure that you close the connection immediately after accessing the database using mysql_close() command from your scripts."
And I didn't make any change recently.
can anyone advise?
This may be due to some uncaught exception in accessing DB, it won't have any problem til there is nothing unexpected as result/entries. but when some unexpected result/ entry comes. then the query may break flow of execution , and may not go through the db close() statement. Make sure nothing unexpected in your DB/ no unusual content.
I am writing an application using NodeJS, Express, mysql, so far everything works fine, but when I run my application after sometime when mysql connection is interrupted my application throughs this exception and my application goes down.
Error: read ECONNRESET
at errnoException (net.js:901:11)
at TCP.onread (net.js:556:19)
From another stackquestion i came to know that i have to handle such uncaught exceptions like this.
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err);
console.log(err.stack);
});
after this now my application does not exit, but instead it hangs up, so my question is how do I handle this exception so that mysql connection is ok even after this exception and my application does not hang up.
I'm not sure if you're using the node-mysql module for your project, but I was, and I encountered the same ECONNRESET issue. Here's a repeat of my answer on my issue:
I reached out to the node-mysql folks on their Github page and got some firm answers.
MySQL does indeed prune idle connections. There's a MySQL variable "wait_timeout" that sets the number of second before timeout and the default is 8 hours. We can set the default to be much larger than that. Use show variables like 'wait_timeout'; to view your timeout setting and set wait_timeout=28800; to change it.
According to this issue, node-mysql doesn't prune pool connections after these sorts of disconnections. The module developers recommended using a heartbeat to keep the connection alive such as calling SELECT 1; on an interval. They also recommended using the node-pool module and its idleTimeoutMillis option to automatically prune idle connections.
I found the solution and I am posting if someone else is facing the same problem this might help you guys as well.
First I caught all uncaught exceptions, which made my application not to exit abnormally.
Second the problem of hanging I had was because when server would close the connection, all my requested queries would fail and my server would simply hang up I guess its node-mysql bug, but I solved it using connecting pooling, in pooling if server close the connection its re-aquired after a second or so again so my problem was solved this way.
Here is how to make most out of node-mysql pooling
I have the following error message:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on
'192.168.50.45' (4)
How would I parse this (I have HY000, I have 2003 and I have the (4).
HY000 is a very general ODBC-level error code, and 2003 is the MySQL-specific error code that means that the initial server connection failed. 4 is the error code from the failed OS-level call that the MySQL driver tried to make. (For example, on Linux you will see "(111)" when the connection was refused, because the connect() call failed with the ECONNREFUSED error code, which has a value of 111.)
Using the perror tool that comes with MySQL:
shell> perror 4
OS error code 4: Interrupted system call
It might a bug where incorrect error is reported, in this case, it might a simple connection timeout (errno 111)
FWIW, having spent around 2-3 months looking into this in a variety of ways, we have come to the conclusion that (at least for us), the (4) error happen when the network is too full of data for the connection to complete in a sane amount of time. from our investigations, the (4) occurs midway through the handshaking process.
You can see this in a unix environment by using 'netem' to fake network congestion.
The quick solution is to up the connection timeout parameter. This will hide any (4) error, but may not be the solution to the issue.
The real solution is to see what is happeneing at the DB end at the time. If you are processing a lot of data when this happens, it may be a good ideas to see if you can split this into smaller chunks, or even pas the processing to a different server, if you have that luxury.
I happened to face this problem. Increase the connect_timeout worked out finally.
I was just struggling with the same issue.
Disable the DNS hostname lookups solved the issue for me.
[mysqld]
...
...
skip-name-resolve
Don't forget to restart MySQL to take effect.
#cdhowie While you may be right in other circumstances, with that particular error the (4) is a mysql client library error, caused by a failed handshake. Its actually visible in the source code. The normal reason is too much data causing an internal timeout. Making 'room' for the connection normally sorts it without masking the issue, like upping the timeout or increasing bandwidth.