I am new to laravel. I use homestead for development. I have two database connection
database.php
'cust_main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'project_db',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'admin_main_db' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'projectadmin_db',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Homestead.yaml
databases:
- homestead
In my local mysql has both databases project_db and projectadmin_db
When I run the project http://homestead.app:8000/ it shows SQLSTATE[HY000] [1049] Unknown database 'projectadmin_db'
What I have missed here? Correct me if anything wrong.
When you run your app in Homestead, it looks for databases in Homestead and not your actual localhost. You can connect to Homestead's MySQL through 127.0.0.1:33060.
On models that use projectadmin_db database, you need to add
protected $connection = 'admin_main_db';
make sure you use the default Homestead port 33060 , when you are creating your databases ,
i.e: you created your databases on the guest vm not on the host machine.
Related
I am using Laravel version 5.1.*. Here is my db config file...
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'unix_socket' => '/cloudsql/zoho-portal-159018:us-central1:zoho-portal',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
it gives error No such file or directory. It seems, unix_socket is the issue. But exact same config works when I use normal PDO connect without laravel....
$DBH_SOCKET = '/cloudsql/zoho-portal-159018:us-central1:zoho-portal';
$DBH_NAME = 'DBH_NAME';
$cns="mysql:unix_socket=".$DBH_SOCKET.";dbname=".$DBH_NAME.";charset=utf8";
$user='user';
$password='password';
try{
$DBH = new PDO($cns,$user,$password);
$DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
If It works in normal single php file without laravel... then it must should work in laravel way. But not not working.. Any comments highly appreciated. Thanks
Do i need to create multiple connection to access different database/schema. Cant I use with one dbconnection. Is there a way to pass the database name in the laravel eloquent or db builder? Currently in raw php i use one connection to query the different schema.
Create different connections to your database.php file and then pass them to your eloquent models.
'mysql1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db1',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
// connection 2
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Suppose i have model User.php uses mysql connection named mysql1
inside my model i will add :
protected $connection = 'mysql1';
if i want to use mysql connection named mysql2
then i will use
protected $connection = 'mysql2';
Here i am setting connections statically in to models.
In Eloquent, I use DB:connection() to set my named connection, like so:
$query = DB::connection('db_connection_name')->table($this->table)
If you weren't aware, the database connections are named in the config app/config/database.php
I have two VMs in vagrant.
My app is on the [web] VM (192.168.10.10).
My db is on the [db] VM (192.168.10.11).
I deployed my app to the [web] VM and then from within the [web] VM, I want to run my migrations so it migrates to the [db] VM.
Essentially, I want to migrate from one VM to another (192.168.10.10 > 192.168.10.11)
I changed my database.php file so it has the right IP and I triple checked my [db] VM to make sure that the credentials match:
return array(
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => '192.168.10.11',
'database' => 'demo',
'username' => 'demo',
'password' => 'demo',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'auth' => array(
'driver' => 'mysql',
'host' => '192.168.10.11',
'database' => 'demo',
'username' => 'demo',
'password' => 'demo',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
);
When I run "php artisan migrate" from my [web] VM it errors out:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
migrate [--bench[="..."]] [--database[="..."]] [--force] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]
Any idea what is happening to refuse the connection?
Thanks!
I want to connect in two differents databases, one mysql and other sql server
I got all the configuration and mysql works, sql server return nothing else but a white screen
Controller
Config::set('database.default', 'sqlsrv');
$teste = DB::connection('sqlsrv')->getDatabaseName();
dd($teste);
database config
'default' => 'mysql',
'sqlsrv' => array(
'driver' => 'sqlsrv',
'host' => 'xxx.xxx.xxx.xxx',
'database' => 'MP11_OFICIAL02',
'username' => 'sa',
'password' => 'asd',
'prefix' => '',
),
'MG' => array(
'driver' => 'mysql',
'host' => 'xxx.xxx.xxx.xxx',
'port' => '3306',
'database' => 'bd',
'username' => 'sistema',
'password' => 'asd',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and also if I dont change my 'default' => 'mysql' I get this error
could not find driver (View: /vagrant/app/views/usuarios/index.blade.php)
and if I change to sqlsrv just get a white screen
What version of SQL Server Native Client you are using? Please go to Control Panel >> Administrative Tools >> Data Sources (ODBC) and then click on the Drivers tab to find out which SQL Server Native Client you have already installed. Also please check your php.ini if you already install PDO driver for sqlsrv. Find 'PDO drivers' in php.ini. If you already install, you can see it will list like this. PDO Drivers => sqlsrv, mysqlsqlite
I'm creating a multi-tenant application with Laravel 4. At the moment I have two connections in my database.php
'connections' => array(
'app' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'homestead',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'tenant' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
When connecting to a tenant database in the application I call something like this:
$tenant = Tenant::where('username', '=', $username)->first();
Config::set('database.connections.tenant.username', $tenant->db_username);
Config::set('database.connections.tenant.password', $tenant->db_password);
Config::set('database.connections.tenant.database', $tenant->db_database);
As you can see all my database names/connection details for tenants are set dynamically.
this is an issue when i'm trying to run a migration as I can't find a way to set the connection details.
The only way I can do this at the moment is to call my migration from a controller. Is their a better way?