17
votes

so I want to deploy my existing laravel porject into my digitalocean vps I user this tut and I upload my site successfully

also 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 what is the problem

[Illuminate\Database\QueryException] SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s chema.tables where table_schema = form and table_name = migrations)

and

[PDOException] SQLSTATE[HY000] [2002] Connection refused

10
Have you defined your database credentials inside .env file?Saumya Rastogi
Take a look here laravel.io/forum/…Froxz
@Froxz seen it already didn't help and his problem was about homesteadMohammad Reza
@SaumyaRastogi yes I attached itMohammad Reza
@MohammadReza check your DB / username/ password check if mysql is runing Connection refused the error it just can't connect, maybe your user doesn't have enough access and etc.Froxz

10 Answers

32
votes

In your .env file, change DB_HOST from 127.0.0.1 to localhost

7
votes

Put string:

DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock

after DB_PASSWORD=

It works for MAMP. Set your path to mysql.sock

3
votes

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.

3
votes

In your .env file, change DB_HOST from 127.0.0.1 to localhost

and then

  1. php artisan cache:clear
  2. php artisan route:clear
  3. php artisan config:clear
  4. php artisan view:clear

    This worked for me.

1
votes

SELinux might be the cause.

Set this rule and try again:

sudo setsebool -P httpd_can_network_connect_db=1
1
votes

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
0
votes

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.

0
votes

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
0
votes

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
0
votes

Before you do changes as per instructed above,

  1. 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.
  2. 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 '%'
  3. 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
  4. If you reach here and still got the same error, try to restart you web server.