Laravel database configuration for local and remote environment - php

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.

Related

Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38) on Godaddy Shared Server [duplicate]

This question already has answers here:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
(43 answers)
Closed 6 years ago.
I tried hosting my Laravel App using Laravel Version 4 On Godaddy and it worked wel, but when I tried logging in, I got this error message:
Can't connect to local MySQL server through socket '/var/mysql/mysql.sock' (38)
NOTE A DUPLICATE: All the Answers there kept pointing to Installing MySQL and mostly referring to this issue on a localhost/System while mine is on a Live server and has nothing to do with MySQL
Okay I finally found the issue, If you ever encounter this on Godaddy, kindly check your Database configuration file in database.php, instead of localhost for the Host, you have to use Godaddy's Host for your Database.
So instead of this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'revailment',
'username' => 'revailment',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Your configuration should be like this:
'mysql' => array(
'driver' => 'mysql',
'host' => 'revailment.db.9860920.hostedresource.com',
'database' => 'revailment',
'username' => 'revailment',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Am hoping that this will also be of help to anyone who may encounter such issue in the Future.

connect Laravel to a database on another IP

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.

Environment variables in database.php

I have added DB credentials as environment variables (using nginx), so that i can use them like so:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USER'),
'password' => getenv('DB_PASS'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
);
The problem is that when I use artisan the environment variables do not seem to be available, so when i run migrate or seed i get errors.
Is there a way around that or should i just write my DB credentials directly in my config file ?
To edit my previous answer (sorry for misunderstandig):
Yes, environment variables are created by server, so they can't be reached or modified from CLI. Before deploying, server is generating those variables, so they can be "injected" into application at runtime.
I am thinking that it is maybe possible to reach those variables through remote Laravel package and SSH ? For example php artisan tail command is reading locally errors from the server side.

Access OpenShift environment vars with Laravel 4

I'm trying to use OPENSHIFT environment variables in my Laravel 4 application, but it doesn't seem to work! I read the following question: Laravel 4 accessing environment variables
But in OnpenShift I don't have access to server config.
I always used
$path = $_ENV['OPENSHIFT_DATA_DIR'] . 'uploads/thumbs/';
But $_ENV and getEnv() always returns me nothing.
How can I get that?
You could try using the Laravel 4 quickstart https://www.openshift.com/quickstarts/laravel-4-on-openshift
The PHP command is getenv('my_var_name'), which should work fine. See PHP Environment Variables in the OpenShift Developer Portal for more info.
I need to set fallback values. then I can keep my local settings. So I do this
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('OPENSHIFT_MYSQL_DB_HOST') ? getenv('OPENSHIFT_MYSQL_DB_HOST') :'127.0.0.1',
'port' => getenv('OPENSHIFT_MYSQL_DB_PORT') ? getenv('OPENSHIFT_MYSQL_DB_PORT') :'3306',
'database' => 'mydatabase',
'username' => 'admintest',
'password' => 'asd123asd',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),

Using Google Cloud SQL with dev_appserver.py and Laravel (PHP)

I am having issues trying to work with a Cloud SQL instance and Laravel. I was able to do local dev work on a Cloud SQL instance with Python but I can't seem to get it with PHP.
Here is the error I get:
SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/cloudsql/project:instance-db'
Here is my app/config/database.php:
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'unix_socket' => '/cloudsql/my-project:instance-id',
'host' => '',
'database' => 'my_database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'migrations' => 'migrations',
);
It all works when deployed just not locally.
You cannot connect to CloudSQL from your local machine right now. For testing you should use a local MySQL instance the CloudSQL instance when running in production.
You should be able to assign an IP address to your Cloud SQL instance and allow your own network to access the instance in the management console.
Assigning an IP address will cost extra.

Categories