8

I have XAMPP installed and turned on which is how I access my phpmyadmin page. I have created a database in phpmyadmin called "firstdb".

I have also created auth in laravel files stored locally. I am trying to migrate tables using php artisan migrate and I am getting the error below.

user@Andress-MacBook-Pro admin-panel % php artisan migrate

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = firstdb and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675| 

      +37 vendor frames 
  38  artisan:37
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

I have looked everywhere, please help.

Andres Osorio
  • 81
  • 1
  • 1
  • 3
  • 1
    The error here is "Connection refused". The query it's trying to run is incidental to that, so your title isn't going to help you get an answer. Presumably, this is just a configuration issue in your Laravel setup. – Greg Schmidt Apr 16 '20 at 04:02
  • 1
    To add to what Greg has said, check that database host, username, password, etc. are correctly set based on what your XAMPP environment requires. – Coder1 Apr 16 '20 at 04:18

8 Answers8

12

It could be the wrong mysql port number in your .env file. Check your .env file and make sure the port number matches the same one mysql is using.

Nelson Katale
  • 1,309
  • 15
  • 21
  • 2
    yes, to add to what Nelson said, always make sure you add the correct values in .env file and also add the such connection and enviornment specific variables in .env file. – Muzaffar Shaikh Jul 10 '20 at 10:44
  • 2
    Since you are using XAMPP, the answer may be different. For me, I am using Homestead, my solution was simple. Change the .env DB_Username to homestead and the DB_Password to secret as per Laravel's documentation. Then I could do the migration. So just set in your .env files the same credentials that you used to connect to your XAMPP database. – El Sordo Nov 22 '20 at 16:30
  • Good answer @MichaelBerrios. – Nelson Katale Nov 23 '20 at 12:53
  • 1
    @NelsonKatale glad that it helped. – El Sordo Nov 26 '20 at 01:07
  • 1
    yes, in my case, in .env was 3306 and docker was 3307... – Edward Ramos May 25 '22 at 21:37
  • Thank you @Nelson Katale. This needs to be marked as the answer. Just like Edward Ramos I was using docker which had an alternate port number. Easy fix. – waltmagic Jun 22 '22 at 03:39
5

I noticed something weird regarding the password that was in the command line output vs what was actually in the env file. Turned out I had a number sign in my password and it was getting cut off.

So check the output after you run php artisan migrate and make sure that the password actually matches the password in your env (and all information for that matter). The fix is to just add quotes if there is a mismatch.

DB_PASSWORD=abcdefghijklmonp5#Q

would become

DB_PASSWORD='abcdefghijklmonp5#Q'

Richard
  • 5,584
  • 1
  • 19
  • 22
  • I had already created multiple other mysql databases on the same server without a quoted password and migrated successfully. But this time this same error was coming up and was solved using the quote even after verifying the password matched exactly. Really weird and inconsistent behavior. Thanks for this suggestion – Udo E. Aug 18 '23 at 05:03
1

In addition to making sure all of your configs are correct escaping password with quotes I ran into a similar issue for a clean install on linux.

SQLSTATE[HY000] [1045] Access denied for user 'username'@'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = studentaffairs and table_name = migrations and table_type = 'BASE TABLE')

To solve the issue I made sure to run composer update and for good measure I ran npm run dev

Then it was able to migrate without issue.

happymacarts
  • 2,547
  • 1
  • 25
  • 33
1

Try to set your DB_HOST on your .env to

DB_HOST=localhost
Dharman
  • 30,962
  • 25
  • 85
  • 135
Kibiku Brian
  • 221
  • 2
  • 11
0

Try to set on php.ini and search:

;extension=pdo_mysql.so

Delete ";" to uncomment

1

vimuth
  • 5,064
  • 33
  • 79
  • 116
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 03 '22 at 08:41
0

First, check the the user plugin:

mysql> SELECT User,Host,plugin FROM mysql.user WHERE user='root';

If the user is using caching_sha2_password change to mysql_native_password, because PHP doesn't yet understand caching_sha2_password

mysql>ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
0

In order to set your database host, you need to edit the .env file and specify the desired hostname instead of IP address. To do this, locate the DB_HOST variable in the .env file and assign it the value of "localhost". This will instruct your application to connect to a local database server, which is typically running on the same machine as the application itself.

For example, you might see a line like this in your .env file:

DB_HOST=

To set the hostname to "localhost", you would simply update the line to read:

DB_HOST=localhost

Once you have saved your changes to the .env file, your application should be able to connect to the local database server using the hostname or IP address that you specified.

Omkar Ghurye
  • 195
  • 1
  • 8
-3

change DB_CONNECTION=127.0.0.7 TO mysql in .env file

steven
  • 33
  • 7