I'm working on our database and I use for that a former project I did.
I used to work with a phpmyadmin version where I didn't need to specify the server choice.
Today I work on the version 4.1.14.8 and I have to specify the server number.
What I like to know is how can I specify that in my connection string by using doctrine.
Here's what I have for the moment :
<?php
// Doctrine (db)
$app['db.options'] = array(
'driver' => 'pdo_mysql',
'charset' => 'utf8',
'host' => 'localhost',
'port' => '3306',
'dbname' => 'dbname',
'user' => 'usr',
'password' => 'pwd',
);
Thanks
The array value:
'host' => 'localhost',
Is what you use to specify the server to use. In the above it is set to 'localhost'. You could just enter in the IP address if your MySQL database is on another host.
Related
I have two applications both developed in Laravel.I have logged into the first application and doing coding. But when I try to log into the second application it gives an error of the missing 'users' table(the table in the second database I am trying to log into now which is not in the first logged in an application).
Seems like the second application is trying to authenticate the user using the first database which is not correct
Can anyone assist me with this
Dear Laroja as i get you want to authenticate with another application which have separate database then you have to define two database auth and try to build the logic if connect to first application then it uses the first database and if you want to connect with another application it will connect to other database.
Please have a sapmle code for connect two databse
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'qmops_live_server',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
# Secondary database connection
'mysql2' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'port' => env('DB_PORT', '3306'),
'database' => 'XXX',
'username' => 'XXX',
'password' => 'QXXXX',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
please use the above code in app/config/database.php hope my solution will help you Thanks.
Ok, my system connects to remote databases on which I consistently change depending on what data I want to access.
The setup is multiple identical structure databases resides on different IP addresses, so I always update the .env DB_HOST line every time I want to access different database.
Now I'm planning to create a view on my system that will accepts an IP address and set it as the current database to use.
Referencing this answer on Laracasts:
Set two database connections in your config (one for your unchanging main connection, the other one for your custom dynamic connection):
'main' => array(
'driver' => 'mysql',
'host' => 'hostname',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'prefix' => '',
),
'dynamic' => array(
'driver' => 'mysql',
'host' => '',
'database' => '',
'username' => '',
'password' => '',
'prefix' => '',
),
Then change your database connection parameters for the second connection as needed:
Config::set('database.connections.dynamic.host', $newHost);
Config::set('database.connections.dynamic.username', $newUsername);
Config::set('database.connections.dynamic.password', $newPassword);
Config::set('database.connections.dynamic.database', $newDatabase);
Config::set('database.default', 'dynamic');
And reconnect the database:
DB::reconnect('mysql');
I am returning a PDOException saying I could not connect to the server and I cannot figure out why... I am Following PHPAccademy's guide on YouTube.
This is the code used to define the connection parameters
'db' => [
'driver' => 'mysql',
'host' => '107.170.30.229',
'name' => 'bsa',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8mb4_general_ci',
'prefix' => ''
],
This is the code that is used to connect to the database
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => $app->config->get('db.driver'),
'host' => $app->config->get('db.host'),
'database' => $app->config->get('db.name'),
'username' => $app->config->get('db.username'),
'password' => $app->config->get('db.password'),
'charset' => $app->config->get('db.charset'),
'collation' => $app->config->get('db.collation'),
'prefix' => $app->config->get('db.prefix'),
]);
$capsule->bootEloquent();
Firstly, please DO NOT post any username/passwords onto SO, regardless of development or not, not everybody reading has a good heart.
Secondly, as i was able to log into your server and test this myself, it seems that you need to use localhost instead of defining your server IP for the host.
So please use:
'host' => 'localhost',
Instead of:
'host' => '107.170.30.229',
I have been using Laravel for a while but mostly on a single database server.
Now, my client has got atleast 5 database from multiple database servers in the same network.
I am able to confirm the communication in between servers using ping through ssh and also using the
mysqli_connect to check the database connections.
However, when I use laravel through its database.php (mysql configs) like :
// This setup is working fine (using localhost)
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'dbname',
'username' => 'user',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
// Other DB (which is failing to connect) but i can confrim the connection using a file outside
// the laravel app through mysqli
'other_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.241',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
'another_db_connection' => array(
'driver' => 'mysql',
'host' => '192.168.33.211',
'database' => 'dbname',
'username' => 'username',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and using the new PDO
// This connectiion functions with no problem
$conn1 = new PDO('mysql:host=localhost;dbname=dbname;port=3306','username','password');
if($conn1) {
echo 'connected';
}
// But this returns an error
$conn = new PDO('mysql:host=192.168.33.241;dbname=dname;port=3306','username','password');
if($conn) {
echo 'connected';
}
// Also, i have no problems using the DB:: or even the model as long as the host is the localhost
it fails to connect, both the PDO and the laravel default DB feature only works if the host is the localhost.
For some reason it throws back an error like this if i use the other servers ip, which is where i would like to communicate with:
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.241' (13)
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.33.211' (13)
When i try using the server ip or the main domain name to connect.
Am I missing something? Why is it that I can connect only on the local db server which is localhost
and not to any other database servers? But I can connect to them with no problem if I use say an index.php file from outside the laravel application and use the mysqli_connect to make queries and make connections?
Thanks for all your help.
You can access all the connections you have specified within database.php with:
$user = DB::connection('other_db_connection')
You don't need to setup new PDO connections or similar, will work like above for you
php artisan migrate:install
{"error":{"type":"ErrorException","message":"PDO::__construct(): [2002] Connection refused (trying to connect via tcp:\/\/127.0.0.1:3306)","file":"\/Applications\/MAMP\/htdocs\/DRCSports\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connectors\/Connector.php","line":47}}
In my database.php I have updated the information to mysql
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'Laravel_DRCSports',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
I am not sure if I am understanding the error right, but to me it looks like my laravel isn't connecting to mysql right. If that is the case I have no clue how to fix it.
The problem was that mysql is running on port 8888, while Laravel's default port value is 3306 (as it's the default port of mysql servers).
The solution is to add 'port' key to the array (For example: 'port' => 8888) and it'll
do the work.
This is what i did... in /app/config/app.php
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost:8889',
'database' => 'pic',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
and at the bottom of the php code
'redis' => array(
'cluster' => false,
'default' => array(
'host' => '127.0.0.1',
'port' => 8888,
'database' => 0,
),
),
it has to work it...
I experienced problems (Laravel 4) when I used MySQL on a port other than 3306.
A browser-run app expects the following app/config/database syntax:
'mysql' => array(
...
'host' => 'localhost',
'port' => '8889',
...
)
While the command-line run artisan expects the following syntax:
'mysql' => array(
...
'host' => 'localhost:8889',
...
)
The issue is described here:
https://github.com/laravel/laravel/issues/1182
Most articles suggest a workaround using Laravel environments, but it results in duplicate config files and violates the DRY principle (Don't Repeat Yourself), so here's another alternative:
At the top of app/config/database.php:
$my_hostname = 'localhost';
$my_port = '8889';
$my_database = 'database';
$my_username = 'username';
$my_password = 'password';
if (App::runningInConsole()) { // artisan runs from the command line
// change 'localhost' to 'localhost:8889'
$my_hostname = $my_hostname.':'.$my_port;
}
And further down:
'mysql' => array(
'driver' => 'mysql',
'host' => $my_hostname,
'port' => $my_port,
'database' => $my_database,
'username' => $my_username,
'password' => $my_password,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Cheers
Change your database information in your config/database.php and .env file.
I did these changes and worked like a champ :
in database.php :
host : localhost:8889
Port: 8889
and my mamp has a password so I there was two way to putting that password either in database.php file or in .env file I changed the password '' value to 'forge' and then use my MAMP password in the .env file
by the way, you can see your specific information about MAMP in MAMP application in the port tab (MySQL one)
Make sure to edit this part of ".env" file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=8889
DB_DATABASE=blog
DB_USERNAME=root
DB_PASSWORD=root
This worked for me.
i guess you can solve this problem by add the following code inside app/config/database syntax:
'mysql' => array(
...
'pconnect' => 'TRUE',
...
)