Connect external Database to Laravel Vapor - mysql

I need a little help connecting an external MySQL database to Laravel Vapor. The database is located on a Hetzner Server and it seems like there is a failure using a tls encrypted connection:
==> Executing Function...
Status Code: 1
Output:
In Connection.php line 712:
SQLSTATE[HY000] [2002] (SQL: SELECT * FROM KURSE_planung )
In Exception.php line 18:
SQLSTATE[HY000] [2002]
In PDOConnection.php line 40:
SQLSTATE[HY000] [2002]
In PDOConnection.php line 40:
PDO::__construct(): SSL operation failed with code 1. OpenSSL Error message
s:
error:1416F086:SSL routines:tls_process_server_certificate:certificate veri
fy failed
I already tried to disable SSL by using the following params in the database URL without any luck:
MYSQL_DATABASE_URL=mysql://username:password#sql168.your-server.de/databasename?charset=utf8mb4&sslmode=disabled&ssl-mode=disabled&useSSL=false
Does anyone know how to disable TLS when connecting to the database or what else I can do about it?
Edit:
I managed to connect to the database. My Hoster Hetzner provides a certificate which needs to be provided to the connection configuration in database.php:
'mysql' => [
'driver' => 'mysql',
'url' => env('MYSQL_DATABASE_URL'),
'host' => env('MYSQL_DB_HOST', '127.0.0.1'),
'port' => env('MYSQL_DB_PORT', '3306'),
'database' => env('MYSQL_DB_DATABASE', 'forge'),
'username' => env('MYSQL_DB_USERNAME', 'forge'),
'password' => env('MYSQL_DB_PASSWORD', ''),
'unix_socket' => env('MYSQL_DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => base_path(env('MYSQL_ATTR_SSL_CA')),
]) : [],
],
Then in the .env file one needs to set the correct path to the certificate using MYSQL_ATTR_SSL_CA.
One question remains: does anyone know how to disable SSL for MySQL on Vapor?
Best regards
Clemens

I don't know if this might help. But In my case Laravel Vapor was hosted with RedHatOS, then I defined the env with the following:
MYSQL_ATTR_SSL_CA=/etc/pki/tls/certs/ca-bundle.crt
In my case, the database is hosted on planetscale this configration a specified on the documentation

Related

Laravel 'could not find driver (SQL: insert into...' [duplicate]

This question already has answers here:
Laravel 5 PDOException Could Not Find Driver [duplicate]
(11 answers)
Closed 2 years ago.
I'm trying to set up a development environment after not having done web dev in over a year. I've installed php 7.2.7 on my computer, then installed composer and WAMP.
I'm using php artisan serve to set up a local server. I'm trying to create a new user in my database's 'users' table, using the following code in my web.php (routes) file.
Route::get('/new', function(){
User::create([
'password' => Hash::make('anything'),
'firstname' => 'Nick',
'lastname' => 'xyz',
'email' => 'xyz#ph.com',
'roleflag' => 0
]);
});
But am getting the following error:
This seems to be a pretty common error, and I have found help on other stackoverflow/laracasts posts such as:
Laravel: Error [PDOException]: Could not Find Driver in PostgreSQL
and
https://laracasts.com/discuss/channels/general-discussion/cant-connect-to-sql-server-could-not-find-driver
I've thus uncommented the two lines from my php.ini file, changed my .env and config/database.php file to have appropriate settings/connection values, etc. but still receive this error.
Relevant config.php code:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('venturebreeder', 'forge'),
'username' => env('root', 'forge'),
'password' => env('', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
I'm a bit slower at troubleshooting since it has been a while since I've done this -- can anyone see what I'm doing wrong? Help much appreciated.
What lines did you uncomment in php.ini file?
Do you have the pdo_mysql extension installed?
Remove the ; from ;extension=pdo_mysql.so and restart your WAMP server
Source: https://stackoverflow.com/a/35240511/6385459

Laravel 5 and remote mysql not working on production

In my local environment I have working a secondary remote mysql connection specified in config/database.php without any problems:
# Secondary database connection
'mysql_remote' => [
'driver' => 'mysql',
'host' => env('DB_HOST_2', 'cherokee.websitewelcome.com'),
'port' => env('DB_PORT_2', '3306'),
'database' => env('DB_DATABASE_2', 'dbname'),
'username' => env('DB_USERNAME_2', 'dbusr'),
'password' => env('DB_PASSWORD_2', 'dbpass'),
'unix_socket' => env('DB_SOCKET_2', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
But when I run this on production server with the exact same database file I get the following error, is like the remote host (cherokee.websitewelcome.com) is not used and replace it with local server host (eu01.server.plus)
SQLSTATE[HY000] [1045] Access denied for user 'dbusr'#'eu01.server.plus' (using password: YES) (SQL: select ...)
Any ideas?
Solved by adding eu01.server.plus to authorized hosts on remote mysql server. Initially I was adding the IP and not hostname.

Multiple database connection failed, PDOException in Connector.php line 47 SQLSTATE[HY000] [2002] A connection attempt failed

I am trying to connect remote database from my localhost, the cause of I want to store data localhost to remote server. Now I want to show simply users table info it get form my localhost and different variable show remote server users table info . I write the code example here :
In my Config/database.php:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'realstate'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'shahin' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '104.219.248.3'),
'database' => env('DB_DATABASE', 'laraveldb'),
'username' => env('DB_USERNAME', 'laraveldb_username'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
In Route.php :
Route::get('mpdb',function(){
$userArray = DB::table('users')->get();
echo "<pre>";
print_r($userArray);
echo "</pre><br>";
$users2 = DB::connection('shahin');
$u = $users2->table('users')->get();
echo "<pre>";
print_r($u);
});
Local Database work's properly but Remote database get error!!
ERROR : SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
To allow external web servers to access your MySQL databases, add their domain names to the list of hosts that are able to access databases on your web site.
see a hosted cpanel demo(Image)
If you want to all incoming hosts you may access like this %.%.%.%
You can see this picture
How to permit other incoming host(Image)

Laravel Can't Connect to database - Migrations - Error 2002

I have searched for a couple of hours now, and still am unable to find this.
I get 2 errors, if I use the database host as 'localhost', I get this error:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
and if I change the database host to '127.0.0.1' I get this error:
[PDOException]
SQLSTATE[HY000] [2002] Connection refused
Things I have tried:
changing where the apache / mysql server is run (either user or `josh (Apache) / josh (MySQL)
changing the port that MySQL runs on in MAMP, and putting that port in the mysql array in the database.php file
changing the host of the connection from localhost to 127.0.0.1 and back.
creating a new user in phpmyadmin
turning off the firewall
Any ideas how to fix this?
I figured it out, add this after the 'host' => '127.0.0.1':
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
So the connection would look like this:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock',
'database' => 'dbname',
'username' => 'josh',
'password' => 'pass',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
)
fist clear your config file by typing
php artisan config:clear
Then modify your .env file
APP_URL = 127.0.0.1
DB_HOST = 127.0.0.1
Hope this works for you.
IF you use lampp . You can try this:
Open file mysql config. (you can open lampp control panel in /opt/lampp/manager-linux-x64.run,after that open Configure of Mysql Database or open /opt/lampp/etc/my.cnf).
Find and see "socket =/opt/lampp/var/mysql/mysql.sock",
Add 'unix_socket' => '/opt/lampp/var/mysql/mysql.sock' to file database.php in laravel:
'mysql' => [
'driver' => 'mysql',
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'luanvan'),
'username' => env('DB_USERNAME', 'huuthang'),
'password' => env('DB_PASSWORD', '123456'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
File mysql.sock depend on your install location of lampp. I let you see if you can't find it. Good luck
I had to do the following in ubuntu 15.10
copy the file or file contents from /opt/lampp/etc/my.cnf to /etc/mysql/my.cnf
Then open database.php file in your app/config folder and add the following line below 'host' => env('DB_HOST', 'localhost')
'unix_socket' => '/opt/lampp/var/mysql/mysql.sock'
That solved my problem, could save someone else's. Please note that it is VERY IMPORTANT you first verify the directory which contains mysql.sock file before configuring the path.

Laravel Migration - Says unknown database, but it is created

when I use php artisan migrate, i get the error SQLSTATE[42000] [1049] Unknown database 'databaseName'.
But the database DOES exists! I even tried going back into terminal, logged into mysql and created the database again, and it said that database already exists!
Why is this giving me this error?
I have the same problem. When I run: php artisan migrate.
In my case I use Vagrant with scotch box.
To solve this, enter:
vagrant ssh
cd /var/www/yourproject
php artisan migrate
And all work well.
In your app/config/database.php file change the default value from databaseName to a real database name that you are trying to use in your application, something like this (for mysql driver):
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'your database name', //<-- put the database name
'username' => 'your user name',
'password' => 'your password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
One thing could be your casing. If you read the docs on case sensitivity it says:
the case sensitivity of the underlying operating system plays a part
in the case sensitivity of database and table names. This means
database and table names are not case sensitive in Windows, and case
sensitive in most varieties of Unix. One notable exception is Mac OS
X, which is Unix-based but uses a default file system type (HFS+) that
is not case sensitive.
Then go and check your application database setting found # app/config/database.php.
Something that looks like this:
'mysql' => array(
'read' => array(
'host' => '192.168.1.1',
),
'write' => array(
'host' => '196.168.1.2'
),
'driver' => 'mysql',
'database' => 'databaseName',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
And double check everything.
Also try using snake_case rather than camelCase for you database name.
In my case I had MySQL installed on this port: 3308, and in Laravel env file there was 3306.