MySQL主从replication半同步设置及支持基于ssl复制配置(一)

2014-11-24 18:45:16 · 作者: · 浏览: 2

一、mysql的主从复制过程:


master中的dump进程将二进制文件读出,具有此服务器中replication client 和replication slave权限的从服务器的I/O 线程
读入主服务器的二进制文件并记录到relay-log中,从服务器的sql线程按照my.cnf中定义的规则,去读取relay-log,并更新到数据库

由上述过程可知,master维护bin-log ,slave维护relay-log 从而实现主从复制

主从实现:
主服务器中的配置 (172.16.21.1)
#vim my.cnf
[mysqld]
server-id=1
sync_binlog=1 //当执行事务时,将产生的数据和DDL立即同步到binlog中
innodb_flush_logs_at_trx_commit=1

#service mysqld restart
登录数据库并添加用户,此用户具有replication client 和replication slave 的权限
mysql>grant replication client,replication slave on *.* to repl@'172.16.%.%' identifided by '123456';
mysql>show grants for repl@'172.16.%.%';
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 11404543 | | |
+------------------+----------+--------------+------------------+

从服务器中的配置 (172.16.21.2)
vim my.cnf
[mysqld]
server-id=11
skip_slave_start=1
read_only=1

#bin-log=mysql-bin
relay-log=relay-bin
relay-log-index=relay-bin.index
登录数据库,将主服务器指向172.16.21.1 用户是repl 密码为123456 与上面的主服务器设置相对应
mysql>change master to master_user='repl',master_host='172.16.21.1',master_bin_log='mysql-bin.000001';
mysql>start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.21.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 11404543
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 11404689
Relay_Master_Log_File: mysql-bin.000001
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: 11404543
Relay_Log_Space: 11404839
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_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)

如果主从服务器都是第一次搭建,且没有数据存入时,在开启slave出错时的解决方法:
在主服务器中的数据库执行
mysql>flush master; //滚动二进制日志
在从服务器中执行
mysql>flush slave; //滚动中继日志

二、主主复制:

主1:(172.16.21.2)
如果是第一次开