How to connect to SQL Remote server in Laravel - php

I deploy my php laravel project (version 5.8) in digitalocean. I use remote SQL server for the database. I can connect to remote sql server by SQL SERVER MANAGEMENT, but when i use in laravel, i get this error
"message": "SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (192.168.x.xxxx:1433) (severity 9) (SQL: select top 1 * from [users] where [email] = )",
here is my .env setting
DB_CONNECTION=sqlsrv
DB_HOST=192.168.0.xxx
DB_PORT=1433
DB_DATABASE=xxxx
DB_USERNAME=xxx
DB_PASSWORD=xxxx
<?php
return [
'default' => env('DB_CONNECTION', 'sqlsrv'),
'connections' => [
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '192.168.x.xxx'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'xxx'),
'username' => env('DB_USERNAME', 'xxx'),
'password' => env('DB_PASSWORD', 'xxxx'),
'charset' => 'utf8',
'prefix' => '',
],
],
'migrations' => 'migrations',
'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
];

Try to change
'driver' => 'sqlsrv',
to
'driver' => 'mysql',

Related

Laravel 8 & Redis - Predis\Connection\ConnectionException Connection timed out

I'm trying to connect Laravel application to Redis (hosted by DigitalOcean) but without success. I'm getting the following error:
Predis\Connection\ConnectionException
Connection timed out [tls://private-db-redis-ams3-xxxxyyyyzzzz.db.ondigitalocean.com:25061]
Here is how my .env file's looking:
REDIS_HOST=db-redis-ams3-xxxxyyyyzzzz.db.ondigitalocean.com
REDIS_PASSWORD=alfjaslkdfjwlkfjlweh
REDIS_PORT=25061
And here's config/database.php
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'scheme' => 'tls',
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_DB', '0'),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', '6379'),
'database' => env('REDIS_CACHE_DB', '1'),
],
],
I added the IP of the web server inside the Trusted Sources section in DigitalOcean's Redis Server Control Panel but it still doesn't want to connect. I tried almost every solution from Google searches. I'm stuck, every help is greatly appreciated!

Connection refused [tcp://127.0.0.1:6379] Laravel Predis

I'm trying to setup Redis client to connect to the Redis server in Laravel 6.0 version, but when I try to change the REDIS_HOST in database.php file in config it gives me an error:
Connection refused 127.0.0.1:6379
This is how my code looks like in routes api.php file
Route::get('test',function(Request $request){
$redis=Redis::connection();
$redis->set("key1","keyValue");
$a=$redis->get("key1");
return response()->json( ['test'=>$a,]);
});
and this is how my database.php file looks like
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'redis'),
'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
],
'default' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'url' => env('REDIS_URL'),
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
So when i change the REDIS_HOST its gives me error,any idea is my code problem the configurations?
Thanks !
I encountered this on my MacBook and fixed it by installing Redis using homebrew as directed here
PS: I'm working with Laravel 8.40

QueryException SQLSTATE[HY000] [2002] Can't connect to MySQL server

QueryException
SQLSTATE[HY000] [2002] Can't connect to MySQL server on '127.0.0.1' (110)
When I put this project into another server it works fine .
Is there any problem with server configuration ?
in database.php
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => 'projapati',
'username' => 'projapati',
'password' => 'AHt!j![(dl',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
How can I solve this problem?
Change this
'host' => env('DB_HOST', '127.0.0.1'),
to this
'host' => env('DB_HOST', 'localhost'),

error - could not find driver , while fetching data from database in laravel

I am getting following error -
could not find driver (SQL: select * from [customer] order by [id] desc)
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'Customer'),
'username' => env('DB_USERNAME', 'SA'),
'password' => env('DB_PASSWORD', '**********'),
'charset' => 'utf8',
'prefix' => '',
],
I have also change the default option in database file like below -
'default' => env('DB_CONNECTION', 'sqlsrv'),
After doing all I am facing mentioned error .

Configuration for Redis cluster

I'm running Laravel in a GlusterFS cluster, and I want to setup a redis cluster too. I'm not able to find out how should I configure Laravel.
This is the example config file:
'redis' => [
'cluster' => false,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
How can I add multiple servers?
I'm assuming you just add another server there and update the cluster boolean:
'redis' => [
'cluster' => true,
'default' => [
'host' => env('REDIS_HOST', 'localhost'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'secondary' => [
'host' => '127.0.0.1',
'password' => 'p#ssw0rd',
'port' => 6379,
'database' => 0,
],
],

Categories