how to set laravel php.ini location - php

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.

Related

How to Change Database in Laravel 5.7

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

How to fix "Illuminate\Database\QueryException: SQLSTATE[HY000] [1044] Access denied for user"

I tried to run: php artisan migrate
Also to connect to MySQL using Xampp on Windows.
I Got this error:
Illuminate\Database\QueryException : SQLSTATE[HY000] [1044] Access
denied for user ''#'localhost' to database 'homestead' (SQL: select *
from information_schema.tables where table_schema = homestead and
table_name = migrations)
at
C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll format the error
661| // message to include the bindings with SQL, which will make this exception a
662| // lot more helpful to the developer instead of just the database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668| Exception trace:
1 PDOException::("SQLSTATE[HY000] [1044] Access denied for user
''#'localhost' to database 'homestead'")
C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
2
PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=homestead",
"homestead", "", [])
C:\Users\harsh\Laravel1\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
Please use the argument -v to see more details.
.env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=
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= // Yout Database Username
DB_PASSWORD= // Your Database Password
The DB_USERNAME should be set to root if you do not have a default username in the installation of MySQL in xampp.
If no password is set on the database, clear it DB_PASSWORD, empty space must also be removed(In the past I've also faceout this problem, window consider blank space as a password)
After completion of .env edit please enter this command in your terminal for clear cache:
php artisan config:cache
For Mac users using MAMP, if the accepted answer was not enough to solve this issue, like me, add socket to the .env file.
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
//or
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
The variable should match with the setting on config > database.php. Look for connections > mysql > unix_socket
Hi go to xampp phpmyadmin create a database for exemple stack and change your .env to this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=stack
DB_USERNAME=root
DB_PASSWORD=
by default phpmyadmin username is root and without password
save and try php artisan migrate
You should try this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your database name
DB_USERNAME= your localhost-database username (i.e `root`)
DB_PASSWORD= your localhost-database password (i.e `root`)
then you run below command
php artisan config:cache
php artisan cache:clear
php artisan migrate
I was stuck with the same error until I changed the DB_PORT and set the value from MAMP
Try this, go to your .env file, change the name of the database.
Go to your phpmyadmin, create a new database with the new name in your .env file.
Then run php artisan migrate
Try this in your terminal: To start XMAPP use
sudo /opt/lampp/lampp start
Console output should be
Starting XAMPP for Linux 7.4.8-0...
XAMPP: Starting Apache...fail.
XAMPP: Another web server is already running.
XAMPP: Starting MySQL...ok.
XAMPP: Starting ProFTPD...ok.
When you get the error
error: /opt/lampp/bin/mysql.server: 264: kill: No such process**
try this again in your terminal:
sudo service mysql stop
sudo service apache2 stop
Then open datatbase after migrating the file.
you fix file .env Pay attention to the password section 'password'
+need to have '
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE= // Your Database Name
DB_USERNAME= // Yout Database Username
DB_PASSWORD= // Your Database Password
I'm using MacOS and MAMP
I tried the above mentioned suggestions but I couldn't solve it.
What solved it for me was switching from PHP 7.x to 8.0.x in MAMP.

Laravel migration "No such file or directory"

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', '')

how to set up a database for laravel 5? (connection refused error)

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

Lumen error when using artisan migrate

I'm new to Lumen and when I try to use:
php artisan migrate
I get the following error.
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
If I change 'localhost' to '127.0.0.1' I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
If I use:
php artisan migrate --database=Lumen
I get:
[InvalidArgumentException]
Database [Lumen] not configured.
Here is my .env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=Lumen
DB_USERNAME=root
DB_PASSWORD=root
I've read other questions on stack overflow and have tried the solutions i.e. configuring php versions and checking the path to mysql.sock and it's all fine. I am using the full path and my PHP version is correct.
I am using MAMP and have a Database called 'Lumen' (there are no tables at the moment).
Created a migration using terminal:
php artisan make:migration --create=items create_items_table
I am using php artisan serve (server 8000) to view my project, which I can view.
MAMP is on:
apache=80
nginx=80
mysql=3306
I used the same configuration for my Laravel project and that is working fine. But laravel does have a database.php file which has all the details inside of it.
I hope that's enough information, any help would be much appreciated!
Thanks in advance
EDIT: This is my app.php file, the rest is left untouched.
require_once __DIR__.'/../vendor/autoload.php';
Dotenv::load(__DIR__.'/../');
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
$app->withFacades();
$app->withEloquent();
Is it possible you forgot to enable the dotenv variables? In your bootstrap/app.php file you can enable the environment variables by uncommenting the following line:
Dotenv::load(__DIR__.'/../');
Lumen Documentation about databases
After a lot of fiddling about, I found the answer.
The problem was with MAMP, the pdo socket for mysql was not in the php.ini file and had to be included there.
[Pdo_mysql]
; If mysqlnd is used: Number of cache slots for the internal result set cache
; http://php.net/pdo_mysql.cache_size
pdo_mysql.cache_size = 2000
; Default socket name for local MySQL connects. If empty, uses the built-in
; MySQL defaults.
; http://php.net/pdo_mysql.default-socket
pdo_mysql.default_socket=
Including that line fixed all my probelems.
To the new guys out there trying out Lumen, after 3 sad hours, I finally solved this issue. It's not you, it's the MAMP!
All this time, MAMP was not using the default ports 80 and 3306 for web and mysql. So click that button. That's all! And I'm posting this on my blog soon :D
If you're using MAMP on macOS you can just add this code in your .env file:
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
And you don't need to modify any MAMP's config.

Categories