Laravel 5.3 - PHP Artisan Migration - php

I'm new to Laravel and just started with Laravel 5.3. While watching my tutorials and on my way to the Models video I encountered a problem with my "connection".
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using pas
sword: YES) (SQL: select * from information_schema.tables where table_schem
a = testing and table_name = migrations)
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using pas
sword: YES)
Here's my database.php: (I tried configuring it before where the values are the same with my .env file but it still didn't work. I'm posting my current database.php file to get more insight/knowledge about configuration.)
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
And here's my .env file:
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3307
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=password
I don't use homestead and running only on my local server. I'm also using xampp on Windows 7.

Change you .env file like this --
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=testing
DB_USERNAME=root
DB_PASSWORD=
leave the password empty. Then try again.
Still error? then check the following --
If using xampp, wampp or any, dont forget to run mysql and apache.
A database with name 'testing' is created in phpmyadmin or any other

It looks like your username or password is not correct.
Are you sure you have .env.example renamed to .env?
This video may be helpful if you are trying to find information regarding artisan command https://www.youtube.com/watch?v=ix6CQ3rh8WY

There are several reasons for this error,
I hope this will save your time:
1- Confirm that the mysql and apache is running.
2- Setup (create) your database before the first deployment.
3- if you use vagrant, use vagrant IP instead of 127.0.0.1
4- Install latest version of PDO.

Related

Laravel application connecting to outside database

I created an .env file in the root of the project. There I have these entries:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=mypassword
In config/database.php I have this that I guess is set by default by laravel:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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', ''),
When I run artisan to execute migration, with
php artisan migrate:fresh
I can see that my local database has been filled with tables.
What I do next is to request a creation of a user, with Auth located in /Http/Controllers/Api/AuthController.php.
However, I got an error saying:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'IP_ADDRESS' (using password: YES) (SQL: select count(*) as aggregate from `users` where `email` = myemail#mail.com)
Why? Isn't it enough to have an .env with my local settings? I cannot understand where the ip address comes from. I searched all the project but the IP does not exist in the code.
A local form in a view goes to
http://localhost:8204/register
This is picked up in
routes/api.php
with this declaration:
Route::post('/register', [\App\Http\Controllers\Api\AuthController::class, 'register']);
The error above is printed by this file:
vendor/laravel/framework/src/Illuminate/Database/Connection.php:692
If you are using special characters on password put it inside " ".
Make sure you are using the right credentials too.
Try to run php artisan config:clear as well
The application was running in docker container. The .env file was wrong as it was pointing to my host database. When inside the container and the app, there was not database set up correctly. Thats why it did not work.

Laravel acces denied for connecting db

I have started laravel recently, and I get the error
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select count(*) as aggregate from `users` where `email` = ***)
on a clean freshly made project when I try to register with the default auth system. php artisan migrate works well, and I can connect to the database through mysql workbrench with the same credentials as in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=udemy-cms
DB_USERNAME=root
DB_PASSWORD=
I am using homestead with laravel, and mysql workbrench. I tried php artisan config:clear but did
not help.
database.php is default
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'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' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
Thanks in advance for any help.
First I would suggest to check if you can actually connect to your mysql using your current credentials. You should be able to connect to it in your command line like this:
mysql -uroot -p
This basically tries to connect to mysql using root as a user with no password. If that does not work your user does not exist or your password is wrong.
If it works you should run following, to see if your database udemy-cms actually exists:
show databases;
If it does not show your database you need to create it.
CREATE DATABASE udemy-cms;
It is also possible that your user does not have enough permissions, in this case you need to grant your current user enough permissions.
Make sure that you updated the below values in your .env file
DB_CONNECTION = mysql
DB_HOST = "127.0.0.1" //ip or localhost
DB_PORT = "3306" //make sure its true one
DB_DATABASE = "your_database_name"
DB_USERNAME = "root" //is as default if you didnt create a new
DB_PASSWORD = "password" //or empty
Once updated .env file, and run command php artisan config:cache then restart the server of your project and serve the project again using the php artisan serve command

SQLSTATE[28000] [1045] Access denied for user [duplicate]

I just installed laravel and made a migration. But when i try to run it i get this error:
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'homestead'#'localhost' (using password: YES)
Can anyone tell me what is wrong? I think the Database.php file looks different than normal. Is this something new in the new Laravel?
My Database config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'LaraBlog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Hope someone can help me :)
I tried to make changes to database.php file present within config folder it looks something like this
'default' => 'mysql',
....
...
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'sample'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
I am not using any VM, I am using my local machine, with the database user as root and password a null.
I have also changed my .env file and it looks something like this:
APP_ENV=local
APP_DEBUG=true
APP_KEY=zLzPMzs5W4FNNuguTmbG8M0iFqhIVnsP
DB_HOST=localhost
DB_DATABASE=sample
DB_USERNAME=root
DB_PASSWORD=null
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
Even after doing all the changes when I try to register using the registration form that is shipped with laravel, I try to add a user to my database I get the following error
After doing all the changes I cleared the cache and loaded it again and it seems to work for me now!
if any one is also facing the same issue just run the following commands
php artisan cache:clear
php artisan config:cache
I figured it out :) Had to chance the .env file :)
Is there any changes in the schemaes?
Shouldn't this work:
public function up()
{
// Create table with columns
Schema::create('users', function($table) {
$table->increments('id');
$table->string('username');
$table->string(Hash::make('password'));
$table->string('firstname');
$table->string('lastname');
$table->string('email')
$table->string('role');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
// Insert table to database
Schema::drop('users');
}
Try to check out the ".env" file in your root directory. These values are taken first. Yours are just the defaults if there are none given in the .env file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Your_Database_Name
DB_USERNAME=Your_UserName
DB_PASSWORD=Your_Password
Make changes in the database.php file in **config > local >database.php
rather than change config > database.php file.
hope it will work ;)
practically when you are using localhost server like xampp, you create the database manually "homestead", such errors are due to missing database arguments. 100% percent working test it and see
hit your cmd and type in:
create database homestead;
i got the same issue after creating laravel project. The authentication command run successfully.
php artisan make:auth
But when i try to register or login i got the same error. I run the following command
php artisan config:clear
then stopped laravel application and also my server. after restarting my laravel app and server everything was ok.
Tried all the solutions here but no work for me
This Worked on the first try:
If you are using the PHP's default web server (eg. php artisan serve) you need to restart your server after changing your .env file values.
I found this solution from here laravel.io (Read the solution of Byjml)

Access denied for user 'homestead'#'localhost' in Laravel

newbie here. I try to run:
php artisan migrate
But I get these errors:
In Connection.php line 664:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (usin
g password: YES) (SQL: select * from information_schema.tables where table_
schema = homestead and table_name = migrations)
In Connector.php line 67:
SQLSTATE[HY000] [1045] Access denied for user 'homestead'#'localhost' (usin
g password: YES)
This is what is contained in my database.php and .env files respectively:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'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' => true,
'engine' => null,
],
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
My DB name is homestead and host is localhost yet it's still giving error messages.
And this is what it looks like in myPhpAdmin user accounts overview page:
How would I fix this?
If you are using homestead without any changes then the default host for the homestead machine is 192.168.10.10.
Change
DB_HOST=localhost
to
DB_HOST=192.168.10.10
Link to the Docs
This means that either your database username or password is wrong. If you are on windows the default DB_USERNAME is root and password is empty. Try correcting your configuration in .env file.

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