How to Change Database in Laravel 5.7 - php

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

Related

using a mysql database using laravel

I'm trying to use a MySQL database using PHP and laravel, so far I have created the database in phpmyadmin called "laravel", i have updated my .env file to the following:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
but when I run "php artisan migrate" in the terminal, I get the following error:
How can I fix this?
after modifying the .env file, you have to use the following command: php artisan config: cache

SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' in laravel 7

Can't able to connect database in only laravel 7
my url is http://localhost:8000/index
Here is my project .env file
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=amazon_services
DB_USERNAME=root
DB_PASSWORD=""
i have run the following commands but still facing error
php artisan config:cache
php artisan config:clear
php artisan cache:clear
But i can able to connect the database in laravel 5.4 version while using url like this http://localhost/larave/laravel5.4/inde but while using laravel 7 with development server not connected
This probably is permission error by mySql.
If you are using root as username with no password please add user privileage to the database table you are connecting to like below.
Furthermore you will also need to grant access to root user to perform specific action on table.
And then
If the above solution did not work for you try following
php artisan config:cache
php artisan config:clear
php artisan cache:clear
Keep your .env file as
DB_USERNAME=root
DB_PASSWORD=
Open .env and change that :
DB_HOST=127.0.0.1
DB_PASSWORD=
If the problem persists, try logging into mysql yourself.

Laravel .env file returns wrong data

I've been trying to get data from my database in laravel 5.7.3, but it seems to be using the wrong database name, username and password as it returns this error:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES) (SQL: select * from `tasks`)
My .env file database section is also according to my needs and set like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mrdb
DB_USERNAME=root
DB_PASSWORD=
PS: My app environment is still local. I can also migrate and put in data into database with artisan tinker without issues, but cannot just fetch data from the database.
The only way it gets to work is to change config/database.php mysql section to my parameters, which I believe it isn't just right.
Why is the .env not wanting the change when the database is called?
Laravel uses caching to speed up your sites loading times, one of the things laravel caches is the config files located in the config/ directory. Most of these files do have references to the .env file by using the env() helper function.
One can create a config cache by using php artisan config:cache which will then be stored in bootstrap/cache/config.php.
To remove this specific cache you can use php artisan config:clear, to clear all cache use php artisan cache:clear.
This is cache issue use these commands
php artisan cache:clear
php artisan config:cache
After that delete all cache file ie inside
storage/framework/views

how to set laravel php.ini location

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.

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

Categories