Database Code

  Home arrow Database Code arrow MySQL Replication Connection Fail Over
DATABASE CODE

MySQL Replication Connection Fail Over
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    This function is for connecting to a pair of MySQL fail over servers. It tries to connect to the first availiable server in the array list of $mysql_servers and so on. The main use for this function is for MySQL servers in a two way replication setup. The client can read from any of the servers using the normal mysql_connect function but for writing mysql_rep_connect is safer in that it only addresses one server at a time, thus preventing data conflict. Add set-variable = max_connect_errors=2147483647 to avoid problems.

    By : Rival7

    <?php
    /*
    Description: Connect to the first available mysql master server
    Author: Nathan Cassano
    License: Public
    */
    function mysql_rep_connect(){
    $timeout = 15;
    $mysql_servers = array(
    array("mysql1.yourdomain.tdl", "username", "password"),
    array("mysql2.yourdomain.tdl, "username", "password")
    );

    $connection = false;
    foreach($mysql_servers as $server){
    list($hostname, $username, $passwd) = $server;

    if($fp = fsockopen($hostname, 3306, &$errno, &$errstr, $timeout)){
    fclose($fp);
    $connection = @mysql_pconnect($hostname, $username, $passwd);
    if($connection > 0)break;
    }
    }
    return $connection;
    }

    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap