一、节点信息:
Master1:192.168.80.143/24 + CA
Master2:192.168.80.144/24

这里两节点同为master,并且是对方节点的slave

二、基本配置:

(1)首先2台都安装mysql

 
  
  1. # pvcreate /dev/sda5   
  2. # vgcreate myvg /dev/sda5  
  3. # lvcreate -L 10G -n mydata myvg  
  4. # mkdir -p /data/mydata  
  5. # mke2fs -j /dev/myvg/mydata   
  6. # mount /dev/myvg/mydata /data/mydata/  
  7.  
  8. # tar xf mysql-5.5.24-linux2.6-i686.tar.gz  -C /usr/local/  
  9. # cd /usr/local/  
  10. # ln -s mysql-5.5.24-linux2.6-i686/ mysql  
  11. # cd mysql  
  12. # useradd -r mysql  
  13. # chown -R mysql.mysql .  
  14. # scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql 
  15. # chown -R root .  
  16. # cp support-files/my-large.cnf /etc/my.cnf  
  17. # vim /etc/my.cnf   
  18. thread_concurrency = 2 
  19. datadir = /data/mydata  
  20.  
  21. # cp support-files/mysql.server /etc/rc.d/init.d/mysqld  
  22. # chmod +x /etc/rc.d/init.d/mysqld  
  23. # service mysqld start 

(2)在master1上配置CA服务

 
  
  1. # vim /etc/pki/tls/openssl.cnf  
  2. dir             = /etc/pki/CA   
  3.  
  4. # cd /etc/pki/CA/  
  5. # mkdir certs newcerts crl  
  6. # touch index.txt  
  7. # echo 01 > serial  
  8.  
  9. # (umask 077;openssl genrsa -out private/cakey.pem 1024)  
  10. # openssl req -x509 -new -key private/cakey.pem  
  11.  
  12. # mkdir /usr/local/mysql/ssl  
  13. # cd /usr/local/mysql/ssl  
  14.  
  15. 主从服务器都需要证书,所以需要4个  
  16. # (umask 077;openssl genrsa 1024 > master1.key)  
  17. # openssl req -new -key master1.key -out master1.csr  
  18. # openssl ca -in master1.csr -out master1.crt -days 365  
  19.  
  20. # (umask 077;openssl genrsa 1024 > master1slave.key)  
  21. # openssl req -new -key master1slave.key -out master1slave.csr  
  22. # openssl ca -in master1slave.csr -out master1slave.crt -days 365  
  23.  
  24. # (umask 077;openssl genrsa 1024 > master2.key)  
  25. # openssl req -new -key master2.key -out master2.csr  
  26. # openssl ca -in master2.csr -out master2.crt -days 365  
  27.  
  28. # (umask 077;openssl genrsa 1024 > master2slave.key)  
  29. # openssl req -new -key master2slave.key -out master2slave.csr  
  30. # openssl ca -in master2slave.csr -out master2slave.crt -days 365  
  31.  
  32. # cp /etc/pki/CA/cacert.pem .  
  33.  
  34. # chown -R mysql.mysql /user/local/mysql/ssl  
  35.  
  36. # scp  -p /etc/pki/CA/cacert.pem master1slave.* master2.* 192.168.80.144:/usr/local/mysql/ssl/ 

三、两节点配置:

Master1:

 
  
  1. # vim /etc/my.cnf  
  2. skip-slave-start=1    //设置重启服务不自动开启线程,需要手动开启  
  3.  
  4. ssl      //指定ssl,CA信息  
  5. ssl-ca=/usr/local/mysql/ssl/cacert.pem  
  6. ssl-cert=/usr/local/mysql/ssl/master1.crt  
  7. ssl-key=/usr/local/mysql/ssl/master1.key  
  8.  
  9. log-bin=mysql-bin  
  10. relay-log=mysql-relay    //开启中继日志  
  11. auto-increment-increment = 2   //每次ID加2  
  12. auto-increment-offset = 1   //设置起始自动增长ID  
  13.  
  14. server-id       = 1 

Master2:

 
  
  1. # vim /etc/my.cnf  
  2. skip-slave-start=1 
  3.  
  4. ssl  
  5. ssl-ca=/usr/local/mysql/ssl/cacert.pem  
  6. ssl-cert=/usr/local/mysql/ssl/master2.crt  
  7. ssl-key=/usr/local/mysql/ssl/master2.key  
  8.  
  9. log-bin=mysql-bin  
  10. relay-log=mysql-relay  
  11. auto-increment-increment = 2 
  12. auto-increment-offset = 2 
  13.  
  14. server-id       = 2 
  15.  

重启服务生效

# service mysqld restart

共同配置复制用户信息,并指定通过SSL:

 
  
  1. mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO repluser@'192.168.80.%' IDENTIFIED BY 'redhat' REQUIRE SSL;  
  2.  
  3. mysql> flush privileges; 


分别查看日志位置信息:
Master1:

 
  
  1. mysql>show master status;  
  2. +------------------+----------+--------------+------------------+  
  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  4. +------------------+----------+--------------+------------------+  
  5. mysql-bin.000011 |      107 |              |                  |  
  6. +------------------+----------+--------------+------------------+  
  7. 1 row in set (0.00 sec 

Master2:

 
  
  1. mysql>show master status;  
  2. +------------------+----------+--------------+------------------+  
  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
  4. +------------------+----------+--------------+------------------+  
  5. mysql-bin.000017 |      107 |              |                  |  
  6. +------------------+----------+--------------+------------------+  
  7. 1 row in set (0.00 sec 


在Master2上配置Master1的slave信息:

 
  
  1. mysql> CHANGE MASTER TO MASTER_HOST = '192.168.80.143' ,  //指定主服务器  
  2.     -> MASTER_USER = 'repluser' ,   //指定用户  
  3.     -> MASTER_PASSWORD = 'redhat' ,    //密码  
  4.     -> MASTER_LOG_FILE = 'mysql-bin.000017' ,  //指定日志  
  5.     -> MASTER_LOG_POS = 107 ,    //指定日志位  
  6.     -> MASTER_SSL = 1 ,  
  7.     -> MASTER_SSL_CA = '/usr/local/mysql/ssl/cacert.pem' ,  
  8.     -> MASTER_SSL_CERT = '/usr/local/mysql/ssl/master1slave.crt' ,  
  9.     -> MASTER_SSL_KEY = '/usr/local/mysql/ssl/master1slave.key';  


在Master1上配置Master2的slave信息:

 
  
  1. mysql> CHANGE MASTER TO MASTER_HOST = '192.168.80.144' ,    
  2.     -> MASTER_USER = 'repluser' ,     
  3.     -> MASTER_PASSWORD = 'redhat' ,      
  4.     -> MASTER_LOG_FILE = 'mysql-bin.000011' ,    
  5.     -> MASTER_LOG_POS = 107 ,      
  6.     -> MASTER_SSL = 1 ,  
  7.     -> MASTER_SSL_CA = '/usr/local/mysql/ssl/cacert.pem' ,  
  8.     -> MASTER_SSL_CERT = '/usr/local/mysql/ssl/master2slave.crt' ,  
  9.     -> MASTER_SSL_KEY = '/usr/local/mysql/ssl/master2slave.key';      

     
2节点查看信息:

 
  
  1. mysql> show slave status\G  
  2. *************************** 1. row ***************************  
  3.                Slave_IO_State: Waiting for master to send event  
  4.                   Master_Host: 192.168.80.144  
  5.                   Master_User: repluser  
  6.                   Master_Port: 3306  
  7.                 Connect_Retry: 60  
  8.               Master_Log_File: mysql-bin.000011  
  9.           Read_Master_Log_Pos: 107  
  10.                Relay_Log_File: mysql-relay.000002  
  11.                 Relay_Log_Pos: 557  
  12.         Relay_Master_Log_File: mysql-bin.000011  
  13.              Slave_IO_Running: No  
  14.             Slave_SQL_Running: No  
  15.               Replicate_Do_DB:   
  16.           Replicate_Ignore_DB:   
  17.            Replicate_Do_Table:   
  18.        Replicate_Ignore_Table:   
  19.       Replicate_Wild_Do_Table:   
  20.   Replicate_Wild_Ignore_Table:   
  21.                    Last_Errno: 0  
  22.                    Last_Error:   
  23.                  Skip_Counter: 0  
  24.           Exec_Master_Log_Pos: 411  
  25.               Relay_Log_Space: 709  
  26.               Until_Condition: None  
  27.                Until_Log_File:   
  28.                 Until_Log_Pos: 0  
  29.            Master_SSL_Allowed: Yes  
  30.            Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem  
  31.            Master_SSL_CA_Path:   
  32.               Master_SSL_Cert: /usr/local/mysql/ssl/master2slave.crt  
  33.             Master_SSL_Cipher:   
  34.                Master_SSL_Key: /usr/local/mysql/ssl/master2slave.key  
  35.         Seconds_Behind_Master: 0  
  36. Master_SSL_Verify_Server_Cert: No  
  37.                 Last_IO_Errno: 0  
  38.                 Last_IO_Error:   
  39.                Last_SQL_Errno: 0  
  40.                Last_SQL_Error:   
  41.   Replicate_Ignore_Server_Ids:   
  42.              Master_Server_Id: 2  
  43. 1 row in set (0.00 sec)  


确认无误后启动slave :

 
  
  1. mysql> start slave;  
  2. mysql> show slave status\G  
  3.             ...  
  4.              Slave_IO_Running: Yes  
  5.             Slave_SQL_Running: Yes  
  6.             ... 

四、测试:


在Master1上建立数据库:
mysql> create database tb;

在Master2上查看已经有了:

在Master2上建立表:

 
  
  1. mysql> use tb  
  2. mysql> create table aa (name varchar(10));  
  3. mysql> insert into aa ('centos'),('jin'); 


在Master1上查看:


至于验证SSL的话,可以用SSL连接试验,如下:

 


可以看到已经有SSL
SSL:   Cipher in use is DHE-RSA-AES256-SHA

至此myslq主-主复制 + ssl认证 就已经OK了,如有错误请指出,非常感谢!