How To Set Up MySQL Database Replication With SSL Encryption On Debian Squeeze
|
Submitted by falko (Contact Author) (Forums) on Sun, 2011-06-05 16:10. :: Debian | High-Availability | MySQL
How To Set Up MySQL Database Replication With SSL Encryption On Debian SqueezeVersion 1.0 This tutorial describes how to set up database replication in MySQL using an SSL connection for encryption (to make it impossible for hackers to sniff out passwords and data transferred between the master and slave). MySQL replication allows you to have an exact copy of a database from a master server on another server (slave), and all updates to the database on the master server are immediately replicated to the database on the slave server so that both databases are in sync. This is not a backup policy because an accidentally issued DELETE command will also be carried out on the slave; but replication can help protect against hardware failures though. I do not issue any guarantee that this will work for you!
1 Preliminary NoteIn this tutorial I will show how to replicate the database exampledb from the server server1.example.com (master) with the IP address 192.168.0.100 to the server server2.example.com (slave) with the IP address 192.168.0.101. Both systems are running Debian Squeeze; however, the configuration should apply to almost all distributions with little or no modifications. The database exampledb with tables and data is already existing on the master, but not on the slave. I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root.
2 Installing MySQL 5 And Enabling SSL SupportIf MySQL 5 isn't already installed on server1 and server2, install it now: server1/server2: apt-get install mysql-server mysql-client You will be asked to provide a password for the MySQL root user - this password is valid for the user root@localhost as well as root@server1.example.com / root@server2.example.com, so we don't have to specify a MySQL root password manually later on: New password for the MySQL "root" user: <-- yourrootsqlpassword Now we must check if both MySQL server support SSL connections. Log into MySQL... mysql -u root -p ... and run the following command on the MySQL shell: show variables like '%ssl%'; If the output is as follows (both have_openssl and have_ssl show DISABLED)... mysql> show variables like '%ssl%'; ... it means that MySQL was compiled with SSL support, but it's currently not enabled. To enable it, leave the MySQL shell first... quit; ... and open /etc/mysql/my.cnf: vi /etc/mysql/my.cnf Scroll down to the * Security Features section (within the [mysqld] section) and add a line with the word ssl to it:
Restart MySQL... /etc/init.d/mysql restart ... and check again if SSL is now enabled: mysql -u root -p show variables like '%ssl%'; Output should be as follows which means that SSL is now enabled: mysql> show variables like '%ssl%'; Type... quit; ... to leave the MySQL shell.
3 Configuring The MasterTo make sure that the replication can work, we must make MySQL listen on all interfaces on the master (server1), therefore we comment out the line bind-address = 127.0.0.1 in /etc/mysql/my.cnf: server1: vi /etc/mysql/my.cnf
Restart MySQL afterwards: /etc/init.d/mysql restart Then check with netstat -tap | grep mysql that MySQL is really listening on all interfaces on the master: server1:~# netstat -tap | grep mysql Now we create the CA, server, and client certificates that we need for the SSL connections. I create these certificates in the directory /etc/mysql/newcerts which I have to create first: mkdir /etc/mysql/newcerts && cd /etc/mysql/newcerts Make sure that openssl is installed: apt-get install openssl Create CA certificate: openssl genrsa 2048 > ca-key.pem Create server certificate: openssl req -newkey rsa:2048 -days 1000 -nodes -keyout server-key.pem > server-req.pem Create client certificate: openssl req -newkey rsa:2048 -days 1000 -nodes -keyout client-key.pem > client-req.pem The output of... ls -l ... should now look as follows: root@server1:/etc/mysql/newcerts# ls -l We must now transfer ca-cert.pem, client-cert.pem, and client-key.pem to the slave (server2); before we do this, we create the directory /etc/mysql/newcerts on server2: server2: mkdir /etc/mysql/newcerts Back on server1, we can transfer the three files to server2 as follows: server1: scp /etc/mysql/newcerts/ca-cert.pem root@192.168.0.101:/etc/mysql/newcerts scp /etc/mysql/newcerts/client-cert.pem root@192.168.0.101:/etc/mysql/newcerts scp /etc/mysql/newcerts/client-key.pem root@192.168.0.101:/etc/mysql/newcerts Next, open /etc/mysql/my.cnf... vi /etc/mysql/my.cnf ... and modify the * Security Features section; uncomment the ssl-ca, ssl-cert, and ssl-key lines and fill in the correct values:
Restart MySQL: /etc/init.d/mysql restart Now we set up a replication user slave_user that can be used by server2 to access the MySQL database on server1: mysql -u root -p On the MySQL shell, run the following commands: GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%' IDENTIFIED BY 'slave_password' REQUIRE SSL; The REQUIRE SSL string is optional; if you leave it out, slave_user will be allowed to connect through encrypted and also unencrypted connections. If you use REQUIRE SSL, then only encrypted connections are allowed. (If you've already set up a replication user, and now want to modify it so that it can only connect through SSL, you can modify the user as follows: GRANT USAGE ON *.* TO 'slave_user'@'%' REQUIRE SSL; ) FLUSH PRIVILEGES; Furthermore we have to tell MySQL for which database it should write logs (these logs are used by the slave to see what has changed on the master), which log file it should use, and we have to specify that this MySQL server is the master. We want to replicate the database exampledb, so we add/enable the following lines in /etc/mysql/my.cnf (in the [mysqld] section): vi /etc/mysql/my.cnf
Then restart MySQL: /etc/init.d/mysql restart Next we lock the exampledb database on server1, find out about the master status of server1, create an SQL dump of exampledb (that we will import into exampledb on server2 so that both databases contain the same data), and unlock the database so that it can be used again: mysql -u root -p On the MySQL shell, run the following commands: USE exampledb; The last command should show something like this (please write it down, we'll need it later on): mysql> SHOW MASTER STATUS; Now don't leave the MySQL shell, because if you leave it, the database lock will be removed, and this is not what we want right now because we must create a database dump now. While the MySQL shell is still open, we open a second command line window where we create the SQL dump snapshot.sql and transfer it to server2 (using scp; again, make sure that the root account is enabled on server2): server1: cd /tmp Afterwards, you can close the second command line window. On the first command line window, we can now unlock the database and leave the MySQL shell: server1: UNLOCK TABLES;
|




Recent comments
6 hours 54 min ago
13 hours 36 min ago
17 hours 26 min ago
19 hours 5 min ago
1 day 3 hours ago
1 day 12 hours ago
1 day 13 hours ago
1 day 17 hours ago
1 day 21 hours ago
1 day 22 hours ago