Mysql server has gone away while storing large (2M

2019-05-01 17:54发布

问题:

I want to store a audio file in mysql database. I am using LONGBLOB to store the base64 encoded string of this audio file. but just as i perform my query, i am getting this warning with no inserting on the database:

Warning: mysql_query() [function.mysql-query]: MySQL server has gone away 

When i upload any image file, i am getting no error and the code works fine. this happens when i upload video and audio file. below in the code i am using:

<?php
    include 'database_handler.php';
    if(isset($_FILES['fileToUpload']['name'])){
    echo "uploading. . .";
    $file = rand(0, 10000000).$_FILES['fileToUpload']['name'];
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $file)) {
        if($fp = fopen($file,"rb", 0))
        {
           $picture = fread($fp,filesize($file));
           fclose($fp);
           $base64 = base64_encode($picture);
           //echo $base64;
           $db = new Database();
           $db->test($base64);
        }
    }
}
?>
<form action="test.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

Here is the function code that i am calling:

function test($base64String){
    if (mysql_query("update users set attribute1='$base64String' where id = '83'")){
                    echo "success";

                }else{
                    mysql_error();
                }
}

thanks

回答1:

Please increase in /etc/mysql/my.cnf,

wait_timeout = 3600

and

max_allowed_packet = 128M

and restart

sudo /etc/init.d/mysql restart

Note: Why are you storing the audio file in the DB, instead store the path of it and store the file in system