五、开启SSL复制
1、创建CA及自签证书,主从服务器的创建私钥申请证书和CA颁发证书的具体过程之前的文档已经有详细说明这里不再给出,详情请看《创建CA颁发证书》 见 http://www.linuxidc.com/Linux/2012-09/71470.htm
这里假设CA的证书存放目录为:/usr/local/MySQL/ssl/cacert.pem
主服务器相关证书和密钥:/usr/local/mysql/ssl/master.crt
/usr/local/mysql/ssl/master.key
从服务器相关证书和密钥:/usr/local/mysql/ssl/slave.crt
/usr/local/mysql/ssl/slave.key
2、编辑主服务器配置文件my.cnf添加下行:
[mysqld]
ssl
ssl-ca=/usr/local/mysql/ssl/cacert.pem
ssl-cert=/usr/local/mysql/ssl/master.crt
ssl-key=/usr/local/mysql/ssl/master.key
[root@master ssl]# service mysqld restart //重启服务至配置生效
验证配置是否生效:
mysql> SHOW VARIABLES LIKE '%ssl%';
+---------------+---------------------------------+
| Variable_name | Value |
+---------------+---------------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /usr/local/mysql/ssl/cacert.pem |
| ssl_capath | |
| ssl_cert | /usr/local/mysql/ssl/master.crt |
| ssl_cipher | |
| ssl_key | /usr/local/mysql/ssl/master.key |
+---------------+---------------------------------+
7 rows in set (0.00 sec)
可以看到have_ssl和have_openssl的状态由原来的DISABLE转化为YES
3、从服务器配置
从服务器不建议在配置文件中配置
停止从服务进程
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.01 sec)
配置ssl相关参数
mysql> CHANGE MASTER TO MASTER_SSL=1,MASTER_SSL_CA='/usr/local/mysql/ssl/cacert.pem',MASTER_SSL_CERT='/usr/local/mysql/ssl/slave.crt',MASTER_SSL_KEY='/usr/local/mysql/ssl/slave.key';
Query OK, 0 rows affected (0.01 sec)
启动从服务进程
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
验证配置是否生效
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.101
Master_User: cpmysql
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000014
Read_Master_Log_Pos: 107
Relay_Log_File: mysql-relay.000005
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000014
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
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: 107
Relay_Log_Space: 551
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: Yes
Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem
Master_SSL_CA_Path:
Master_SSL_Cert: /usr/local/mysql/ssl/slave.crt
Master_SSL_Cipher:
Master_SSL_Key: /usr/local/mysql/ssl/slave.key
...
确保:Slave_IO_Running、Slave_SQL_Running和Master_SSL_Allowed三个的值为Yes
4、验证ssl
在从服务器上使用之前创建的复制用户cpmysql测试能否通过ssl登录主服务器
[root@slave ~]# /usr/local/mysql/bin/mysql --ssl-ca=/usr/local/mysql/ssl/cacert.pem --ssl-cert=/usr/local/mysql/ssl/slave.crt --ssl-key=/usr/local/mysql/ssl/slave.key -u cpmysql --host 192.168.56.101 -p
mysql> \s;
--------------
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.5.24, for linux2.6 (i686) using readline 5.1
Connection id: 10
Current database:
Current user: cpmysql@192.168.56.102
SSL: Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.5.24-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 192.168.56.101 via TCP/IP
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
...
可以看到已经使用了SSL加密。
5、修改cpmysql用户使之强制必须使用ssl连接
在主服务器执行,添加REQUIRE SSL选项:
mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO cpmysql@'192.168.56.102' IDENTIFIED BY 'cpmysql' REQUIRE SSL;
Query OK, 0 rows affected (0.03 sec)
刷新授权表
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
在从服务器上登录测试:
[root@slave ~]# /usr/local/mysql/bin/mysql -u cpmysql -p --host 192.168.56.101
Enter password:
ERROR 1045 (28000): Access denied for user 'cpmysql'@'192.168.56.102' (using password: YES)
从上边提示可以看到不指定ssl信息时连接被拒绝
使用ssl登录
[root@slave ~]# /usr/local/mysql/bin/mysql --ssl-ca=/usr/local/mysql/ssl/cacert.pem --ssl-cert=/usr/local/mysql/ssl/slave.crt --ssl-key=/usr/local/mysql/ssl/slave.key -u cpmysql -p -h 192.168.56.101
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.24-log MySQL Community Server (GPL)
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
登录成功!
六、主主模型的配置
主主模型既是互为主从关系,因此双方都需要在各自的服务器上开启二进制日志和中继日志,且都需要创建能够用来让对方复制数据的用户,由于主主模型本身存在很多问题且并不常用,配置也很简单这里只大概说明下配置过程:
1、修改了两个节点的主配置文件都要开启二进制日志和中继日志,设置不同的server-id,然后重启服务使生效;
2、分别在两个节点上创建用于复制数据的mysql用户,能够让该用户登录对方;
3、查看各自当前使用的二进制文件和Position值;
4、配置参数指定对方为自己的主服务器,并启动从服务进程即可,操作过程请看前边主从复制时的说明。
说明:主主模型不推荐使用。
