Environment variables in database.php - 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.

Related

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.

Laravel Forge detecting environment variables in only 3 of 4 deployed sites

they are all configured identically.
the environment is definitely set to production.
the database credentials do not get read at all - it wants to use ''#localhost password: NO as the default.
i set
'mysql' => array(
'driver' => 'mysql',
'host' => getenv('DB_HOST'),
'database' => getenv('DB_NAME'),
'username' => getenv('DB_USERNAME'),
'password' => getenv('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
in all 4 of the sites app/config/database.php, and i set the related environment variables in forge. i have tried leaving the environment field blank, and also typing in 'production'. i also tried using $_ENV['environmentvariable'] in database.php.
this gives me a different error of Undefined Index DB_HOST. so clearly the env vars aren't getting read.
i've taken everything down and re-created the repo and the laravel server many times. all 3 other sites are configured identically. it detects the environment in bootstrap/start with
$env = $app->detectEnvironment(function()
{
return getenv('ENV') ?: 'development';
});
i appreciate any help guys. it works fine on homestead btw.
edit: i can ssh into forge and do whatever i want in any of the sites or databases as well, except in the problem one any php artisan command fails with the 'Access Denied for ''#localhost password: NO', even if i run it with --env="production".
I am not entirely sure if you are using .env files at root to specify different environment vars, but after going through this myself I realized that while when environment is 'local' it reads '.env.local.php', when environment is 'production', it wants '.env.php'.
It's in the docs, but I forget almost every time. I'm hoping that typing this out will help. :)

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