This is my database.php file:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'learning_laravel',
'username' => 'forge',
'password' => 'bomb',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
My route.php file:
<?php
Route::get('/',function(){
$username=DB::table('username')->get();
return $username;
});
I created a database called learning_laravel and created a table called username and given input to it but i am not getting any output.
When i open localhost:8000/ by typing php artisan serve this is the output i get.
My Output file:
Database[]not configured
$connections = $this->app['config']['database.connections'];
if (is_null($config = array_get($connections, $name)))
{
throw new \InvalidArgumentException("Database [$name] not configured.");
}
You haven't your database connection configured.
You can find config in following file app/config/database.php with example content
'default' => 'mysql', //your database name according to prefix in connections subarray
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name here',
'username' => 'your username here',
'password' => 'your password here',
'charset' => 'utf8',
'collation' => 'utf8_bin',
'prefix' => '',
),
Related
I am having some trouble with disconnecting my created conn.
$dbName = session("key")["db_name"]["0"]->main_db;
$oldEmail = $this->connection()->table("tbl_user")->where("id",$UserID)->get();
DB::disconnect($dbName);
$User = User::where("email",$email)->get();
return $User;
I think the connection bypass my model and search for the table name of "User" in my created connection, I don't get the error, though it has the same name in my migration.
This is my codes in generating another connection:
public function connection() {
// SESSION
$dbName = session("key")["db_name"]["0"]->main_db;
// MAKE CONNECTION
Config::set("database.connections.".$dbName, array(
"driver" => config("app.DB_CONNECTION"),
"host" => config("app.DB_HOST"),
"database" => $dbName,
"username" => config("app.DB_USERNAME"),
"password" => config("app.DB_PASSWORD"),
));
DB::setDefaultConnection($dbName);
$conn = DB::connection($dbName);
return $conn;
}
app/config/database.php
return array(
'default' => 'mysql',
'connections' => array(
# Primary/Default database connection
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database1',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database2',
'username' => 'root',
'password' => 'secret'
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
Useful Links
https://laracasts.com/discuss/channels/eloquent/laravel-5-multiple-database-connection
I have a local Laravel site installed and it was working fine and now it's throwing a DB connection error. Here is the function it's failing on:
My local database is set up, the username is root and there is no password (or the password is blank) so that should work fine.
I'm getting an error:
PDOException
SQLSTATE[HY000] [1045] Access denied for user ''#'localhost' (using password: NO)
It doesn't make sense because it was working and as you can see, it's not even recognizing the 'root' username anymore.
I have a file named '.env' in the root of my whole installation and it has the right credentials as far as I know:
DB_HOST=localhost
DB_DATABASE=my_db
DB_USERNAME=root
DB_PASSWORD=
And those are all correct.
Any ideas why this would stop working all of a sudden?
For more data, my app/config/database.php file is:
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__.'/../database/production.sqlite',
'prefix' => '',
),
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'pgsql' => array(
'driver' => 'pgsql',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
),
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'localhost',
'database' => 'my_db',
'username' => 'root',
'password' => '',
'prefix' => '',
),
),
Just change the root password and do appropriate action in .env and database.php
Please dont use root without having password.after all this do a mysql restart and an apache restart.all the best. :)
I configured the way I separate the database to read and write.
'mysql' => [
'read' => [
'host' => '192.168.1.1',
],
'write' => [
'host' => '196.168.1.2'
],
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
But now, I want to change the database driver in the session and I want to be able to read and write operations to a single host of the session value.
How can I only run on a single host in the session process without disturbing the above structure?
I think I found a solution.
We are adding a new connection into the first database.php connections.
'session' => array(
'host' => "HOST_NAME",
'driver' => 'mysql',
'database' => 'database',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
then we give the name of the connection a little bit before we install the connection value in session.php file.
'connection' => "session",
Thats it.
My problem is that I get the following error in MySqlConnector.php.
Undefined variable: host error
I am using two different connections for both read/write. I merged them in config/database.php file depending on environment that system uses. Here is the mysql connection codes.
<?php
switch($_SERVER['LARAVEL_ENV']){
case 'production':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'beta':
$connections = array(
'mysql' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'read' => array(
'host' => '127.0.0.1',
),
'write' => array(
'host' => 'hosturl',
),
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
case 'development':
$connections = array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'mysql2' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
);
break;
}
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => $connections,
'migrations' => 'migrations',
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
);
All I wanted to do is using a different host for reading and another one for writing. When I use one connection in localhost, I get no error. But in multiple connections, I get the error. What is the reason for the error?
Ok I found the problem. It was because of laravel version. I updated laravel 4.0 to 4.1 and problem was solved.
just for the record of your environment implementation.
You have a build in laravel the environmen detection and loading of specific conf files.
you can read here: http://laravel.com/docs/configuration#environment-configuration
but the general idea is to define your environments in the :
$env = $app->detectEnvironment(array(
'local' => array('your-local-machine-name'),
'production' => array('your--prod-machine-name'),
));
and than you go to config folder and create inside it a folder foreach environment you have.
create a production folder, and than each file you want to override simply copy paste it into the folder and edit the things you want to override.
implement this, and than check your errors back here again
I am developing a project using laravel 4 framework. In my database.php file I get the following error:
Undefined index: driver
And my connection is as following:
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'mysql2' => array(
'read' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'driver' => 'mysql',
'database' => 'app_userdata',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
)
);
I am also using environments in order to set different mysql connections. What is wrong with the code?
In my case it was because I deleted
'default' => 'mysql',
by mistake from app/config/database.php.
Moving the 'driver' key up a level should fix the issue.
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'write' => array(
'host' => 'localhost',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'driver' => 'mysql'
),
Most of the other params that are shared can me moved as well
$connections = array(
'mysql' => array(
'read' => array(
'host' => 'localhost',
),
'write' => array(
'host' => 'localhost',
),
'driver' => 'mysql',
'database' => 'app_system',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
This happens to me because I deleted
'default' =>env('DB_CONNECTION', 'mysql'),
From app/config/database.php. It's necesary have a default connection
Go to root directory .env
This values are taken first.
If you have multiple different connections (for example to multiple databases on the same host) and it doesn't make sense to set a default connection, you can specify a connection in the Model class.
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Character extends Model {
protected $connection = 'my_db_connection_name_here';
}
This would be on the of the connections defined in config/database.php.
I solved my problem by adding some permissions. My .env file was exists but wasn't readable. I just added 755 permission.