mysql5.5的主从复制
master端:
1.修改/etc/my.cnf,配置日志参数
log-bin=master-bin
log-bin-index=master-bin.index
sync_binlog=1
2.修改/etc/my.cnf,选择一个唯一的server_id
server_id=N
3.创建一个复制权限的用户
mysql> grant replication slave on *.* to repluser@myslave;
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication client on *.* to repluser@myslave;
Query OK, 0 rows affected (0.00 sec)
主服务器端通过dump线程将数据传送至从服务器.
4.备份主服务器,并将备份数据传送至从服务器.
innobackupex --user=backupuser --password=backupuser /tmp/bak/fullbk
innobackupex --apply-log --redo-only /tmp/bak/fullbk/2014-08-15
slave端:
1.修改/etc/my.cnf,设置日志参数
relay-log=slave-log
relay-log-index=slave-log.index
read_only=1
2.修改/etc/my.cnf,选择唯一的server_id
server_id=N
3.恢复主服务器数据.
innobackupxe --copy-back /tmp/2014-08-15
4.启动mysql,并连接至主服务器,启用复制
service mysqld restart
mysql> CHANGE MASTER TO
-> MASTER_HOST='mymaster',
-> MASTER_USER='repluser',
-> MASTER_PASSWORD='repluser',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin.000002',
-> MASTER_LOG_POS=699,
-> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.04 sec)
5.显示从服务器状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: mymaster
Master_User: repluser
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 699
Relay_Log_File: myrelay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No
Slave_SQL_Running: No
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: 699
Relay_Log_Space: 107
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: NULL
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: 0
1 row in set (0.00 sec)
6.启动从服务器的进程.
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
从服务器端通过I/O thread接受主服务器传来的事务日志,并通过sql thread应用事务日志.
启动I/O与sqlthread命令如下:
mysql> start slave sql_thread;
mysql> stop slave sql_thread;
mysql> start slave io_thread;
mysql> stop slave io_thread;
7.在mysql从数据库的datadir目录下会出现relay-log.info和master.info.
其中master.info记录了执行change master to所需要输入的信息
如master_host,master_user,master_password,master_port,mater_log_file,master_log_file_pos,mater_connet_retry
reley-log.info记录了如下信息:
当前主服务器的日志名称及pos信息,从服务器中继日志的名称,与pos信息.
8.read-only设置为on后,具有super权限的用户不生效.
配置mysql的半同步复制:
1.半同步复制的lib文件位于$MYSQL_HOME/lib/plugin下.
主数据库半同步复制的lib文件为:semisync_master.so
从数据库半同步复制的lib文件为:semisync_slave.so
2.检查是否支持动态添加插件:
mysql> select @@have_dynamic_loading ;
+------------------------+
| @@have_dynamic_loading |
+------------------------+
| YES |
+------------------------+
1 row in set (0.00 sec)
4.安装半同步复制插件
主服务器
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected (0.02 sec)
检查并启用半同步复制:
mysql> show global variables like '%rpl%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
5 rows in set (0.00 sec)
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)
//并将该参数写入到/etc/my.cnf中,确保重启后生效.
mysql> show global variables like '%rpl%';
+------------------------------------+-------+
| Variable_name | Value |
+------------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_no_slave | ON |
+------------------------------------+-------+
5 rows in set (0.01 sec)
mysql> show global status like '%rpl%';
+--------------------------------------------+-------------+
| Variable_name | Value |
+--------------------------------------------+-------------+
| Rpl_semi_sync_master_clients | 1 |
| Rpl_semi_sync_master_net_avg_wait_time | 20398 |
| Rpl_semi_sync_master_net_wait_time | 40796 |
| Rpl_semi_sync_master_net_waits | 2 |
| Rpl_semi_sync_master_no_times | 1 |
| Rpl_semi_sync_master_no_tx | 1 |
| Rpl_semi_sync_master_status | ON |
| Rpl_semi_sync_master_timefunc_failures | 0 |
| Rpl_semi_sync_master_tx_avg_wait_time | 0 |
| Rpl_semi_sync_master_tx_wait_time | 0 |
| Rpl_semi_sync_master_tx_waits | 0 |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0 |
| Rpl_semi_sync_master_wait_sessions | 0 |
| Rpl_semi_sync_master_yes_tx | 0 |
| Rpl_status | AUTH_MASTER |
+--------------------------------------------+-------------+
15 rows in set (0.02 sec)
从服务器
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected (0.11 sec)
mysql> show global variables like '%rpl%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
3 rows in set (0.00 sec)
mysql> set global rpl_semi_sync_slave_enabled=1;
Query OK, 0 rows affected (0.06 sec)
//并将该参数写入到/etc/my.cnf中,确保重启后生效.
mysql> show global variables like '%rpl%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_recovery_rank | 0 |
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
3 rows in set (0.00 sec)
重启IO_thread后,设置生效.
mysql> stop slave io_thread;
Query OK, 0 rows affected (0.02 sec)
mysql> stop slave io_thread;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> start slave io_thread;
Query OK, 0 rows affected (0.00 sec)
mysql> show global status like '%rpl%';
+----------------------------+-------------+
| Variable_name | Value |
+----------------------------+-------------+
| Rpl_semi_sync_slave_status | ON |
| Rpl_status | AUTH_MASTER |
+----------------------------+-------------+
2 rows in set (0.01 sec)
mysql复制的过滤选项:
主数据库端(为保证主数据库的二进制日志的完整性,不建议在主数据库上启用过滤选项):
binlog_do_db:仅将指定的相关数据库修改操作记录二进制日志
binlog_ignore_db:除了指定的数据库修改操作不记录二进制,其他的均记入二进制日志.
从数据库端:
replicate_do_db:仅将指定的相关数据库修改操作记录中继日志
replicate_ignore_db:除了指定的数据库修改操作不记录中继日志,其他的均记入日志.
replicate_do_table:仅将指定的相关表修改操作记录中继日志
replicate_ignore_table:除了指定的表不记录中继日志,其他的均记入日志.
replicate_wild_do_table:仅将指定的相匹配的表的操作记录到中继日志
replicate_wild_ignore_table:仅将指定的相匹配的表的操作不记录到中继日志,其他的均记入.
检查设置的参数:
[root@idc126 ~]# mysql -e 'show slave status\G' |grep -i replicate
Replicate_Do_DB: mydb
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Replicate_Ignore_Server_Ids:
mysql5.6通过引入gtid的功能,使得可以使用多线程复制:
slave_parallel_workers=数据库的个数 //设置从服务器并行线程的个数,等于0,则禁用该功能.
mysql5.6依赖python2.5,故不能直接在centos5系统上.