Laravel SQL Connection - Artisan Migrate Works, but Application Can't Connect - php

I'm an irregular Laravel user, and am trying to connect to a remote SQL database.
I've gotten the artisan commands working to create migrations and from the command line I can successfully connect to the database. But, when I try to load the application, the connection fails.
Could not connect to the database. Please check your configuration. error:PDOException: SQLSTATE[HY000] [1045] Access denied for user 'myuser'#'222.222.222.222' (using password: YES) in ... {file location}
What are artisan and the actual app framework doing differently that this connection would respond differently between the 2?
Here is the mysql connection in the database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '123.123.123.123'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'mydb'),
'username' => env('DB_USERNAME', 'myuser'),
'password' => env('DB_PASSWORD', 'mypass'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '','strict' => true,
'engine' => null,
'sslmode' => 'require',
'options' => array(
PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false,
PDO::MYSQL_ATTR_SSL_KEY => 'certs/client-key.pem',
PDO::MYSQL_ATTR_SSL_CERT => 'certs/client-cert.pem',
PDO::MYSQL_ATTR_SSL_CA => 'certs/server-ca.pem',
),
And this is the local environment file:
DB_CONNECTION=mysql
DB_HOST=123.123.123.123
DB_PORT=3306
DB_DATABASE=mydb
DB_USERNAME=myuser
DB_PASSWORD=mypass
I am running the application locally, but the MySQl server is remote.

It turns out that the root cause was that PDO doesn't like the cert generated by Google Cloud SQL.

Related

MYSQL error: SQLSTATE[HY000] [2002] No such file or directory (FEDORA LARAVEL)

I have an aplication that works with angularjs and its backend is on Laravel. I'm working on Fedora 37.
The back is in a docker container, and I have a local database with mysql.
When I try to query anithing to the database, it brings the next error:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from
usuarios where email = admin#test and usuarios.deleted_at is
null limit 1)
The .env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wymaq
DB_USERNAME=root
DB_PASSWORD=
Frontend is located in localhost:9001, and its linked in config.app.js to back (localhost:8080) like this:
.constant('API_CONFIG', {
'url': 'http://127.0.0.1:8080/', etc...
My database.php file is configured like 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' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'unix_socket' => '/var/lib/mysql/mysql.sock',
'strict' => true,
'engine' => null,
'modes' => [
'ONLY_FULL_GROUP_BY',
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_ENGINE_SUBSTITUTION',
],
],
In windows, there is no problem. I have the database in another container and it works. I tried it here in fedora but its the same error.
I get my mysql socket and its: '/var/lib/mysql/mysql.sock'
When i type php -i | grep pdo in the back container i get this:
pdo_mysql.default_socket => no value => no value
Anytime I do local dev w/ docker, and a SQL database (running on the host), the host name for the database that works for me is: host.docker.internal

Access denied for user 'root'#'pop-os' when running migrations on external host

I'm trying to run laravel migration on another mariadb in the same network (for localhost it works fine). I'm able to access this database using mysql -u root -p 192.168.2.100 just fine.
However using php artisan migrate I get an error
Error message:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'pop-os' (using
password: YES) (SQL: select * from information_schema.tables where
table_schema = 92.168.2.166 and table_name = migrations and table_type
= 'BASE TABLE')
I am running this from pop-os (linux) , but I do not understand why it's using this as a host? I do not have 'pop-os' in my configuration anywhere.
Configuration
ENV File:
DB_CONNECTION=mysql
DB_HOST=192.168.2.100
DB_PORT=3306
DB_DATABASE=Laravel_db
DB_USERNAME=root
DB_PASSWORD=***
database.php (mysql)
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'Laravel_db'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', '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'),
]) : [],
],
laravel version 9.29.0
Are you sure there is an database user named 'root' with password you are using?
Try to log directly into that database (for example from console) and see if that works.
Also try to give permissions to user 'root':
GRANT ALL PRIVILEGES ON *.* TO 'root'#'ip_address' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;

Cannot run Laravel migration on Homestead

I have been trying to run migration since yesterday but database connection is preventing it
Note: I can access the database very well from other projects. This only happens with laravel on homestead. Here is the output of the php artisan migrate command:
SQLSTATE[HY000] [1045] Access denied for user
'abdellah'#'192.168.10.10' (using password: YES) (SQL: select * from
information_schema.tables where table_schema = furbook and table_name
= migrations and table_type = 'BASE TABLE')
This is my .env database config
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=furbook
DB_USERNAME=abdellah
DB_PASSWORD=bonjour1
And this is the database.php file
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'furbook'),
'username' => env('DB_USERNAME', 'abdellah'),
'password' => env('DB_PASSWORD', 'bonjour1'),
'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 am running on Homestead environment.
Thanks.
Have you made sure everything is spelled correctly?
The only other thing I can think of is adding an extra 0 at the end of the port (DB_PORT=33060).
It's weird but I had a fresh Laravel project using homestead and the only way I could access the DB artisan commands in the terminal was to add a 0 on the port but then I had to take it off again once accessing it through HTTP.
I was able to run the migration successfully by setting the DB username to homestead and DB password to secret.
DB_USERNAME=homestead
DB_PASSWORD=secret

ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO) on Laravel Application

I have this error when i try to access to my laravel home project that have db access.
I know this is a common issue, so I have already read many post and tried this:
check if username and password was correct (The same user and password work from command line and from MySql Workbench );
check permission on Sql Side. There is an entry for root#localhost
clean cache .env and restart server many times. I also tried to insert temporarily the db info directly on database.php
database.php
'default' => 'mysql',
'mysql' => [
'driver' => 'mysql',
'dump_command_path' => '/opt/lampp/bin', // only the path, so without 'mysqldump' or 'pg_dump'
'dump_command_timeout' => 60 * 5, // 5 minute timeout
'dump_using_single_transaction' => true, // perform dump using a single transaction
'host' => env('DB_HOST', '10.8.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', ''),
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
.env
#MYSQL
DB_HOST=127.0.0.1 //I have tried also localhost
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

SQLSTATE[HY000] [2002] Connection refused when deploying a laravel app to Heroku

I’m trying to deploy a laravel app to heroku. I’ve succesfully done so already with one of my apps, but here I get the following error when running the command heroku run php artisan migrate:
In Connection.php line 664:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from
information_schema.tables where table_schema = todolist and
table_name = migrations)
In Connector.php line 67:
SQLSTATE[HY000] [2002] Connection refused
I have checked that database.php and .env have matching configurations:
.env:
DB_CONNECTION=pgsql
DB_HOST=foo.amazonaws.com
DB_PORT=5432
DB_DATABASE=database
DB_USERNAME=user
DB_PASSWORD=password
database.php:
return [
'default' => env('DB_CONNECTION', 'pgsql'),
'connections' => [
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'foo.amazonaws.com'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
]
]
I have read some similar posts and tried changing DB_HOST from 127.0.0.1 to localhost but it didn’t work.
The database values were given by heroku after I used postgres for my database. Am I missing anything here? All advice is welcome.
The .env file is ignored by git, so that won’t be pushed to Heroku. To solve your problem, the credentials should be in the database.php file the same way you have them:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'ec2-12-34-56-789.compute-1.amazonaws.com'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'dbXXXXXXXXXXXX'),
'username' => env('DB_USERNAME', 'XXXXXXXXXXXXXX'),
'password' => env('DB_PASSWORD', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
]
The password should be 64 characters long, so I would say maybe double check the values and then push the changes again. After a successful push, run:
$ heroku run php artisan migrate

Categories