I started working with Laravel recently and followed every step from this tutorial:
https://www.toptal.com/laravel/restful-laravel-api-tutorial
For now I create the model and configured the .env and database files.
.env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=bank
DB_USERNAME=root
DB_PASSWORD=
For me everything seems correct, MyPhpAdmin is running and I have added a bank database to it.
But every time I try to migrate I get the following error:
Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = bank and table_name = migrations)
This looks unrelated to Laravel, I think it's to do with PHP not being able to connect to MySQL correctly. See here - PHP - MySQL connection not working: 2002 No such file or directory
I add DB_SOCKET in .env file refer to my.cnf (socket) then run php artisan migrate. It works!
DB_SOCKET=/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock
And double check your config/database.php, comment or delete if there are other unix_socket except this 'unix_socket' => env('DB_SOCKET', '')
Related
when typing the command php artisan migrate, it is giving me an error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1049] Unknown
database 'proj' (SQL: select * from information_schema.tables where
table_schema = proj and table_name = migrations and table_type = 'BASE
TABLE')
Although, i edited the .env file and i had created the database proj and restart the cmd and also i tried
php artisan config:cache
and similar commands but still not working
view error image
phpmyadmin view
.env file view
This means that you don't have this database created. First create the database and after run: php artisan migrate:fresh to create the tables.
By the screenshots you have uploaded, you have many servers. Check the port of the server mysql and change the port on the .env file. This will work.
Open the .env file and edit:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= // db Name
DB_USERNAME= // db Username
DB_PASSWORD= // db Password
Run below commands:
php artisan config:cache
php artisan migrate
I hope this will help you
I created a new project and set up my database accordingly in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=example_olddb
DB_USERNAME=example_user
DB_PASSWORD=example_password
After much development, I want to change my database to a new database. So I edited the '.env' file again.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=example_newdb///<-- change here
DB_USERNAME=example_user
DB_PASSWORD=example_password
However, I got the following error.
SQLSTATE[HY000] [1049] Unknown database 'example_olddb'
It used to work in Laravel 5.5 and 5.6, but not now in 5.7. I tried to clear the cache, but same error persists. How can I resolve this issue?
After completion of .env edit, You can clear the configuration cache with the following artisan command: php artisan config:cache
I deleted the migrations table from a Laravel 5.4 database named laravel. When I run php artisan migrate:install, I get this error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] No such file or directory
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations)
I deleted and recreated the database. I also ran composer update. No luck. I can run the command in phpMyAdmin and create the table manually.
This problem sometimes also manifests itself with similar 2002 errors:
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations and table_type = 'BASE TABLE')
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
(SQL: select * from information_schema.tables where table_schema = laravel
and table_name = migrations and table_type = 'BASE TABLE')
If you are using localhost as your DATABASE_HOST in the .env file, change it to 127.0.0.1, then run php artisan config:clear and now try php artisan migrate:install again.
Here's what I recommend for .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
Then in Database.php add folder location:
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
Finally, try:
php artisan config:clear
php artisan migrate:install
I fixed this issue by setting environment variables in .env file:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
Actually I just changed from DB_HOST=127.0.0.1 to DB_HOST=mysql.
If you are using MAMP Pro (not always necessary for the free version of MAMP) then the best thing to do is add this to your .env file:
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
This assumes your MAMP installation is at /Applications/MAMP of course (the default).
Credit: A comment in this question by #Uncoke
I am using MAMP on macOS and after editing localhost to 127.0.0.1 and port to 8888 this problem fixed by adding the following
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
to config/database.php file.
For mac users, add this to your .env file
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
In some cases this may happen because mysql server is not running. It happened to me using Laravel 5.7 and restarting mysql server solved it. For ubuntu check the status of mysql server service mysql status. You can restart mysql server using command service mysql restart
Posting a resolution to my own question:
I deleted the folder and recreated the code base, making sure to point my environment to the correct database server. This time it worked. I don't know exactly what had gone missing to cause the original error.
I want to use mysql database with my laravel 5.2 framework. I'm not able to access phpmyadmin after I run 'php artisan serve' and open a localhost page.
My .env file :
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=people
DB_USERNAME=pftest
DB_PASSWORD="pftest_2016#9"
After doing this, I ran 'php artisan migrate' and got following error:
[PDOException] could not find driver
Following which, I have installed php-mysql extension and checked for its presence in php.ini file. But still I'm getting following error :
[PDOException] SQLSTATE[HY000] [2002] Connection refused
So what is the issue and how to solve it ?
I was told to check for php.ini locaton. I did,and after I ran php --ini , I found the configuration file pointing at this place: /etc/php/7.0/cli/php.ini. Where is it suppose to point ?? how to know which path its suppose to point ??
I have been through the links below in order to find answer :
Set path to php.ini
How is php.ini related to php.ini-development and php.ini-production?
Overriding global php.ini file
Yor are not define port in your .env file Db_PORT=3306
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=people
DB_USERNAME=pftest
DB_PASSWORD="pftest_2016#9"
At the beginning of this file bootstrap/autoload.php yuo
can try this file
[PDOException] SQLSTATE[HY000] [2002] Connection refused
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.https://laravel.com/docs/5.2/artisan#usage
Answer is addition to Ayaz's answer,
In .env file, set
APP_ENV=local
DB_PORT=3306
and the do php artisian migrate --env="local"
Same is valid for development,production environment.
I am trying to get a laravel5-example project working. One of the steps seems very basic but I do not know how to do it:
Create a database and inform .env
With laravel 5 my .env file looks like:
APP_ENV=local
APP_DEBUG=true
APP_KEY=B0ST6bRoqxVJ2JtUNPtittJup6mRJef2
DB_HOST=127.0.0.1
DB_DATABASE=laravel_base
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=file
SESSION_DRIVER=file
When I try to seed the database I get an error:
$ php artisan migrate --seed
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
How do I create a database so I can run my laravel application on my mac? I have postgres installed. The default for this application is mysql ('default' => 'mysql') in config/database.php. If I change it to 'default' => 'pgsql' I get a different error:
$ php artisan migrate --seed
[PDOException]
could not find driver
I don't really care if I have mysql or psql database, just want it to work. Mysql is the default though, so might be better to stick with that. What are the commands to set up mysql/psql databases for laravel projects?
You should make sure:
You have in your php.ini PDO PostgreSQL enabled (extension=php_pdo_pgsql.dll not starting with ;)
Set in .env DB_CONNECTION to pgsql (or default do pgsql in config/database.php depending what specific version of Laravel you use (5.0 or 5.1/5.2))
If you won't be able to make it work, you should think of installing Laravel Homestead that has everything needed to develop Laravel applications