I'm aware this can be a recurring question, but none of the solutions i found online seem to be working with my problem.
I have a Laravel 5.8 project but i can't access to my database from a controller, all artisan command works fine (like php artisan migrate)
But when a try a simple thing like:
protected function test()
{
return User::all();
}
I get SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `users` where `users`.`deleted_at` is null)
I simply trying to access my local database (from OSX)
My Config :
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=secret
database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'database'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
],
My database is working (i can access it with an IDE or else)
Thanks for your help !
it isn't the best answer, but create new database user with all privileges and use that user.
Related
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
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
How can i connect my laravel to an extneral Database?
Example: I have a laravel on my local machine which is running on xammp. And i want it to connect to a cloud Server database.
Open the .env file and edit it. Just set up correct external DB credentials:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 // set external DB_Host
DB_PORT=3306 // Your Database Port
DB_DATABASE= // Your Database Name
DB_USERNAME= // Your Database Username
DB_PASSWORD= // Your Database Password
DB_USERNAME should be set to root if you do not have a default username in the installation time
If no password is set on the database, just clear it DB_PASSWORD
After .env edit, must be clear cache:
php artisan config:cache
In .env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database.php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.
.env
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=laravel
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=
config/database.php
'mysql_second' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND', '127.0.0.1'),
'port' => env('DB_PORT_SECOND', '3306'),
'database' => env('DB_DATABASE_SECOND', 'forge'),
'username' => env('DB_USERNAME_SECOND', 'forge'),
'password' => env('DB_PASSWORD_SECOND', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
If You are use Eloquent Model
add this line
protected $connection = "mysql_second";
If You are using Database Facades
DB::connection('mysql_second')->table('table_name')->get();
In .env(This is in root folder) file change below credentials accordingly with external DB connection:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
and in config/database.php change the below credentials if trying to connect with external MySQL otherwise there are more options for others or please mention from which DB you want to connect with
'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'),
]) : [],
],
1. Enable MYSQL Remote
First, you need to read your hosting docs to get your server data and instructions to enable mysql remote for your local IP by whitelisting your IP or if you want to allow access to anyone you can use wildcard %
in my case HostGator: https://www.hostgator.com/help/article/how-to-connect-to-the-mysql-database-remotely,
2. Change .env configuration
then fill .env file with the provided data by your hosting provider.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 //replace with your external server IP
DB_PORT=3306 // check open port for mysql usually is 3306
DB_DATABASE=laravel // replace with the name of your external database
DB_USERNAME=root //replace with the username associated with database
DB_PASSWORD= //put your username password
I am getting the error
SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from users where 1)
from any query I attempt to send to my database.
Running php artisan migrate runs completely fine from command line showing that my settings are correct! Whats going wrong?!
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=vehiclexray
DB_USERNAME=root
DB_PASSWORD=kzurxsxz1
database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'database'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'password'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' =>
]
I created a database using WAMP. Now I have to move everything to Laravel.
How can I use this database?
I have a lot of stored procedures written there, I would really like to use them again.
I tried changing .env file and config/database.php file, but it won't help.
config/database.php
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'db_file'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'psi'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'latin2_general_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_file
DB_USERNAME=root
DB_PASSWORD=psi
You don`t need to configure both env and database.php.Configure one.I recommend you to configure env file .You need a model to access the database.
Models-
https://laravel.com/docs/5.4/eloquent
Controllers-
https://laravel.com/docs/5.4/controllers#resource-controllers
Always follow the documentation