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

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.

Related

laravel Remote connection [] not defined issue

In Laravel 5 I configured two remote servers in config/remote.phpand I used it for more than a year. When I added a third server now it always triggered this error
"laravel Remote connection [servername] not defined."
I answer my own question because I can't find the correct answer anywhere and I spent few hours to fix it. I believe this may help others or help myself in future.
After trying everything, I suspected that the configuration is not served from config/remote.php so i renamed config/remote.php to config/remote.php1 now also both servers added last year worked without issue so i searched my app directory
grep -iRl "myoldservername.com"
it returned with
bootstrap/cache/config.php
config/remote.php1
config/remote.php.save
I confirmed that the remote server configuration has been served from cached config file.
I renamed config/remote.php1 back to config/remote.php again and executed
php artisan cache:clear
php artisan config:clear
Now the new and old servers worked fine.

The 'file_get_contents()' is not working on local Laravel/PHP environment

I have a Laravel (php) application running on a unix server, and now when I tried to pull it to my local environment (Mac OX 10.11) I encounter the following error:
file_get_contents(): SSL: Connection reset by peer
I've searched here, and saw that many users in the community suggest to check the 'php.ini' file and make sure that
allow_url_fopen = On
extension=php_openssl.dll is enabled.
And so I modified the file accordingly - yet the problem persists.

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

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.

PHP php_network_getaddresses: getaddrinfo failed: No such host is known

I am having DNS issues with a certain target domain. I am using fopen() (but same issue with other functions) to retreive an image, but I get this error:
Warning: fopen(): php_network_getaddresses: getaddrinfo failed: No such host is known
I am able to ping or nslookup the domain from the command prompt, but for some reason php throws this error. When I try fopen('http://www.google.com', r); or other domains, all goes fine. But above mentioned domain simply won't resolve with PHP. I have flushed the DNS, restarted Apache, but still no luck..
I have tried with:
Windows 7, Apache 2.2 PHP 5.3.6
Windows server 2008r2, Apache 2.2 PHP 5.3.6
What can cause this single domain to not resolve?
IMO it's the different way to resolve a name from the OS and PHP.
Try:
echo gethostbyname("host.name.tld");
and
var_export (dns_get_record ( "host.name.tld") );
or
$dns=array("8.8.8.8","8.8.4.4");
var_export (dns_get_record ( "host.name.tld" , DNS_ALL , $dns ));
You should found some DNS/resolver error.
Your "localhost" cannot resolve the name www.google.com, which means your machine doesn't/can't reach a valid dns server.
Try ping google.com on the console of that machine to verify this.
It is more flexible to use curl instead of fopen and file_get_content for opening a webpage.
getaddrinfo failed: No such host is known
The above error is caused due to mistake in Database Host (DB_HOST) in .env file
Open the .env file and Specially check for this env variables
DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password
Your target domain might refuse to send you information. This can work as a filter based on browser agent or any other header information. This is a defense against bots, crawlers or any unwanted applications.
I had a lot of issues with this on Windows. I just performed git clone, copied my .env.example file to .env. updated it, and executed my trusty PHP script to update initial database used for a project. And it failed, and whatever I did, it kept on failing. Out of desperation, I reinstalled MySQL, but that didn't fix things.
Finally, I figured it out. PHP script extracted the settings from .env file using file_get_contents(), and exploding it using \n, but the .env file was encoded with CRLF, so my DB_HOST was empty. After I changed the encoding of the .env file, everything was fine.
So, my point is, echo the parameters supplied to mysqli::construct, and it might be the culprit.
A weird thing I found was that the environment variable SYSTEMROOT must be set otherwise getaddrinfo() will fail on Windows 10.
What had caused this error on my side was the following line
include_once dirname(__FILE__) . './Config.php';
I managed to realize it was the culprit when i added the lines:
//error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
//ini_set('display_errors', 1);
to all my php files.
To solve the path issue i canged the offending line to:
include_once dirname(__FILE__) . '/Config.php';

Wordpress installation - php and mysql are not talking to each other to access database?

I'm trying to get a wordpress local install working on my Mac.
I've recently set up a database, username and password using the MySQL client as per the instructions in the wordpress codex. That part is ok.
When I go to the local site on my computer I'm prompted by wordpress to set up the wp-config.php file. Putting in the same details that definitely work with the MySQL client returns an error.
Manually updating these details in the 'wp-config-sample.php' file and renaming it as 'wp-config.php' also doesn't work - I'm now greeted with a big bold message: "Error establishing a database connection".
From what I can gather, php and mysql are not communicating with each other properly.
I've changed the apache 2 configuration file found in 'etc/apache2/httpd.conf' and uncommented the line which loads the php5 module: LoadModule php5_module libexec/apache2/libphp5.so
and restarted the apache server. This has still not fixed my problem. I'm stuck on what to do next... help appreciated please!
It's been fixed now.
php was looking in the wrong place for the mysql.sock file, and couldn't communicate with the mysql database, meaning wordpress couldn't be set up.
On the command line I typed
ps aux | grep mysql
to bring up as much info about mysql that I could. From this I saw that the mysql socket on my computer is being used at /tmp/mysql.sock
Meanwhile, in the "wp-config.php" file, I changed the line define('WP_DEBUG', false); to define('WP_DEBUG', true); and refreshed my web browser that was showing my localhost setup. This now said a lot more info than "Error establishing a database connection" - infact I could see that the mysql.sock was not configured correctly at all.
Creating a 'phpinfo.php' file with just the single line of code <?php phpinfo(); ?> was the pivotal turning point for me.
Putting that 'phpinfo.php' file in my localhost directory and then viewing it from my web browser not only definitely confirmed that the mysql.sock file was not configured properly, but it also showed where the configurations were being loaded from on my machine: '/private/etc/php.ini'
The next thing I did was open up the 'php.ini' file in a text editor and doing a quick find and replace to update the correct location of mysql.sock.
Finally, a restart of the local apache server needed to be done to fully update all the new settings that I'd been fiddling about with.
sudo apachectl restart
Now everything works fine!
The Error-Message "Error establishing a database connection" indicates clearly, that the php-script can not establish the connection. This can have one of the following reasons:
Your host in the wp-config.php is not correct.
The mysql-server does not listen on that host
The user you set up in the mysql has no privileges to connect the way you are trying
The firewall blocks the access of the php-script to your mysql-host
Try to connect the database with the same settings of your wordpress using Sequel Pro (or any other GUI to connect a mysql database). If the connection works, the problem 2. and 3. isn't your problem.
Look in your firewall-log to make sure 4. is also not your problem.
If all problems are excluded (even 1. after a final check), try to run something like phpMyAdmin to connect your database, to make sure your php-installation has the needed drivers.

Categories