mysql server has gone away error during installing migration (laravel) - php

So I am using my cmd on my laravel folder and I tried to do (php artisan migrate:install). 2 errors came up.
[PDOException] SQLSTATE[HY000] [2006] MySQL server has gone away
[ErrorException] PDO::__construct(): MySQL server has gone away
Can anyone please explain what I did wrong?

You have Lost SQL connection to server during query. It is temporally issue. This is because of very low default setting of max_allowed_packet.
Raising max_allowed_packet in my.cnf (under [mysqld]) to 8 or 16M usually fixes it.
[mysqld]
max_allowed_packet=16M
NOTE: This can be set on your server as it's running. You need to restart the MySQL service once you are done.
Use: set global max_allowed_packet=104857600. My value sets it to 100MB.

This is not a Laravel issue, but a general MySQL Issue. Maybe the server is not running. Are you sure you're running MySQL in the background?
Check this link: MySQL Gone Away
Do the following checks in your system:
The Database Engine is running
You have created your database
You have created an user and granted permissions to the database
You have setup the user and the database in your Laravel's .env file.
After this, try to run the migrations command again, which is:
php artisan migrate
As explained Here
Let us know if that helps :).

For me the problem seemed to be that I assigned the wrong port to my Laravel project's .env file. Later, when I matched it with the my.cnf file, it worked.
I'm using Ubuntu 16.04 + nginx + MariaDB + Laravel project.

I encountered the same problem. The solution was to delete the mysql port number from 3306 or 80, and leave it empty
In files
.env DB_PORT=3306 to DB_PORT=
and on
database.php 'port' => env('DB_PORT', '3306'), to 'port' => env('DB_PORT', ''),

In my case the problem was I changed DB_HOST to localhost but it was fixed by keeping it default ie 127.0.0.1 and the port to default 3306.
Here is the configuration for localhost in xampp:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=hmb
DB_USERNAME=root
DB_PASSWORD=
Although I use localhost:8081/phpmyadmin to access my db

In addition to other answers:
Try changing localhost domain to 127.0.0.1 both in .env and config/database.php.
Also If you're using git then check git status and see if any unwanted files have been changed.
Because In my case options array for mysql connection configuration in file config/database.php was blank array somehow. I checked out git checkout config/database.php file and it starting to work fine.

If someone is looking for a solution, try to clean all cache you have, you can manually delete cache files in bootstrap/cache folder.

My solution is due to fix the DB_HOST parameter. Indeed I use PHPStorm and I set the public url in .env in order to use DB tools from the IDE and this broken Laravel.
So I changed DB_HOST to localhost and Laravel now works fine.

Related

(HY000/2002): No connection could be made because the target machine actively refused it [duplicate]

An error suddenly occurred while I was debugging my code.
It has this series of errors regarding the connection to database.
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
( ! ) Fatal error: Uncaught exception 'PDOException' with message ' in C:\wamp\www\web\main\users.php on line 15
( ! ) PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. in C:\wamp\www\web\main\users.php on line 15
this is the code of where the error is pointing
function __construct()
{
$this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
I don't know what to do since I do not usually get problems like this so I haven't studied much about WAMP.
Thank You for your help.
If the WAMP icon is Orange then one of the services has not started.
In your case it looks like MySQL has not started as you are getting the message that indicates there is no server running and therefore listening for requests.
Look at the mysql log and if that tells you nothing look at the Windows event log, in the Windows -> Applications section. Error messages in there are pretty good at identifying the cause of MySQL failing to start.
Sometimes this is caused by a my.ini file from another install being picked up by WAMPServers MySQL, normally in the \windows or \windows\system32 folders. Do a search for 'my.ini' and 'my.cnf' and if you find one of these anywhere outside of the \wamp.... folder structure then delete it, or at least rename it so it wont be found. Then restart the MySQL service.
Possibly using different Port for MySQL than using in your Code
$conn = new PDO("mysql:host=".SERVER_NAME.";port=3307;dbname=".DB_NAME, DB_USER, DB_PASS);
Both ports should be same.
I had a similar issue when trying to migrate a Drupal website from one local host to another. From Mac running XAMMP to Windows running WAMP.
This was the error message I kept getting when trying to access the pages of the website.
PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
in drupal_get_installed_schema_version() (line 155 of C:\wamp\www\chia\includes\install.inc).
In settings.php, I've changed everything correctly, database name, user and password.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'mydatabasename',
'username' => 'mydbusername',
'password' => 'mydbpass',
'host' => 'localhost',
'port' => '8889',
'driver' => 'mysql',
'prefix' => '',
),
),
);
After a couple of hours of mindless google searching I've changed the port to a empty value:
'port' => '',
And after that the site loaded properly.
Check whether MySQL server is running at all. If you not run apache server(or any server you use) this could happen. After run your local server and refresh the page.
Restart your wampServer... that should solve it.
and if it doesn't..
Resta
Ensure your WAMP Server (or XAMP) is working, i.e. the wamp icon should be green.
Delete the following files from the directory:
\wamp\bin\mysql\mysql5.6.17\ib_logfile0
\wamp\bin\mysql\mysql5.6.17\ib_logfile1
\wamp\bin\mysql\mysql5.6.17\ibdata1
This solved my problem.
You should restart your Xampp or whatever server you're runnung, make sure sq
I've had the same issue too, I didn't remember to start my WAMPSERVER, and so the connection couldn't be made...
All you need to do is to start or restart your WAMPSERVER or your XAMMP again and it will work....
All you have to do it insert you exact db details and restart your mysql server
if you are using XAMPP and WAMP together in the same machine add sql server port number
<connection>
<host><![CDATA[localhost:3390]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[sritoss_1910]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
I had the same problem, I just deleted all log files in mysql/data files like:
mysql-relay-bin - 2020#002d...
Just these files and it worked.
I hope this helps.
Just restart your wamp server and then run php bin/magento cache:clean
I had the same problem and I searched a lot in web but no solution worked for me .finally I noticed by chance that mysql is using port 3308 instead of port 3306 which seems to be default ,,, I changed the port to 3306 and surprisingly it worked :) .... (my problem was in connecting php to mysql database, php my admin page was shown perfectly)
'dsn' => 'mysql:host=localhost;port=3307;dbname=yourdbname',
I was getting this error attempting to run "php artisan migrate" on Windows with a virtual box / vagrant / homestead installation.
The documentation said I had to run this command on the virtual machine.
This worked!!!
make sure to do it inside your current project folder.

Symfony 5 - SQLSTATE[HY000] [2002] No such file or directory

I'm running a Symfony 5 project on my Ubuntu machine, and when I try to import my database I had created to my project I get the following error:
The thing is I'm going through a tutorial a second time to create my own project, and the first project works just fine. The DATABASE_URL in the .env files on these two project are the same (double and triple checked),
here's the .env
I've tried changing localhost to 127.0.0.1 but the error changes to 'Connection refused', and I also tried adding the real path to the mysql.sock to the php.ini. These are the solutions I've found that apply to my problem, nothing worked so far.
Any ideas would be greatly appreciated!!
Have you defined a path to your socket in the config/packages/doctrine.yaml? See the marked answer here
I think that setting localhost to 127.0.0.1 fixes your initial problem, but then you are getting connection refused as the server isn't accepting your credentials.
If you aren't using a socket, then you need to pass a password in to your DATABASE_URL:
DATABASE_URL=mysql://root:password#127.0.0.1:3306/beers?serverVersion=5.7
This problem sometimes occurs, because on Linux (unix) machines, we need to indicate unix_socket="value". You should find "mysql.sock" file in your local server folder and copy/paste the path as its value.
e.g. on my distant Gandi server this file is called "mysqld.sock" and lives in /srv/run/mysqld/ .
So, the distant .env is : DATABASE_URL="mysql://root:password#localhost/db_name?unix_socket=/srv/run/mysqld/mysqld.sock"
On my local ubuntu this file lives in /opt/lampp/var/mysql/mysql.sock . In my local .env file : DATABASE_URL="mysql://root:password#localhost/db_name?unix_socket=/opt/lampp/var/mysql/mysql.sock"
Hope, it helps someone having the same issue.

phpMyAdmin: Cannot log into the MySQL server no error code

I recently copied my mysql db into a new data directory and changed a few settings.
I also accidentally deleted my user directory /home/user and had all the fun of the fair recreating that.
I can now connect to mysql on the command line but cannot connect via phpMyAdmin.
I get the message:
Cannot log in to the MySQL server
I have seen this before, but not without the error code #1045 or #2002 prepended.
Would there be any logs or documentation anywhere about this message without an error code?
UPDATE
phpMyAdmin is installed on the same server and prompts the web user for a username/password in the browser login page, on submit with the correct details it returns to this login page with the error message displayed.
I have tried changing the /etc/phpMyAdmin/config.inc.php line to:
$cfg['Servers'][$i]['host'] = '127.0.0.1'; // previously 'localhost'
I have also tried FLUSH PRIVILEGE to no avail.
According to me the following can be the issue and the ways to resolve it
Causes:
--> path to save php_session is not set or is uncorrectly set:
--> Either php do not have sufficient rights to write to session directory or the directory does not exists.
Solution:
To define the php_session directory add the below line to php.ini file:
session.save_path="/tmp/php_session/"
And give the write rights to the http server.
Mostly, the http server run as user daemon in group daemon. For this case, the following commands will do the work for you :
chown -R :daemon /tmp/php_session
chmod -R g+wr /tmp/php_session
restart http server.
Try it out and let me know
UPDATE
I have found this:-
In some rare cases, if your MySQL process has existed for a long time without any updates to your password, it may be storing your password in a format phpMyAdmin can't authenticate against. This will cause you to be unable to log in via phpMyAdmin, even with the correct username and password. In these cases, it is usually sufficient to change your MySQL password by another means (e.g. the command line), even if you "change" the password to the same thing.
so try just resetting your password through command line as
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Finally fixed it, after much confusion.
Turns out, my my.cnf file was binding the address to our server's internal network IP.
bind-address = 192.168.etc
Previously, before moving the database (and socket location), it was also connecting with 'localhost' correctly (which uses the socket) and allowing login from phpMyAdmin.
After moving the db, the socket connection didn't work, and changing the config.inc.php line to 127.0.0.1 from localhost causes mysql to connect with TCP instead of the socket. This now caused conflict with the bind-address:
$cfg['Servers'][$i]['host'] = '127.0.0.1'; // previously 'localhost'
Removing the bind-address restriction allowed me to login.
A better solution however, was of course to fix the socket connection.
After fussing over permissions for a while, and asking around, someone help me find a setting in mysqli.ini (the extension phpMyAdmin uses to connect to the db):
mysqli.default_socket = /new/location
This fixed the localhost socket connection and I could reinstate my my.cnf bind-address and revert config.inc.php to use 'localhost' again.
in my situation, I just switch the php version from:5.6.16 to 7.0
and then it got login to nothing.
just switch it back, maybe reboot server it will support work in PHP 7.0

Artisan-commands need different hostname in db-config than Laravel itself

This is a strange one I've never been able to solve ever since I started using Laravel.
When I issue artisan-commands like 'php artisan migrate' in my db-config (I'm using MySQL via MAMP) the host line has to be:
'host' => 'localhost:8889',
However when loading a page in the web-browser I get:
SQLSTATE[HY000] [2005] Unknown MySQL server host 'localhost:8889'
It magically works when I change it to:
'host' => 'localhost',
On the live-server I'm running the application on I don't have this problem.
What is going wrong here?
This is most likely happening because you just cd'd into the directory using Terminal and ran the php artisan migrate command from there. You'll need to ssh into your vagrant box using vagrant ssh, then navigate to your project folder and run the migrate command from there.
What port is your MySQL server running on locally? By default, MySQL looks for a server on :3306, so you'll get the error unless something is there to serve it or re-direct it
My guess is the prod server is running MySQL on :8889 and your local environment is running MySQL on :3306
In your local environment, You could either change the port to 8889:
http://www.mamp.info/en/documentation/#q11
Or forward the port:
http://www.chrisvanpatten.com/port-forwarding-mavericks

ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it

An error suddenly occurred while I was debugging my code.
It has this series of errors regarding the connection to database.
ERROR: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
( ! ) Fatal error: Uncaught exception 'PDOException' with message ' in C:\wamp\www\web\main\users.php on line 15
( ! ) PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it. in C:\wamp\www\web\main\users.php on line 15
this is the code of where the error is pointing
function __construct()
{
$this->con = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
I don't know what to do since I do not usually get problems like this so I haven't studied much about WAMP.
Thank You for your help.
If the WAMP icon is Orange then one of the services has not started.
In your case it looks like MySQL has not started as you are getting the message that indicates there is no server running and therefore listening for requests.
Look at the mysql log and if that tells you nothing look at the Windows event log, in the Windows -> Applications section. Error messages in there are pretty good at identifying the cause of MySQL failing to start.
Sometimes this is caused by a my.ini file from another install being picked up by WAMPServers MySQL, normally in the \windows or \windows\system32 folders. Do a search for 'my.ini' and 'my.cnf' and if you find one of these anywhere outside of the \wamp.... folder structure then delete it, or at least rename it so it wont be found. Then restart the MySQL service.
Possibly using different Port for MySQL than using in your Code
$conn = new PDO("mysql:host=".SERVER_NAME.";port=3307;dbname=".DB_NAME, DB_USER, DB_PASS);
Both ports should be same.
I had a similar issue when trying to migrate a Drupal website from one local host to another. From Mac running XAMMP to Windows running WAMP.
This was the error message I kept getting when trying to access the pages of the website.
PDOException: SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
in drupal_get_installed_schema_version() (line 155 of C:\wamp\www\chia\includes\install.inc).
In settings.php, I've changed everything correctly, database name, user and password.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'mydatabasename',
'username' => 'mydbusername',
'password' => 'mydbpass',
'host' => 'localhost',
'port' => '8889',
'driver' => 'mysql',
'prefix' => '',
),
),
);
After a couple of hours of mindless google searching I've changed the port to a empty value:
'port' => '',
And after that the site loaded properly.
Check whether MySQL server is running at all. If you not run apache server(or any server you use) this could happen. After run your local server and refresh the page.
Restart your wampServer... that should solve it.
and if it doesn't..
Resta
Ensure your WAMP Server (or XAMP) is working, i.e. the wamp icon should be green.
Delete the following files from the directory:
\wamp\bin\mysql\mysql5.6.17\ib_logfile0
\wamp\bin\mysql\mysql5.6.17\ib_logfile1
\wamp\bin\mysql\mysql5.6.17\ibdata1
This solved my problem.
You should restart your Xampp or whatever server you're runnung, make sure sq
I've had the same issue too, I didn't remember to start my WAMPSERVER, and so the connection couldn't be made...
All you need to do is to start or restart your WAMPSERVER or your XAMMP again and it will work....
All you have to do it insert you exact db details and restart your mysql server
if you are using XAMPP and WAMP together in the same machine add sql server port number
<connection>
<host><![CDATA[localhost:3390]]></host>
<username><![CDATA[root]]></username>
<password><![CDATA[]]></password>
<dbname><![CDATA[sritoss_1910]]></dbname>
<initStatements><![CDATA[SET NAMES utf8]]></initStatements>
<model><![CDATA[mysql4]]></model>
<type><![CDATA[pdo_mysql]]></type>
<pdoType><![CDATA[]]></pdoType>
<active>1</active>
</connection>
I had the same problem, I just deleted all log files in mysql/data files like:
mysql-relay-bin - 2020#002d...
Just these files and it worked.
I hope this helps.
Just restart your wamp server and then run php bin/magento cache:clean
I had the same problem and I searched a lot in web but no solution worked for me .finally I noticed by chance that mysql is using port 3308 instead of port 3306 which seems to be default ,,, I changed the port to 3306 and surprisingly it worked :) .... (my problem was in connecting php to mysql database, php my admin page was shown perfectly)
'dsn' => 'mysql:host=localhost;port=3307;dbname=yourdbname',
I was getting this error attempting to run "php artisan migrate" on Windows with a virtual box / vagrant / homestead installation.
The documentation said I had to run this command on the virtual machine.
This worked!!!
make sure to do it inside your current project folder.

Categories