I have defined my database connectivity in database.php. I have 4-5 connections. Some are remote IP's. Problem occours when some IP goes down, i can access my application. Is there a work around for this i.e. application should work even if some IP connectivity is not there.
If you are using PDO, put inside a try.
If you are using mysql_connect use an IF to check the connection
Related
Really can't figure out how to word what I need...
I work with a site through SCP. And sometimes it crashes due to the database server fault. But actually I don't connect to that database. So I need to see all possible connections that my site can use. I found two files at include path that contain connection to server that faults, but my files don't include them. If I can give some more info please ask me.
All connections are made through odbc_connect("DSN_NAME;host_address", $login, $pass)
How did you find out that your SCP crashes because of database server fault? It shouldn't. I think the problem is not in database. By the way, MySQL allows to configure many connections, so I believe the problem is in something else.
$con=mysqli_connect("example.com","peter","abc123","my_db");
On an admin dashboard page of a PHP web application, I would like to display details about the database connection. Depending on the local setup, the connection may be direct, or it may be brokered by PgBouncer. Is it possible to detect exactly what I'm connecting to? This information could be useful for debugging.
Checking the default ports (6432 vs 5432) is possible but obviously unreliable. Does PgBouncer inject anything into the connection that would help differentiate it from a direct connection?
There is no direct way. Pgbouncer does not populate this info in pg_stat_activity.application_name or somewhere else. Of course you will see IP of the machine where you run pgbouncer in pg_stat_activity.client_addr, but it won't mean it is pgbouncer for sure. One way to bond pg_stat_activity with show clients command of pgbouncer together - this way you know the source of a query, I described this in another post here: Find source of query through pgbouncer
But still it would require later bond of php sql process pid with all together to see the connection.
Also - php can consume both connections (any amount) so theoretically you need to check each statement on how it connects.
Perhaps an alternative would be to use different access credentials based on your 'local setup'? Connections configured through pgbouncer could use a different username/password to those that go direct. This would allow you to differentiate the connections. Obviously depends on your local setup of course whether that is possible.
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.
I am using joomla 1.5, and my site was running fine, but suddenly couple of days back I got the error Database Error: Unable to connect to the database:Could not connect to MySQL
Its not always, the error is coming. Sometimes the error is coming, and sometimes not. If I have wrong database connection values, then why, is the site coming perfect sometimes. I double checked my database connection values, and are completely fine.
Try http://www.scholarsmarket.com/beta/top-ranked-students this link. Refresh this couple of times. You will get the problem
I am stuck with this error. Can't get out of this.
It a very common issue to tell exact reason, I think you should start from asking your hosting support about any limitations e.g. Database connections?
then take a look at the logs and statistics - probably you site suddenly got too much visitors at that time?
If this error seems to pop up 'randomly' it's usually because either persistent connections are used (or another reason why the connection goes into sleep mode for some time). Persistent connections are generally not a good idea on medium- to high-traffic sites, since it clogs up your database with 'sleeping' connections. Once those connections hit the maximum connection account for your database user, the MySQL server will reject any new connections and throw the error you are seeing.
So, in order to fix this problem, make sure your db config is not set to use persistent connections. You can also verify if this is the problem by running this query on your database (given that you have the proper permissions to do so):
SHOW PROCESSLIST;
I have a server that's returning an unexpected, intermittent error and wondering if anyone has either experienced it before or can guess what might be going on. My search has so far has been unfruitful.
Im connecting to a remote mysql server using mysql_connect() in the usual way, but for the last 2 weeks (couple of times a day) the connection to the database fails and I get an unable to connect to socket error?
Error: 2002 - Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Obviously this is unexpected as Im trying to connect to a remote host. Does mysql_connect default to localhost if the remote host is unavailable? I've not known it to do that, and nothing in the docs that say it does.
There doesn't seem to be any pattern to the failed connections, various files and it happens at all sorts of time of day. Im thinking it might be network traffic related, because if you refresh the page straight away it connects fine, but am confused as to why Im getting an unable to connect to socket error.
the database provider has checked and are unable to find an issue with the server itself.
anyone have any ideas?
Im sure it doesnt default to localhost, and im also pretty sure that it is the remote server sending you the error message. So it's "Local" on your mysql server, not local on "your" server.
In this case its probably due to downtime on the remote server.
Gr.
ok, facepalm moment... after this hitting me for ages I finally found why mysql_error() was reporting the socket error. in my code there was a mysql_select_db() that I didnt notice and it was that, thats reporting the socket error as there is no open connection for it to use.
actual error the mysql_connect is reporting is (2003) Can't connect to MySQL server on 'mysql.hostname.tld'. Im still leaning towards some sort of DNS lookup/network traffic issue, Im currently using the IP address to connect to see if that errors.
But as far as this question is concerned, the answer is "Im a numpty".
Have a look at this
Had a similar thing a while back.
Are you connecting from home? The reason I ask is that my ISP likes to mess around re-assinging IP addresses to my home account, and I have an IP authorization on my remote MySQL.
I think I opened up the IP connnection limitation thing to a string that was IP Addresses for my region and that solved the problem.
If you're running Windows, there might be too many connections already, if so go to http://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html#can-not-connect-to-server-on-windows and have a read.
Else, perhaps someone someone may have removed the Unix socket file that mysqld uses (/tmp/mysql.sock by default). For example, you might have a cron job that removes old files from the /tmp directory.