I am trying to connect a laravel application to a remote database.The connection works successfully on my local machine but does not work in the production environment.The laravel application is connected to its own database hosted in the cloud but i want to access another database hosted by another provider.Hence,I did the following:
In Config/database file in laravel folder,I added another mysql connection
config the .env file as well.
'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', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql3' => [
'driver' => 'mysql',
'host' => env('DB_HOST_THIRD','localhost'),
'port' => env('DB_PORT_THIRD'),
'database' => env('DB_DATABASE_THIRD',''),
'username' => env('DB_USERNAME_THIRD',''),
'password' => env('DB_PASSWORD_THIRD',''),
],
Then in .env file:
DB_CONNECTION=mysql3
DB_HOST_THIRD=remote IP
DB_PORT_THIRD=3306
DB_DATABASE_THIRD=Remote DB Name
DB_USERNAME_THIRD=Username
DB_PASSWORD_THIRD=password
With the above setting, i can connect to the remote database successfully on my local machine.
Upon deployment, I am unable to connect to this remote database,even though a designated user has been created and access granted through the control panel.This remote database is hosted by GoDaddy and i have called but they said nothing is blocking my application, that everything is ok from their end. Below are what I have done:
I am able to connect the remote Database using Sqlyog, Mysql client successfully.
I connected to Terminal on the Remote Cpanel and try to grant all access to the User at its IP but the response is: "Access denied for user 'username#Remote IP'(using password:YES)".
From web application, the error is : "SQLSTATE[HY000] [2002] Connection refused".
I have telnet the remote IP with port 3306, which respond well. Please, can someone help me, perhaps there are things i am missing or is this kind of issue peculiar to Godaddy shared hosting?. I have checked the following from Stackoverflow:
Enable remote MySQL connection: ERROR 1045 (28000): Access denied for user
Related
I have a Heroku app where I'm hosting my Laravel app. I started the development initially with MySQL, so I wanted to continue doing so using Amazon's RDS service. I create the instance there and managed to successfully connect via my MySQL client, the console etc.
The problem is that the Laravel app can't connect the database after numerous desperate attempts for me to fix it. I have found some articles suggesting the use of DATABASE_URL environment variable is mandatory, so I added it via the Heroku app settings. It looks like so:
mysql://myusername:mypass#myhostnamefromamazon/mydb?sslca=/app/storage/certs/amazon-rds-ca-cert.pem
I found this solution on Heroku's website. I have placed the amazon-rds-ca-cert.pem file on my Laravel's storage folder, like so: /app/storage/certs/amazon-rds-ca-cert.pem
This didn't solve my issue, so then I kept looking and found a Stackoverflow question which had this issue on Lumen. I adjusted my config/database.php according to the answer, but it's still not working for me!
<?php
$credentials = get_db_credentials();
$config = [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $credentials->host),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', $credentials->database),
'username' => env('DB_USERNAME', $credentials->username),
'password' => env('DB_PASSWORD', $credentials->password),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
];
if (env('APP_ENV') == 'production') {
$config['connections']['mysql']['options'] = [PDO::MYSQL_ATTR_SSL_CA => '../storage/certs/amazon-rds-ca-cert.pem'];
}
return $config;
The get_db_credentials() function simply parses the DATABASE_URL environment variable.
The exact exception that I get is:
[2018-10-25 19:32:16] production.ERROR: SQLSTATE[HY000] [2002] Connection timed out {"exception":"[object] (Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /tmp/build_05920c42a6de0a378402b798320d3f04/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:50
I'm totally lost on this and unsure how to proceed.
Your AWS Security Groups must permit traffic from Heroku's IP address range.
https://devcenter.heroku.com/articles/amazon-rds
You must grant Heroku dynos access to your RDS instance. The recommended way to do this is to configure the RDS instance to only accept SSL-encrypted connections from authorized users and configure the security group for your instance to permit ingress from all IPs, eg 0.0.0.0/0.
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
]);
I created a new user on my Macbook Air (OS X Version 10.11.2). The new user has full administrator access. I'm using MAMP. The MySQL port is set to 3306. I can connect to MySQL with everything else just fine. All of my Laravel projects stopped connecting to MySQL. When i try to use
php artisan migrate
I get:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Here is the database portion of my .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=store
DB_USERNAME=clerk
DB_PASSWORD=inventory
I have tried using the root user and password. I have tried using different ports. I have tried switching the host to localhost. I have verified that the username and password are correct, and that the user has access to the database.
Here is the MySQL portion of my config/database.php file:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I have been searching for a few days, and i can't seem to find the answer. My old Laravel 5.2 projects won't connect either. I'm pretty sure that this was caused by creating a new user, but i don't know enough about MySQL to figure out the problem.
Having followed troubleshooting processes as I've suggested in comments below your question, all should be working as expected.
Should that not be the case, you must be missing something; ... a PATH issue I presume.
I suggest you export your database, uninstall your MySQL, then download a new one at here.
Follow the approach elaborated here for a possible fix.
With this, your challenge will surely be addressed.
Try changing your db_host = localhost:3306
You define an engine = null. The engine is used inside the MySqlGrammar.php specifically when compiling a create query as far as I know. I have only seen null engines for VIEWS.
Try the following config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'store'),
'username' => env('DB_USERNAME', 'clerk'),
'password' => env('DB_PASSWORD', 'inventory'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
You did mention it in your question and take it that the following works from a terminal
mysql -uclerk -pinventory -h127.0.0.1 -P3306 store
Could be that it needs to be
mysql -uclerk -pinventory -hlocalhost -P3306 store
Something else you can check with just an info.php file in your project to double check that everything seems fine with the reference to PDO as a module.
<?php
phpinfo();
Then also play around with just a little PDO php script to see if your connection is working properly. Something like:
<?php
$servername = "127.0.0.1";
$username = "clerk";
$password = "inventory";
try {
$conn = new PDO("mysql:host=$servername;port=3306;dbname=store;", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Something else you can also check inside a dummy route what env('DB_DATABASE') etc gives you.
Updated things to check 2016.09.15
To make sure that env() is fetching the correct information include the route below and then include the output of the following:
Route::get('dbtest', function () {
$config = [
'host' => env('DB_HOST'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
];
dd($config);
});
Then also access mysql as the root user and include the output of the select statement:
mysql> select host, user, password, authentication_string, password_expired from mysql.user;
As you are using MAMP make sure to use the mysql commandline tool from /Applications/MAMP/Library/bin/mysql
It's been solved. After a few hours of pulling my hair out, i decided to take #nyedidikeke advice and re-install mysql. I was using MAMP, and I have been wanting to stop using it for a while. I uninstalled it, and installed mysql through Homebrew. Everything is working just fine, thanks for all your help!
I have successfully completed deploying my application on elastic beanstalk. But when I call the URL it shows (I have exported my table to RDS DB instance)
ErrorException in Connector.php line 47:
SQLSTATE[HY000] [2002] Connection timed out (View: /var/app/current/local/resources/views/themes/default1/client/cart.blade.php)
My database.php configuration is:
'mysql' => [
'driver' => 'mysql',
'host' => 'rds.cvp31y7ebg1x.us-west-2.rds.amazonaws.com:3306',
'database' => 'rdsdb',
'username' =>'rdsuser',
'password' => '******',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Please help me to figure out this problem.
Thanks.
1) You may provide the port using the "port"-key in config:
'port' => env('DB_PORT', '3306'),
2) Check RDS Security groups, or - as most probably you're using RDS inside a VPC:
3) Check VPC Security groups. Attach VPC security groups to your RDS instance and whitelist your IP / your account (bad practice) / your instance or scaling group in these VPC security groups. Remember: If you use RDS inside VPC, it's the VPC Security group that's important for RDS!
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.