I'm using Yii with postgres database in support library of PDPPDO.
I am facing issue in PDO library to conenct Postgres Database.
I'm using some background process to work so ultimately it will be 24X7 continues task. Now, As per use case it's possible that my Postgres will go OFF/Crash somehow. That time, pg_prepare() function returning me FALSE.. That's tottaly Corrent !!!
Now, after few minutes, if Postgres get start. So ultimately pg_prepare() should give me resource Id. But it's still giving me FALSE !!
I believe that due to restarting postgres server it's not allowing me to use same resource again. In that case I need infinite loop that willy continuously ping over Postgres for new connection.
If need to open connection again then I think following like would help,
// THOUGH IT'S CHECKING FOR $this->_pdo
// I need to make it NULL before process...
Yii:app()->db->open();
Now, Is that right flow? or do you guys have any other idea for this?
Thanks ...
After doing plenty of research, found that there isn't any solution for automatically inform Yii that connection is started again. So to maintain this, I am keeping one flag internally to check connection.
In Yii, to reconnect any database you need to delete previous connection using,
Yii::app()->db->setActive(false);
It will remove all your previous connection and sources and allow you to make connection again using,
Yii::app()->db->setActive(true);
If still not able to find connection then we can delay our background job for few minutes. Benefit, you don't need to restart your background workers.
Regards
Related
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.
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.
Im trying to optimize some of my PHP code. I found out that most time of my PHP script is spent during the connection to my mysql database at the beginning of the script.
I only connect to the database once at the beginning and close the database connection at the end of the script.
But for each user requesting this page a new connection has to be established.
Is there a way to hold a reference to the database and share it for all requests?
Yes, you can achieve that.
If you're using the MySQLi extension (the old one without the i at the end is outdated!), you can create a persistent connection by passing p: as a prefix to the hostname when creating the instance:
See mysqli::__construct.
If you're really using the old MySQL extension, there's mysql_pconnect for persistent connections.
Alternatively, if you're using PDO, then you can use setAttribute() to use PDO::ATTR_PERSISTENT.
Documentation on using persistent connection in PDO.
You can make your connection persistent (for PDO use PDO::ATTR_PERSISTENT => true), but my recomandation is to find out why your script is spending a lot of time connecting to mysql and make some improvments there.
Consider this:
Use MySQL server IP instead of hostname, to eliminate time needed to resolve dns.
Disable mysql autocompletition: (comment skip-auto-rehash from my.cnf)
If you make a lot of queries which will return the same thing over and over again, consider using a caching system to cache for query results.
Post your code on codereview.stackexchange.com to benefit from others expertise on further improving your code.
To further debug the problem, connect from console to MySQL server and check the time needed to open connection, change database, select, etc.
My Environment:
I'm using: OpenSuse 11.4 kernel 2.6.x.x; Apache 2.2; PHP 5.3; MySQL 5.5 Community; Pearl 5 version 12 and Bash.
I have been used BIND DNS, the whole process until now is manually (Add, Update, Get, etc..) hosts.
Now I need to develop some automation for this kind of task as I mentioned above. The problem is I do not want to use Cpanel, WHM or whatever software in the market to do this.. I'm looking to develop some script in perl or php or whatever language it need to be.. I really want something very simple that I just need to query the database to get all the information I need and execute the operation in the BIND.
I intend to use cronjob to fire the "script" to query the information of new hosts added in my table and then execute BIND.
I do not know if I was clear enough, if not please ask me.
I do not have anything yet. I'm just grabbing some ideas for a while.
Cheers.
[EDITED]
I need to Add, Delete, Update, Get and Set information in my DNS Zone. Create the files every time the script query the database and after export to BIND.
Bind has a nice tool included called nsupdate which allows you to edit the zone from the command line. You should use that if you're writing a script.
Bind can run backed by a database; when you do this, updating the database updates the zone. There are no zone files at all!
I read specifications of mysql_pconnect of the function of PHP..
"mysql_pconnect is that connection with the SQL server is not closed even if the practice of the script is finished".
There is it unless connection is closed, but will it be that connection stays until a timing of the reboot of mysql?
I think, and I may be wrong, but...
I think the pconnect is a persistant connection held not for the running of the script, but for the duration of the PHP session / MySQL session, that there is a socket connection maintained by PHP regardless of the which script is being run. A bit like having multiple documents open in word instead of multiple instances of notepad running. by using a common database link, the processing power for creating individual links is not needed.
However, after reading, I think that it only seems to be of benefit if PHP is being run as an Apache module, not in CGI mode.
Call me out on my misinformation.
mysql_pconnect allows Apache's mod_php module to do connection pooling. You can still call mysql_close, but mod_php will not actually close it; it will just invalidate your resource handle. Unfortunately, the module doesn't have any configuration for this, so pooled connections are reaped by the MySQL server via its wait_timeout parameter. The default value of this is quite high so if you want to take advantage of it, you will probably want to lower that variable.
Connection pooling saves two things: connection setup and MySQL thread creation. Connection setup is very fast with MySQL compared to other databases, but a highly in-demand web site could still see a benefit from reducing this step. The cost of thread creation in MySQL is more dependant on the underlying OS but it can still be a win for a busy site.
Both aspects need to be looked at in the bigger picture of website speed and the load it presents on your database. It is possible to run out of connection threads on the database with a busy enough site using connection pooling. There is also the aspect that your application needs to do its best to leave the connection in a consistent state, as you can no longer rely on closing the connection to do things like unlock tables and rollback transactions.
There is more information in the PHP documentation.
yes, but you can not close it using mysql___close.
It will close when you reboot mysql server, or when the connection stays idle (not used) for a specific amount of time, defined in the configuration variable wait_timeout.