I have granted access for the Amazon RDS MySQL instance (5.7.19) for 0.0.0.0/0 as suggested by Heroku. This makes the connection work successfully. When I force SSL for the mysql-user (ALTER USER 'user'#'%' REQUIRE SSL;) the connection breaks. I have followed these instructions from Heroku.
My Heroku DATABASE_URL config variable:
mysql://username:password#AMAZONRDSMYSQLURL/DATABASE?sslca=config/amazon-rds-ca-cert.pem
The certificate is stored under /config/amazon-rds-ca-cert.pem
From my localhost terminal I can connect via SSL to the Amazon RDS instance (with the same certificate from above) using this command (works also without --ssl-mode=VERIFY_IDENTITY):
mysql -h AMAZONRDSMYSQLURL --ssl-ca=/Users/Documents/amazon-rds-ca-cert.pem --ssl-mode=VERIFY_IDENTITY -u USERNAME -p
My database configuration in Lumen (/config/database.php):
<?php
$url = parse_url(getenv("DATABASE_URL"));
$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'port' => '3306',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
Any idea whats going wrong here? Thanks!
You haven't told your Database file to use SSL.
<?php
$url = parse_url(getenv("DATABASE_URL"));
$host = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$database = substr($url["path"], 1);
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'port' => '3306',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => array(
"sslmode" => "require",
"sslrootcert" => "config/amazon-rds-ca-cert.pem"
)
],
I finally made it work with:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'port' => '3306',
'host' => $host,
'database' => $database,
'username' => $username,
'password' => $password,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'options' => array(
PDO::MYSQL_ATTR_SSL_CA => '../config/amazon-rds-ca-cert.pem'
)
],
Related
I'm working with laravel on Manjaro and I installed php and Mariadb.
in my laravel project when it wants to connect to database it show me "Illuminate\Database\QueryException
could not find driver (SQL: select * from users where email = sample.co#example.com limit 1) ";
database config:
'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'),
]) : [],
],
My env file:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=farsifor_m
DB_USERNAME=root
DB_PASSWORD=mypassword
I replaced php8 by php 7.4 . It solved the issue.
I try to make 2 connections to a database in laravel. I always get:
error database [] not configured.
Pls help me master.
This is my controller:
class DashboardController extends Controller
{
public function index(request $request)
{
$tes = DB::connection('mysql2')->select('SELECT * from hari');
// $tes = tes::find(1);
dd($tes);
}
---env--
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crud55
DB_USERNAME=root
DB_PASSWORD=
DB_DATABASE_2=db_susantokun
DB_USERNAME_2=root
DB_PASSWORD_2=
------------database.php
'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'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_2', 'db_susantokun'),
'username' => env('DB_USERNAME_2', 'root'),
'password' => env('DB_PASSWORD_2', ''),
'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'),
]) : [],
],
This error occurs when calling http://localhost:8000/dashboard
this is InvalidArgumentException
Database [mysql2] not configured.
how to fix it?
You may have to rebuild the configuration cache. Run the following command in your laravel root directory:
php artisan config:cache
after chnage .env file you should restart php artisan serve service
I deployed my project to an AWS EC2 instance using Elastic Beanstalk. I used this tutorial https://www.youtube.com/watch?v=ISVaMijczKc as a reference while deploying. I am following everything as it is in the tutorial but I ended up with an error.
Database hosts array is empty. (SQL: select * from
resource_categories)
The following are my codes.
database.php
<?php
define('RDS_HOSTNAME', $_SERVER['RED_HOSTNAME']);
define('RDS_USERNAME', $_SERVER['RED_USERNAME']);
define('RDS_PASSWORD', $_SERVER['RED_PASSWORD']);
define('RDS_DB_NAME', $_SERVER['RED_DB_NAME']);
return [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'host' => RDS_HOSTNAME,
'port' => env('DB_PORT', '3306'),
'database' => RDS_DB_NAME,
'username' => RDS_USERNAME,
'password' => RDS_PASSWORD,
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => false,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'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' => '',
'prefix_indexes' => true,
'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' => '',
'prefix_indexes' => true,
],
],
'migrations' => 'migrations',
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
];
.ENV
APP_NAME="MyProject"
APP_ENV=development
APP_KEY=base64:FlVBd61BUEzVx6ACa6OOn3Jrp4z+VRpug+F1K1ZeJOs=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myDB
DB_USERNAME=root
DB_PASSWORD=
I've gone through a similar problem and MarkB helped me. There's a specific procedure you must follow when you SSH into the instance.
If you run
export
you can see that there's no variable named RDS_HOSTNAME in that Linux shell and that's why you're getting that error.
If you run
/opt/elasticbeanstalk/bin/get-config environment
you can see an object with the list of properties, including that RDS_HOSTNAME.
If you run
/opt/elasticbeanstalk/bin/get-config environment -k RDS_USERNAME
you get the value associated with that particular property. This value needs to be saved in a variable and exported so that other commands can recognize it.
If you run
export RDS_USERNAME="value"
then when you run
export
you can see that this is now available.
Now if you run the command you wanted it's likely gonna work (you might need to repeat this for RDS_USERNAME, RDS_PASSWORD and RDS_DB_NAME).
Note: if that didn't work, then your problem might be similar to this one.
This is a bug report which might be related to this problem. The long and the short is that if the ConnectionFactory gets passed a NULL value it will (erroneously) trigger that error. I suspect that all of your RDS variables are empty.
To set these values, you can follow instructions here.
I want to use mysql and mongoDB in my laravel project, I know I can define multiple connections array in database.php file and call them like :
$users = DB::connection('foo')->select(...);
but my problem is how can I use mongoDB and mysql alongside each other in a project?
The real problem here is .env file, because it only uses one database configurations.
so let me clear this for you this is my database.php file in laravel v5.3 :
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'iranad'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'mysql'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '27017'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'options' => [
'database' => 'admin'
]
],
],
And this is my .env file :
APP_ENV=local
APP_KEY=base64:NN3Me+qA1UOfdYW2SQyAXtxODazCAYBAKfFdRAqcakg=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=mysql
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=
PUSHER_KEY=
PUSHER_SECRET=
As you can see my default database connection is mysql, and in .env file configuration is set to mysql, now how can I use mongoDB in my application ?
FYI : I want mysql to be default connection and I use mongodb in some cases.
I've found the solution thanks to #astroanu
you can change the the default env variable names, it will not break the functionality of you appication
you can change your database.php to something like this:
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('MYSQL_DB_HOST', 'localhost'),
'port' => env('MYSQL_DB_PORT', '3306'),
'database' => env('MYSQL_DB_DATABASE', 'iranad'),
'username' => env('MYSQL_DB_USERNAME', 'root'),
'password' => env('MYSQL_DB_PASSWORD', 'mysql'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', '27017'),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => [
'database' => 'admin'
]
],
],
then on the .env define each variable name
#sql
MYSQL_DB_HOST=...
MYSQL_DB_PORT=...
MYSQL_DB_DATABASE=...
MYSQL_DB_USERNAME=...
MYSQL_DB_PASSWORD=...
# mongo
MONGO_DB_HOST=...
MONGO_DB_PORT=...
MONGO_DB_DATABASE=...
MONGO_DB_USERNAME=...
MONGO_DB_PASSWORD=...
on your models define protected connection attribute: this should be either of your connection names you defined on the database.php
protected $connection = 'mongodb';
the only problem for you here would be implementing relations between databases, which is impossible, you will need to write your own queries for that.
FYI : For testing your connections in tinker :
DB::connection('mongodb')->collection('migrations')->get();
Notice mongodb in here is the connection name in database.php file, and migrations is the collection name.
I'm using laravel and let say I'm using 2 connection
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'localDatabase'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'mysql_server' => [
'driver' => 'mysql',
'host' => 'serverHost',
'port' => '3306',
'database' => 'serverDatabase',
'username' => 'serverDummy123',
'password' => 'dummy123',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and my DB connect to server
$dbServer = DB::connection('mysql_server');
after that I just want to select data from my localhost database
$sql = "select * from localDatabase.users";
echo print_r($dbServer->select($sql));
And then I tried to give my localhost DB some privilege
username : serverDummy123
hostname : serverHost
type : database-specific
privileges : All privileges
grant : yes
But all still not working.
You can also configure .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=localDatabase
DB_USERNAME=root
DB_PASSWORD=