Undefined index: REMOTE_ADDR while Laravel migrate - php

When I run php artisan migrate on my local server (running by php artisan serve) I get the following error:
[ErrorExeption]
Undefined index: REMOTE_ADDR
I tried also php artisan migrate --database db_name, output is like this:
[InvalidArgumentExeption]
Database [db_name] not configured.
My app/config/local/database.php looks like following:
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost', // also tried 127.0.0.1
'database' => 'db_name',
'username' => 'root',
'password' => 'mypassword',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
)
I'm sure MySQL works, I checked it via command line and phpMyAdmin.
Also I included HostnameLookups On inside my httpd.conf, then restarted apache, but nothing helps.
I use Laravel 4.2, Debian.
Any ideas? Thanks in advance.
P.S. Sorry for my English:)
UPD:
Output of php artisan env is Current application environment: local
UPD2:
I created simple route:
Route::get('/test', function() {
return $_SERVER["REMOTE_ADDR"];
});
It returns ::1, but when I execute echo $_SERVER["REMOTE_ADDR"]; in php interactive mode (php -a) I get a Notice: Undefined index: REMOTE_ADDR

When you get an artisan error, that error has possibly nothing to do with the command currently tried to run with artisan, but with some any other coding error you recently made. Search in your code for any recent reference to REMOTE_ADDR.

If you haven't done so, make sure you're accessing SERVER_ADDR using the $_SERVER array to obtain the value of SERVER_ADDR, which is an element of this array.
$_SERVER['SERVER_ADDR'];
If that doesn't work, it may mean that your server doesn't provide that information.
From PHP.net:
There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

Related

Laravel 5 + PostgreSQL: "Database [postgres] not configured." Error

I'm trying to get started with Laravel + PostgreSQL and been following the database tutorial.
Unfortunately, after updating the database configuration file and running php artisan migrate, the following error appears:
[InvalidArgumentException]
Database [postgres] not configured.
What puzzles me is that I didn't specify the "postgres" database in the configuration, but another database I set through cPanel, say "example_database".
Here's some relevant parts of my /config/database.php configuration:
'default' => env('DB_CONNECTION', 'postgres')
And inside the connections array of the same file:
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'example_database'), // This seems to be ignored
'username' => env('DB_USERNAME', 'example_username'),
'password' => env('DB_PASSWORD', 'example_password'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public'
],
The actual database credentials I'm using are working perfectly on my SQL Workbench client, so this seems to be a Laravel config problem. Any ideas? I have searched around for at least an hour to no avail.
You have to enter your configuration in the .env file.
The configuration you made will only be loaded if they are not already defined in .env
You need to use pgsql instead of postgres.
DB_CONNECTION=pgsql
DB_HOST=localhost
DB_DATABASE=DB_NAME
DB_USERNAME=USER
DB_PASSWORD=PW
Laravel sometimes caches your configurations. If you run into this problem while everything looks alright try running
php artisan config:cache
I know this is an old question and it already has an answer; however, here is an small explanation why:
If you check your database.php in the config directory, you will see that you have few connections types, including pgsql. So, the key have to match to the DB_CONNECTION in .env file. You can definitely replace pqsql connection key with postgres, and it will work on the same way.
However, I would recommend replacing the value DB_CONNECTION, instead of modifying the config.
DB_CONNECTION=pgsql

Laravel/MySQL database connection

I have an unusual problem with my Laravel 4.2 project connecting to MySQL database. This is my database.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database_name',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
I use XAMPP for Ubuntu.
First, I've created a database in phpMyAdmin. When I tried to run php artisan migrate, it gave me an error of unknown database 'database_name'. I thought it was silly since I've just created a database in phpMyAdmin. Then I tried creating a database through the command line and tried running the migration again. It worked. Tables were created under the database I specified when I run show tables in the command line. It seems to me that the Laravel project is connected to a different mysql server than the phpMyAdmin. Is that even possible? How can I solve this problem?
I've been trying to solve this for hours but still, no luck. Hope someone can help me with my problem.
I solved the problem by following the solution in the link provided by Bulk. I just have to run sudo /opt/lampp/bin/php artisan migrate instead of the plain php artisan migrate. With this, artisan will use the MySQL version provided by XAMPP.

Make Laravel artisan play with vagrant from localhost

When I ssh into my local vagrant machine I can execute all artisan commands. Hower outside of it, any commands that needs database access such as artisan migrate gives Access denied for user 'root'#'localhost'. If it is possible, how can I use artisan without having to log in to the vagrant machine?
I would also like to do for example Artisian::call('migrate') for example during testing. But that gives the same error.
The reason for the access denied error is that mysql by default limits access to databases to the local machine. It is possible to open up for remote connections as described here, but it wouldn't be a good idea for production environments.
A better way is to add aliases for the commands you want as described here.
For doing calls to artisan etc in code, Laravel has a built in way of running commands on remote servers. First add connection information to the app/config/remote.php file. For vagrant it should look something like this:
'connections' => array(
'production' => array(
'host' => 'localhost',
'username' => 'vagrant',
'password' => 'vagrant',
'key' => '',
'keyphrase' => '',
'root' => '/vagrant',
),
),
Then execute artisan migrate like this:
SSH::run(array('cd /vagrant', 'php artisan migrate'));
You can do more advanced things as well. Here is the documentation.

Cannot connect to PgSQL via Laravel SQLSTATE[08006] [7] FATAL

I'm trying to connect to pgsql via laravel and finally got everything setup (pgsql server running, pdo installed, all libs installed). I'm running on a VPS (CentOS) managed via CPanel/WHM.
Here's what I'm doing:
I'm trying to create a user database via artisan (Laravel's command line) using migrate:install.
For those that don't use Laravel, artisan uses php's PDO for pgsql to connect. Here are my settings:
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
),
I've also setup a test.php file:
$dbconn = pg_connect("host=localhost port=5432 dbname=dbname user=username password=password");
which also fails. I used phpPgAdmin to see what's up and all of the permissions are set correctly, the database shows up, the username is correct, same with password. I checked where postgre (version 8.4.12 btw) was running and phpPgAdmin tells me "localhost:5432".
The error I get via the command line is the following:
SQLSTATE[08006] [7] FATAL: no pg_hba.conf entry for host "::1", user "myusername", database "my_database", SSL OFF
Now, I tried to find the pg_hba.conf file but I'm not entirely sure where to look for it. Same goes for the error/access logs for pg and google hasn't been any help as far as this goes.
Any idea on what I can do?
localhost points to IPV6 ::1 address on your system. Postgresql makes the difference between ipv6 and ipv4 addresses when dealing with access list.
I was able to install/configure everything correctly. I changed the "host" to 127.0.0.1, not sure why that made a difference but it did :)

CakePHP: Can't access MySQL database

I'm new to CakePHP and am just running through the configuration process, but am stumped why Cake can't access my MySQL database. The Cake info page says my tmp directory is writable, the FileEngine is being used for caching (don't know what this means), and my database configuration file is present, but that CakePHP cannot connect to the database.
Here are my setup details:
PHP 5.3 (pre-installed on Snow Leopard)
MySQL 5.1.40 64-bit
CakePHP 1.2.4.8284
Here are the steps I went through:
Created a MySQL schema called cake_blog
Created a MySQL user called cake_blog_user
Granted cake_blog_user the appropriate permissions on cake_blog#localhost and cake_blog#%
Copied the database.php.default file to database.php and edited the database connection details as appropriate
Here is the relevant configuration data from database.php:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Am I missing something here? I should also mention that if I insert an echo mysql_error(); into the /cake/libs/view/pages/home.ctp file right before it tests the database connection, the error displayed is "No such file or directory." I have no idea what file or directory it's talking about.
Thanks!
What usually bites me in it's that MySQL thinks of 'localhost' as 'connect thru the unix socket' and '127.0.0.1' 'connect thru TCP port'. With things like XAMPP (at least on mac) the unix socket file isn't there. Just use 127.0.0.1 instead.
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => '127.0.0.1',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
);
Should work all the time.
If it is the socket, just edit /etc/php.ini to reflect the following
pdo_mysql.default_socket=/tmp/mysql.sock
and
mysql.default_socket = /tmp/mysql.sock
I believe you can also do the following
<?php
public $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'cake_blog_user',
'password' => 'cake_blog_password',
'database' => 'cake_blog',
'prefix' => '',
'port' => '/tmp/mysql.sock',
)
?>
doing this might mean you need to edit the database.php file when you go live on the production server.
Thanks everyone for pointing me in the right direction. The mysql.sock file has been moved to /tmp/mysql.sock instead of its default location at /var/mysql/mysql.sock. Editing the php.ini file to reflect this has fixed the problem.
check your phpinfo and use the socket listed. that worked for me.
On Ubuntu, if you installed both 7.0 and 5.6 versions of PHP this wont work.
Youll need to switch if you have both versions:
Look first if is 7.0 version: the command is php -v.
Next do
sudo a2dismod php7.0
sudo a2enmod php5.6
sudo service apache2 restart

Categories