I'm running PHP web app that uses PDO to connect to postgres (https://github.com/fusionpbx/fusionpbx/blob/bc1e163c898ea2e410787f8e938ccbead172aa5a/resources/classes/database.php#L202).
I'm running a failover cluster and so basically I just put 2 hosts names and my connection string looks like this:
"pgsql:host=host1,host2 port=5432 dbname=fusionpbx user=fusionpbx password=password target_session_attrs=read-write"
This works ok, if host1 is standby, host2 is selected with very little delay. The only issue is the host1 is unreachable or down. In this case PDO/driver (?) always tries host1 first, waits 30s until it timeouts and goes to host2. It seems that the fact that host1 is not available is not being remembered.
I found 3 workarounds:
add PDO::ATTR_TIMEOUT=2 when creating PDO. Yes, stupid I know, but allows at least temporary workaround, in case of failure, until I figure out the right solution.
externally monitor postgres and change nodes order in the connection string, putting active node always first. I'm starting to think it's least evasive.
PDO::ATTR_PERSISTENT => true - I've tested this and on the first look it works quite nicely, but given that I'm not really PHP guy, and the application is not mine but 3rd party application, I'm reluctant to make such impactful change.
Maybe someone could share their experience? I'm quite surprised how little can be found about this over the net. Also, on same box I'm running lua scripts, also connecting to same postgres in the same way, and it seem to have no problem in handling this scenario. It's the same version of libpq since it's the same linux box and I'm not adding anything specific to the connection string.
Related
I'm trying to implement a CMS on AWS using mostly free tier services and am doing so using PHP, and have already implemented this using PHPMyAdmin in school.
The problem:
I wanted to do this using RDS, but I've come up with a pretty strange issue.
When I upload my site to Elastic Beanstalk and try to connect to the DB from there, I face incredibly long wait times. Sometimes it returns error 504 from long wait times.
This is not an issue with the Database's speed, however. I know this because I can run this code on localhost and it works exactly as intended (see photo)
Only half a second to load!
What I think is happening:
Something must be going on while it's being executed in Elastic Beanstalk. I don't know quite what, but it's taking way too long.
Extra info:
I connect to the DB using the following code (credentials are spoofed):
$conn = mysqli_connect("mydb.cebelvm3fa0n.ca-central-1.rds.amazonaws.com","USERNAME","PASSWORD","mydb");
Both my database and EL instance are run from ca-central. The connection is not failing, it is taking an extreme amount of time.
the page that is failing is http://howardpearce.ca/posts.php
If you have any ideas on what it could be, I would appreciate it very much. Thanks!
NOTE:
I will post certain bits of my code, but creating an MCVE is really
not feasible as I would have to give my DB password, and you would
need to re-create my AWS environment, so please don't ask for one, I
can always give more detail.
After #colde mentioned checking logs, I quickly found an error code which lead me to the right resources.
My database was only configured to allow connections from my IP, and the firewall was blocking out my Elastic Beanstalk instance, I just had to add a new security rule.
Thanks!
Say you have multiple Mongo shards in a production environment, so each of them are replica sets. You have three different Mongos instances running for connecting to these replica sets, so you're doing something like this:
new MongoClient("mongodb://mongos1.example.com:27017,mongos2.example.com:27017,mongos3.example.com:27017");
What scheme does the PHP Mongos driver use to determine which of these it's going to connect to? Doing a search for this, I've been able to find relatively little information, and the few things I have come across tend to contradict each other, some saying it picks the first one to respond, and others saying it picks the first one you enter in your code.
Does anyone know?
This is for PHP 1.3.4.
As Sammaye stated, the MongoClient connects to the "nearest" mongos first, where "nearest" is determined by latency. If you're interested, you can take a look at manager.c and read_preference.c in the php driver.
I'm using a self-made customer system in PHP running with a local mySQL Database.
Now i have a second computer on a different location which has to use this Database too. So i gave this mysql Database on a Server reachable through internet.
My problem is now, that the first one has often problems with the internet connection and then the program will not work. But it has to work every time!
Now i do not know how i should handle this problem?
A local Database and one in the internet, but how should i merge them?
Should i make a local DB per computer and match them together in one?
I also want to change the framework behind this system to symfony2 so is there a way to solve this problem with this framework (e.g. doctrine?)
Thanks for your help!
Update:
My limitation is the Internet connection on the first computer which could not be eliminated.
If you really have limitations of (1) not being able to move the database off of the machine with a bad connection and (2) not being able to fix the bad connection; you are going to have to keep some sort of local instance on the second machine.
I would try to setup master-master replication from the first machine with the bad connection to the second machine. I'm not sure how reliable this will be considering the replication will be failing often due to the first machine's bad connection. This problem may be extrapolated if one or both machines are using old versions of MySQL. MySQL 5.5, for example, can be configured to actively monitor replication connectivity.
If the majority of your application does READS instead of WRITES, perhaps you could install Memcached (or something similar) on the second machine so that the application can pull data from local memory without requiring a connection to the MySQL server.
There are a few ways to achieve what you want (although maybe not exactly how you described), but the best way is definitely do host the database on a server that doesn't have Internet connectivity problems. Look for hosting that allows remote MySQL connections.
I already read a few threads here and I also went through the MySQL Replication Documentation (incl. Cluster Replication), but I think it's not what I really want and propably too complicated, too.
I got a local and a remote DB that might get both accessed/manipulated by 2 different persons at the same time. What I'd like to do is to sync them as soon as possible (means instantly or as soon the local machine goes online). Both DB's only get manipulated from my own PHP Scripts.
My approach is the following:
If local machine is online:
Let my PHP Script on the loal machine always send the SQL Query to the remote DB too
Let my PHP Script on the remote machine always store its queries and...
...let the local machine ask the remote DB every x minutes for new queries and apply them locally.
If local machine is offline:
Do step 2. also for both machines and send the stored queries to the remote DB as soon as
local machine goes online again. Also pull the queries from the remote machine for sure.
My questions are:
Did I just misunderstand Replication or am I right that my way would be easier in my case? Or is there any other good solution for what I'm trying to accomplish?
Any idea how I could check whether my local machine is online/offline? I guess I'd have to use JavaScript, but I don't know how. The browser/my script would always be running on the local machine.
What you're describing is master-master or multi-master replication. There are plenty of tutorials on how to set this up across the web. Definitely do your research before putting a solution like this into production as replication in MySQL isn't exactly elegant -- you need to know how to recover if (when?) something goes wrong.
I have a standard php app that uses SQL Server as the back-end database. There is a serious delay in response for each page I access. This is my development server, so its not an issue with the live setup, but it is really annoying for working on the system.
I have a 5 - 8 second delay on each page.
I am running SqlServer 2000 Developer Edition on a Virtual Machine (Virtual PC).
I have installed SqlServer on my development machine but get the same delay.
I have isolated the issue to the call to mssql_connect (calling mssql_pconnect has no effect)
It is a networking issue on how I have set up (or not set up, since I didn't really change default config) SQL server. It's not a strictly a programming issue but I thought I might get some valuable feedback here.
Can anyone tell me if there is a trick, specific set of protocols, registry setting, something that will kill this delay?
I was also experiencing a 5-10 second delay on every connect, using the official Microsoft SQL drivers for PHP (as suggested by #gaRex) - none of the answers posted here solved it for me.
As suggested by #ircmaxell, my problem was a DNS issue - and the solution was to edit the \windows\system32\drivers\etc\hosts file (your local local host file) and add the name of my own machine to it.
In the "system properties" dialog, find the "computer name" of your machine - then add a line like 127.0.0.1 my-computer to your local host file.
For me, the delay occurred once more, on the following attempt to load the page - after that, it was super fast, no delay at all.
Note that this problem may occur even on a physical machine, not only on a VM.
I came across network issues when running virtual pc, everything network related is slow, try adding this entry on your registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
Create new DWORD value named DisableTaskOffload and set its value to 1.
Restart the computer.
It worked for me, source.
Is it perhaps a DNS issue? I know that MySQL does a reverse DNS lookup on each login (not each connection). If you don't have a reverse dns record for your server (or your dns is slow) it can cause a major delay at login. There's an option in MySQL to disable that. I'm not sure about SQL Server, but I'd assume it may be doing something similar...
I remember the same problem, but forgot, how we have solve it.
To clarify please specify exact connect strings, your SQLserver versions and also try to start this old good utility c:\WINDOWS\system32\cliconfg.exe, which is also can bring some light.
Yes, I know, it's from 2k, but guys at m$ don't like to create client tools from scratch.
Also try to get "right" mssql client dlls for PHP.