主从同步:实现数据自动同步的服务结构
-主服务器:接受客户端访问连接
-从服务器:自动同步主服务器数据
主从同步原理:
Master:启用binlog日志
Slave:
-Slave_IO:复制master主机binlog里的SQL命令到本机的relay-log文件里
-Slave_SQL:执行本机relay-log文件里的SQL语句,实现与master一致
主从同步分类:
-一主一丛
-一主多从
-主从从同
-主主同步
客户端:192.168.4.50
master服务器:192.168.4.51
slave服务器:192.168.4.52
[root@host51 ~]# vim /etc/my.cnf
server_id=51
log_bin=master51
[root@host51 ~]# systemctl restart mysqld
repliccation slave 授权命令权限
mysql> grant replication slave on *.* to repluser@"%" identified by "123qqq...A";
mysql> show master status\G
*************************** 1. row ***************************
File: master51.000002
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
[root@host52 ~]# vim /etc/my.cnf
[mysqld]
server_id=52
[root@host52 ~]# systemctl restart mysqld
在指定之前要确保主从服务器的数据一致
mysql> change master to master_host="192.168.4.51",master_user="repluser",master_password="123qqq...A",master_log_file="master51.000002",master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
Slave_IO_Running: No
Slave_SQL_Running: Yes
这里我的io线程是no,我首先去看了一遍host52的var/lib/mysql/master.info配置,master主机的ip、日志文件、偏移量都没错;删除配置文件在重新配置一边slave,io线程还是no状态;
后来经过我上网查询,从这篇博客中得知原来是我的两台服务器的uuid一样
于是我修改slave的uuid后再重启,io进程就显示yes了
uuid修改地址:/var/lib/mysql/auto.cnf
博客地址:mysql主从复制 Slave_IO_Running: NO_mysql主从复制,从库slave_io_running: no_李在奋斗的博客-CSDN博客
mysql> show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
复制模式:
异步复制
半同步复制
全同步复制
mysql> show variables like "have_dynamic_loading";
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| have_dynamic_loading | YES |
+----------------------+-------+
1 row in set (0.00 sec)
mysql> install plugin rpl_semi_sync_master soname "semisync_master.so";
mysql> install plugin rpl_semi_sync_slave soname "semisync_slave.so";
//查看
mysql> select plugin_name,plugin_status from information_schema.plugins where plugin_name like "%semi%";
+----------------------+---------------+
| plugin_name | plugin_status |
+----------------------+---------------+
| rpl_semi_sync_master | ACTIVE |
| rpl_semi_sync_slave | ACTIVE |
+----------------------+---------------+
2 rows in set (0.00 sec)
[root@host51 ~]# vim /etc/my.cnf
[mysqld]
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master_enabled=1
rpl_semi_sync_slave_enabled=1