MySQL主从复制
主从复制(一主多从)
实验环境准备:
(A)虚拟机:192.168.200.111 (master)
(B)虚拟机:192.168.200.112 (slave1)
(C)虚拟机:192.168.200.113 (slave2)
三台服务器同时关闭防火墙:
systemctl stop firewalld
setenforce 0
iptables -F
实验要求:
查看三台服务器时区是否一致
date(查看命令)
不一致的话同步网络时间
注意事项:三台服务器MySQL版本尽量相同
配置MySQL主服务器master
修改配置文件/etc/my.cnf添加以下内容(开启二进制日志)
server-id =1 //标明节点并且不能与其他服务器相同
log-bin=mysql-bin //设置二进制日志前缀
log-slave-updates=true //开启从日志
将服务从新启动
systemctl restart mariadb //重新启动服务(yum安装方式的)
ls /var/lib/mysql //查看是否生成日志文件
进入主mysql给从服务器授权
grant all on *.* to 'myslave'@'192.168.200.%' identified by '123';
flush privileges; //刷新授权表
show master status; //查看主授权表
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 1462 | | |
+------------------+----------+--------------+------------------+
配置从服务器slave
修改配置文件打开中继日志/etc/my.cnf
server-id=2 //表明节点
relay-log=log-bin //日志前缀
relay-log-index=slave-relay-bin.index //索引文件
进入从mysql内进行主从设置
stop slave; //关闭从库
change master to master_host='192.168.200.111',master_user='myslave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=1462; //设置主服务器
start slave //开启从库
show slave status\G //查看线程是否开启
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.111
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1462
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 929
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes //yes就是开启
Slave_SQL_Running: Yes //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: 1462
Relay_Log_Space: 2318
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)
另一台从服务器相同配置但server-id要改不能与其他相同