connect Laravel to a database on another IP - php

I need my Laravel project to use a database that is on a virtual machine on another network.
I know there's two database configuration on
app/config
app/config/local
I'm using MySql, how should I configure the database.php file?
Right now is configured to my local:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'db_name',
'username' => 'root',
'password' => 'the_password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

You should specify remote MySQL server host IP address in database.php configuration.
Also you need to make sure you have enable remote connection to your MySQL database (normaly most MySQL servers are restricted to connections only from localhost). For this purpose you will need to create new database user with the following command
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'IP' IDENTIFIED BY 'PASSWORD';
Where you put IP of the server where you laravel installation is located.
Also after that you must execute
FLUSH PRIVILEGES;
And the you can connect to remote database.

Related

Phalcon, Docker MySQL connection fails

So I have 3 docker containers, one with PHP+PhalconPHP, one with MySQL and another with the webserver. Now on my host system I can connect to the MySQL in the docker container and I can also connect with PDO using the following code
new PDO('mysql:host=mysql;port=1024;dbname=database', 'root', 'password');
But for some reason the phalcon framework is unable to connect to the MySQL database using the config.php file like so
return new \Phalcon\Config([
'database' => [
'adapter' => 'mysql',
'port' => 1024,
'host' => 'mysql',
'username' => 'root',
'password' => 'password',
'dbname' => 'database',
'charset' => 'utf8',
],
]);
In this case, for some reason I am getting a Can't connect to MySQL server on 'mysql' (111 "Connection refused) even though I am 100% sure I can actually connect (as proven before with the PDO connection). I also tried hardcoding the actual MySQL container ip address but no luck either.
Any ideas?
So it appeared to be kind of a stupid "mistake" but here's the solution.
Apparently when you use the Phalcon Developer Tools to scaffold your project, it does not initialize the database with the port configuration as it outright ignores it. You can easily fix this by going into the app/config/services.php file and changing the db service to also include the port configuration.
$connection = new $class([
'host' => $config->database->host,
'port' => $config->database->port,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
'charset' => $config->database->charset
]);

Cakephp installation

I have installed cakephp 2.7.5 on xampp but when I run my application I get an error message >CakePHP is NOT able to connect to the database.
Database connection "Mysql" is missing, or could not be created.< I suspect that my mysql server is not properly configured and I have particular concerns over the PDO and pdo_mysql settings in the php.ini file. How do I enable them? (I am totally lost here)Thanks in advance.
You don't mention configuring the connection, so your problem is most probably that of a missing initial database configuration.
You will obviously need to create a database (and maybe a user with sufficient permissions in that DB) before you setup the configuration. Don't mess with PDO configuration in php.ini, it should run out of the box with XAMPP.
Rename file name from database.php.default to database.php .Create a database in mysql. Open the file and change your mysql login,password and database from default variable.Then run your application.
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);

Laravel database configuration for local and remote environment

I just have a database issue on my project/app/config/database.php. I want to my application can change its database when detecting its database environment. For example, if application is develop in local database, the database setting are all local parameter. If it detect that the database is on the remote server, it changes into remote database settings.
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'laravel_',
),
I've solved the problem, however, not by adding local folder in to config folder as the document said.
I check the article Working with Configuration in Laravel 4, it is useful if you use .env.local.php to protect you local database configuration.

SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9) Laravel

I'm getting this error when trying to connect to an SQL Server DB from Laravel 4.2.
Here's my DB config:
'connections' => array(
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'example.noip.me',
'database' => 'db',
'username' => 'sa',
'password' => 'pass',
'prefix' => '',
),
),
//...
It works perfectly in my home computer, but not in production. This project is in a hosting service, so I can't change much of the config (can still use ini_set though).
I tried adding the port to the server address but it didn't work.
Edit: I forgot to mention that the hosting and the database server are on different computers, so it could be a security configuration problem.
I contacted tech support and it turned out to be a matter of configuration in the hosting server (Remote Database Access Hosts, which I thought was for MySQL only).

Connect to an external Database (other Host) on Drupal 7

I want to connect to an external Database on another Host (not localhost) on Drupal because i want to migrate Content with the Migrate Module.
thats in my settings.php
$databases['for_migration']['default'] = array(
'driver' => 'mysql',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'host' => 'other-host.com',
'prefix' => '',
'port' => '',
);
And in another method im calling the database via:
$query = Database::getConnection('default', 'for_migration')
thats the error which appears:
PDOException: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'host.com' (13) in MigrateMigration->__construct() (Zeile 16 von /var/www/html/x/sites/all/modules/migrate_x/x.inc).
So if i call a database on localhost, there are no errors and all migrates perfectly. But i cant connect to a database on an external host.
That could be due to:
1) connection limitation (firewall block, for example).
2) the account you are using does not allows logins outside of "localhost". In this case, you will have to edit your account hostname mask on the 'user' table inside the 'mysql' database for '%' (which means, any address) or the machine IP address you are accessing from.
It was my fault. I have set httpd_can_network_connect on the remote server to on.
But the solution was to set it on my local machine. After that i can connect to an external Host. So it was a SELinux Problem.
Thanks to #fobia

Categories