Using Mac OS X and Homestead 2.2.1 with Laravel 5.2.
In terminal (within homestead in my project folder) I can do php artisan to see all the available commands. When I try to run php artisan migrate I get a connection error:
SQLSTATE[HY000] [2002] Connection refused
I have setup a Laravel project with these .env settings
DB_HOST=127.0.0.1
DB_DATABASE=tcv
DB_USERNAME=homestead
DB_PASSWORD=secret
I have also tried localhost for DB_HOST and root for DB_USERNAME and DB_PASSWORD. And all possible variations of these put together!
In Sequel Pro (db management application) I CAN connect with these settings
Host 127.0.0.1
Username homestead
Password secret
Database tcv
Port 33060
But this database is obviously empty, because I cant migrate to it from terminal ...
As far as I can make out it is a configuration issue, since I can connect to it with Sequel Pro. But I have honestly no freaking idea what is setup wrong.
Thanks for the help !!
EDIT
For some reason I get the same SQLSTATE[HY000] [2002] Connection refused error when moving my project to MAMP and running php artisan migrate.
Now I am completely lost ...
I just ran into this and found that changing this in the .env file from 127.0.0.1 to localhost fixed it.
DB_HOST=localhost
Problem
In Laravel you have config/database.php where all the setup for the connection is located. You also have a .env file in the root directory in your project (which everyone uses for timesaving). This contains variables that you can use for the entire project.
On a standard L5 project the MySql section of config/database.php looks like this:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Notice there is no port set!
Although in my .env file I had set DB_PORT=33060. But that value (3306) was never read into the config/database.php.
So don't be a dumbass like myself and forget to check the database.php file.
FIX
Simply add 'port' => env('DB_PORT', 3306), to your config/database.php and set that value in .env like this DB_PORT=3306
If you are using MAMP on a mac OS add the following line to your mysql database config file
'unix_socket' => env('DB_SOCKET', ''),
and on your .env file add
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Use localhost instead of 127.0.0.1 (in your .env file), then run command:
php artisan config:cache
PROBLEM SOLVED
I had the same problem and fixed just by converting DB_HOST constant in the .env File FROM 127.0.0.1 into localhost
JUST DO THIS AS GIVEN IN THE BELOW LINE
DB_HOST = localhost.
No need to change anything into config/database.php
Don't forget to run
php artisan config:clear
Another solution for anyone else who has a problem. I had all the settings correct but for some reason my changes weren't updated. Laravel actually caches the config file (which I find completely stupid).
Here was my solution after updating the configs:
php artisan config:clear
If you have your backend and database started on docker
Instead of putting localhost or 127.0.0.1 as DB_HOST put the name of the registered service that indicates your database service in the docker-compose file.
In my case for example I replaced 127.0.0.1 with db because in my docker-compose file I had defined the name of the service for the database as db
My docker-compose looks something like that
services:
db: <------ This is the name of the DB_HOST
container_name: admin_db
image:mysql:5.7.22
.
.
.
I was having this problem. When connecting with Sequel Pro, I need to use 33060 as the port, but in the .env file it has to be 3306. I had 33060 in the .env file. Changed it to 3306 and it worked.
This is a simple fix. Your mysql database has lost its connection to the server. If you are running a local server run this in your terminal:
mysqld
That will reconnect your database. Then (if you are using homebrew) run:
brew services start mysql
This will automatically connect your database on login.
In my case, I was having the same error using docker and the trick was in setting in the .env file this DB_HOST=db where db is the name of the container running the database server.
I had the same problem, try this works
DB_HOST=localhost
It's possible that your mysql hasn't started or is not to the port 3306
In my case this error appeared out of blue. While staring at that mysterious error I've realized, that I was trying to run the command outside of vm...
After you put all configuration on .env file, if you already run php artisan serve, RESTART IT.
For me enclosing the credentials in quotes did the trick
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE='zzz'
DB_USERNAME='yyy'
DB_PASSWORD='XXX'
When Laravel connects to mysql on Homestad box and running commands on local environment:
DB_HOST=127.0.0.1
the app needs DB_PORT=3306
the migrations need DB_PORT=33060 otherwise gives Connection refused
phpunit needs DB_PORT=33060 otherwise gives Connection refused
DB_HOST=localhost
the app works with both DB_PORT=3306 or DB_PORT=33060
migrations cannot run
phpunit cannot run
Have in mind that phpunit takes its values from the .env.testing file, if exists, otherwise from the phpunit.xml file on the root folder of the app
Me, Im using vagrant, yet im executing php artisan outside the box so basically it doesn't have permission
I could be because you might have not restarted PHP artisan since long
So After making DB changes and config:clear Tinker works fine
But to make browser refect the new DB connection you need to re-run
php artisan serve
terminate sqld from task manager
stop MySQL from xampp control panel and restart it
if it still throw the same problem, from root folder run
php artisan cache:clear
php artisan config:cache
php artisan serve
if you still have the problem, check whether you are using multiple copies of sql server, for example through Ubuntu app, if so stop the MySQL server in ubuntu
sudo service MySQL stop
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=8080
DB_DATABASE=flap_safety
DB_USERNAME=root
DB_PASSWORD=mysql
the above given is my .env
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'mysql'),
// 'port' => env('DB_PORT', '8080'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', 'mysql'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
the above given is my database.php file. i just commented out port from database.php and it worked for me.
If you are using Homestead then you should be running it with the default mysql port. So instead of using DB_PORT=33060, you should be using DB_PORT=3306 in your .env file. Also, remember to run your php artisan migrate commands in your homestead installation and everything should be ok.
Hope that helps.
The only thing that solved it for me was to put the connection details in config/database.php instead of the .env file. Hope this helps
I had a similar problem and no suggestions placed here helped me.
This what has fixed my problem was to set the application name and database hostname with the same value. In my case, 127.0.0.1 works correctly.
APP_URL=127.0.0.1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=zzz
DB_USERNAME=yyy
DB_PASSWORD=XXX
Okay update it seems the host is not the issue but actually the port.
So its actually the port 3306 for the browser testing and yet for terminal and Sequel Pro 33060. Could it have something to do with adding it in the Homestead.yaml and set it here?
On reading the setup in relation to Laravel it says:
MySQL: 33060 → Forwards To 3306
I encountered
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
projects)
The cause was that I had not started the MySQL server.
It may help to check that the MySQL is running as the first step in troubleshooting the error.
I found that my server just needed to be restarted and "bam" it was fixed.
If you are using Homestead like I do, do not forget to ssh into it before running artisan test for example.
I don't think it even reads config/database.php.
As soon as edited my entries into .env file it started working.
In case this helps someone in the future... I have to run artisan commands and so on using WSL but MySQL lives on my Windows environment. I struggle with the correct terminology but my brain says "I don't care if you think you're a virtual machine, you're quite literal and literally right in front of me so when I say connect to yourself I expect you to do so."
If I understand correctly, PHP is executing the commands in magical WSL-land so it needs the IP of the database in relation to WSL-land.
From a command prompt (Windows, not in WSL) I ran ipconfig to find my IPv4 and put that for my Laravel DB_HOST. Now, when I migrate in WSL it works.
I realize this was overly wordy but I personally get very confused with all of the virtual machine stuff, so I put the answer I wish I'd found weeks ago.
Wish I'd never gone back to Windows.
If you have any kind of proxy or vpn , turn them off and close them . it was my problem .
Related
I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate or php artisan db:seed) I get an error message:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.
One of simplest reasons for this error is that a MySQL server is not running. So verify that first. In case it's up, proceed to other recommendations:
Laravel 4: Change "host" in the app/config/database.php file from "localhost" to "127.0.0.1"
Laravel 5+: Change "DB_HOST" in the .env file from "localhost" to "127.0.0.1"
I had the exact same problem. None of the above solutions worked for me. I solved the problem by changing the "host" in the /app/config/database.php file from "localhost" to "127.0.0.1".
Not sure why "localhost" doesn't work by default but I found this answer in a similar question solved in a symfony2 post. https://stackoverflow.com/a/9251924
Update:
Some people have asked as to why this fix works so I have done a little bit of research into the topic. It seems as though they use different connection types as explained in this post https://stackoverflow.com/a/9715164
The issue that arose here is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP (Transmission Control Protocol), which essentially means it runs through the "local internet" on your computer being much more reliable than the UNIX socket in this case.
The error message indicates that a MySQL connection via socket is tried (which is not supported).
In the context of Laravel (artisan), you probably want to use a different / the correct environment. Eg: php artisan migrate --env=production (or whatever environment). See here.
I got the same problem and I'm running Mac OS X 10.10 Yosemite. I have enabled the Apache Server and PHP that already comes with the OS. Then I just configured the mCrypt library to get started. After that when I was working with models and DB I got the error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
The reason I found is just because PHP and MySQL can't get connected themselves.
To get this problem fixed, I follow the next steps:
Open a terminal and connect to the mysql with:
mysql -u root -p
It will ask you for the related password. Then once you get the mysql promt type the next command:
mysql> show variables like '%sock%'
You will get something like this:
+-----------------------------------------+-----------------+
| Variable_name | Value |
+-----------------------------------------+-----------------+
| performance_schema_max_socket_classes | 10 |
| performance_schema_max_socket_instances | 322 |
| socket | /tmp/mysql.sock |
+-----------------------------------------+-----------------+
Keep the value of the last row:
/tmp/mysql.sock
In your laravel project folder, look for the database.php file there is where you configure the DB connection parameters. In the mysql section add the next line at the end:
'unix_socket' => '/tmp/mysql.sock'
You must have something like this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'SchoolBoard',
'username' => 'root',
'password' => 'venturaa',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'unix_socket' => '/tmp/mysql.sock',
),
Now just save changes, and reload the page and it must work!
I encountered the [PDOException] SQLSTATE[HY000] [2002] No such file or directory error for a different reason. I had just finished building a brand new LAMP stack on Ubuntu 12.04 with Apache 2.4.7, PHP v5.5.10 and MySQL 5.6.16. I moved my sites back over and fired them up. But, I couldn't load my Laravel 4.2.x based site because of the [PDOException] above. So, I checked php -i | grep pdo and noticed this line:
pdo_mysql.default_socket => /tmp/mysql.sock => /tmp/mysql.sock
But, in my /etc/my.cnf the sock file is actually in /var/run/mysqld/mysqld.sock.
So, I opened up my php.ini and set the value for pdo_mysql.default_socket:
pdo_mysql.default_socket=/var/run/mysqld/mysqld.sock
Then, I restarted apache and checked php -i | grep pdo:
pdo_mysql.default_socket => /var/run/mysqld/mysqld.sock => /var/run/mysqld/mysqld.sock
That fixed it for me.
The answer from #stuyam solved the "No such file or directory" issue for me
Short answer: Change "host" in the /app/config/database.php file from "localhost" to "127.0.0.1"
But then I had a "Connection refused" error. If anyone had the same issue, my solution for this was to update the app/config/local/database.php file so the port is 8889:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '8889',
'database' => 'databaseName',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
In my case i had no problem at all, just forgot to start the mysql service...
sudo service mysqld start
If you are using Laravel Homestead, make sure you're calling the commands on the server.
homestead ssh
Then simply cd to the right directory and fire your command there.
It worked after I change from DB_HOST=localhost to DB_HOST=127.0.0.1 at .env file
This is because PDO treats "localhost" host specially:
Note: Unix only: When the host name is set to "localhost", then the
connection to the server is made thru a domain socket. If PDO_MYSQL is
compiled against libmysqlclient then the location of the socket file
is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled
against mysqlnd a default socket can be set thru the
pdo_mysql.default_socket setting.
(from http://php.net/manual/en/ref.pdo-mysql.connection.php)
Changing localhost to 127.0.0.1 will "force" the use of TCP.
Note: mysqli_connect is working fine with localhost.
Add mysql.sock path in database.php file like below example
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
Eample
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '8889'),
Mamp user enable option Allow network access to MYSQL
Building on the answer from #dcarrith ...
Instead of editing the config files, I created an alias in the location that PHP is looking that connects to the real mysql.sock. (source)
Just run these two commands (no restart needed):
mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock
Step 1
Find the path to your unix_socket, to do that just run netstat -ln | grep mysql
You should get something like this
unix 2 [ ACC ] STREAM LISTENING 17397 /var/run/mysqld/mysqld.sock
Step 2
Take that and add it in your unix_socket param
'mysql' => array(
'driver' => 'mysql',
'host' => '67.25.71.187',
'database' => 'dbname',
'username' => 'username',
'password' => '***',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'unix_socket' => '/var/run/mysqld/mysqld.sock' <-----
),
),
Hope it helps !!
I'm running on MAMP Pro and had this similar problem when trying to migrate (create db tables). Tried a few of these mentioned suggestions as well but didn't do it for me.
So, simply (after an hour googling), I added two things to the /config/database.php.
'port' => '1234',
'unix_socket' => '/path/to/my/socket/mysqld.sock'
Works fine now!
Just i do one change in .env file
I have following line of code.
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
Change host name localhost to 127.0.0.1
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=root
DB_PASSWORD=
That is work in my case because that can't find any hostname like localhost
And after changing hostname write following command
php artisan config:clear
php artisan migrate:install
php artisan migrate
I had this problems when I was running my application using docker containers.
The solution was put the name of the MySQL service container I was using in docker_compose.yml on DB_HOST. In my case, it was db :
DB_HOST=db
Hope it helps.
I ran into this problem when running PHPUnit in Elixir/Gulp, and Homestead as my Vagrant enviroment.
In my case I edited the .env file from DB_HOST=localhost to DB_HOST=192.168.10.10 where 192.168.10.10 is the IP of my Vagrant/Homestead host.
Check your port carefully . In my case it was 8889 and i am using 8888.
change "DB_HOST" from "localhost" to "127.0.0.1" and vice versa
I had similar problems accessing my Drupal website. I fixed it by opening the command line, and restarting my MySQL server or service:
service mysqld restart
This should work. If it doesn't, restart your local webserver:
service httpd restart
That should be enough. Hope it works for other environments, too. Note that these commands generally require superuser privileges.
I had the same problem using Docker and MySQL service name db in docker_compose.yml file:
I added the following in the .env file:
DB_HOST=db
you should also assure that your host is discoverable from the php app.
It was because PHP didn't figure out which host to use to connect.
I got the same problem in ubuntu 18.04 with nginx. By following the below steps my issue has been fixd:
First open terminal and enter into mysql CLI. To check mysql socket location I write the following command.
mysql> show variables like '%sock%'
I got something like the below :
+-----------------------------------------+-----------------------------+
| Variable_name | Value |
+-----------------------------------------+-----------------------------+
| mysqlx_socket | /var/run/mysqld/mysqlx.sock |
| performance_schema_max_socket_classes | 10 |
| performance_schema_max_socket_instances | -1 |
| socket | /var/run/mysqld/mysqld.sock |
+-----------------------------------------+-----------------------------+
4 rows in set (0.00 sec)
In laravel project folder, look for the database.php file in the config folder. In the mysql section I modified unix_socket according to the above table.
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database_name',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'unix_socket' => '/var/run/mysqld/mysqld.sock',
),
Now just save changes, and reload the page and it worked.
As of Laravel 5 the database username and password goes in the .env file that exists in the project directory, e.g.
DB_HOST=127.0.0.1
DB_DATABASE=db1
DB_USERNAME=user1
DB_PASSWORD=pass1
As you can see these environment variables are overriding the 'forge' strings here so changing them has no effect:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
More information is here https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables
solved
in my case it was a logic issue in code, the connection values are in a if statement:
if($_SERVER['HTTP_HOST'] == "localhost")
so the solution was to add a pipe and add 127.0.0.1, that solved the problem for me
if($_SERVER['HTTP_HOST'] == "localhost" || $_SERVER['HTTP_HOST'] == "127.0.0.1")
If you are using Laravel Homestead,
here is settings
(include Vagrant-Virtual Machine)
.bash-profile
alias vm="ssh vagrant#127.0.0.1 -p 2222"
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Terminal
vm
vagrant#homestead:~/Code/projectFolder php artisan migrate:install
Attempt to connect to localhost:
SQLSTATE[HY000] [2002] No such file or directory
Attempt to connect to 127.0.0.1:
SQLSTATE[HY000] [2002] Connection refused
OK, just comment / remove the following setting from my.cnf (on OS X 10.5: /opt/local/etc/mysqlxx/my.cnf) to obtain:
[mysqld]
# skip-networking
Of course, stop and start MySQL Server.
If anyone are still looking for the answer, just check your .env file. For some reason laravel create a .env.example file, so all this answers didn't work for me. I fixed my issue renamming .env.example to .env
This happened to me because MySQL wasn't running. MySQL was failing to start because I had a missing /usr/local/etc/my.cnf.d/ directory.
This was being required by my /usr/local/etc/my.cnf config file as a glob include (include /usr/local/etc/my.cnf.d/*.cnf).
Running mkdir /usr/local/etc/my.cnf.d, and then starting MySQL, fixed the issue.
In my case, I was running php artisan migrate on my mac terminal, when I needed to ssh into vagrant and run it from there. Hope that helps someone the headache.
In my case I had to remove the bootstrap/cache folder and try it again.
My cenario was after a server migration.
When using a VirtualMachine make sure you ssh into that machine and navigate to your App folder and call the php artisan migrate command from there.
I am new in Laravel development.
I have updated Xampp to 7.3.11 on my Mac Mojave 10.14.6.
In Laravel project when I hit php artisan migrate command I got following error.
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
information_schema.tables where table_schema = laravel and table_name
= migrations and table_type = 'BASE TABLE')
When I start Xampp service, my admin panel run on http://127.0.0.1:8080/phpmyadmin.
My working project in Laravel is also not connecting with database saying connection refused.
I tried by changing DB_Port and DB_Host in .env file.
I tried by clearing cache.
Any Help will be appreciated.
My .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
I've come across this Error too by building up a new project with Laravel running in docker-compose for development.
My solution was to compare the prebuild .env-File with the actual credentials I used for building the database container.
Especially I was using DB_HOST=127.0.0.1 instead of the correct service name of my docker-compose setup: DB_HOST=mysql
Open localhost/phpmyadmin and find a tab called User accounts.
Find the root user and set its password in your .env and also don't forget to create the database named laravel if it doesn't exist
Then you can clear config cache
php artisan config:clear
And migrate
php artisan migrate
Just simple step i follow and solved
open .env file
change DB_HOST = 127.0.0.1 to localhost
done
I had the same issue while running laravel and mysql within a docker container (MacOs).
I figured out that the problem was within the .env file.
the default configuration in .env was :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=my_db_name
DB_USERNAME=sail
DB_PASSWORD=password
Some answers suggested to change DB_HOST to:
DB_HOST = localhost
But it didn't work for me...
After some research I found out that, while running laravel in docker, the DB_HOST expects the database service that is running in docker too, in our case the service is mysql.
So I had to change the .env to this:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=my_db_name
DB_USERNAME=sail
DB_PASSWORD=password
I also changed the DB_HOST in config/database.php
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'mysql'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'my_db_name'),
'username' => env('DB_USERNAME', 'sail'),
'password' => env('DB_PASSWORD', 'password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
It's only then that everything started working fine.
I had the same problem and when I applied the following operations, the problem was solved.
php artisan key:generate
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear
Try out doing
php artisan config:cache
then on your .env file specify your database and your port
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= YOUR_DATABASE_NAME
DB_USERNAME=root
DB_PASSWORD=
then run your mysql server and run
php artisan migrate
Note: Make sure your Xammp server is perfectly fine & have already started mysql (using apache server or any server you are using) and also make sure you already created your database before running php artisan migrate Hope it works for you
I had the same issue. It was resolved by just restarting the local host server i.e Xampp
In most cases it's an issue with the port configuration. You need to check which port your server is running on. Then you have to edit the .env file of your project. For instance if you are using mamp the port should be changed to 8889 instead of the default 3606 for laravel. The same applies to laravel 8.
Try this conf in .env (i'm using Laravel v7.0), it's working for me :
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=root
I had this issue, I use MAMP for MYSQL server.
It was a configuration issue that I solved by changing the port to the correct one I found in MAMP instructions, which is 8889.
So the correct configuration in my .env file is:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=dbname
DB_USERNAME=root
DB_PASSWORD=rootpassword
I'm using laravel 8 and mysql with docker, i had the same problem i tried several solutions and none worked, i deleted my containers and volumes and nothing too, so i decided to restart my os and go into incognito mode and boomm it went smoothly so i went back for my tab that was giving error and I deleted the session cookies and boom the problem was solved, another tip is to use the database container IP using docker inspect [container--database-id] instead of DB_HOST=localhost. Sorry for the writing, I don't speak English, I hope I helped.
Verify port:
'${FORWARD_DB_PORT:-3306}:3306'
In my case (Windowsx64 + WSL + php/laravel8.1) I was able to add data source via PhPStorm/PyCharm using valid credentials but php artisan migrate ended up with SQLSTATE[HY000] [2002] Connection refused error every time.
I had to stop MySql service from XAMPP control panel and setup DB and USER via mysql-cli
after stopping services from XAMPP, do sudo service mysql start and then:
sudo mysql -u root
SHOW DATABASES;
CREATE USER 'elite'#'localhost' IDENTIFIED BY 'elite';
CREATE DATABASE laravel
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE
TEMPORARY TABLES, LOCK TABLES ON laravel.* TO 'elite'#'localhost';
exit
Damn, I feel so stupid lol.
This is what helped me after scratching my head for hours.
Check what port your MySQL is running on and that's the one you use in your .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=laravel
DB_USERNAME=yourusername
DB_PASSWORD=yourpassword
Make sure when you create your user that you grant all privileges for the user. If you're still confused then ask me and ill find time to help you out. Happy coding everyone!
I was using docker compose file. All you need to do is to expose the port of the mysql
db:
image: mysql
command: --default-authentication-plugin=mysql_native_password
restart: always
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: example
MYSQL_USER: example
MYSQL_PASSWORD: example
ports:
- 3306:3306
adminer:
image: adminer
restart: always
ports:
- 8080:8080
It was actually this in my case.
DB_HOST=127.0.0.1
DB_PORT=33060
Before it was
DB_HOST=127.0.0.1
DB_PORT=3306
Many solutions were suggesting to change DB_HOST to "localhost". In my case it was just a tiny little 0 in DB_PORT.
Hello dear StackOverflow community!
This is my first time operating a Laravel deployment, and I'm running into issues that probably have a simple answer that I couldn't find after many hours of research.
Laravel is returning an error SQLSTATE[HY000] [2002] No such file or directory when I look at my production homepage, and I was able to track down the issue with the Database by using the following SSH command:
mysqladmin -u root -p status
Which returns:
mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)'
Check that mysqld is running and that the socket: '/var/run/mysqld/mysqld.sock' exists!
So it seems Laravel logs into my local .env values, yet I only have one .env file that is configured for production on the server:
DB_CONNECTION=mysql
DB_HOST=my_distant_mysql_server_domain
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=user_name
DB_PASSWORD=xxxxxxxx
I also configured database.php, to no avail:
'default' => env('DB_CONNECTION', 'mysql'),
[...]
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'my_distant_mysql_server_domain'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database_name'),
'username' => env('DB_USERNAME', 'user_name'),
'password' => env('DB_PASSWORD', 'xxxxxxxx'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Is anyone here aware of something I might have missed?
I also tried all the resolutions offered in this thread, but my issue persists:
Laravel 5 app keeps using old database connection
Unfortunately I have to use the mysql server domain name, and not the IP, as my hosting doesn't provide any IP.
Thank you very much for your help on this matter :)
Thanks to Chris, I realized this issue came from the fact I didn't remove the socket config while trying to connect to a remote database, before then (important step!) re-running the following commands:
php artisan cache:clear
php artisan config:clear
php artisan config:cache
php artisan route:clear
php artisan route:cache
Doing those two steps solved my issue :)
I have a problem with Laravel. My website has no problem accessing and performing operations on the database but when I try to run
php artisan migrate
On a terminal I get the error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost'
(using pas sword: NO) (SQL: select * from
information_schema.tables where table_schema = test and
table_name = migrations)
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost'
(using pas sword: NO)
I have read every thread on the Internet about this error that I could find and none of them answered my question. It was always because people did not enter their database information correctly... However that is not my case since everything works fine in browser, I just cannot perform database related operations via the console like migrating or creating new database entries via artisan tinker.
Here is what my config/database.php file:
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
]
And my .env config:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=
I am stuck on figuring this problem out, so I appreciate your help a lot. I'm running XAMPP on my local machine and its on macOS 10.1.
Root is username for system administrator for linux and Mac base Os. "php artisan" command is not set to run under 'root' user. Try to run under non administrator(non root) user
In my case, I've just set the DB_PASSWORD and it worked!
Like this:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=bacon
DB_USERNAME=root
DB_PASSWORD=secret
The problem for me was that the password contained special characters, and so had to be quoted in the .env file:
DB_USERNAME={user}
DB_PASSWORD="{password}"
For those who are still having this problem.
Laravel command lines depend on the command mysql being accessible globally. Mine was not, I was using the absolute path to link to it because I am using XAMPP.
I corrected this issue by making the mysql command available globally. With a XAMPP installation in Mac just run this command in your terminal:
export PATH=/opt/local/bin:/opt/local/sbin:/Applications/xampp/xamppfiles/bin:$PATH
This should make mysql globally available and fix your migration problems.
I followed the following steps and I was able to overcome this issue-
Created another user in mysql other than root.
Grant all on . to 'newuser'#'localhost'
Changed the user in .env file
Made the mysql exe file location as a value in the PATH system variable in windows
Restarted the system
Restarted the wamp development server
php artisan cache:clear
php artisan config:cache
Now the next thing to check was the mysql port
Enter via terminal to mysql:
mysql -u root -p
and then type the following in the mysql prompt:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
This showed that the port was 3308 and not 3306 in my windows 10 machine
Changed the port in .env file
Again php artisan clear:cache
php artisan config:cache
Now gave the command- php artisan migrate:fresh --seed
Finally it was successful.
None of the other answers worked for me. I solved the issue by simply adding a password to DB_PASSWORD= in .env, which was empty initially.
I'm on a Mac OS Yosemite using Laravel 5.0.
While in my local environment, I run php artisan migrate I keep getting :
Access denied for user 'homestead'#'localhost' (using password: YES)
Configuration
Here is my .env
APP_ENV=local
APP_DEBUG=true
APP_KEY=*****
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
app\config\database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'unix_socket' => '/tmp/mysql.sock',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
How do I avoid this kind of error ?
I've tried :
1
in app/database.php
Replace localhost with 127.0.0.1
'host'=> env('DB_HOST', 'localhost') -->'host' => env('DB_HOST', '127.0.0.1')
Also, in .env
DB_HOST=localhost --> DB_HOST=127.0.0.1
2
Try specify environment
php artisan migrate --env=local
3
Check to see if the MySQL is running by run
mysqladmin -u homestead -p status Enter password: secret
I got
Uptime: 21281 Threads: 3 Questions: 274 Slow queries: 0 Opens: 327 Flush tables: 1 Open tables: 80 Queries per second avg: 0.012
Which mean it's running.
4
Check MySQL UNIX Socket (This step work for me)
The reason of Access denied for user ‘homestead’#’localhost’ laravel 5 error is caching-issue of the .env.php file cause Laravel 5 is using environment based configuration in your .env file.
1. Go to your application root directory and open .env file (In ubuntu may be it’s hidden so press ctrl+h to show hidden files & if you are in terminal then type : ls -a to show hidden files) in your editor and change database configuration setting. then save your .env file
DB_HOST=localhost
DB_DATABASE=laravelu
DB_USERNAME=root
DB_PASSWORD=''
2. then restart your apache server/web server. and refresh your page and you have done
3. If still issue try to run below command to clear the old configuration cache file.
php artisan config:clear
Now you are done with the error
TLDR: You need to stop the server Ctrl + c and start again using php artisan serve
Details:
If you are using Laravel, and have started local dev server already by php artisan serve
And after having above server already running, you change your database server related stuff in .env file. Like moving from MySQL to SQLite or something. You need to make sure that you stop above process i.e. Ctrcl C or anything which stop the process. And then restart Artisan Serve again i.e. y php artisan serve and refresh your browser and your issue related to database will be fixed. This is what worked for me for Laravel 5.3
Two way to solve it
First way (Not recommended)
Open your database config file (laravel_root/config/database.php) & search for the below code block.
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'blog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
Change the code block as below
'host' => 'yourHostName',
'database' => 'YourDatabastName',
'username' => 'YoutDatabaseUsername',
'password' => 'YourDatabasePassword',
Second way (Recommended by Laravel)
Check your Laravel root there have a file call .env if not exist, look for .env.example, copy/rename it as .env after that the file looks blow !
APP_ENV=local
APP_DEBUG=true
APP_KEY=someRandomNumber
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
Modify the below block as follow
DB_HOST=yourHostName
DB_DATABASE=yourDatabaseName
DB_USERNAME=yourDatabaseUsername
DB_PASSWORD=youPassword
Now it will work fine.
if you are using php artisan migrate NOT from vagrant box but from host machine then you must define inside the .env the ip of the box 192.168.10.10
and obviously set permission to homestead user from your ip
something like
grant all privileges on *.* to 'homestead'#% identified by 'secret';
in .env file
DB_HOST=192.168.10.10
DB_DATABASE=homestead
DB_USERNAME=homestead
If you are using the PHP's default web server (e.g. php artisan serve) you need to restart your server after changing your .env file values.
I had the same issue and in the end It turned out that I just had to restart the server and start again
Ctrl + c then
php artisan serve
When you install Homestead, this creates a default "homestead"
database in the VM. You should SSH into the VM homestead ssh and run
your migrations from there. If you are working locally with no VM,
you'll need to create your database manually. By default, the database
should be called homestead, the username is homestead and the password
is secret.
check this thread on laracasts or this blog post for more details
if you are getting
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
try changing "host" in the /app/config/database.php file from "localhost" to "127.0.0.1" . you can find more details and other fixes here on this thread.
Also check whether you have specified the correct unix_socket . check this thread .
Check MySQL UNIX Socket
Find unix_socket location using MySQL
mysql -u homestead -p
mysql> show variables like '%sock%';
+-----------------------------------------+-----------------------------+
| Variable_name | Value |
+-----------------------------------------+-----------------------------+
| performance_schema_max_socket_classes | 10 |
| performance_schema_max_socket_instances | 322 |
| socket | /var/run/mysqld/mysqld.sock |
+-----------------------------------------+-----------------------------+
3 rows in set (0.00 sec)
Then I go to config/database.php
I update this line : 'unix_socket' => '/tmp/mysql.sock',
to : 'unix_socket' => '/var/run/mysqld/mysqld.sock',
That's it. It works for my as my 4th try.I hope these steps help someone. :D
In Windows PC Follow below.
Access the Root folder of your application.
Edit the .env file
Edit the Highlighted and change the UserName and the password adn The database Name accordingly.
in my case,
after restarting my server the problem was gone.
exit/close the "php artisan serve" command and
re-run the command "php artisan serve" command.
in my case (laravel 5+) i had to wrap my password in single quotation marks and clear config cache:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=user_name
DB_PASSWORD='password'
and then run:
php artisan config:clear
Sometime in the future. Try to clear your config first
php artisan config:clear.
Close all the terminal /cmd windows and then restart terminal/CMD and this should get rid of the error message. See if it works.
after config db restart the:
php artisan serve
If the serve is active before set db config.
i using laravel 5.* i figure i have a file call .env in the root of the project that look something like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
And that was over writing the bd configuration so you can wheather deleted those config vars so laravel take the configuration under /config or set up your configuration here.
I did the second and it works for me :)
Got it! Log in as root and grant homestead#localhost the rights to everything.
From your terminal:
$ homestead ssh
$ mysql -u root -p
Enter password: secret
mysql> grant all privileges on *.* to 'homestead'#'localhost' identified by 'secret';
Query OK, 0 rows affected (0.00 sec)
exit
Now homesteads regular user has access to all of your tables, and as such, should be able to run things like migrations.
After change .env with the new configuration, you have to stop the server and run it again (php artisan serve). Cause laravel gets the environment when it's initialized the server. If you don't restart, you will change the .env asking yourself why the changes aren't taking place!!
All you have to do is alter your .env file.
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
In front of DB_DATABASE, write the name of the database and in front of DB_USERNAME, use root.
I just wanted to post this because this issue frustrated me for about 3 hours today. I have not tried any of the methods listed in the other answers, though they all seem reasonable. In fact, I was just about to try going through each of the above proposed solutions, but somehow I got this stroke of inspiration. Here is what worked for me to get me back working -- YMMV:
1) Find your .env file.
2) Rename your .env file to something else. Laravel 5 must be using this file as an override for settings somewhere else in the framework. I renamed mine to .env.old
3) You may need to restart your web server, but I did not have to.
Good luck!
You have to run the $ php artisan migrate command from within Homestead, not your Mac.
Check your ".env" file in the root folder. is it correct?
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
i have find solution
Go to your application root directory and open .env file (In ubuntu may be it’s hidden so press ctrl+h to show hidden files) in your editor and change database configuration setting. then save your .env file
then restart your apache server/web server. and refresh your page and you have done
If still issue try to run below command to clear the old configuration cache file.
This worked for me gl ...
just change these things in the .env file of your root folder.
DB_DATABASE=your_db_name
DB_USERNAME=your_db_user_name
DB_PASSWORD='your_db_password'
It worked for me.
Log into MYSQL - use the mysql database.
Select * from User;
Make sure that your HOST column is correct. It should be the host that you are connecting from (your application server) be it IP address, or DNS name. Also '%' will work (meaning wildcard) but will not be secure.
Check your .env file, if you have edited any of the variables, kindly restart laravel server, and your problem will be solved
A. After updating the .env file with database settings, clear laravel setting by "running php artisan config:clear"
B. Please make sure you restart your apache server / rerun the "php artisan serve" command for your settings to take effect.
I had the same issue using SQLite. My problem was that DB_DATABASE was pointing to the wrong file location.
Create the sqlite file with the touch command and output the file path using php artisan tinker.
$ touch database/database.sqlite
$ php artisan tinker
Psy Shell v0.8.0 (PHP 5.6.27 — cli) by Justin Hileman
>>> database_path(‘database.sqlite’)
=> "/Users/connorleech/Projects/laravel-5-rest-api/database/database.sqlite"
Then output that exact path to the DB_DATABASE variable.
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=/Users/connorleech/Projects/laravel-5-rest-api/database/database.sqlite
DB_USERNAME=homestead
DB_PASSWORD=secret
Without the correct path you will get the access denied error
If you have an error returning something like PDOException in Connector.php line 55: SQLSTATE[HY000] [1049] Unknown database 'laravelu' is due to you are changing your batabase config as DB_DATABASE=laravelu. So for now you either:
Change the DB_DATABASE=[yourdatabase] or
create a database called laravelu in your phpmyadmin
this should be able to solve it
In my case the error was "caused" by my Homestead / Vagrant configuration about which I forgot :) trying to run the migration just from command line.
In case of using Homestead Vagrant Box environment you should run your migration from within your Homestead machine (after connecting to it using ssh: vagrant#192.168.10.10:22) then the access rights will be OK and will allow the migration to run.
From your question, it seems you are running homestead. In that case, make sure you're running the commands in your VM. Many devs including me often make mistake and run artisan commands outside of VM which the commands will try to connect to our local database accessible through localhost which is different from the database used by homestead. Cd to your Homestead directory and run
vagrant ssh
then cd into code if that is where you keep your projects and cd into your project and run php artisan migrate again
I hope this will help other people.
My app had been set up to use .env.local. I was checking .env the whole time.
Check that you are editing the correct .env file if you have several.