Laravel mySQL database error "Unknown database" - laravel

Im trying to use the command php artisan migrate, however i keep getting the error:
PDOException
SQLSTATE[HY000] [1049] Unknown database 'forge'
at first i got the:
access denied for user 'homestead'#'localhost'
but that was fixed when I changed the database.php and .env files to:
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
.env
DB_HOST=localhost
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=''
I read somewhere that I had to create the database in the mySQL database. If i do need to do this why is that, is there an alternative more effecient way from the command line. My development environment is Windows 7.
UPDATE
I have solved this error by going to localhost/phpmyadmin and creating a database with the same name and collation. But i still dont understand why i have to use phpmyadmin to create it, how would i be able to do this from the command line.
Also, inside this database i have tables for users, if i get more info from the users, do i create another database or just use a table with the values ?

Usually you have all of your data contained in one database and have the user only have access to that table. The method you're trying to go through is not secure as you have to give laravel (or the app) permission to create any tables, delete any tables (to undo the tables created), and read any tables, which can get hairy in a environment that share a database with other applications.
It also goes to say that if the application doesn't adhere to a prefix naming of dbs it could make them at will and collide with another application since namespacing doesn't exist.
If however you really do need this functionality you can follow the guide written by Koster (http://laravel.io/forum/09-13-2014-create-new-database-and-tables-on-the-fly).

Related

larvel 9 PDOException::("could not find driver") on Windows

I wanted to connect a MySQL database to my Laravel application.
I created one using PHPMyAdmin administration tool of MySQL, then I added it in the .env as whereas the database.php application files.
I ran the terminal command: php artisan migrate which gave me the following error:
could not find driver (SQL: select * from information_schema.tables where table_schema = pl_project and table_name = migrations and table_type = 'BASE TABLE')
at C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connection.php:760
756▕ // If an exception occurs when attempting to run a query, we'll format the error
757▕ // message to include the bindings with SQL, which will make this exception a
758▕ // lot more helpful to the developer instead of just the database's errors.
759▕ catch (Exception $e) {
➜ 760▕ throw new QueryException(
761▕ $query, $this->prepareBindings($bindings), $e
762▕ );
763▕ }
764▕ }
1 C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDOException::("could not find driver")
2 C:\Users\u\Documents\pl_project_test\pl_project_test\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70
PDO::__construct()
Here is the .env excerpt that I edited:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pl_project
DB_USERNAME=root
DB_PASSWORD=
Here is the database.php excerpt that I edited:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'pl_project'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_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'),
]) : [],
]
Here is the output of the command: php -m
[PHP Modules]
bcmath
calendar
Core
ctype
curl
date
dom
fileinfo
filter
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
Phar
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
xml
xmlreader
xmlwriter
zip
zlib
[Zend Modules]
A couple of notes:
I did add PHP to my env variables as well as MySQL.
I did uncomment extension=pdo_mysql in php.ini.
But none of it changed anything and I still get the same error.
There is no need in editing database.php file as it is using .env for database info. In case this is new project that did not have .env file, try running php artisan config:clear
Have you tried restarting PC after enabling extension? If you are running your project with artisan serve it will not reflect after you change ini file
I also had few similar issues on couple of machines with database, it got solved with commenting 'url' => env('DATABASE_URL'), line in mysql
Hope some of this helps
Comment:
I checked all the php.ini files there were 2 so I edited the second
one. restarted the server then tried to migrate again only to get the
same error.. – OP - https://stackoverflow.com/posts/comments/131493101?noredirect=1
From what I can recall, the default XAMPP installation lucks a php.ini file in the path xampp/php/.
It however has php.ini-development and php.ini-production configuration files in that path. You would normally have to create a duplicate copy of either of those two files depending on your use case and rename the 'duplicate copy' to php.ini This renamed file would then be considered as the 'active/default PHP configuration file'.
You would then open this new renamed php.ini file, uncomment any necessary extension libraries, save and restart your XAMPP web server (Apache).
Addendum
You may need to enable/uncomment the extension libraries below for your case.
mysqli
pdo_mysql
pdo_sqlite
Addendum 2
A. Enable/uncomment the line below. Change the path to your XAMPP installation path. I.e:
extension_dir="C:\xampp\php\ext"

why my laravel project connected with old database

My laravel project keeps connecting with the old database. I already changed .env's database name but still, my project connects with the old database. I have no idea why is this happening.
I ran all these commands
php artisan cache:clear
php artisan config:clear
php artisan config:cache
php artisan route:clear
php artisan route:cache
also, restart my server, update my composer but nothing works...
This is the error shown in the terminal :
SQLSTATE[HY000] Unknown database 'users'
Here is my .env
DB_DATABASE=user_manager
DB_USERNAME=root
DB_PASSWORD=
There must be still issue with your ENV file. can you please take a double check in this? also please check that is there database exists in phpMyAdmin?
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_name
DB_USERNAME=root
DB_PASSWORD=
in config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'options' => [
\PDO::ATTR_PERSISTENT => true
]
],
for more info,
link to laravel official site
Error resolved.
The error was occurring because of the config.php file. My old database name is stored there. I deleted that config.php file and everything is working perfectly fine.
Your should stop your artisan serve(ctrl+c) and then reserve project
php artisan serve
Aren't you missing DB_HOST in your env File? Looks like you have the DB_HOST hardcoded to your database host or your php-fpm.conf.
I faced the same problem and solved by using the following command:
composer dump-autoload
PHP artisan config:cache
PHP artisan config:clear

laravel 5 unable to migrate

I know this question has been and answered before, but none of the answers apply in my situation. Essentially, I have a Laravel install. I try to run
php artisan migrate
I get the following error:
Access denied for user 'homestead'#'localhost'
Now here's the deal. I am not using homestead. It's a standard LAMP install and I have changed the database settings in config/database.php and in the .env file to reflect the correct username and password for my database. I can find no logical reason for artisan to still be attempting to use the homestead user. My APP_ENV is set to local and there is no production folder in either the app or config folders. I'm at a loss as to where it is pulling the homestead user.
ETA:
I have restarted apache, and run both
php artisan config:clear
and
php artisan cache:clear
It's still pulling the homestead user from something, but I simply cannot figure out where.
This is a good task to practice debugging skills! :)
So, it's obvious that Laravel gets 'homestead' username from somewhere – which could be a config file, a default value in the code (in reality this is unlikely but we are debugging), etc.
So my first step would be to execute this while in the base folder of your Laravel app:
find . -type f -size -30k -exec grep -H "homestead" \{\} \;
Which would look for files smaller than 30 kilobytes containing 'homestead' anywhere inside, and in case if found – show filename and the line containing this pattern. We can safely assume that 'homestead' is not stored anywhere in the database because it's used to access the database, so it's necessary beforehand.
Once you find the place, fixing the bug may be as easy as editing the file!
If find returns no results, there are still possibilities:
If your Linux user is 'homestead' and you try to connect to MySQL with no username specified, 'homestead' will be used automatically. Which would mean that Laravel for some reason cannot access your configuration files (permissions? wrong path?)
Check storage/logs/laravel.log and see the last error in more details. Go through the code file by file (list of files is given in the error, start from the top) and see how it fetches MySQL username.
I truly have no idea what happened with that particular build, but I am closing this question just to be done with it. I trashed the build and started over without any problem whatsoever. What happened there will forever remain a mystery.
You can have multiple database.php config files. The config file that is loaded is dependent upon the context in which the application is executed.
For example, I have three different database.php config files in my project:
app/config/database.php
app/config/local/database.php
app/config/testing/database.php
In my "testing" config file, I have two different connections defined:
return [
'default' => 'sqlite',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'user',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'sqlite' => array(
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => ''
),
)
];
Notice that the default key references sqlite, so it does not matter what credentials I enter into the mysql definition. (Unless I override the default value at runtime using Config::set('database.default', 'mysql'))
Search your config/ directory for the string 'homestead'. You'll find the config file that your application is loading. Change the default parameter to match the connection that you want to use.

Laravel OpenShift Deployment Issue in database migrations

I am trying to deploy a Laravel 4.2 application on RedHat OpenShift.
I have successfully transferred the code from GitHub but I am stuck at the database integration.
How do I run the migrations?
I have created a new mysql_openshift database connection with
OpenShift credentials and updated the same.
I have a migration file in the migrations folder.
But on running the php artisan migrate i get the following error:
http://i.stack.imgur.com/nQbam.png
Please help!
The Laravel 4.2 QuickStart was updated 2 days ago. The QuickStart now automatically runs php artisan migrate when you deploy changes to OpenShift. I definitely recommend switching over to using the QuickStart or updating to the latest version.
Anyways, the short answer to the problem you're seeing: your database is not configured properly in Laravel. See lines 60-70 of app/config/database.php in the Laravel 4.2 QuickStart for a reference on configuring the host/port connection info.
Your openshift db parameters are not set correctly.
Look at your config/database.php you will see how the setting in .env are used. Like before we try to get environmental variables from Openshift. The function env('DB_HOST', 'default') will take either DB_HOST from .env or use the default value in the second parameter. Since .env is excluded from version control, Laravel will take the Openshift-specific settings instead.
So put the following code in your config/app.php :
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST')),
'database' => env('DB_DATABASE', getenv('OPENSHIFT_APP_NAME')),
'username' => env('DB_USERNAME', getenv('OPENSHIFT_MYSQL_DB_USERNAME')),
'password' => env('DB_PASSWORD', getenv('OPENSHIFT_MYSQL_DB_PASSWORD')),
'port' => env('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT')),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,],

Laravel Database connection not working as it should

I am currently setting up a Laravel installation and running through a tutorial to familiarize myself with this new framework.
I have managed to install composer and the Laravel base installation fine, but when i attempt to create a new database and seed it using artisan, nothing actually happens. Artisan tells me that is has seeded it fine but nothing seems to change in MYSQL.
My WAMP server is running on a local machine with Apache and PHPMyadmin and is setup correctly and has no issues (been using it for many projects before.)
Here are my database config settings:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'celebrity_v1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I had to set the database up manually as even though the Artisan migrate command said it was working, it wouldn't actually create a new table, same with the seed.
My commands are as follows:
php artisan migrate:make create_comments_table --create=comments
php artisan migrate
And to seed:
php artisan db:seed
Can anyone see any problems with anything I am attempting to do here or suggest why I am having these issues? I don't have a lot of experience with Laravel.
Thanks
You should set the values in /app/config/local/database.php instead of app/config/database.php

Resources