I am not finding a instructions on how to run a Yii project on a different driver than PDO.
Our configuration is similar to the following:
'components' => [
'db' => [
'class' => '\yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
Related
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'
)
],
i am trying to send mail via swift mailer using yii2 but it is getting this error.
Swift_TransportException Connection could not be established with host smtp.gmail.com [Connection timed out #110]
when i send mail using local host than it is sending but when i used the these settings on my test server than it is not sending the mail.
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=db553769847.db.1and1.com;dbname=db553769847',
'username' => 'myusername',
'password' => 'mypassword',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport'=>false,
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host'=>'smtp.gmail.com',
'username' => 'myuserename',
'password' =>'mypassword',
'port'=>'587',
'encryption'=>'tls',
],
],
],
];
I have deploy a Yii2 website from one server to godaddy. Its showing an error :-
'If you receive error " Connection could not be established with host smtp.gmail.com [Connection refused #111] "'
I have set the mail configs below:-
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 't********m#gmail.com',
'password' => '***************',
'port' => '465',
'encryption' => 'tls', //depends if you need it
],
],
I resolved it by just commenting the transport tag as below:-
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
//'transport' => [
// 'class' => 'Swift_SmtpTransport',
// 'host' => 'smtp.gmail.com',
// 'username' => 't********m#gmail.com',
// 'password' => '***************',
// 'port' => '465',
// 'encryption' => 'tls', //depends if you need it
// ],
],
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 want to send emails via my gmail account.
My mailer config:
[
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,//set this property to false to send mails to real email addresses
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'my#gmail.com',
'password' => 'pass',
'port' => '587',
'encryption' => 'tls',
],
]
I wrote command MailController:
<?php
namespace app\commands;
use yii\console\Controller;
use Yii;
/**
* Sanding mail
* Class MailController
* #package app\commands
*/
class MailController extends Controller
{
private $from = 'my#gmail.com';
private $to = 'to#gmail.com';
public function actionIndex($type = 'test', $data = null)
{
Yii::$app->mailer->compose($type, ['data' => $data])
->setFrom($this->from)
->setTo($this->to)
->setSubject($this->subjects[$type])
->send();
}
}
When I'm trying to run: php yii mail
I get: sh: 1: /usr/sbin/sendmail: not found
But why it requires sendmail if I want just SMTP connection to smtp.gmail.com?
I think you have configured the mailer wrongly. Because it is still using the default mail function. From the documentation the configuration should be like below. The mailer should be inside components.
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'username',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
],
],
...
],
One more suggestion is to use port "465" and encryption as "ssl" instead of port "587", encryption "tls".
Yii2 Has different config files for web and console works. So you need to config both of them. Regarding this issue, I had to make mail config file (for example mailer.php) and include it in both config files (web.php & console.php) like:
'components' => [
...
'mailer' => require(__DIR__ . '/mailer.php'),
...
],
Might be useful for someone as reference:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'username',
'password' => 'password',
'port' => 587,
'encryption' => 'tls',
'streamOptions' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
],
]