I have created a new laravel project in laravel 8. I have set up Sail installation correctly, everything working fine for database connection in local but when I try to connect my database using sail it gives me an error
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel8 and table_name = migrations and table_type = 'BASE TABLE')
I have already cleared my config and cache files.
I was researching the same problem and found this.
https://github.com/laravel/sail/blob/7457004969dd62fa727fbc596bb2accccb1409a5/src/SailServiceProvider.php#L31
The code shows an artisan command added by laravel sail - sail:install which overwrites your .env file host variables with service names.
I changed DB_HOST from 127.0.0.1 to mysql and it was fixed
On cmd just run this command
docker network inspect bridge
You will get response like (similar) this:
[
{
"Name": "bridge",
"Id": "695c284c7b184839edf97d48161922dfe6c4827d0db917c3278f5634af7e00e2",
"Created": "2022-02-22T22:17:30.8583513Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "172.17.0.0/16",
"Gateway": "172.17.0.1"
}
]
},
.....
}
]
Copy "Gateway" ip address and replace with DB_HOST value in .env file. It will work
if you want run mysql in different port for example 3307 like below
ports:
- 3307:3306
you should add FORWARD_DB_PORT environment variable to .env file like this
DB_PORT=3306 // this is for container port
FORWARD_DB_PORT=3307 // this is for local port
Changing DB_USERNAME from sail to root worked for me. I guess sail did not have the required privileges.
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root # changed from sail
DB_PASSWORD=password
I had this same issue after creating a second sail project in my docker. It wasn't (still isn't) accepting the sail username or password but I could go in with root which gave me the error you gave above. I went to the Desktop Docker app, clicked the running container, found the mysql portion and ran the CLI, typed mysql and then ran the query CREATE DATABASE yourdatabasename; migration ran perfectly fine.
Maybe you should match the environment variables of .env.testing, since, as willpercey-gb says, the sail:install command updates the environment variables of .env, but not of .env.testing.
First off
stop all services using brew especially MySQL
brew services stop MySQL
in your docker-yaml file:
laravel.test
ports:
- '70:80'
mysql:
ports:
- '3456:3306'
Now run your sail artisan migrate command.
this should solve your problem.
Related
I've attempted to access my PostgreSQL database from both a Laravel project, DBeaver and Artisan. Accessing it from Laravel project or Artisan ($ php artisan migrate) always results in:
Illuminate\Database\QueryException
SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?
Because I'm not relying in my Laradock settings and I'm not familiar with Laravel in general, I did re-download Laradock with default settings, but situation continues same.
Partial laravelProject/.env:
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=default
DB_USERNAME=default
DB_PASSWORD=secret
Partial laradock/.env:
POSTGRES_VERSION=alpine
POSTGRES_DB=default
POSTGRES_USER=default
POSTGRES_PASSWORD=secret
POSTGRES_PORT=5432
Environment used to run Artisan:
$ docker-compose exec --user=laradock workspace bash
I installed Laravel in this environment using composer global require laravel/installer.
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=yourprojectdatabase
DB_USERNAME=default
DB_PASSWORD=secret
To connect using DBeaver:
Host: localhost
Port: 5432
Database: you can leave it empty
Username: default
Password: secret
Click on the PostgreSQL tab right there in Connection.
Make sure "Show all databases" is checked.
I have read a lot of posts regarding artisan migration issues, but unfortunately none of them are applicable to my situation.
So I am running Laravel Homestead via Vagrant and I have created a new database in the Vagrant VM.
On my Ubuntu desktop I can connect to the database via DBeaver and it shows the database. Because I am running the database in a Vagrant VM, I am connected to it through 192.168.10.10 (specified in the Homestead.yaml file) and not localhost (127.0.0.1). So far so good.
I created a migration file by running the following command: php artisan make:migration create_todo_table. However, when I run php artisan migrate I get the following exception error:
Illuminate\Database\QueryException
Could not find driver (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = 'BASE TABLE')
The data regarding the database in my .env file is as follows:
DB_CONNECTION=mysql
DB_HOST=192.168.10.10
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
I have tried clearing the cache by running php artisan config:cache but had no luck.
Can someone give me some insight, please?
DB host inside your vagrant should be localhost or 127.0.0.1 because project and database both are inside vagrant but DBeaver is outside vm so 192.168.10.10 is okay for DBeaver.
DB_HOST=localhost
I got in fixed by editing my php.ini file.
For Windows I changed ;extension=php_pdo_mysql to extension=php_pdo_mysql.dll.
For Ubuntu I changed it into extension=pdo_mysql.so.
After that, I still got an error stating that the connection was actively refused. I fixed this by changing DB_HOST=localhost to DB_HOST=192.168.10.10.
Hope this will help someone someday!
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.
I moved the Laravel project from localhost to server. Which I have done every step on the server.
I am able to view the login page on my server. The problem is I am not able to connect with my MySQL server.
My .env file:
APP_NAME=Transport
APP_ENV=local
APP_KEY=base64:mrakeyidharhaikonsdf
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=transport_db
DB_USERNAME=root
DB_PASSWORD=mypass
I tried to change the host to 127.0.0.1 and also tried to put my server's IP address. It didn't helped me. Am I missing something?
My error:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select count(*) as aggregate from users where email = user.email#gmail.com)
I know this question may have answers already on Stack Overflow. But I have different issue here.
To be honest, while working on Laravel 6, I also faced this issue many times and probably was unable to figure out the solution. I tried commands like
php artisan config:cache and php artisan config:clear, but these two commands didn't help.
To come over this issue, you have to execute all the below commands:
php artisan route:cache
php artisan route:clear
php artisan config:cache
php artisan config:clear
php artisan optimize
Note: Make sure in .env you have add db_password and it is not null and also check if your db_password uses any special character. Always enclose them in "".
Example:
DB_PASSWORD="%123456%"
To debug this issue you can also create a test route and then dump the .env variable there to check if they have the correct values or not.
Route::get('/test/env', function () {
dd(env('DB_DATABASE')); // Dump 'db' variable value one by one
});
Note: Please make sure to restart your server.
Make sure your database credentials and database host are set correctly:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"
If you have not set any database password then add:
DB_PASSWORD=""
The problem is that the project made a cache file. 'config.php' must be deleted in order to allow the system to restart and you can do so by locate this file and deleting it:
bootstrap/cache/config.php
I was working in the Laravel framework using homestead and had such a problem. In fact, this error was because I did not create the database in Vagrant. So, I created the database. In more details:
run vagrant ssh to get homestead command line.
run mysql to start the MySQL command line client.
run create database database_name; to create your database. Note that the database should be inside two ` (not ') marks.
Modify the .env file of the Laravel project as below:
DB_CONNECTION=mysql
DB_HOST=192.168.10.10
DB_PORT=3306
DB_DATABASE=askidb2
DB_USERNAME=homestead
DB_PASSWORD=secret
(if you previously made your model and configured files in Laravel projects for creation tables), run php artisan migrate in the command line of the project's folder. This causes it to create tables in the askidb2 database.
(perhaps optional for you) since before I set needed files for seeding the question table I just run php artisan db:seed to seed my desired table(s).
If you use Homestead to serve your application, you must connect to the Homestead built-in database. Otherwise, I get an error (SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost') when I try to connect to the MySQL database that is installed on my Mac.
Here is how I connect to the Homestead database. I am not sure why the host → 127.0.0.1 from the Laravel documentation doesn't work.
In the .env file
DB_CONNECTION=mysql
DB_HOST=192.168.10.10
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=homestead
DB_PASSWORD=secret
I use Sequel Pro as the GUI to manage the database, and here is how I connect Sequel Pro to the Homestead MySQL database.
Connect with standard mode
Host: 192.168.10.10
User: homestead
Password: secret
Port: 3306
Connect with SSH mode
So the problem is mine. I apologize for that.
I missed to put my DB_PASSWROD inside "". So that caused my two hours.
I have the password which contains some special characters at the beginning. So it should be inside the "" to avoid the problems.
For example, if my password is "%helloworld#",
then in the .env file, it should be:
`DB_PASSWORD="%helloworld#" `
Previously, I had
`DB_PASSWORD=%helloworld#
I had this error while my connection with the database was established. The problem was that I was on localhost, not 127.0.0.1.
First
DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"
Then type
php artisan config:clear
This will make your problem clear. Then type
php artisan migrate
I had this error because my XAMPP user Account access for root and password did not match Laravel .env database DB_USERNAME & DB_PASSWORD.
This will Work:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="your_database_name"
DB_USERNAME="put_db_user_name _here"
DB_PASSWORD="put_db_password_here_if_have_set"
This may also be due to the DB_PORT. Go to the PhpMyAdmin Database server and check the server.
So I changed from this
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
to
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
Change the .env as follows
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="Your DB name"
DB_USERNAME=root
DB_PASSWORD=
and then run php artisan config:cache
and then run php artisan migrate:fresh --seed
and then run php artisan serve
Please make sure to have the same db name in .env file and db name in php myadmin
Kindly check your mysql port no as well.
If you have changed your xampp mysql port, then do check PORT Number in your projectfolder ".env" file as well as "database.php" file.
In my case, if I have Bitnami WordPress. It's a service using an Apache/localhost address, so my XAMPP instance is only accessible via 127.0.0.1 and when accessing localhost I got 'Access denied error ...'. Then when I stop the Bitnami service, everything is back to normal.
so I want to deploy my existing Laravel project into my digital ocean VPS
I used this tutorial and I uploaded my site successfully.
My .env file is:
APP_ENV=local
APP_KEY=my app key
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=my ip
DB_PORT=3306
DB_DATABASE=form
DB_USERNAME=root
DB_PASSWORD=my pass
But here is the problem: I used MySQL and created and ran
php artisan migrate
and got these errors:
[Illuminate\Database\QueryException] SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = form and table_name = migrations)
and
[PDOException] SQLSTATE[HY000] [2002] Connection refused
In your .env file, change DB_HOST from 127.0.0.1 to localhost
Put string:
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
after DB_PASSWORD=
It works for MAMP. Set your path to mysql.sock
In your .env file, change DB_HOST from 127.0.0.1 to localhost
and then
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear
This worked for me.
There are a few possibilities why your getting this error.
database server isn't started, check if you can still use your phpmyadmin. If you can still use it then nothing is wrong with your database server.
wrong config; check username, password and server settings in both your .env file and your config/database.php
no privileges to the server and or database with given credentials
Looking at the steps you already took I think it's the last.
Check to see if this user has access to the database from the remote location. By default the root user has access to the server locally (localhost,127.0.0.1 and ::1).
So check to see if the user has access from either your remote ip or from anywhere.
As you are using digitalocean I would suggest to check if they have a specific configuration to connect to mysql database.
SELinux might be the cause.
Set this rule and try again:
sudo setsebool -P httpd_can_network_connect_db=1
After long hours this worked for me:
Changing the port in php My Admin previously 8889
APP_NAME=laravel
APP_ENV=local
APP_KEY= YOUR KEY
APP_DEBUG=true
APP_URL=127.0.0.1
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
and then
php artisan cache:clear
php artisan route:clear
php artisan config:clear
php artisan view:clear
Before you do changes as per instructed above,
check if your database server running or not. If you are using command prompt, running mysql.server status or mysqld status shall show the database status.
If the database is running, check if the dbuser is allow to connect from '%' or 'localhost' or '127.0.0.1'. If not, just grant the user access from '%'
If you have done 1 and 2 and still can't connect, see if the db user is allow to access the schema. If not, grant access to the schema
If you reach here and still got the same error, try to restart you web server.
My composer was installed globally, i run php artisan serve without starting xampp & getting this error. After running my xampp server, it's working fine from my side.
In attempt to provide a solution for those of you that have not found success in any of the already mentioned answers, I will share my solution.
Coming from Windows 10 I was of the understanding that the password for the root user was always blank thus my .env file looked as follows:
DB_USERNAME=root
DB_PASSWORD=
On Mac, the password is defaulted to "root" also. Changing my .env file to look as follows fixed my issue:
DB_USERNAME=root
DB_PASSWORD=root
in your .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ur_db_name
DB_USERNAME=ur_db_username
DB_PASSWORD=ur_dbuser_password
If you are using XAMPP on Mac, just use the embedded IP Address from your XAMPP Dashboard as DB_HOST instead of 127.0.0.1 or localhost.
If It still refuses to connect deal with a user but stick on that idea.