Getting Started With MySQL High Availability
You can monitor the common culprits that threaten high availability in databases either manually or by creating your own shell scripts or programs.
Join the DZone community and get the full member experience.Join For Free
keeping databases running consistently and continuously is crucial to many organizations. when your site or application fails to load because of problems with your databases, you risk losing revenue — especially in a business with a high-traffic site that is the main source of revenues. if it happens often enough, you'll lose not only transactions but customers.
there are many reasons why a database system may be unavailable, or at least not consistently available. it could be straightforward problems with your databases, or it could be hardware limitations. there are several potentially weak components of a database system. it's important to know where the potential weak points are and to have a clear sense of what's required to maintain a highly available database system.
if this concept is moderately new to you, it may be overwhelming. however, please understand that it's achievable and learnable. you can start by focusing on one component, one area of potential weakness, and then move on to strengthening the next. start by determining what you have, how it's configured, how it can be improved, and then try making some changes. for many changes, the results will show nothing but will prevent server problems later.
let's go through some of the hardware vulnerabilities that can potentially cause problems for your databases. a common problem that will disable mysql is not enough memory (i.e. ram). you can see how much memory you have, how much is used, and how much is free with this linux command,
free -wh total used free shared buffers cache available mem: 1.8g 783m 120m 54m 0b 933m 793m swap: 1.0g 185m 838m
this command will tell you how much ram is on the server, but also how much swap space has been allocated. you may discover you don't have enough memory available, in which case, you may need to add more ram. you might discover you haven't enabled swap space. if so, enable it. incidentally, the options
are not available for all versions. as an alternative to using
, you could read the contents of the
at a minimum, mysql will need ram for storing the grants tables, as well as caching the
for the server and for every session. it needs memory for many things. if there's no ram available for any of this, it will lock mysql.
checking hard drives
the next potential hardware problem is the hard drive. to check how much hard-drive space you have available, you can use the
command like so:
df -h filesystem size used avail use% mounted on /dev/xvda2 500g 169g 332g 34% /
a full hard drive can cause problems for mysql. for instance, when mysql executes
statements that use
clauses, it will retrieve all of the rows from the storage engine and then create a temporary table to store the results before sorting or grouping the data. it will store that temporary table in ram — if there's enough memory available to hold it. otherwise, it will store the temporary table on the hard drive in a temporary directory (see the
variable for the location). this is slower but necessary for large amounts of data. if there is very little ram available, it may have to write to the hard drive for every query. that will greatly reduce performance. however, if the hard drive is full, it will not be able to create temporary tables. this will also cause mysql to lock.
there are other hardware considerations for maintaining mysql high availability of a database system, such as using better quality hard-drives, checking your cpu usage (try the top utility for this) and network equipment and configuration. making sure you have good equipment and that mysql has enough room in which to work is the first step in ensuring mysql high availability.
there is one thing with which you should be aware when using the command-line utilities mentioned: they provide static results — they only tell you the state of the server at the time they're executed. if you run out of memory or hard drive space during the night while you're sleeping, your database system will be down. the problem might be resolved by the time you wake, but you will be unaware that it occurred, although your customers in other time zones may be very aware that there was a problem. unfortunately, you can't count on customers to tell you when there is a problem. you need a method to monitor these key hardware components.
you could write a set of shell scripts to check regularly the server using several command-line utilities and record in a log file when values exceed certain levels. that might work well, but it's a lot of trouble to create such scripts to check everything. if you want to have alerts sent to you when there are problems, and if you want to track usage so that you can watch for trends, that's, even more, programming work. all of this, though, is easy to do with monyog . if you don't have it already, consider downloading it.
monyog: edit server.
if you have monyog installed on your server, open it in your web browser and click on servers in the left margin. look for the box for your server — probably labeled localhost . there will be ellipses on that box indicating more options. click on the ellipses and you'll see a list of choices. choose edit server (see screenshot). if this isn't a new installation, you probably have the server configured already for mysql. you just need to enable ssh to be able to use the linux monitor group. click on the ssh tab and then the switch to enable it. choose the os system used — we're assuming linux for this article. if monyog is installed on the server which is running mysql, try using 127.0.0.1 for the host. the port will probably be 22 .
next, enter the username for ssh. if you're using amazon's aws, it might be something like
. monyog can authenticate with a key, but it has to be an openssh key. monyog can also authenticate with a password, but you may have to edit
. when you're finished, click on
. if it works successfully, click
. for this to work, you will need to have a user named
on the linux filesystem and that user will have to have access to the
directory and should be in the same group as the mysql user. if you already have monyog running and are monitoring mysql, all of this is probably already done.
now, you're ready to enable the linux monitor. click on monitors in the left margin. then click on the icon at the top right for managing monitor groups — it looks like a list of bullet items. there will be a list of group choices for what monyog will monitor. scroll down and enable linux, and then save . now, monyog is monitoring several things on your linux server.
monyog: linux monitor group.
while still on the monitors page, click on the linux monitor group — it's probably at the bottom of the list of monitor groups (see screenshot). you'll then be able to see plenty of information on the server: cpu usage, total memory, memory used and available, swap memory, and hard-disk space. you can also see how much memory mysql is using. if something isn't being monitored on the server, you can click the plus-sign next to the heading monitors to add a monitor. you will need to enter the command and settings for monyog to collect the information.
looking again at the linux monitor group, if you click on the small bar graph icon for a particular monitor, you can see a graphical representation of the information over time. this will help you to spot trends. if you click on the flag icon for a monitor, you can have monyog send you an email or an snmp trap to alert you when it exceeds parameters you set. this will allow you to detect a threat to the high availability of the database servers so you can have time to resolve a problem before there can be a loss of service.
achieving high availability for database servers can require some work and vigilance for a dba. there are many tools built into linux and other operating systems that can help. you can monitor the common culprits that threaten high availability either manually or by creating your own shell scripts or programs.
once you've made sure you have good equipment, that's it's configured properly, and you're monitoring system usage, including alerts for when parameters you set are exceeded, you're ready to consider the next component in maintaining high-availability: multiple servers. we'll look at that option in the next article in this series on high-availability.
Published at DZone with permission of Veer Abheek Singh Manhas. See the original article here.
Opinions expressed by DZone contributors are their own.