how can I solve laravel php artisan migrate error problem? - php

I'm using laravel socialite to make facebook login, following this website: Social Logins with Socialite
as soon as I type a command in the terminal:
php artisan migrate
This shows the error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

Open the .env file and edit it. Just set up correct DB credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= // Your Database Name
DB_USERNAME= // Your Database Username
DB_PASSWORD= // Your Database Password
The DB_USERNAME should be set to root if you do not have a default username
If no password is set on the database, clear it DB_PASSWORD
After completion of .env edit must be clear cache: php artisan config:cache
After Run the migrate Artisan command: php artisan migrate

The database called laravel cannot be found in PhpMyAdmin
Please follow these steps to solve this problem :
Go to phpmyadmin in your localhost dashboard
Create a database and name it laravel
run the php artisan migrate CMD or Terminal of your editor

I had the same issue But changing localhost with 127.0.0.1 solved it for me
DB_CONNECTION=mysql
DB_PORT=3306
DB_HOST=127.0.0.1
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Question is not about facebook login.
You migration command could not establish connection with your database actually.
Check you .env file, you need to have something like this
DB_CONNECTION=mysql
DB_PORT=3306
DB_HOST=localhost
DB_DATABASE=laravel // This is line which you need to update, set here the real database name
DB_USERNAME=root
DB_PASSWORD=
If you have not file .env you can rename .env.example to .env, setup you db configs, and you will be able to migrate your tables with migrate command.
If you want to call you db as laravel, it means you forgot to create that database, first create database using command line with mysql or any application which can work with mysql (phpMyAdmin, DataGrip, phpStorm, jetbrain Db, etc)

Looks like you forgot to setup your Laravel application.
You can specify database connections and other basic settings in your .env file in the root of your project.

At first stance this points to a problem in the .env and ./config/database.php files. In the .env file you should have something like this
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
and in ./config/database.php something like this
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DATABASE_HOST','localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DATABASE_NAME','laravel'),
'username' => env('DATABASE_USERNAME','root'),
'password' => env('DATABASE_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'),
]) : [],
],
If your console were showing you something like this
Database hosts array is empty. (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')
you had to ensure to have specified the host. This would've happen if you had in ./config/database.php something like this (notice that it doesn't refer 'localhost' like before)
'host' => env('DATABASE_HOST'),
Assuming all is fine here and you have the database name you want to use, then it means that your database doesn't exist. Just go to phpmyadmin, or so, and create one.

The issue for me was the database.php config file did not have the PORT config, so it was default to 3306, but my database used a different port. I just added the line of config to the mysql config:
'mysql' => [
'port' => env('DB_PORT', '3306')
]

Solution founded. I had the same problem. the reason for the problem is there is no any database called "laravel" in phpmyadmin. It may be deleted or renamed. So check whether there is a database called 'laravel'. If it's not present create database in phpmyadmin using the name,
laravel
After that run the command,
php artisan migrate
This worked for me

As you can see from the answers above that you need the change the name of the database in the .env file , make sure you are changing on the .env instead of .env.example file

php artisan route:cache
apply this command and solved my error

i solved the problem by writing the database name in single quotes...
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE='laravel'
DB_USERNAME=root
DB_PASSWORD=

I had the same issue But changing DB_HOST=127.0.0.1 to DB_HOST:localhost solved it for me
DB_HOST=localhost //DB_HOST=127.0.0.1 to DB_HOST:localhost solved
DB_PORT=3306
DB_DATABASE=laravel_multiauth
DB_USERNAME=root
DB_PASSWORD=

Related

Laravel 6.4.1 SQLSTATE[HY000] [2002] Connection refused

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.

Laravel Artisan access denied for root#localhost on console only

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.

Laravel 5 - SQLSTATE[HY000][1045] Access denied for user 'root'#'localhost' (using password: NO)

I am trying to make CRUD operations in laravel and I alway have this error message. I already configured the .env and the config/database.php files to my local setting but it doesn't work.
I tried php artisan cache:clear,php artisan config:clear too...
My config/database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', ''),
'database' => env('DB_DATABASE', 'feki_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,
],
My .env:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=
DB_DATABASE=feki_test
DB_USERNAME=root
DB_PASSWORD=
I was facing the same issue. But I found the solution by deleting the config.php file saved in the bootstrap/cache/config.php. After that simply run these commands
php artisan config:clear
php artisan cache:clear
php artisan config:cache
Based on the comments, you're trying to access the local xampp mysql server from within Homestead. This won't work as the application is in an isolated virtual machine. You will need to use Homestead's mysql server for your application. Use the default connection information for this.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
To access Homestead's server on your local machine change the port to 33060. This can be helpful if you want to use a SQL tool to connect to the database.
First of all, you need not write credentials in config/database.php.
then delete you .env file and then rename your .env.example file to .env
afterward run the PHP artisan key:generate command on your terminal .
give your database credential.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=feki_test
DB_USERNAME=root
DB_PASSWORD=
and the last restart your development server.
.
In .env and config->database.php file
Instead of this
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=feki_test
DB_USERNAME=root
DB_PASSWORD=
Use this
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=root
DB_PASSWORD=''
and then restart your server. It will work for sure
i changed 127.0.0.1 to localhost And its worked
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=efaf
DB_USERNAME=root
DB_PASSWORD=

Can't migrate in laravel

I made a small project in laravel using MAMP, and everything works fine.
Now I'd like to put the project online.
I copied the project with FTP and changed the database.php file.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'private.be.mysql'),
'database' => env('DB_DATABASE', 'private_be'),
'username' => env('DB_USERNAME', 'private_be'),
'password' => env('DB_PASSWORD', 'private'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
The installation works fine when visiting the website, but when visiting a page that uses a connection to the database I get following error.
SQLSTATE[42S02]: Base table or view not found:
1146 Table 'private_be.users' doesn't exist (SQL: select * from `users`)
So I found out I had forgot to migrate my project. But when typing the command 'php artisan migrate' I get the following error in terminal.
[ErrorException]
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: nodename
nor servname provided, or not known
At first I thought maybe my settings for 'mysql' were incorrect, but I'm sure they are.
My .env file looks like this
APP_ENV=local
APP_DEBUG=true
APP_KEY=***
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
Anybody who can help me fixing the migrate error?
Sounds like you didn't run your database migrations.
After you moved the files over, did you SSH into the server and go to your project directory and run the command
php artisan migrate
Running the above command will create all database tables you created migrations for. If you already have some data that needs to be rolled back first then rebuilt you can use:
php artisan migrate:refresh

how to connect to db with laravel 5

I updated my laravel4 to laravel5 now my problem is i cant connect to database
My mysql
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'lue'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ' '),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
i returns this problem
PDOException in Connector.php line 47:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
help :)
You need to edit your .env file in the root of your application and change the DB username and password there
Make sure you are editing the .env file, and not a .env.php file. In Laravel 4 there was a way to use .env.environment.php but in Laravel 5 it's all done with a .env file (note that it doesn't have a php suffix)
Also the data in the .env file must not be in the array format. It should just be as follows:
APP_ENV=local
APP_DEBUG=true
APP_KEY=longstring
DB_HOST=localhost
DB_DATABASE=dbname
DB_USERNAME=homestead
DB_PASSWORD=secret
I had the same problem and this is how I fixed it.Go to the root of your laravel folder (application folder) in my case it is C:\firstapp and open .env file in notepad++ Update the following code accordingly.
DB_HOST=localhost
DB_DATABASE=firstapp (this is database name)
DB_USERNAME=root (user name)
DB_PASSWORD= (password , leave empty if none , do not even put "")
Important
and then restart your application like this php artisan serve (using command line) while you are in the root of you application folder. See this image for clarification link.
You need to edit your .env file in the root of your application. Just configure as following example:
APP_ENV=local
APP_DEBUG=true
APP_KEY=6CqAQJdOd8oKoh0NhbefkmS0Pd8FDH6h
DB_HOST=localhost
DB_DATABASE=your database name
DB_USERNAME=your database user name
DB_PASSWORD=your password
CACHE_DRIVER=file
SESSION_DRIVER=file

Categories