Archive for MySQL
MySQL Master-Master Replication
Posted by: | CommentsIn my opinion, having dual-writable master MySQL databases (in a replication configuration) is not worth the hassle. There are a host of problems, enough that you should seriously consider what you’re trying to gain when attempting it. However, the master-master replication scheme still has some very good uses when used in an active-passive way. The two most compelling reasons for me are:
1. Keeping a “hot spare”. You have an additional database already configured as a master. This might not seem like much, since you “almost” gain the same thing in a master-slave setup. However, if your master-master includes slave servers, this topology provides a very high degree of fault tolerance. Especially when each master-server pair is geographically separated.
2. Making changes to the database. Certain changes made to the database may require a long time to complete, particularly if you have a very large database. In the master-master setup, you can take one of the servers “offline” (by telling the other master not to replicate its changes), and make the changes necessary. Then bring it back online, after the changes have been made, make it the active master, and let the other master perform the changes.
This could be seen as an added benefit of keeping a “hot spare”.
The good news is that setting this up is identical to setting up master-slave replication, you simply do it twice. Each master is essentially the master of and a slave to the other database. To keep it active-passive, one of the databases will need to be made read-only. Here are the changes you will need to the my.cnf configuration file:
Active Master my.cnf
log_bin = mysql-bin server_id = 1001 relay_log = mysql-relay-bin log_slave_updates = 1
Passive Master my.cnf
log_bin = mysql-bin server_id = 1002 relay_log = mysql-relay-bin log_slave_updates = 1 read_only = 1 # Notice this line
Then set up the replication user accounts, as described in this post: Simple MySQL Master-Slave Replication
Finally you issue the slave directives, and start the slave process:
Active Master ‘change master’
# Active master is slave to passive host CHANGE MASTER TO MASTER_HOST='passive.mysql.host', MASTER_USER='rep_user', MASTER_PASSWORD='reppassword', MASTER_LOG_FILE='mysql-bin-000001', MASTER_LOG_POS=0; start slave;
Passive Master ‘change master’
# Passive master is slave to active host CHANGE MASTER TO MASTER_HOST='active.mysql.host', MASTER_USER='rep_user', MASTER_PASSWORD='reppassword', MASTER_LOG_FILE='mysql-bin-000001', MASTER_LOG_POS=0; start slave;
Simple MySQL Master-Slave Replication
Posted by: | CommentsI’ve been doing a lot of research (not cutting edge type stuff) into MySQL scalability, and the first exercise I went through was configuring a simple master-slave replication setup. It was much simpler than I thought it would be. Here are the steps.
Editing the my.cnf Files
Because of the way replication works in MySQL, you will need to turn on binary logging. Essentially, the slave is going to connect to the master and request the log. After it gets the log (or the parts that it needs) it will replay it, executing the queries to bring it up to date. You also need to give the server an ID. So add these lines to the my.cnf (usually /etc/my.cnf) on the master database:
<mysqld> log_bin = mysql-bin server_id = 1001
You can give it any ID you want, you just want it to have a different ID than the slave. In the slave we will add a few additional lines, as well:
<mysqld> log_bin = mysql-bin server_id = 1002 relay_log = mysql-relay-bin log_slave_updates = 1 read_only = 1
Note: Replace the angle brackets above with square brackets. I haven’t figured out how to make my code plugin not treat [mysqld] as another code block definition.
According to Schwartz, et al. in High Performance MySQL (review forthcoming), the only parameter required on a slave is the server_id. The other parameters make it easy to switch the server between being a master or a slave. They also mention the read_only parameter is a good safety precaution but might not be applicable in all cases.
User Account Setup
Replication privileges need to be granted to the replication user on the master. If you want to be able to easily switch your server between master or slave, you can grant these privileges on both servers.
GRANT REPLICATION SLAVE ON *.* TO 'rep_user'@'10.0.0.%' IDENTIFIED BY 'reppassword';
There are certain features that are accessible by granting the REPLICATION CLIENT privilege. You can also limit the replication privileges to only certain databases or tables if so desired. Though it is not recommended, you could also grant access to all hosts, just as you might in any other GRANT statement.
Starting the Replication Procedure
Now that both the master and slave are configured, and correct permissions are granted to the replication user, the slave needs to be “started”. This is done by declaring the master host, and indicating necessary credentials and log file information, and then issuing the ’start slave’ command.
The host and credential information can be declared in the my.cnf file. However there are some advantages to making the declarations as MySQL commands. Specifically you will be able to make changes to these without having to restart the daemon. Here is the command you should issue to configure the slave:
CHANGE MASTER TO MASTER_HOST='master.mysql.host', MASTER_USER='rep_user', MASTER_PASSWORD='reppassword', MASTER_LOG_FILE='mysql-bin-000001', MASTER_LOG_POS=0;
And then you issue the command to start the slave:
start slave
If you were previously using binary logs on the master, and then cloned the server to create the slave, you might have trouble getting replication started without first deleting the binary log from the slave.
Sources
The MySQL documentation
High Performance MySQL, by Baron Schwartz et al.
Book Review: MySQL Stored Procedure Programming
Posted by: | CommentsI’ve been using MySQL for almost 7 years now without realizing it had stored procedure capabilities. So when I saw MySQL Stored Procedure Programming, by Guy Harrison with Steven Feurerstein, I decided to take the opportunity to advance my skills with MySQL. It’s a pretty good sized book, and it took me a while to get through it because it’s just one of those books you have to keep putting down.
That’s a good thing in my opinion, because it means the material is so interesting that I can’t read for more than a chapter without getting on the computer and trying it out. The first chapter was a tutorial, I thought I knew everything after I had gone through it and it took quite a bit of discipline (as well as a few error messages) for me to get back to the book and go through the topics.
There are three things I especially like about the book.
First was the additional coverage on triggers and transactions. After reading this I feel like I haven’t really used MySQL at all– having never used stored procedures, OR transactions, OR triggers. They were all topics that have been immediately applicable to my projects, because they were needed somewhere I just didn’t realize I could do them.
Second was the discussion of the material in the context of sound software engineering principles. I always enjoy a refresher in those, and when I’m learning a new technology that’s usually when I need it most because I’m ready to hack everything together in my excitement. For example, there’s an entire chapter on “Creating and Maintaining Stored Programs” as well as som optimization material and a discussion of best practices.
Third was their treatment on using stored procedures with specific programming languages. These may some day go out of date, but they had chapters devoted to showing how to use stored procedures from PHP, Java, Perl, Python, and .NET. All of which are relevant 4 years after publishing.
This book was an excellent choice for someone who has database experience, and some stored procedure experience (Oracle). Even if you’re only familiar with the basics of MySQL, you will benefit from this book. And it isn’t at all over the head of anybody with some database programming experience. My only regret is that I didn’t find this book 4 years ago when it came out.

