Laravel fails to connect to other database servers - php

I have been using Laravel for a while but mostly on a single database server.
Now, my client has got atleast 5 database from multiple database servers in the same network.
I am able to confirm the communication in between servers using ping through ssh and also using the
mysqli_connect to check the database connections.
However, when I use laravel through its database.php (mysql configs) like :
// This setup is working fine (using localhost)
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
// Other DB (which is failing to connect) but i can confrim the connection using a file outside
// the laravel app through mysqli
'other_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.241',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'another_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.211',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and using the new PDO
// This connectiion functions with no problem
$conn1 = new PDO('mysql:host=localhost;dbname=dbname;port=3306','username','password');
if($conn1) {
echo 'connected';
}
// But this returns an error
$conn = new PDO('mysql:host=192.168.33.241;dbname=dname;port=3306','username','password');
if($conn) {
echo 'connected';
}
// Also, i have no problems using the DB:: or even the model as long as the host is the localhost
it fails to connect, both the PDO and the laravel default DB feature only works if the host is the localhost.
For some reason it throws back an error like this if i use the other servers ip, which is where i would like to communicate with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.241' (13)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.211' (13)
When i try using the server ip or the main domain name to connect.
Am I missing something? Why is it that I can connect only on the local db server which is localhost
and not to any other database servers? But I can connect to them with no problem if I use say an index.php file from outside the laravel application and use the mysqli_connect to make queries and make connections?
Thanks for all your help.

You can access all the connections you have specified within database.php with:
$user = DB::connection('other_db_connection')
You don't need to setup new PDO connections or similar, will work like above for you

Related

Laravel application trying to authenticate user using another database

I have two applications both developed in Laravel.I have logged into the first application and doing coding. But when I try to log into the second application it gives an error of the missing 'users' table(the table in the second database I am trying to log into now which is not in the first logged in an application).
Seems like the second application is trying to authenticate the user using the first database which is not correct
Can anyone assist me with this
Dear Laroja as i get you want to authenticate with another application which have separate database then you have to define two database auth and try to build the logic if connect to first application then it uses the first database and if you want to connect with another application it will connect to other database.
Please have a sapmle code for connect two databse
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'qmops_live_server',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => env('DB_PORT', '3306'),
'database' => 'XXX',
'username' => 'XXX',
'password' => 'QXXXX',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
please use the above code in app/config/database.php hope my solution will help you Thanks.

server choice on phpmyadmin

I'm working on our database and I use for that a former project I did.
I used to work with a phpmyadmin version where I didn't need to specify the server choice.
Today I work on the version 4.1.14.8 and I have to specify the server number.
What I like to know is how can I specify that in my connection string by using doctrine.
Here's what I have for the moment :
<?php
// Doctrine (db)
$app['db.options'] = array(
'driver' => 'pdo_mysql',
'charset' => 'utf8',
'host' => 'localhost',
'port' => '3306',
'dbname' => 'dbname',
'user' => 'usr',
'password' => 'pwd',
);
Thanks
The array value:
'host' => 'localhost',
Is what you use to specify the server to use. In the above it is set to 'localhost'. You could just enter in the IP address if your MySQL database is on another host.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server [duplicate]

This question already has an answer here:
How to connect a Laravel app to MySQL using MAMP?
(1 answer)
Closed 8 years ago.
I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61)
It points to:
/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
This is the function it is pointing to:
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
Up to this point, I had not received this error.
I have a local environment and production environment set up (the default).
in config/local/database.php I have:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?
Also, how have you started MAMP? With the standard MySQL 3306 port? Or the alternative port MAMP uses for non-admins: 8889?
I bet your server is running, but is attempting to connect to 3306 so you need to set the port to 8889. Change your config to be like this; note the addition of the port param:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '8889',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
EDIT: I just found this question thread that addresses the issue of connecting Laravel to MAMP via port 8889.

PDO connection refused with laravel, but not with $con = new PDO()

I've configured mysql for my laravel 5.0-dev project like so:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST') ?: 'localhost',
'database' => env('DB_DATABASE') ?: 'homestead',
'username' => env('DB_USERNAME') ?: 'homestead',
'password' => env('DB_PASSWORD') ?: 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
However, I get connection refused ('PDOException' with message 'SQLSTATE[HY000] [2002]') whenever I try to interact with the database using either php artisan, or the laravel app itself.
The weird thing is, when I run the code below, the connection doesn't get refused.
<?php
$dsn = 'mysql:dbname=homestead;host=localhost';
$user = 'homestead';
$password = 'secret';
try
{
$dbh = new PDO($dsn, $user, $password);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
I've been googling for 3 hours now and I haven't found a single solution which actually works. So any help is greatly appreciated.
The problem is fixed. First off, if you're using a VM, like I am, you can't set your host to localhost, it has to be set to 127.0.0.1. Secondly, the port through which the connection is made needs to be explicitly defined (default for mysql is 33060). So in my case the connection configuration is defined as follows:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '33060',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Lastly, you need to modify the .env file in your project:
DB_HOST=127.0.0.1
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
This is very tricky stuff of PDO.
If you set URI as "localhost", PDO will use a local unix socket! Where as a plain IP, PDO will use a TCP connection.

SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61) error Laravel 4.1 [duplicate]

This question already has an answer here:
How to connect a Laravel app to MySQL using MAMP?
(1 answer)
Closed 8 years ago.
I am receiving the following error on my localhost for Laravel 4.1 (using MAMP)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (61)
It points to:
/Applications/MAMP/htdocs/crowdsets/laravel-master/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php
This is the function it is pointing to:
public function createConnection($dsn, array $config, array $options)
{
$username = array_get($config, 'username');
$password = array_get($config, 'password');
return new PDO($dsn, $username, $password, $options);
}
Up to this point, I had not received this error.
I have a local environment and production environment set up (the default).
in config/local/database.php I have:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
An error like that means the server itself is not even reachable. Did you start MySQL in MAMP?
Also, how have you started MAMP? With the standard MySQL 3306 port? Or the alternative port MAMP uses for non-admins: 8889?
I bet your server is running, but is attempting to connect to 3306 so you need to set the port to 8889. Change your config to be like this; note the addition of the port param:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => '8889',
'database' => 'database',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
EDIT: I just found this question thread that addresses the issue of connecting Laravel to MAMP via port 8889.

Categories