473,503 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to escape mysql_connect() DNS caching?

Hello people,

There is a problem is that mysql_connect() somehow caches last sessions
IP and is not using the one which you put into host place.

Has anyone made mysql_connect() from php to multiple SQL servers so
that script tries to connect to resolved IPs until it finds one that
works?

I have set up DNS so that on request sql.example.com it returns two
IPs (rotating on each request).
When I see output of script below I see that php function
gethostbyname() does DNS requests in for() cycle and does return sql
nodes IP in rotated manner, but from tcpdump I see, that
mysql_connect() is using just one IP to connect to port 3306.

Is there some way to flush the mysql_connect() dns cache?
$dbaze="abc";
for($cnt=1;$cnt<=6;$cnt++){
$ip=gethostbyname('sql.example.com');
echo $ip."</br>";
$db =mysql_connect($ip, "user", "pwd",1);
if($db)break;
}
if(!$db){
die("Error connecting to the Server");
exit();}
I think this is the problem in mysql library used by php, so I tried to
drill down there and found comment beolw in developers page. I am not
wery well in C so I am
not sure where to put this piece of code so that recompiled mysql and
php(or just php)
would contain correct mysql_connect() function that does connect to IP
which is given.

This problem is very strange mostly because right now mysql is
advertising its cluster - so
connection to multiple SQLs is just natural to be really "no single
point of failure".

Can anyone suggest a solloution to this?
Txh in advance!

versions:
OS=FreeBSD 6.1
mysql=5.0.24(from source)
php=5.1.5 (from source)

----
From: http://dev.mysql.com/doc/refman/5.0/...l-connect.html
Posted by Andrew Donkin on March 4 2005 2:23am [Delete] [Edit]

If you have configured your DNS to return a list of MySQL servers, and
you want your client
to work through that list until it finds one that works,
you can put real_connect inside a getaddrinfo() loop.
I wish the client library did it by itself:

struct addrinfo *addr, *addrlist;
struct addrinfo hints = {0, PF_UNSPEC, SOCK_STREAM,
0, 0, 0, 0, 0};

int gai_result = getaddrinfo(host, 0, &hints, &addrlist);
if (gai_result) bomb(Unknown host);

for (addr = addrlist; addr; addr = addr->ai_next) {
#define IPNAMELEN 20
char ipnamebuff[IPNAMELEN];
const char *ipname = inet_ntop(((struct sockaddr_in *) addr->ai_addr)
->sin_family,
(void *)&((struct sockaddr_in *) addr->ai_addr) ->sin_addr,
ipnamebuff, IPNAMELEN-1);

mysql_real_connect(mysql, ipname, user, pass, database, ...);
}
---

Aug 19 '06 #1
1 4346
Hello,

found the problem:
1)PHP didn't read correct config file - so mysql.connect_timeout was
not correct
2)as I use FreeBSD jails - tcpdump didn't show packets between jails on
same machine because they do not cross interface..

Problem was between screen and chair..:)

Aug 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1834
by: LRW | last post by:
I'm working on some scripts someone else made, and it's littered with repetitive mysql_connect's and mysql_select_db's. For example: // MAKE SURE IT IS A VALID PRODUCT $connection2 =...
2
2096
by: Bilal | last post by:
Hi, Does anyone know the escape character for & and # like in : Update GDO_INFO_ER set V_COMMENTAIRE='B&A' where V_USERMODIFICATION='bilal123' or Update GDO_INFO_ER set V_COMMENTAIRE='B#A' where...
10
5096
by: Alvaro G Vicario | last post by:
I need that my script retrieves data from two different databases so I use mysql_connect() to open two connections. I have only one server, user and password, just two different databases....
4
5742
by: Andrew Clark | last post by:
Hello, I am having trouble connecting to my server with mysql_connect(). I can connect via the command line on the server and with phpMyAdmin over our network, but not though mysql_connect()....
2
2821
by: Sugapablo | last post by:
I have a small test script connecting to a MySQL database. It seems to work, unless I try to use the resource link identifier returned by mysql_connect(); This works and returns all the rows in...
19
8090
by: Michael | last post by:
Hi, I'm trying to do something which should be very simple - connect to the MySQL database. Here is the call, followed by the error msg. $conn = mysql_connect("localhost", "root", ""); ...
7
5403
by: avenpace | last post by:
Hi, I got error when using mysql_connect function in my php script. If i set the db host to localhost, it give me that error altough all the user and password that I wrote is true(I can login...
6
3351
by: GD | last post by:
Hi All, I've got MySQL 5.0.21 running on Windows Server 2003, and php running on Apache on a Linux box (Fedora Core 4). Previously when the pages were running on an IIS server the connection...
7
2834
by: criveraf | last post by:
Hi there, I wasn't sure where to put this question, since it deals with both PHP and MySQL. I apologize if this is not the correct forum for this. I am working with a simple PHP application...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5594
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5022
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.