Script to run a DB query, but have multiple DB servers (for failover) I want to loop through until I find one that is available

Conceptually I'm not sure the best way to go about this, looking for some guidance.  I'm running a MySQL query and I have the code working against a single MySQL server.  However I want to build some redundancy into the code so if it tries one
DB and it's not available, it tries the next one.  I have come up with two ways to do this.
First one is basically an If statement looking at error[0] as a condition.  Tries to connect to the first server and if throws an error, it tries the second and if that fails it exits the code.  This is less scalable as I have to code in connection
strings for each server.
Second one is a loop that goes through connection to each DB server I setup in an array until the connection.state = "open", then it should proceed to the rest of the code.
Foreach ($MySQLHost in $SQLHostArray) {
$error.clear()
$ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" + $MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword + ";database="+$MySQLDatabase
$Connection.ConnectionString = $ConnectionString
do {$connection.open()}
until ( $Connection.state -eq "open" )
However I don't know how to exit that loop if all servers fail. 
Which is the best bet for building failover into the script, or is there a better way I haven't thought of yet?

You could use a counter - 
$lexit = 0
$count = ($SQLHostArray.count -1)
Foreach ($MySQLHost in $SQLHostArray) {
$lexit++
$error.clear()
$ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" + $MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword + ";database="+$MySQLDatabase
$Connection.ConnectionString = $ConnectionString
do {$connection.open()}
until ( $Connection.state -eq "open" -or $lexit -eq $count)

Similar Messages

  • Can I have multiple incoming servers for one email address on mail?

    I am currently trying to connect my school email address to my Mail app on my Mac.
    Unfortunately, it seems that when connected to the school network, I can't access the Microsoft Exchange Web sever, whether through any web browser or Apple Mail, but I can at home.
    I have found the mail servers from the Outlook settings at school, so if I change the settings from the Exchange Web server (mail.xxx.co.uk) to the networked mail server (xxx-exchange.domain.local), I can access the emails through the school network, but when I come home, I then have to change the servers back, and then Mail tries synchronising and I seem to lose emails off my Mac, but they are still on the server.
    Is there any way of setting up the account on Mail so I can access the emails from either server address, depending on my location, without having to do it manually?
    Thanks in advance

    terrihubble wrote:
    I have 2 email accounts that come to my mac mail box. Can I set a Smart folder that all of one of the account emails will go and then set another Smart folder for the other email account?
    you can but there is no need to do it. click on the little triangle next to your inbox. it will expand and show the inboxes for each of your accounts.
    If so, can someone tell me how to set it up?

  • Why does the software update say that drivers for my printers are not available? I have the drivers installed from the CD but can't add the printer because it wants to go through software update.

    I have the latest operating system, but can't get my printers added. Why does the software update say that drivers for my printers are not available? I have the drivers installed from the CD but can't add the printer because it wants to go through software update.

    It's pretty difficult to offer suggestions when you say nothing about your Mac, the version of Mac OS it's running, or the models of printers you have. This forum, however, is intended for questions about old, pre-G3 hardware, so unless you are asking about an very old Mac, this is not the forum where you'll be most likely to get help with your problem. I'd suggest you ask this question in the forum for your model of Mac or version of Mac OS X it's running. You'll be more likely to get assistance there, assuming you provide sufficient detail for anyone to make a diagnosis of the problem.
    Regards.

  • What a horrible way to treat customers. I was due for updates on my phones and thought I should maybe check some other prices but have been with Verizon for 20 yrs so ended up there. I got 2 new IPhones 5s and the wife couldn't decide what she wanted so s

    What a horrible way to treat customers. I was due for updates on my phones and thought I should maybe check some other prices but have been with Verizon for 20 yrs so ended up there. I got 2 new IPhones 5s and the wife couldn’t decide what she wanted so she stayed with old phone but this locked me into a new 2 yr contract . Within a wk one of the new iPhones started turning on and of like 10 –15 times a day. Wk 2 it turned off and would not turn on again . It was my sons who was in college at the time so we talked and he took it in to a Verizon store where he was told I need to come in since the account was in my name. He was away from home , Verizon could see on account it was his phone they said it was not abused, they could not even turn it on , they sold me the phone give him another phone maybe even a loaner till he gets another. So now I need to go to store and explain to 3 different people this is my phone and my son has it and I need a new one. what a waste of time after about 2 hrs and talking to different people and yes they said there were notes on his account from other person he had talked to from different store. So I finally walk out with a receipt in hand and being told I would have a new phone in a couple days. As I sat in my vehicle thinking this is stupid I looked at my receipt and noticed it said droid on it so back into Verizon I went. The salesman said that’s what your son has on his account. My son had activated a friends old phone so he has one since VERIZON REFUSED to give him one. Another hr 3 people and yes they can see he had a new iPhone and notes on it from other store. Sometimes sorry just doesn't do it. I was now late for a appointment . Now I walk out and have been told I will get a new iPhone in the mail in about 2 wk Yes 2 wks   again I bought it there just give me a new one and you send old one back. I will also get a new droid that I have to send back because they said they cant cancel it  . In about 3 days I get the Droid and sent it back Verizon mistake and a waste of my time . After waiting over 2 wks and not receiving a new iPhone back to Verizon I went . I am now bitter at Verizon after doing 20 yrs of business with them. 3 people 1 hr later I was told it got delivered to my post office . I got the tracking num called the post office and they say o yes that was the droid. Go back in to Verizon another hr of explaining I walk out being told I will get a new iPhone in a couple of days, We’ll see . Once again I got the phone in your store just give me a new one . Do you realize how much of my time you have wasted ? do you care? O what is Your Policy ? How many Billion did you make last year ? I am Locked into a new 2 yr contract . Why don’t you just release me ? After 20 yrs do you think I will ever renew my contract ?

    Simple process. If an iPhone go to the Apple Store and not Verizon
    the phone from Verizon will be a refurbished device and not new unless under the 14 day worry free guarantee
    good luck

  • My wife an I share one iTunes account but have separate Apple id for our devices - prior to 8.1 we shared storage on iCloud now we can't and when I try to buy more storage on her phone instead of accessing the shared iTunes account it try's to sign

    MMy wife an I share one iTunes account but have separate Apple id for our devices - prior to 8.1 we shared storage on iCloud now we can't and when I try to buy more storage on her phone instead of accessing the shared iTunes account it try's to sign in to iTunes using her Apple id - I checked the iTunes id and password on both devices - can anyone help

    Have a look here...
    http://macmost.com/setting-up-multiple-ios-devices-for-messages-and-facetime.htm l

  • I purchased a magazine using the Zinio app which I'd recently downloaded on to a recently purchased iPad. I received an error message at the time (can't recall the details) but have since been invoiced for the $8.99. How can I get the Magazine I purchased

    I purchased a magazine using the Zinio app which I'd recently downloaded on to a recently purchased iPad. I received an error message at the time (can't recall the details) but have since been invoiced for the $8.99. How can I get the Magazine I purchased?

    FOR ASSISTANCE WITH ORDERS - iTUNES STORE CUSTOMER SERVICE
    For assistance with billing questions or other order inquiries, please refer to our online support page by clicking here: http://www.apple.com/support/itunes/store/. If you cannot find the answers you are seeking in our robust knowledge base, you can contact us by visiting the following URL http://www.apple.com/support/itunes/store/, clicking on the appropriate Customer Service topic, then using the contact button or email form at the bottom of the page. Responses to emails will be provided as soon as possible.
    Phone: 800-275-2273 How to reach a live person: Press 0 four times
    Hours of Operation: Mon-Fri: 9am-5pm ET
    Email: [email protected]
    How to report an issue with Your iTunes Store purchase
    http://support.apple.com/kb/HT1933
    How to Get a Refund from the App Store
    http://gizmodo.com/5886683/how-to-get-a-refund-from-the-app-store
    Canceling a Digital Subscription
    http://gadgetwise.blogs.nytimes.com/2011/10/14/qa-canceling-a-digital-subscripti on/
     Cheers, Tom

  • Greetings: I have multiple iPads and iPhones. I want all to be able to be able to stream to our Apple TV. All of the docs I see say you must have the same Apple ID for all the devices, but we each have our own Apple ID. Is this just a doc short coming?

    Greetings: I have multiple iPads and iPhones. I want all to be able to be able to stream to our Apple TV. All of the docs I see say you must have the same Apple ID for all the devices, but we each have our own Apple ID. Is this just a doc short coming?

    You can each have your own ID for your own iTunes accounts, but in order for a device to stream via AirPlay to the same Apple TV, everything must use the same homesharing ID. This is not the same as your iTunes account ID (although it can be for one of the devices)

  • Hi, we trialed from central and have bought the plus version but have still been charged for another month of the trial. Would you be good enough to refund the charge please considering we have bought the main product?

    Hi, we trialed from central and have bought the plus version but have still been charged for another month of the trial. Would you be good enough to refund the charge please considering we have bought the main product?

    After much back and forth drama, and the assistance of one awesome person at Apple, and also a great iTunes representitive, they have agreed to refund me.
    I have not yet recieved the refund but will post here when I do.
    This has been a long and drawn out headache, but I am glad that finally things seem to be set straight.

  • I have transferred Aperture 3 from an old Imac to a new. Now I´m requested to put in the serial number for v2, which I upgraded from several years ago. I can`t find it, but have the serial number for the upgrade. What to do?

    I have transferred Aperture 3 from an old Imac to a new. Now I´m requested to put in the serial number for v2, which I upgraded from several years ago. I can`t find it, but have the serial number for the upgrade. What to do? Can I find the original serial number in my old Imac?

    You'll have to contact Apple. See Pro Application Replacement Serial Numbers

  • I bought a movie...verified my payment,but have nooo idea how to get the movie downloaded or where to even find it. did not purchase coud. that ought to be a free feature

    I bought a movie...verified my payment,but have nooo idea how to get the movie downloaded or where to even find it. did not purchase coud. that ought to be a free feature

    Go to Settings>General>Usage. How much space does it show you have?
    If you connect your phone to your computer, what does iTunes say is on your phone? Do you see a large amount of "Other"? If so, you may have a corrupted database. You'll need to sync your phone then restore it as NEW (not from back up) then re-sync your data).

  • I am updating my iPhone 4 but have never backed it up. If I select "Set Up as New iPhone" will that erase everything -pictures, layout, downloaded apps?

    I am updating my iPhone 4 but have never backed it up. If I select "Set Up as New iPhone" will that erase everything -pictures, layout, downloaded apps?

    Yes.

  • I have an old MAC using OS 10.5 (does not have Intel processor). I need a new printer, but i cannot find one that will work with such an old operating system. Is there a new printer that will work with this old system?

    I have an old IMAC using 10.5, and it cannot use any newer operating system. I need a new printer, but I cannot find one that is compatible with this old system.
    Is there a new printer out there that will work with 10.5?
    Thanks,
    Niles Stroh

    Canon Pixma iP4950 works great with my PPC G5 iMac on Leopard. Maybe other of their models do as well.

  • I have a Cisco/Linksys WRT-54G wireless router and 2 Airport Extremes (the small ones that plug directly into the wall). Is it possible to extend the network from the router using these two AEs? I have a DVD player and Ext HD plugged into the router too..

    I have a Cisco/Linksys WRT-54G wireless router and 2 Airport Extremes (the small ones that plug directly into the wall). Is it possible to extend the network from the router using these two AEs? I have a DVD player and Ext HD plugged into the router too. Any ideas? I'm guessing the only way is to do what I've seen in these community pages which states that it can be done but it will drop the bandwidth by 50%. Thoughts?? Thanks!

    The Cisco/Linksys WRT-54G was one of the very few routers said to be compatible with Apple's implementation of WDS (Wireless Distribution System) settings.
    The info that I have on file indicates that only the WRT-54G versions 4 and under were compatible, so that would be one bridge to cross.
    Even if you find that your Cisco/Linksys might be the right version, Apple never published instructions on how to configure the Express devices with other manufactures, so users were left to their own devices to try to figure out how to get things working. Apple's instructions to connect to other Apple devices are in the link below:
    WDS network
    If you were hoping to use 2 Express devices in this type of configuration...even if it works...the bandwidth penalties will be extremely severe.
    The first Express drops the bandwidth (and speed) on the entire network in half and the second halves everything again. So, the result, in effect would be a "g" wireless network running at 25% speed. Few users would consider installing this type of network.
    At this point, it becomes one of those things where the fact that you might be able to do something does not mean that there would be much value in doing so. But, it is your decision to decide if you want to try to proceed.

  • I changed laptops and went from Vista to Windows 8. It requires icloud. I did icloud setup but it will not allow me access and wants to give me a new password. That is OK but it will not send me the promised password

    I changed laptops and went from Vista to Windows 8. It requires icloud. I did icloud setup but it will not allow me access and wants to give me a new password. That is OK but it will not send me the promised password even though itunes recognizes my password

    This has been posted for almost 2 weeks and not one reply. Thanks a bunch! As a newbie to OOo and Mozilla, I am not impressed with responsiveness to support needs with either. So, the future of my new experiment with going non-Microsoft remains an open question.
    Oddly enough, the problem mysteriously went away. I have followed the same routine and changed no settings. Suddenly all of the downloaded spreadsheets were no longer read-only. So I consider the matter resolved.

  • The default account option in iOS for contacts is missing; while I have multiple account options for mail and contacts are turned on for iCloud and other accounts.

    The default account option in iOS for contacts is missing; while I have multiple account options for mail and contacts are turned on for iCloud and other accounts.  I previously was able to select between my Microsoft Exchange Contacts (work account) and iCloud (personal), but both those options are now gone.  I tried turning contacts for both off and on and rebooting, but it didn't help.  Any ideas?

    Just a guess but if the exchange account is from your employer, your employer may have installed a security profile to your device preventing you from adding or editing contacts in the exchange account by removing the Default Account setting.  You would need to check with your employer's IT department to confirm this.  You could also test this by adding a Gmail account and turning on contacts syncing with Gmail to see if the setting reappears.

Maybe you are looking for