Box 1 : Ubuntu Hardy 8.04 server edition (Master)
Box 2 : Ubuntu Hardy 8.04 desktop edition (Slave)
Prerequisites:
Make sure your mysql compiled with –-have-openssl enabled. How to check the ssl service already enabled? From your mysql console type:
SHOW VARIABLES LIKE 'have_openssl';
The results will show you something like this:
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| have_ssl | DISABLED |
+---------------+-------+
The value can be DISABLED, YES or NO. If the value in there is NO, means your mysql server doesn’t support openssl and you must reinstall mysql with openssl enabled. If the value is DISABLED, it means your mysql server already support openssl but it’s not configured yet.
Creating Keys, Certificates and other fun SSL Stuff
Create your own Certification Authority (CA)
Change to the mysql ssl directory (you may need to create it)
mkdir /etc/mysql/sslCreate your own Certification Authority (CA) if you do not already have one (e.g. for signing web or mail server certificates)
cd /etc/mysql/ssl/
openssl req -x509 -new -days 9999 -newkey rsa:2048 -nodes -keyout ca-key.pem -out ca-cert.pemCreate a server certificate
Create the server certificate request
openssl req -new -newkey rsa:2048 -nodes -keyout server-key.pem -out server-csr.pem(optional) Remove the passphrase from the key
openssl rsa -in server-key.pem -out server-key.pem
Sign this server request with the CA key to make a proper server certificate.
openssl x509 -req -days 9999 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -CAserial ca-srl.txt -in server-csr.pem -out server-cert.pemAdjust the following lines (such as the paths) in /etc/mysql/my.cnf as necessary (note to self - the server keys go in the [mysqld] section, the client keys go in the [client] section):
[mysqld]Create the client certificates
ssl-key=/etc/mysql/ssl/server-key.pem
ssl-cert=/etc/mysql/ssl/server-cert.pem
ssl-ca=/etc/mysql/ssl/ca-cert.pem
Create the client certificate request
openssl req -new -newkey rsa:2048 -nodes -keyout client-key.pem -out client-csr.pem(OPTIONAL) Remove a passphrase from the key
openssl rsa -in client-key.pem -out client-key.pemSign this server request with the CA key to make a proper server certificate
openssl x509 -req -days 9999 -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -CAserial ca-srl.txt -in client-csr.pem -out client-cert.pemCopy the client files client-key, client-cert and ca-cert.pem to /etc/mysql/ssl on the client machine and adjust your /etc/mysql/my.cnf:
[client]Configure SSL Login User
master_ssl = 1
master_ssl_capath = /etc/mysql/ssl/
master_ssl_ca = /etc/mysql/ssl/ca-cert.pem
master_ssl_key = /etc/mysql/ssl/client-key.pem
master_ssl_cert = /etc/mysql/ssl/client-cert.pemIf you ever need to swap slave/master
Example:
mysql> grant replication slave on *.* to 'replicate'@'%' identied by 'password' require SSL;
mysql> flush privileges;
Replication Setting on my.cnf
Master:
server-id=1Slave:
log-bin=/var/log/mysql/mysql-bin.log
server-id = 2Before you restart mysql service. Please backup all data in master and pass it to slave.
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
#this is example, you can put another db to ignore replicate in this section.
replicate-ignore-db = mysql
#you can put direct ip / hostname for master-host
master-host = masterhost/10.0.0.1
master-port = 3306
master-user = mysqldb-rep
master-password = password
master-connect-retry = 60
After you dump all data from master to slave, you can restart both server to reload new setting (my.cnf).
root# /etc/init.d/mysql restartISSUES:
In ubuntu hardy apparmor prevent mysql to run over ssl. You must change the apparmor setting or stop the service so you can run mysql replication over ssl.
How to start replication:
Master>
mysql > reset master;
Slave>
mysql> start slave;Your slave status should
mysql> show slave status\G
mysql> show slave status\GMake sure this 3 variables value is YES:
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.1
Master_User: replicate
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000001
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: Yes
Master_SSL_CA_File: /etc/mysql/ssl/ca-cert.pem
Master_SSL_CA_Path: /etc/mysql/ssl/
Master_SSL_Cert: /etc/mysql/ssl/client-cert.pem
Master_SSL_Cipher:
Master_SSL_Key: /etc/mysql/ssl/client-key.pem
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_SSL_Allowed: Yes