PHP mySQLi:: MySQL server has gone away - php

I'm trying to append a hush database directly from php.
$mysqli->query(file_get_contents('huge.sql'));
but I'm getting :
Warning: mysqli::query(): MySQL server has gone away in
I was add those 2 line at the beginning of my php code :
ini_set('mysql.connect_timeout',3000);
ini_set('default_socket_timeout',3000);
Not very helpful...

Unless you're getting an actual error and not a Warning, this doesn't necessarily mean you're doing something wrong.
With PDO for example, this warning can be issued when pooled connections are in use, such as when PHP is running as an Apache Module. There is a similar post here: How to fix the "server has gone away" warning when mysqli reconnects a persistent connection?
If what you're getting is simply a warning, and the query runs fine anyway, then you can either choose to request a persistent mysql connection, or ignore and suppress the warning.
If you're getting an error and your connection is getting terminated, then you should see https://dev.mysql.com/doc/refman/8.0/en/gone-away.html for most common causes of this issue.

Related

ssh2_connect(): Error starting up SSH connection(-43): Failed getting banner

Have a docker container build using php-ssh2. php version 7.2 When trying to use
$con = ssh2_connect('hostname');
I am getting Error starting up SSH connection(-43): Failed getting banner . Interesting thing is 43 here. Whats the significance of 43. What does that mean? Also any idea how to fix this? There is no heavy load, running connection manually.
Deepdive into libssh2
This number -43 is an error code that comes directly from libssh2, specifically LIBSSH2_ERROR_SOCKET_RECV. The Failed getting banner message is the dynamic error message that accompanies the error code. These two pieces of information give the location where this error is thrown, namely in the receive_banner.
Underlying problem
It was the result of the socket throwing a receive error when libssh2 tried to read from it as part of initialising your SSH session. Either the server is misconfigured and is not sending a banner or the underlying connection disconnected for some reason.
Solution
The best course of action seems to have adequate retrying in place for these kinds of errors. You are connecting to a network which is an action that can fail. As the number of servers you are connecting to increases, you are going to run into errors that are a result of the underlying network. Adequate error handling is your best course of action.
You can find how to set exception handlers from the PHP docs.

"Attempt to read a row while there is no result set associated with the statement" on Linux server but no error on Windows computer

I am using mysqli to connect to a MySQL database with PHP.
As I am programming on a windows computer with error_reporting(E_ALL) and mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
While coding I get neither errors nor warnings. But on the server I get
Fatal error: Uncaught exception 'mysqli_sql_exception' with message
'Attempt to read a row while there is no result set associated with the statement'
I think this is a little bit strange.
The error is thrown, if you call mysqli_stmt::fetch() on a statment that provides no results, e.g. after executing an insert, update or delete query, or if you already called mysqli_stmt::free_result().
There was a part of my code that was messed up and I fixed it already.
But my question is, why is this error not thrown on my development platform?
Is there an option in the php.ini or somewhere I have to set?
I now tested some settings and the result is:
If you use "MySQL Client Library" instead of "MySQL Native Driver" you get the error described in the question. I don't know, why the server were not using mysqlnd, as it is default since PHP 5.4.0 and there seams to be no reason to not use it but some advantages, so I changed it.
But however, one should not try to fetch results from an insert, update or delete query or after freeing the result. This is just senseless.

kohana db exception [0]

I have a big problem with connecting to mysql database. I tried everything and nothing worked.
If i try to connect on localhost with xampp to db on kohana (3.1.2), i get this error:
Database_Exception [ 0 ]: ~ MODPATH\database\classes\kohana\database\mysql.php [ 67 ]
but if i try run it on some server, it runs there and i dont know why, because everything is same (except db user and password).
I thought that it can be with mysql config, but i dont know what there.
I just came across this problem and figured out what it was so even though this question is old, people having this problem should know the answer.
Basically when you get this error its because your using a version of php 5.5+.
mysql_connect(...) is a depreciated function starting in php 5.5 and above so the call in the framework is wrapped around a try/catch but the output from the catch doesn't give you any details about the error for whatever reason but if you remove the try/catch you'll see the real php error is simple saying mysql_connect is depreciated and will be removed in the future and to use mysqli instead.
To fix this open index.php in your root and basically you need to change the error_reporting(...) call to tell it not to error on depreciation errors which you can do like so to report all errors except warnings and deprecations.
error_reporting(E_ALL ^ (E_WARNING | E_DEPRECATED));
Another fix is to change your database config file to connect via PDO instead of mysql.
I checked in Kohana 3.2, and there it means it can't connect to your database and throws an exception on that. But just go to the line 67 of the mysql.php file and check if the exception is triggered just after trying to connect to the DB.
Check your credentials in database config and make sure your MySQL server is up and running.

PHP and SQL - Database overload

I'm building a web app that uses lots of requests to my database. Every thing was working perfectly smooth until a half an hour ago when the requests weren't returning... I checked the PHP file directly and it displays the following:
<br />
<b>Warning</b>: mysql_connect() [<a href='function.mysql-connect'>function.mysql-connect</a>]: Too many connections in <b>/home/sanity/public_html/dev/forest/js/database.php</b> on line <b>7</b><br />
Unable to connect to MySQL
So I figured let's check phpMyAdmin, but it's not showing me ANYTHING except for a big red box that says:
SQL query: Edit Edit
SET CHARACTER SET 'utf8';
MySQL said: Documentation
#1045 - Access denied for user 'root'#'localhost' (using password: NO)
Between the last time it worked and now I haven't changed any configurations or code.. How do I begin to fix this?
Could this be caused by the fact my PHP files don't close the connection after using it? If so should I be closing the connection after every query? I figured the connection would close automatically when the user leaves the web site.
EDIT: The requests are sending through now and phpMyAdmin is back up, but how do I prepare this site for heavier traffic?
When I started my job, one of my first tasks was to continue working on what one of the directors had started coding. In his code, I saw this monstrosity:
function getTicket($id) {
mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql = mysql_query("select * from tickets where id = ".intval($id));
return mysql_fetch_assoc($sql);
}
In other words, he was creating a whole new database connection every single time he wanted something from the database, and never closing any of them (instead letting them be closed at the end of the script automatically)
This was fine for basic testing, but as soon as I started writing more advanced stuff (before I'd discovered this piece of code) things majorly screwed up with the same "too many connections" error as you.
The solution was simple: restart the server to clear all pending connections, and fix the code to only connnect once per script execution.
This is what you should do. There should only ever be one call to mysql_connect (or indeed any database library's connect function) in your script (and I don't mean in a function that gets called several times!)
You should also check the database's configuration, just in case someone accidentally set the maximum connections too low, but generally this shouldn't be a problem if you manage your connections properly.
Though the mysql_* functions are deprecated (use a modern driver like PDO for instance), you should take a look at the mysql_close($con) function.
Here is the doc.
EDIT
If you are not using mysql_pconnect function, then your connection should be closed at the end of the execution of your script.
Apparently, one reason for such error is shared hostings. If you are using a shared hosting, generally speaking, the maximum connections to the server allowed by the hosting is not the greatest.
If you can change the max_connections system variable then try to change it to a greater number:
max_connections = 200

MySQL server has gone away - ErrorException

I started getting this error when trying to query the database on a connection that had timed out.
Fatal error: Uncaught exception 'ErrorException' with message 'mysql_query(): MySQL server has gone away'
So I did a bunch of research and 99% of the users on the forum say you can use the mysql_ping command to check for a connection, so I put this in place:
if(!mysql_ping($this->sDBLink))
Connect(true);
Now I get the same error, just in reference to the mysql_ping function instead of the mysql_query function:
Fatal error: Uncaught exception 'ErrorException' with message 'mysql_ping(): MySQL server has gone away'
How do I reliably check that a connection still exists? mysql_ping throws an exception.
Why does a connection time out? Because it has been unused for a period of time longer than a particular threshold. This kind of thing is necessary for all kinds of network applications to make them resilient. What if a user abruptly switches off a client machine while it is connected to a MySQL server? The server needs to be able to drop the client connection after a while.
This kind of thing is inherent in network programming. It's a feature, not a bug.
What can you do about this?
You can switch to a more modern mysql connection management library that handles this stuff.
Especially if your client software gets used infrequently, you can reorganize your software to connect to MySQL, use the connection, and disconnect. That way you won't need a persistent connection. But that's impractical if your client server gets used a lot; there's a lot of overhead to establishing a connection.
You can change the timeout value. See here. how to change timeout for mysql persistent connections
You can use the connection regularly. mysql_ping uses the connection without actually doing any server work. So would a query that said something like SELECT 1. If you ping the connection every minute or so, then a two minute timeout won't cause your MySQL server to conclude that your client has gone away and disconnect it.
You can handle the ErrorException you're getting correctly, by trying to re-establish the connection instead of just blowing out your program with an error message. Use PHP code something like this:
try {
some operation on the mysql connection.
}
catch (ErrorException $ex) {
disconnect mysql
connect mysql
}
So I know this is ugly and makes me sick it, but it will work until we can get enough income to warrant an upgrade.
All I did was close the connection then recreate it as I know the connection will have timed out almost 95% of the time.
if($this->sDBLink){
mysql_close($this->sDBLink);
}
if($bPersistant){
$this->sDBLink = mysql_pconnect($this->sHostname, $this->sUsername, $this->sPassword);
} else {
$this->sDBLink = mysql_connect($this->sHostname, $this->sUsername, $this->sPassword);
}

Categories