How to change database queue connection in laravel queue - php

Im currently using laravel queue and works perfectly fine when I only have 1 database connection. But now that I need 2 database connection (one for local, one for production). How can I set the local as my connection for my database queue?
What I tried
queue.php
'database' => [
'connection' => 'mysql_local',
'driver' => 'database',
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
'after_commit' => false,
],
So I've added a connection key but queue still using my other connection.
.env
DB_CONNECTION=mysql
DB_HOST=PRODUCTIONIP
DB_PORT=3306
DB_DATABASE=core3
DB_USERNAME=adm
DB_PASSWORD=secret1234
DB_CONNECTION_LOCAL=mysql_local
DB_HOST_LOCAL=127.0.0.1
DB_PORT_LOCAL=3306
DB_DATABASE_LOCAL=core3
DB_USERNAME_LOCAL=root
DB_PASSWORD_LOCAL=secret
I need to use the mysql_local connection, but no idea on how to use it.

Related

Laravel app hosted on Heroku with a Amazon RDS MySQL Database results in a connection time out

I have a Heroku app where I'm hosting my Laravel app. I started the development initially with MySQL, so I wanted to continue doing so using Amazon's RDS service. I create the instance there and managed to successfully connect via my MySQL client, the console etc.
The problem is that the Laravel app can't connect the database after numerous desperate attempts for me to fix it. I have found some articles suggesting the use of DATABASE_URL environment variable is mandatory, so I added it via the Heroku app settings. It looks like so:
mysql://myusername:mypass#myhostnamefromamazon/mydb?sslca=/app/storage/certs/amazon-rds-ca-cert.pem
I found this solution on Heroku's website. I have placed the amazon-rds-ca-cert.pem file on my Laravel's storage folder, like so: /app/storage/certs/amazon-rds-ca-cert.pem
This didn't solve my issue, so then I kept looking and found a Stackoverflow question which had this issue on Lumen. I adjusted my config/database.php according to the answer, but it's still not working for me!
<?php
$credentials = get_db_credentials();
$config = [
'default' => env('DB_CONNECTION', 'mysql'),
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', $credentials->host),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', $credentials->database),
'username' => env('DB_USERNAME', $credentials->username),
'password' => env('DB_PASSWORD', $credentials->password),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
],
];
if (env('APP_ENV') == 'production') {
$config['connections']['mysql']['options'] = [PDO::MYSQL_ATTR_SSL_CA => '../storage/certs/amazon-rds-ca-cert.pem'];
}
return $config;
The get_db_credentials() function simply parses the DATABASE_URL environment variable.
The exact exception that I get is:
[2018-10-25 19:32:16] production.ERROR: SQLSTATE[HY000] [2002] Connection timed out {"exception":"[object] (Doctrine\\DBAL\\Driver\\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /tmp/build_05920c42a6de0a378402b798320d3f04/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:50
I'm totally lost on this and unsure how to proceed.
Your AWS Security Groups must permit traffic from Heroku's IP address range.
https://devcenter.heroku.com/articles/amazon-rds
You must grant Heroku dynos access to your RDS instance. The recommended way to do this is to configure the RDS instance to only accept SSL-encrypted connections from authorized users and configure the security group for your instance to permit ingress from all IPs, eg 0.0.0.0/0.

Redis server connection error in laravel v4.2

Hi I working in laravel v4.2. Now I want to send push notifications to users in queue so for this purpose I use redis server for queue push notifications. But the problem is that I want use the redis server of of other server instead of home/local server. Is there any solution to solve this problem. Below is my queue.php file configuration that I am using
'redis' => array(
'driver' => 'redis',
'queue' => 'default',
'cluster' => true,
'default' => array(
'host' => '192.168.1.244',
'port' => 6379,
'database' => 0,
'password' => ''
),
If I use my own pc IP address then I receice queue notification but when I try to use the other pc redis server then I receive following error in exception
Connection refused [tcp://127.0.0.1:6379]
Please let me know if anyone know that how to cofigure or user redis server remotely.
Thank you
I found the issue issue in my code I was doing configuration in queue.php instead of database.php file. So when I change the host in database.php file then I become able to work with redis server.

running laravel queue with multiple database

I have Laravel 5 project. This is multi-tenant project, so I have one folder project with multiple databases.
When I use php artisan queue:listen, it only works with the current database setup. I use the database queue driver, so each tenant has their own notifications table. How do I setup a queue listener to check all database jobs?
If you want to continue using the database driver, I would suggest setting up another database that contains all your queued jobs and failed jobs.
Although it is not in the config or mentioned by the documentation, after taking a look at the code, it looks like you should be able to add a connection parameter to your queue configuration, and then the queue will interact with the database specified by that connection.
So, in your config/database.php, define a new connection for your queue database:
'connections' => [
// your existing connections
'queue' => [
'driver' => 'mysql',
'database' => 'your-queue-database',
// rest of connection information (host, port, etc)
],
],
Then, in your config/queue.php, tell your database queue to use your new connection:
'connections' => [
'database' => [
'driver' => 'database',
'connection' => 'queue', // connection name from database config
'table' => 'jobs',
'queue' => 'default',
'retry_after' => 90,
],
]
The other option would be to move to another queue driver. Setup redis, sqs, or beanstalk.

Phalcon, Docker MySQL connection fails

So I have 3 docker containers, one with PHP+PhalconPHP, one with MySQL and another with the webserver. Now on my host system I can connect to the MySQL in the docker container and I can also connect with PDO using the following code
new PDO('mysql:host=mysql;port=1024;dbname=database', 'root', 'password');
But for some reason the phalcon framework is unable to connect to the MySQL database using the config.php file like so
return new \Phalcon\Config([
'database' => [
'adapter' => 'mysql',
'port' => 1024,
'host' => 'mysql',
'username' => 'root',
'password' => 'password',
'dbname' => 'database',
'charset' => 'utf8',
],
]);
In this case, for some reason I am getting a Can't connect to MySQL server on 'mysql' (111 "Connection refused) even though I am 100% sure I can actually connect (as proven before with the PDO connection). I also tried hardcoding the actual MySQL container ip address but no luck either.
Any ideas?
So it appeared to be kind of a stupid "mistake" but here's the solution.
Apparently when you use the Phalcon Developer Tools to scaffold your project, it does not initialize the database with the port configuration as it outright ignores it. You can easily fix this by going into the app/config/services.php file and changing the db service to also include the port configuration.
$connection = new $class([
'host' => $config->database->host,
'port' => $config->database->port,
'username' => $config->database->username,
'password' => $config->database->password,
'dbname' => $config->database->dbname,
'charset' => $config->database->charset
]);

Laravel 5 store session in database/redis

I want to use database (currently, MySQL) to store session. But, when I tried to config, table sessions still has no record.
In file config/session.php when I change 'driver' => env('SESSION_DRIVER', 'file'),
to 'driver' => env('SESSION_DRIVER', 'database'),
or 'driver' => env('SESSION_DRIVER', 'redis'),
What should I change for 'connection' => null,?
I tried:
'connection' => 'mysql', with mysql is configured in app/database.php
'connection' => 'database.connections.mysql',
'connection' => Config::get('database.connections.mysql'), got class not found error
All you need to do is specify the right driver in the environment file, and generate the schema as outlined in the documentation.
This line:
'driver' => env('SESSION_DRIVER', 'database')
This is telling the config to get the SESSION_DRIVER variable from your environment file, and use database as a default. I can only assume you have forgotten to update your .env file.
I hade to change SESSION_DRIVER=database in .env file
config/session.php changes didn't work for me

Categories