Laravel DataBase Connection: Access denied for user - mysql

I install laravel on Ampps (windows 10) with .
it works.
But Now I want connect to mysql.
I create a 'blog' DB and change these two files:
.env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD='mysql'
and /config/database.php :
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'blog'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'mysql'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
but still I get the below message when I want to migrate:
C:\Program Files (x86)\Ampps\www\blog>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = blog and table_name = migrations)
[PDOException]
SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)

Password should be this
DB_PASSWORD=mysql
Not this
DB_PASSWORD='mysql'

You should write this
'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', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
and your env file is
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=mysql
Also restart your server.

First change your password in .env file from DB_PASSWORD='mysql' to DB_PASSWORD=mysql without single quotation. Second you should set more secure passwords for your databases.
You should give root user access to your database.
GRANT ALL PRIVILEGES ON blog.* TO 'root'#'localhost' identified by 'mysql';
FLUSH PRIVILEGES;

Try running below command
php artisan cache:clear
This should update the settings saved in the cache.

Related

Trying to connect Laravel to Mysql via XAMPP

I'm trying to get my laravel app and the mysql database in XAMPP connected and struggling. On http://localhost:8080/aws-upload/public/ I get the following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydb2.files' doesn't exist (SQL: select * from `files`)
It then prompts me to migrate, if I migrate on the chrome button provided by laravel the site appears and starts to work, however if I try to migrate through the terminal I get:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = mydb2 and table_name = migrations and table_type = 'BASE TABLE')
I've created the database in phpMyAdmin called mydb2 and updated my .env file to:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mydb2
DB_USERNAME=root
DB_PASSWORD=
This is my database.php file:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb2'),
'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'),
]) : [],
],
Thanks
In your database.php file:
'host' => env('DB_HOST', 'localhost'), try changing localhost to 127.0.0.1
In your .env file:
At the top of the file, you'll see it says APP_URL=http://example.com - change the url to APP_URL=http://127.0.0.1

Cannot do mysql with docker laravel

When I run php artisan migrate with the container started with docker, the following error occurs. Is it the root#xxxxxx:/#php artisan migrate that migrates with the container started, or is it $php artisan migrate in another terminal? The latter is done.
Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES) (SQL: select * from information_schema.tables where table_schema = ogp and table_name = migrations and table_type = 'BASE TABLE')
Exception trace:
1 PDOException::("SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: YES)")
2 PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=ogp", "root", "pass", [])
#docker-compose.yml
version: '3'
services:
db:
image: mysql:5.7
command: >
--character-set-server=utf8mb4
--collation-server=utf8mb4_unicode_ci
--general-log=true
--general-log-file=/var/log/mysql/general.log
environment:
- MYSQL_DATABASE=ogp
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_USER=default
- MYSQL_PASSWORD=password
- TZ=Asia/Tokyo
ports:
- "3306:3306"
#.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ogp
DB_USERNAME=root
DB_PASSWORD=pass
#database.php
'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'),
]) : [],
You need to grant privileges to your db user
GRANT ALL PRIVILEGES ON ogp.* TO 'root'#'127.0.0.1';
FLUSH PRIVILEGES;

Laravel 7 access denied for user root password root

I have the username: root and password: root. I cannot connect to my db whenever I type root in my password. It keeps saying ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES) However, when I press enter leaving the password empty, I can go in.
In my .env file I have:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=fin_book_09
DB_USERNAME=root
DB_PASSWORD=root
I also changed my config/database to:
'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', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'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'),
]) : [],
],
I can't seem to understand why this is happening.
Check Your Database connection in PHPMyAdmin.
PHPMyAdmin Default
user name: 'root'
Password : ''
for password set,
SET PASSWORD FOR root#localhost = PASSWORD('your_root_password');
If you are serving your application by laravel command php artisan serve.
Then close that and serve again.
And if still not work try to regenerate your application key again.
php artisan key:generate
Hope it helps :)

Laravel 5 on digitalocean: cannot connect to database

I have this in env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE={db_name}
DB_USERNAME=root
DB_PASSWORD={db_password}
I config/database.php
'default' => env('DB_CONNECTION', 'mysql'),
'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', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false, //true is default
'engine' => null,
],
That's how I got the password:
cat /root/.digitalocean_password
But I get
SQLSTATE[HY000] [1698] Access denied for user 'root'#'localhost' (SQL: select * from `users` where `email` ....
I even cleared the password value in env file but ti doesn't work too. Any help please? Thanks in advance.
To backtrace your issue. try to connect via SSH on your database server . then try to do this
$ sudo mysql -uroot -p
$ password : ******** (enter your password here)
If you were able to connect, probably you shouldn't have any problem on your app level.
second option is : try to use host "localhost" instead of "127.0.0.1" as your root username might only pointed to #localhost instead of #127.0.0.1
Here's the solution I got:
I had to create another user rather than root and add all permissions to that new user which will sure have a new password. I used that username and password in .env file for database connection and it worked fine.

Laravel 5.1 - Homestead MySQL connection. `Connection Refused` & `No such file or directory`

When my .env file is like this:
DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Terminal php artisan migrate works successful but on localhost test, it throws:
PDOException in Connector.php line 50:
SQLSTATE[HY000] [2002] Connection refused
But when my .env file is like this:
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
On localhost everything (registration) works well however php artisan migrate on terminal throws:
[PDOException] SQLSTATE[HY000] [2002] No such file or directory
My database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'port' => '33060',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Inside the VM the sql port is 3306. Outside of the homestead VM the host machine just has a forward to the SQL port on the homestead VM. That is why 33060 points to 3306.
Laravel Homestead Vagrant Box Database Problems
To fix the issue. Do the following:
.env
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'port' => env('DB_PORT', 3306),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
That will get your browsing fixed. Now to run artisan commands, just do vagrant SSH and run them on the VM.