Working locally, can't connect in Laravel to remote DB - pdo

I'm trying to work locally in Laravel and am having trouble connecting to a remote database. I've used my domain name (domain.org) as my host, and then password, username and database name as normal. But I get this error:
PDO::__construct(): Premature end of data (mysqlnd_wireprotocol.c:553)
If I look at the file where the error is happening, it's in my PDO connection in the MySQL driver. I asked my ISP about settings and he said to use domain.org or the IP. I tried both and I still get the error. I tried setting the port to the standard 3306 and still get the error. I can't use localhost as my host because that would refer to my local computer. Can someone help? What do I need to do to connect?

I think this is a difference between the encryption used in the version of MySQL on your web host, and that used by your PHP version.
Have a look at the accepted answer on this question:
Remote mySQL connection throws "cannot connect to MySQL 4.1+ using the old insecure authentication" error from XAMPP
As regards a solution, your web host may or may not be willing to change your MySQL settings - but my guess is that if they are running an old MySQL, they will also be running an old version of PHP. Laravel requires 5.3.

To use "Remote Mysql" you would need to add your IP to the white-list on the server and also configure Mysql to accept remote connections

Old question but, a lot people may need this, so adding what worked for me
In 5.6+, also will be working on lessor version
DB_CONNECTION_REMOTE=mysql
DB_HOST_REMOTE=remotehost
DB_PORT_REMOTE=3306
DB_DATABASE_REMOTE=database_name
DB_USERNAME_REMOTE=database_user
DB_PASSWORD_REMOTE=database_password
and in config/database.php
'connections' => [
'mysqlRemote' => [
'driver' => 'mysql',
'host' => env('DB_HOST_REMOTE', '127.0.0.1'),
'port' => env('DB_PORT_REMOTE', '3306'),
'database' => env('DB_DATABASE_REMOTE', 'forge'),
'username' => env('DB_USERNAME_REMOTE', 'forge'),
'password' => env('DB_PASSWORD_REMOTE', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and then in your controller
$records = DB::connection('mysqlRemote')->select( DB::raw('SELECT `col1`, `col2` FROM `table`') );
if your IP is white-listed, it should work fine, tested in Laravel 5.6

Related

AWS SES simple email service, time out error with Zend email

I was using Amazon AWS SES simple email service to send emails for my application. I using the following codes of Zend framework2 to send emails. It worked fine before, but I got the following error in my AWS EB EC2 instance now. However in my localhost, I can still use the same code and the same AWS SES account to send emails and it is still working fine now in my localhost.
$sc_Zend_mail_smtpOptions = new \Zend\Mail\Transport\SmtpOptions();
$sc_Zend_mail_smtpOptions->setHost($sc_server_email_host)
->setConnectionClass('login')
->setName($sc_server_email_host)
->setConnectionConfig(array(
'username' => $sc_server_email_account,
'password' => $sc_server_email_password,
'ssl' => 'tls',
'port' => 587
));
$sc_Zend_mail_transport = new \Zend\Mail\Transport\Smtp($sc_Zend_mail_smtpOptions);
Is the problem from AWS EB EC2 ssl setting ? What is wrong with the codes? It was working fine in AWS EC2 before.
The error message is:
Fatal error: Uncaught exception 'Zend\Mail\Protocol\Exception\RuntimeException'
with message 'Connection timed out' in /var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php:209 S
tack trace: #0 /var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/Smtp.php(148): Zend\Mail\Protocol\AbstractProtocol->_connect('tcp://email-smt...')
#1/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(375): Zend\Mail\Protocol\Smtp->connect()
#2/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(361): Zend\Mail\Transport\Smtp->connect()
#3/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(372): Zend\Mail\Transport\Smtp->lazyLoadConnection()
#4/var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Transport/Smtp.php(229): in /var/app/current/utils/zendfw/ZendFw2/vendor/zendframework/zendframework/library/Zend/Mail/Protocol/AbstractProtocol.php on line 209
It seems that server instance in AWS EC2 does not allow the old port set up any more. Moving the port setup outside setConnectionConfig array does make it work in AWS EB EC2 again as well as in localhost.
$sc_Zend_mail_smtpOptions->setHost($sc_server_email_host)
->setConnectionClass('login')
->setName($sc_server_email_host)
->setPort(587)
->setConnectionConfig(array(
'username' => $sc_server_email_account,
'password' => $sc_server_email_password,
'ssl' => 'tls'
));

Roundcube SSL connection IMAP Error: Login failed

This is probably a reoccurring problem, where you are unable to Login to Roundcube (Connection to storage server failed), due to an IMAP (ssl) connection error.
Many Posts reports this as an IMAP issue, and tells you that this is due to the config/config.inc.php Imap configuration, but are failing to give any proper debugging of the IMAP.
So, this is another post, of the same problem : Unresolved.
Roundcube reports this error:
IMAP Error: Login failed for user#myserver.com against host.myserver.com from IP_ADDRESS. Could not
connect to ssl://host.myserver.com:993: Unknown reason in
../lib/Roundcube/rcube_imap.php on line 200 (POST /webmail/?_task=login&_action=login)
I tried this to check if ssl connection to imap was possible:
openssl s_client -connect host.myserver.com:993
And I am able to connect to the IMAP with ssl, but connection ends with :
BYE Too many invalid IMAP commands.
So ... it seem that the ssl certificate is valid, and I am able to connect on port 993.
Roundcube still reports an Unknown reason for not connecting properly.
The certificate was generated by the ./mkcert.sh bash script, where you can enter the proper hostname of your server, to be included in the certificate.
After a lot of debugging, and no proper debugging methodology I found the easy solution, by bypassing ssl verification.
A stupid solution:
$config['imap_conn_options'] = array(
'ssl' => array(
'verify_peer' => false,
'verfify_peer_name' => false,
),
);
$config['smtp_conn_options'] = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
),
);
Do anyone have some suggestions for the next thing to try
Thanks!

How can I push a page from one MediaWiki to another SSL based wiki with self signed certificate using the Push extension?

https://www.mediawiki.org/wiki/Extension:Push
is an extension that will let you push pages from one MediaWiki toanother.
As documented in
https://www.mediawiki.org/wiki/Topic:Saza08eh1dcygs3c
I had trouble getting this to work with target Mediawikis that use SSL and self signed certificates.
Push would choke up with the message: Dateitransfer fehlgeschlagen: Authentifizierung auf https:///api.php ist fehlgeschlagen.
The issue is now solved and I am adding this question to make the solution available for Stackoverflow users to find.
First as outlined in https://www.mediawiki.org/wiki/Topic:Saza08eh1dcygs3c
https://github.com/BITPlan/Push/commit/cf393a32423bd8ae07af50a5587f847f20cfb9b9
has a commit with a few extra lines for error handling. The resulting message is:
Authentication at .../mediawiki/api.php(http-curl-error:SSL certificate problem: unable to get local issuer certificate) failed.
This is a major improvement in handling such situations - especially the debugging works better.
So the natural attempt to add:
ini_set("openssl.cafile","BITPlanValidCerts.pem")
ini-set("curl.cainfo","BITPlanValidCerts.pem")
didn't work - you have to set these in the php.ini file. A curl https:// test then works. But still no go with Mediawiki and the push extension.
https://github.com/BITPlan/Push/commit/3f68dd36a65ff6ad8c68df807c2c32311073dae4
fixes the issue by adding the curl.cainfo php ini setting to the curl options
getHttpRequest( $target,
array(
array(
'postData' => $requestData,
'postData' => $requestData,
'method' => 'POST',
'method' => 'POST',
- 'timeout' => 'default'
+ 'timeout' => 'default',
+ 'caInfo' => ini_get('curl.cainfo')
)
)
);
);
The same fix might also apply to similar situations where the upgrade advice:
https://www.mediawiki.org/wiki/Manual:Extension_support/1.17/ExtUpgrading
has been followed. MWHttpRequest will handle the caInfo option if it is supplied see https://doc.wikimedia.org/mediawiki-core/master/php/HttpFunctions_8php_source.html

Rails App on AWS Elasticbeanstalk mail send fails with ESMTP No Relay Access Allowed From IP

I have a rails app that I have deployed to AWS Elastic Beanstalk. The app uses devise to handle user authentication, and its set to be able to invite users.
My issue is that when I try to invite a user, I get the following error:
Net::SMTPFatalError (554 secureserver.net ESMTP No Relay Access Allowed From <my_eb_assigned_ip>
(I am hosting the domain on GoDaddy).
In development, the mailer functionality works fine; my smtp settings are set to (common to all environments):
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtpout.secureserver.net",
:port => 80,
:domain => "www.my_domain.com",
:authentication => :plain,
:user_name => "do-not-reply#my_domain.com",
:password => my_pass,
}
And in my production.rb config file:
config.action_mailer.default_url_options = { :host => 'aws_sb.elasticbeanstalk.com' }
Is there another setting I have to enable in Elasticbeanstalk to allow relay access? Or am I missing a production specific setting from my rails configuration?
I figured out it was the port value that I was setting....when I switched the port to 25, it works in production. However, for development, port 25 wasn't working; it would only work in dev when the port was 80.
So I ended up moving the entire smtp mailer settings into the environment specific settings (from the config/environment.rb file), and setting the production port to 25, and the development port to 80, and that appeared to make both environments work.
Edit: After another push, I was seeing the same issue, and none of the ports I tried were resolving the issue. So I ended up switching all my mail functionality to be sent through Amazon SES, and that appears to be functioning great so far.

mandrill send e-mail from localhost

I'm using Mandrill to build a feedback form for users on the website (they fill the form and send me an e-mail).
I want to test the e-mail functionality in development. I use unicorn as a server and my local address is 0.0.0:8080
However I get a 500 server error, Net::SMTPServerBusy : Relay Access Denied
I followed the Heroku instructions step by step.
Here is m application.rb configuration:
config.action_mailer.smtp_settings = {
:address => 'smtp.mandrillapp.com',
:port => '587',
:domain => 'heroku.com',
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_APIKEY']
}
ActionMailer::Base.delivery_method = :smtp
I followed the instructions on mandrill/heroku web page to set up.
I have a .env file set up with a MANDRILL_USERNAME and my MANDRILL_APIKEY
Here is my ActionMailer file:
class FeedbackMailer < ActionMailer::Base
default :from => ""
default :to => "xxx#stanford.edu"
default :subject => "feedback about xxx"
def send_feedback(message)
#debugger
#message = message
mail(:from => message[:sender_email])
end
end
Any help would be appreciated.
I can confirm e-mails get sent in production.
If all of your settings are working in production but not locally, there are a couple of possibilities:
How are you loading the variables from .env to ENV? It's possible the environment variables aren't getting loaded as expected locally. If you hard code the credentials locally, does it work?
You could be running in to an issue with the port or outbound SMTP traffic being blocked. Consider trying port 2525, as it may be less likely to be blocked by local ISPs. Port 465 with SSL enabled may also work even if your ISP is blocking other SMTP traffic