Laravel 5 on digitalocean: cannot connect to database - mysql

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.

Related

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 :)

SQLSTATE[28000] [1045] Access denied for user 'elsharkawyazq_climate_app'#'localhost' (using password: YES)

My env Database is Right but I get this error
SQLSTATE[28000] [1045] Access denied for user 'elsharkawyazq_climate_app'#'localhost' (using password: YES)
I'm using laravel 5.8 on PHP 7.3 on my server
I tried other working database didn't work so env file data is correct but I don't know why this is happening
My Env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=elsharkawyazq_climate_app
DB_USERNAME=elsharkawyazq_climate_app
DB_PASSWORD=SomePassword!####!
My Database
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'elsharkawyazq_climate'),
'username' => env('DB_USERNAME', 'elsharkawyazq_test'),
'password' => env('DB_PASSWORD', 'SomePassword!####!'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
i used Xampp on local and everything was fine
The pound sign acts as a comment. You cannot use the pound sign (#) as a character in a .env file when it is not escaped. Surround your password with quotes:
DB_PASSWORD="Your-password-with-###-here"
When I enter the php artisan tinker environment, I can load the environment variables from the .env and when I have a pound sign in a value, it acts as a comment. When put inside quotes it does not.
In your .env DB_PASSWORD don't use any (!) and (#) character. You can use (#) or (%) character in your password. So remove these characters from your DB_password.
DB_PASSWORD="Use only the laravel allowed db password allowed characters"
I think it should work for all lavavel lovers.

SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO)

I want to take all information from my database. Bu when i refresh the page error access denied occurs. How can i solve this. Can you please help me?
this is 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' => true,
'engine' => null,
],
this is .env;
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HMSysJsvQBx9lKnctKT2Z+7Gv//of2W8O18uD+5wO0o=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=
The error is:
"SQLSTATE[HY000] [1045] Access denied for user 'root'#'localhost' (using password: NO) (SQL: select * from tasks)"
Run:
php artisan config:clear
php artisan cache:clear
Or
php artisan config:cache
You will receive:
Configuration cache cleared!
Configuration cached successfully!
You must remember that .env datas are keep in laravel cache engine. So clear cache everytime when you make any changes in .env
I think you have to clear your config cache. Restart your application or run php artisan config:cache from your command line.
php artisan cache:clear
php artisan config:clear
php artisan optimize
Check on different browser better to check on chrome incognito after these commands.
Check if your username and password in the .env file are correct. It might be that user doesn't exist, or that the user has a password

Laravel DataBase Connection: Access denied for user

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.

Laravel Can't Connect to database - Migrations - Error 2002

I have searched for a couple of hours now, and still am unable to find this.
I get 2 errors, if I use the database host as 'localhost', I get this error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
and if I change the database host to '127.0.0.1' I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I have tried:
changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
changing the host of the connection from localhost to 127.0.0.1 and back.
creating a new user in phpmyadmin
turning off the firewall
Any ideas how to fix this?
I figured it out, add this after the 'host' => '127.0.0.1':
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
So the connection would look like this:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'dbname',
'username' => 'josh',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
fist clear your config file by typing
php artisan config:clear
Then modify your .env file
APP_URL = 127.0.0.1
DB_HOST = 127.0.0.1
Hope this works for you.
IF you use lampp . You can try this:
Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).
Find and see "socket =/opt/lampp/var/mysql/mysql.sock",
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'luanvan'),
'username' => env('DB_USERNAME', 'huuthang'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck
I had to do the following in ubuntu 15.10
copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf
Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.