How to Connect Remote Database in Laravel 5 using ssh - php

I am using Laravel 5.0. I Want to Know How to Access Remote Database using SSH.
database.php
'mysql' => [
'driver' => 'mysql',
'host' => 'www.xxxxx.in',
'port' => '2222',
'database' => 'xxxx_xxx',
'username' => 'xxxxx_xx',
'password' => 'xxxx0xx',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],

You should create SSH tunnel.
More about SSH tunnel and some examples here:
http://chxo.com/be2/20040511_5667.html
Example:
ssh -fNg -L 3307:127.0.0.1:3306 myuser#remotehost.com
mysql -h 127.0.0.1 -P 3307 -u dbuser -p db
Of course, then you need to change credentials:
'mysql' => [
'driver' => 'mysql',
'host' => 'www.xxxxx.in',
'port' => '3307',
'database' => 'xxxx_xxx',
...
],

You will need to create an SSH Tunnel as Alexey says.
You will also need to port-forward the MySQL connection port to your local; as Alexey has done also.
Then in your Laravel config, set the port to the forwarded port. So building off Alexey's answer, your database configuration would read thus
'mysql' => [
'driver' => 'mysql',
'host' => 'www.xxxxx.in',
'port' => '3307',
'database' => 'xxxx_xxx',
'username' => 'xxxxx_xx',
'password' => 'xxxx0xx',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
EDIT
In case Alexey's answer goes away, these are the relevant parts to my answer
Create the ssh tunnel
ssh -fNg -L 3307:127.0.0.1:3306 myuser#remotehost.com
Connect locally using
mysql -h 127.0.0.1 -P 3307 -u dbuser -p db

Related

Database connection settings with laravel 5.2

I'm developing an application with laravel 5.2, I'm trying to connect a remote database, if I do that in the command line I have to write this:
mysql -u root -p -h 'remote_ip' --skip-secure-auth
if I write that in the command line I don't have any problem.
But now I have to do the connection with laravel, how should I write the options?
--skip-secure-auth on my database.php file
'estudiantesdb' => [
'driver' => 'mysql',
'host' => 'remote_ip',
'port' => '3306',
'database' => 'estudiantesdb',
'username' => 'root',
'password' => 'root_password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
mysql -u root -p -h 'remote_ip' --skip-secure-auth
Here, your password is empty. So in configurations too you have to set password as empty:
'password' => ''
Your config looks like this:
'estudiantesdb' => [
'driver' => 'mysql',
'host' => 'remote_ip',
'port' => '3306',
'database' => 'estudiantesdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],

Laravel 5 with homestead having unknown database

Hi I am using Laravel 5 with homestead. I am experiencing homestead first time so facing problem. I have created a database 'myDb' and imported data into this. I have installed vagrant and homestead, cloned my code and configured homestead.yaml file accordingly. I have set up my .env. When I try to run my project is is giving me unknown database myDb error. I can see my database in my phpmyadmin but I do not know why it is giving me this error. My .env file looks like
DB_HOST=localhost
DB_DATABASE=myDb
DB_USERNAME=homestead
DB_PASSWORD=secret
And I have same settings in my config/database.php too.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'myDb'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
Its really making me mad. What Am I doing wrong? I am getting following
in Connector.php line 55
at PDO->__construct('mysql:host=localhost;dbname=myDb', 'homestead', 'secret', array('0', '2', '0', false, '0')) in Connector.php line 55
at Connector->createConnection('mysql:host=localhost;dbname=myDb', array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql'), array('0', '2', '0', false, '0')) in MySqlConnector.php line 22
at MySqlConnector->connect(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 60
at ConnectionFactory->createSingleConnection(array('driver' => 'mysql', 'host' => 'localhost', 'database' => 'myDb', 'username' => 'homestead', 'password' => 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, 'name' => 'mysql')) in ConnectionFactory.php line 49
One common mistake that you might be making is that you're probably accessing MySQL server on your localhost while you should remember that Homestead is a complete virtual dev. environment. It has it's own Web Server, MySQL server and etc.
To see if you have "myDB" on the Homestead MySQL server as well, try accessing homestead via the following command.
homestead ssh
Once done that, try opening the mysql console via the mysql command.
mysql -u username -p
And after that list all your databases using
show databases
If you can't see "myDb" database there, than you should try connecting to Homestead MySQL server using Navicat or MySQL Workbench and move your database from your local MySQL server to the Homestead MySQL server.
That would probably fix your problem.
If you are migrating your database, check if you are migrating the same database name inside your Homestead.yaml file and the database name in your .env file.
Inside Homestead.yaml file:
databases:
- laravel
Inside your .env file:
DB_DATABASE=laravel

MAMP with Laravel Unix Socket

I'm working with MAMP on my local development server on my laravel application and I'm trying to figure out how I can safely setup my server so I don't have to use the following into the database connections mysql array because that should only be used when I'm on my development server. It works when I add the line into the mysql array however that isn't used if I was on a production server. Any ideas?
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
.env.development.php
<?php
return [
'DB_HOST' => '127.0.0.1',
'DB_USERNAME' => 'root',
'DB_PASSWORD' => '1234',
'DB_NAME' => 'mytable'
];
app/config/database.php
'connections' => array(
'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' => '',
),
There is even simple solution. add this to ur .env file
DB_HOST=localhost;unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock
On config/database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST'),
'unix_socket' => env('UNIX_SOCKET'),
'port' => env('DB_PORT'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
On .env:
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=mytable
DB_USERNAME=root
DB_PASSWORD=1234
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Check the environment detection part in the bootstrap/start.php. You should add your machine's name to the array that has local key. (If you don't know your machine's name, run hostname in terminal. If it's something stupid, Google how to change it. It's pretty simple.) Then copy and paste your database configurations to app/config/local/database.php. Create the file if it doesn't exists.
Make sure MAMP preference is set to Apache port: 80, Nginx Port: 80, MySQL Port: 3306
Here's what worked for me with Laravel 5.7:
go to config/database.php and find the line 54 below:
before:
'unix_socket' => env('DB_SOCKET', ''),
After:
'unix_socket' => env('DB_SOCKET', '/Applications/MAMP/tmp/mysql/mysql.sock'),
Save the file.
Then in terminal run:
php artisan config:cache
php artisan migrate
If none of the above solutions worked for you,
Try actually starting your webserver as this was the fix for me

Laravel Homestead can't connect to db

I'm having trouble connecting to the database when using Laravel Homestead. I can't really understand why it doesn't work I've tried god damn everything I guess. Now i got stuck and can't try much more and I start to interact with more people. Does stackoverflow have any good answer or advice to give me? :)
This is my settings
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'port' => '33060'
)
I've also tried this with the same response.
'mysql' => array(
'driver' => 'mysql',
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USERNAME'],
'password' => $_ENV['DB_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'port' => '3306'
)
This is my ENV variables.
{
host: "127.0.0.1",
database: "homestead",
username: "homestead",
password: "secret"
}
The return is:
PDOException
SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (111)
In both Migrations as the website.
It works to connect with Sequel Pro, it looks like this in Sequel Pro.
EDIT:Is it possible that there's some kinda collision between my VagrantBox's IP and the MAMP one? I've stopped every MAMP thing I've running but still the same issue, just wondering if I need to stop it somewhere else?
You shouldn't need to specify the port at all, I'd remove that line. For host, try localhost instead. Also make sure that Homestead uses the environment you have set your database to, most likely local.
MAMP can't conflict with it, unless you've at some point manually set your MAMP MySQL port to 33060, which I very much doubt.
Here's a default file which works great for me:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'mydbname',
'username' => 'homestead',
'password' => 'secret',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
Lycka till!
What have you got when you open the PHP REPL by typing php artisan tinker at the root of your project and then print_r(Config::get('database.connections')['mysql']); ?
You should have this:
Array
(
[driver] => mysql
[host] => localhost
[database] => homestead
[username] => homestead
[password] => secret
[charset] => utf8
[collation] => utf8_unicode_ci
[prefix] =>
[port] => 33060
)
If you have something different your environment variables are not good or you are not detecting the local environment.

Laravel 4 migration:install ErrorException

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',
...
)

Categories