MySQL互为主从模型实现基于SSL复制
时间:2014-05-02 02:51:38
收藏:0
阅读:445
一、MySQL复制
1、MySQL复制过程描述
MySQL主服务器上每一次发生的有可能产生修改或者产生修改的操作都会在主服务器上基于语句或基于行写入二进制日志,从服务器会在此期间启用一个IO线程不断的向主服务器发送请求,主服务器的二进制日志一但有更新,则会启用binlog dump线程,把数据发送给对方,从服务器接收到数据后则会将二进制日志的内容同步至本地的中继日志保存,而后启用SQL线程,将日志中的操作语句写入本地从服务器数据库;
2、mysql复制的同步和异步
同步:客户端向主服务器执行一条修改操作时,主服务器将操作记录至二进制日志,然后通知从服务器,从服务器接收到主服务器发送的二进制日志内容于是将这些操作写入中继日志,主服务器在从服务器的日志写入成功后,于是将执行结果返回给客户端;
异步:客户端向主服务器执行一条修改操作时,主服务器将操作记录至二进制日志,日志写好后于是将执行结果返回给客户端,中间全程不与从服务器通信;
二、MySQL互为主从模型实现基于SSL复制配置
主机A
hostname:mysql1.wumoumou.com
IP:172.16.36.1
MySQL: Server version: 5.5.33
主机B
hostname:mysql2.wumoumou.com
IP:172.16.36.2
MySQL: Server version: 5.5.33
1、为mysql建立安全的SSL加密
1)、在主服务器生成CA证书。进入目录/etc/pki/CA,在该目录创建CA的自签证书。
[root@mysql1 CA]# (umask 077; openssl genrsa 2048 > private/cakey.pem)
[root@mysql1 CA]# openssl req -new -x509 -key private/cakey.pem -days 3655 -out cacert.pem
[root@mysql1 mysqldata]# mkdir ssl
[root@mysql1 mysqldata]# ls
binlog data ssl
[root@mysql1 mysqldata]# chown mysql.mysql ssl
[root@mysql1 mysqldata]# cd ssl
[root@mysql1 ssl]# (umask 077; openssl genrsa 1024 > master.key)
[root@mysql1 ssl]# openssl req -new -key master.key -out master.csr
[root@mysql1 ssl]# openssl ca -in master.csr -out master.crt
[root@mysql1 ssl]# chown mysql.mysql *
[root@mysql1 ssl]# ls
master.crt master.csr master.key
[root@mysql1 ssl]# vim /etc/my.cnf
[mysqld]
ssl
ssl-ca=/etc/pki/CA/cacert.pem
ssl-cert=/mysqldata/ssl/master.crt
ssl-key=/mysqldata/ssl/master.key
[root@mysql1 ssl]# service mysqld restart
[root@mysql1 ssl]# mysql -uroot -hlocalhost -p
mysql> show variables like ‘%ssl%‘;
+---------------+---------------------------+
| Variable_name | Value |
+---------------+---------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca |/etc/pki/CA/cacert.pem |
| ssl_capath ||
| ssl_cert |/mysqldata/ssl/master.crt |
| ssl_cipher ||
| ssl_key |/mysqldata/ssl/master.key |
+---------------+---------------------------+
7 rows in set (0.01 sec)
[root@mysql2 mysqldata]# mkdir ssl
[root@mysql2 mysqldata]# chown mysql.mysql ssl
[root@mysql2 mysqldata]# cd ssl
[root@mysql2 ssl]# (umask 077;openssl genrsa 1024 > slave.key)
[root@mysql2 ssl]# openssl req -new -key slave.key -out slave.csr
[root@mysql2 ssl]# scp slave.csr mysql1.wumoumou.com:/tmp
[root@mysql1 ssl]# openssl ca -in /tmp/slave.csr -out /tmp/slave.crt
[root@mysql1 ssl]# scp /tmp/slave.crt mysql2.wumoumou.com:/mysqldata/ssl/
[root@mysql1 ssl]# scp /etc/pki/CA/cacert.pem mysql2.wumoumou.com:/mysqldata/ssl/
[root@mysql2 ssl]# chown mysql.mysql *
[root@mysql2 ssl]# ls
cacert.pem slave.crt slave.csr slave.key
[root@mysql2 ssl]# vim /etc/my.cnf
[mysqld]
ssl
ssl-ca=/mysqldata/ssl/cacert.pem
ssl-cert=/mysqldata/ssl/slave.crt
ssl-key=/mysqldata/ssl/slave.key
[root@mysql2 ssl]# service mysqld restart
[root@mysql2 ssl]# mysql -uroot -hlocalhost -p
mysql> show variables like ‘%ssl%‘;
+---------------+---------------------------+
| Variable_name | Value |
+---------------+---------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca |/mysqldata/ssl/cacert.pem |
| ssl_capath ||
| ssl_cert |/mysqldata/ssl/slave.crt |
| ssl_cipher ||
| ssl_key |/mysqldata/ssl/slave.key |
+---------------+---------------------------+
7 rows in set (0.00 sec)
mysql> grant replication slave,replication client on *.* to ‘repluser‘@‘172.16.%.%‘ identified by ‘redhat‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
datadir=/mysqldata/data
innodb_file_per_table=ON
log-bin=/mysqldata/binlog/master-bin
binlog_format=mixed
server-id =100
relay-log =/mysqldata/relaylog/relay-bin
auto-increment-offset=1# 起始值
auto-increment-increment=2# 步长
skip_slave_start # 跳过slave自动启动,不让从服务器的IO和SQL两线程自动启动;
[root@mysql1 mysqldata]# mkdir relaylog && chown mysql.mysql relaylog;
datadir=/mysqldata/data
innodb_file_per_table=ON
log-bin=/mysqldata/binlog/master-bin
binlog_format=mixed
server-id =200
relay-log =/mysqldata/relaylog/relay-bin
auto-increment-offset=2
auto-increment-increment=2
skip_slave_start
[root@mysql2 mysqldata]# mkdir relaylog && chown mysql.mysql relaylog;
mysql> show master status\G
***************************1. row ***************************
File: master-bin.000005
Position:107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
mysql> show master status\G
***************************1. row ***************************
File: master-bin.000004
Position:107
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.03 sec)
mysql> change master to master_host=‘172.16.36.2‘,master_user=‘repluser‘,master_password=‘redhat‘,master_log_file=‘master-bin.000004‘,master_log_pos=107,master_ssl=1,master_ssl_ca=‘/etc/pki/CA/cacert.pem‘,master_ssl_cert=‘/mysqldata/ssl/master.crt‘,master_ssl_key=‘/mysqldata/ssl/master.key‘;
Query OK, 0 rows affected (0.04 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:172.16.36.2
Master_User: repluser
Master_Port:3306
Connect_Retry:60
Master_Log_File: master-bin.000004
Read_Master_Log_Pos:107
Relay_Log_File: mysql1-relay-bin.000002
Relay_Log_Pos:254
Relay_Master_Log_File: master-bin.000004
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:411
Until_Condition: None
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed: Yes
Master_SSL_CA_File:/etc/pki/CA/cacert.pem
Master_SSL_CA_Path:
Master_SSL_Cert:/mysqldata/ssl/master.crt
Master_SSL_Cipher:
Master_SSL_Key:/mysqldata/ssl/master.key
Seconds_Behind_Master:0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:0
Last_IO_Error:
Last_SQL_Errno:0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:1
1 row in set (0.00 sec)
mysql> change master to master_host=‘172.16.36.1‘,master_user=‘repluser‘,master_password=‘redhat‘,master_log_file=‘master-bin.000005‘,master_log_pos=107,master_ssl=1,master_ssl_ca=‘/mysqldata/ssl/cacert.pem‘,master_ssl_cert=‘/mysqldata/ssl/slave.crt‘,master_ssl_key=‘/mysqldata/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:172.16.36.1
Master_User: repluser
Master_Port:3306
Connect_Retry:60
Master_Log_File: master-bin.000005
Read_Master_Log_Pos:107
Relay_Log_File: mysql2-relay-bin.000002
Relay_Log_Pos:254
Relay_Master_Log_File: master-bin.000005
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:411
Until_Condition: None
Until_Log_File:
Until_Log_Pos:0
Master_SSL_Allowed: Yes
Master_SSL_CA_File:/mysqldata/ssl/cacert.pem
Master_SSL_CA_Path:
Master_SSL_Cert:/mysqldata/ssl/slave.crt
Master_SSL_Cipher:
Master_SSL_Key:/mysqldata/ssl/slave.key
Seconds_Behind_Master:0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno:0
Last_IO_Error:
Last_SQL_Errno:0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id:100
1 row in set (0.00 sec)
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> use db1;
Database changed
mysql> create table t1(id int(10) PRIMARY KEY AUTO_INCREMENT,name char(20));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 (name) value (‘tom‘);
Query OK, 1 row affected (0.01 sec)
mysql> insert into t1 (name) value (‘jery‘);
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|1| tom |
|3| jery |
+----+------+
2 rows in set (0.00 sec)
mysql> use db1;
Database changed
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| t1 |
+---------------+
1 row in set (0.00 sec)
mysql> select * from t1;
+----+------+
| id | name |
+----+------+
|1| tom |
|3| jery |
+----+------+
2 rows in set (0.00 sec)
mysql> insert t1(name) value(‘Jason Bourne‘);
Query OK, 1 row affected (0.01 sec)
mysql> insert t1(name) value(‘James Bond‘);
Query OK, 1 row affected (0.02 sec)
mysql> select * from t1;
+----+--------------+
| id | name |
+----+--------------+
|1| tom |
|3| jery |
|4| Jason Bourne |
|6| James Bond |
+----+--------------+
4 rows in set (0.00 sec)
评论(0)