集群扩容
mysqldump -u root -p --all-databases > backup.sql# 输入密码
mysql -u root -p < backup.sql# 输入密码
半同步复制
Mysql的主从集群和互主集群都存在一个隐患,就是有可能会丢数据。这是为什么呢?这要从MySQL主从数据复制分析起。
MySQL主从集群默认采用的是一种异步复制的机制。主服务在执行用户提交的事 务后,写入binlog日志,然后就给客户端返回一个成功的响应了。而binlog会由一 个dump线程异步发送给Slave从服务。
由于这个发送binlog的过程是异步的。主服务在向客户端反馈执行结果时,是不 知道binlog是否同步成功了的。这时候如果主服务宕机了,而从服务还没有备份到 新执行的binlog,那就有可能会丢数据。那怎么解决这个问题呢,这就要靠MySQL的半同步复制机制来保证数据安全。
搭建半同步复制集群
首先我们登陆主服务,安装semisync_master模块:
mysql> show master status;
+-------------------+----------+--------------+--------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+--------------------------------------------+-------------------+
| master-bin.000008 | 157 | testdemo | information_schema,performation_schema,sys | |
+-------------------+----------+--------------+--------------------------------------------+-------------------+
1 row in set (0.00 sec)mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show global variables like 'rpl_semi%';
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | OFF |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)mysql> set global rpl_semi_sync_master_enabled=ON;
Query OK, 0 rows affected (0.00 sec)mysql> show global variables like 'rpl_semi%';
+-------------------------------------------+------------+
| Variable_name | Value |
+-------------------------------------------+------------+
| rpl_semi_sync_master_enabled | ON |
| rpl_semi_sync_master_timeout | 10000 |
| rpl_semi_sync_master_trace_level | 32 |
| rpl_semi_sync_master_wait_for_slave_count | 1 |
| rpl_semi_sync_master_wait_no_slave | ON |
| rpl_semi_sync_master_wait_point | AFTER_SYNC |
+-------------------------------------------+------------+
6 rows in set (0.00 sec)install plugin rpl_semi_sync_master soname 'semisync_master.so'; 是通过扩展库来安装半同步复制模块,需要指定 扩展库的文件名。show global variables like 'rpl_semi%'; 查看系统全局参数rpl_semi_sync_master_timeout就是半同步 复制时等待应答的最长等待时间,默认是10秒,可以根据情况自行调整set global rpl_semi_sync_master_enabled=ON; 是打开半同步复制的开关。rpl_semi_sync_master_wait_point其实表示一种半同步复制的方式。
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> show global variables like 'rpl_semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)mysql> set global rpl_semi_sync_slave_enabled = on;
Query OK, 0 rows affected (0.00 sec)mysql> show global variables like 'rpl_semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | ON |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
2 rows in set (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.02 sec)slave端的安装过程基本差不多,不过要注意下安装完slave端的半同步插 件后,需要重启下slave服务。