SQL SSL errors when deploying laravel project to azure - php

I deployed the project to azure and both the webpage displays properly and links work fine, but for the user registration I need to connect the database. Everything works well locally and I followed all the instructions of creating/connecting a sqsl db in azure but I get thte following 3 errors and not sure what to do, how to debug, its a project I was given to deply and didnt create myself,( I am only gona show the first 3 lines of the error since they are long):
UPDATE: It works if I disable the SSL on azure so its got something to do with my local app config for that
1-
SQLSTATE[HY000] [9002] SSL connection is required. Please specify SSL options and retry. (SQL: select count(*) as aggregate from `users` where `email` = ***#***.com)
in Connection.php line 647
at Connection->runQueryCallback('select count(*) as aggregate from `users` where `email` = ?', array('***#***.com'), object(Closure))
in Connection.php line 607
2-
(2/3) PDOException
SQLSTATE[HY000] [9002] SSL connection is required. Please specify SSL options and retry.
in PDOConnection.php line 50
at PDOConnection->__construct('mysql:host=***.mysql.database.azure.com;port=**+;dbname=uwiredb', '***#***', 'MySQLAzure2017', array(0, 2, 0, false, false))
in Connector.php line 65
at Connector->createPdoConnection('mysql:host=***.mysql.database.azure.com;port=***;dbname=***', '***#***', 'MySQLAzure2017', array(0, 2, 0, false, false))
in Connector.php line 44
3-
(1/3) PDOException
SQLSTATE[HY000] [9002] SSL connection is required. Please specify SSL options and retry.
here is the DB section of my .env file:
DB_CONNECTION=mysql
DB_HOST=mydbserver.mysql.database.azure.com
DB_DATABASE=mydb
DB_USERNAME=myuser
DB_PASSWORD=mypass
MYSQL_SSL=true
And here is the connection part of my database.php file in the config folder, I know the hosts
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'sslmode' => env('DB_SSLMODE', 'prefer'),
'options' => (env('MYSQL_SSL')) ? [
PDO::MYSQL_ATTR_SSL_KEY => '/ssl/BaltimoreCyberTrustRoot.crt.pem',
] : [],
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '1433'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
],
],
I know the host is set to local but that is how I understood it should be from the azure docs that say:
You can use the PHP getenv method to access the settings. the Laravel code uses an env wrapper over the PHP getenv. For example, the MySQL configuration in config/database.php looks like the following code:
PHP
Copy
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
...
],
Thanks

Related

How do i solve a Doctrine\DBAL\Driver\PDOException : SQLSTATE[HY000] [2002] No such file or directory - Laravel problem?

I'm using a php7.4 and I get this error whenever I try any of the artisan code
error image.
Here's my config/database.php
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
'prefix' => '',
'strict' => false,
'engine' => 'InnoDB',
],
I've tried these links but none has solved the issue:
PDOException SQLSTATE[HY000] [2002] No such file or directory
https://laracasts.com/discuss/channels/eloquent/sqlstatehy000-2002-no-such-file-or-directory
https://www.digitalocean.com/community/questions/sqlstate-hy000-2002-no-such-file-or-directory

Not able to connect Laravel app to database which is hosted on Azzure over ssl

I am struggling in connecting Laravel app with MYSQL database. I can connect to database over MySQL Workbench and also with PDO connection custom string but with Laravel I cannot. Not sure what is wrong.
Custom connection string: It works file is inside laravel app. I put this just for checking
$conn = mysqli_init();
mysqli_ssl_set($conn,NULL,NULL, "BaltimoreCyberTrustRoot.crt.pem", NULL, NULL) ;
mysqli_real_connect($conn, 'sever.host', 'server-username', 'server-password', 'database', 3306, MYSQLI_CLIENT_SSL);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
else{
echo "Connected";
}
Then I added in .env file following, beside defining username password host and db name.
BaltimoreCyberTrustRoot.crt.pem file is on same path as .env file
MYSQL_SSL_KEY=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CERT=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CA=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL_CIPHER=BaltimoreCyberTrustRoot.crt.pem
MYSQL_SSL=true
In config/database.php looks like this
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => (env('MYSQL_SSL')) ? [
PDO::MYSQL_ATTR_SSL_KEY => env('MYSQL_SSL_KEY'), // /path/to/key.pem
PDO::MYSQL_ATTR_SSL_CERT => env('MYSQL_SSL_CERT'), // /path/to/cert.pem
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_SSL_CA'), // /path/to/ca.pem
PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER')
] : [],
Not sure what is wrong, any idea?
I get these errors:
"error":"SQLSTATE[HY000] [2002] An attempt was made to access a socket in a way forbidden by its access permissions.
[{"file":"D:\home\site\wwwroot\vendor\laravel\framework\src\Illuminate\Database\Connection.php","line":624,"function":"runQueryCallback","class":"Illuminate\Database\Connection","type":"->","args":["select * from backend_token limit 1",[],{}]},
It helps to know which version of Laravel you are on to be absolutely sure, but on Laravel 8 my database config has:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('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 => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Don't change your database.php config but in your .env add MYSQL_ATTR_SSL_CA=/path/to/BaltimoreCyberTrustRoot.crt.pem
FYI sometimes if you are using private IP addresses then edit your hosts file
10.10.10.10 hostname.domain.com
Where of course the ip number matches the ip of the host.
If that doesn't work, add the cipher line from you config PDO::MYSQL_ATTR_SSL_CIPHER => env('MYSQL_SSL_CIPHER'),

Laravel 5.0 Auth uses Homestead instead of .env DB

I've setup Laravel 5.0 (that's a requirement) and set it up to use remote MySQL DB. All migrations and data interactions pass OK, but when trying to use Auth login POST, it fails with PDOException in Connector.php line 47:
SQLSTATE[HY000] [2002] No such file or directory, mentioning " PDO->__construct('mysql:host=localhost;dbname=homestead', 'homestead', 'secret', array('0', '2', '0', false, false))"
WHERE did it take that homestead DSN? Why does it skip the database.php config and .env config? If I try to add to .env some socket info (well it should not count here, right?) the socket path passes to the mentioned PDOException call stack, but hostname, login etc. NOT!
Confused. What am I doing wrong?
UPD
Here's the .env:
APP_ENV=local
APP_DEBUG=true
APP_KEY=SomeRandomString
DB_HOST=mysql.someremteohosting.net
DB_DATABASE=mydb
DB_USERNAME=myuser
DB_PASSWORD=mysuperpassword
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
And here's database.php:
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path().'/database.sqlite',
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'prefix' => '',
],
],
'migrations' => 'migrations',
],
];
Again, all migrations work. I can create, save and fetch objects in tinker without any problem. The only reference to homestead is in .env.example file.
Not sure what exactly was wrong here. The only place where I found 'homestead' in the app code was in .env.example. I deleted it an ran composer dump-autoload. After that it worked. Still surprised about laravel lurking into .example config without any reason.

How to connect to sql remote database from linux shared hosting

I have an application developed in laravel that communicate with 2 databases, once is mysql it's in linux shared hosting. The another is sql and is in a remote server.
In localhost it's work well, I use sqlsrv driver to communicate with sql database but when I pass the project to shared hosting it can't connect with database, just with mysql database.
What I have to ask the hosting provider specifically?
This is my connections parameters:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '8889'),
'database' => env('DB_DATABASE', 'dbname'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'prefix' => '',
],
'sql' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'remote_ip'),
'database' => env('DB_DATABASE', 'dbname'),
'username' => env('DB_USERNAME', 'user'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'prefix' => '',
],
First you should have the php_sql_server driver installed on the server (as php_extension).
for more info read this: https://learn.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server and (the download files) https://www.microsoft.com/en-us/download/details.aspx?id=20098
second, you should add the connection as a separate one in your env, like:
DB_HOST=127.0.0.1
DB_PORT=null
DB_DATABASE=GRH
DB_USERNAME=sa
DB_PASSWORD=sfsd42563
DB_HOST2=remote_ip
DB_DATABASE2=db_name
DB_USERNAME2=username
DB_PASSWORD2=password
adn on your config/database.php use as:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'sqlsrv' => [
'sqlsrv_Org' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST'),
'port' => null,//sqlsrv no port
'database' => env('DB_DATABASE2'),
'username' => env('DB_USERNAME2'),
'password' => env('DB_PASSWORD2'),
'charset' => 'utf8',
'prefix' => '',
],

Laravel switch mysql to mongodb

After completed my project I deiced switch mysql to mongodb. I am using Laravel 5.4 version and for mongodb use jenssegers. For laravel mongdb installation I follow jenssegers installation documentations and installation successfully completed. But after installation I go for migration and use command php artisan migrate. When I use this command shown error InvalidArgumentException in /vendor/mongodb/mongodb/src/Database.php line 83: $databaseName is invalid:
My database.php
'default' => env('DB_CONNECTION', 'mongodb'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'project_db'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin' // sets the authentication database required by mongo 3
]
],
],
My .env
DB_CONNECTION=mongodb
DB_HOST=localhost
DB_PORT=27017
DB_DATABASE=admin
DB_USERNAME=
DB_PASSWORD=
And I also facing one more problem if whatever I change in .env and database.php like if I change port 27017 to 3303 but on browser shows 27017. I cleared cache and all. I don't know what is problem in my code.
These are the steps Envoyer (made by Laravel's creator) takes to deploy an app on production -- I've annotated them below:
# Clear the old boostrap/cache/compiled.php
php artisan clear-compiled
# Recreate boostrap/cache/compiled.php
php artisan optimize
# Migrate any database changes
php artisan migrate

Categories