PHP Codeigniter 3 server went down

I am running a Cron Job for a PHP script in Codeigniter to convert uploaded videos. Everything runs without any problem, but as soon as it gets up to insert data into the database, I get this error:

Error Number: 2006 MySQL server has gone away

The process is basically converting the video, if it was successful, create a thumbnail and poster with the video, and then insert the video data into the database. I tried to add $this->db->reconnect();

to my model function. Here is the function:

function add_video($data)
{
  //BECASUE THE CONVERSION TAKES SO LONG WE NEED TO RECONNECT TO THE DATABASE AFTER EACH EXEC
  $this->db->reconnect(); 

  $this->db->insert('video_uploads', $data);
}

      

It didn't work, I got the error anyway. If I add $this->db->reconnect();

after each exec

, I get the following errors:

Message: mysql_ping() expects parameter 1 to be resource, boolean given

Message: mysql_real_escape_string() expects parameter 2 to be resource, boolean given

As FYI, I am using FFMPEG to convert my videos.

How can I keep the connection online after all executions have finished so that I can insert data into the database?

EDIT: This is what the Codeigniter functions look like $this->db->reconnect();

:

public function reconnect()
{
    if (mysql_ping($this->conn_id) === FALSE)
    {
        $this->conn_id = FALSE;
    }
}

      

+3


source to share


4 answers


To avoid this error, you can write

$this->db->database();



before calling

$this->db->reconnect();

+1


source


It may be easier to test the connection and restore it if necessary.

See PHP: mysqli_ping for info on this



For reference

0


source


I had a similar problem for a different reason - I was heavily clogging the database with> 12,000 select / updates. the mysql server just gave up and "went on vacation"

Anyway, a little free time is all it takes, in the loop I added:

if ($this->db->conn_id->ping() === FALSE)
{
    sleep(1);
    $this->db->reconnect();
}

      

0


source


For me it was a setting in the database file where I was migrating servers and not fully configuring mysql. In mysql, I am using ssl to connect to the server.

Once I commented on the following, everything went well.

$db['default']['options'] =  array(
    PDO::MYSQL_ATTR_SSL_KEY  => $_SERVER['DOCUMENT_ROOT'].'/application/third_party/client-key.p$
    PDO::MYSQL_ATTR_SSL_CERT => $_SERVER['DOCUMENT_ROOT'].'/application/third_party/client-cert.$
    PDO::MYSQL_ATTR_SSL_CA   => $_SERVER['DOCUMENT_ROOT'].'/application/third_party/ca-cert.pem'
  );

      

I started by disabling libraries and other autoloaded files until I realized it wasn't the model or controller that was causing the problem. once i disabled the database library i didn't get 500 server error or mysql error. After checking the connection settings, I figured out what was causing the problem.

0


source







All Articles