1.理解MySQL主从复制原理。
2.完成MySQL主从复制。
1 、 MySQL 将数据变化记录到二进制日志中;2 、 Slave 将 MySQL 的二进制日志拷贝到 Slave 的中继日志中;3 、 Slave 将中继日志中的事件在做一次,将数据变化,反应到自身( Slave )的数据库
- 主数据库有个bin-log二进制文件,记录了所有增删改Sql语句。(binlog线程)
- 从数据库把主数据库的bin-log文件的sql语句复制过来。(io线程)
- 从数据库的relay-log重做日志文件中再执行一次这些sql语句。(Sql执行线程)
主节点 192.168.186.251 master
从节点 192.168.186.100 slave1
关闭防火墙
[root@master ~]# systemctl disable firewalld
关闭selinux[root@master ~]# setenforce 0
时间同步
主、从服务器使用相同版本的mysql数据库
(1)、启用二进制日志 ,并为当前节点设置一个全局唯一的ID号,然后创建目录修改权限
[root@master ~]# vim /etc/my.cnf
[mysqld]
log_bin = /data/logbin/mysql-bin
server_id = 1
[root@master ~]# mkdir /data
[root@master ~]# mkdir /data/logbin
[root@master ~]# chown mysql.mysql /data/logbin/
(2)、重新启动mysql服务
[root@master ~]# systemctl restart mysqld
(3)、查看/data/logbin/目录是否有数据
[root@master ~]# ls /data/logbin
mysql-bin.000001 mysql-bin.index
(4)、登录mysql,创建账号,账号授权
[root@master ~]# mysql -uroot -p123456
mysql5.7 [(none)]>create user wp@'192.168.186.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql5.7 [(none)]>grant replication slave on *.* to wp@'192.168.186.%';
Query OK, 0 rows affected (0.01 sec)
mysql5.7 [(none)]>show grants for 'rep'@'192.168.186.%';
+---------------------------------------------------------+
| Grants for [email protected].% |
+---------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.186.%' |
+---------------------------------------------------------+
1 row in set (0.00 sec)
(5)、锁表设置只读,查看主库状态
mysql5.7 [(none)]>flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql5.7 [(none)]>show variables like '%timeout%';
+-----------------------------+----------+
| Variable_name | Value |
+-----------------------------+----------+
| connect_timeout | 10 |
| delayed_insert_timeout | 300 |
| have_statement_timeout | YES |
| innodb_flush_log_at_timeout | 1 |
| innodb_lock_wait_timeout | 50 |
| innodb_rollback_on_timeout | OFF |
| interactive_timeout | 28800 |
| lock_wait_timeout | 31536000 |
| net_read_timeout | 30 |
| net_write_timeout | 60 |
| rpl_stop_slave_timeout | 31536000 |
| slave_net_timeout | 60 |
| wait_timeout | 28800 |
+-----------------------------+----------+
13 rows in set (0.00 sec)
mysql5.7 [(none)]>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 607 | | | 388ebd53-291b-11ee-b68c-000c29090543:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
mysql5.7 [(none)]>\q
Bye
(6)、备份数据库数据
[root@master backup]# mysqldump -uwp -p -A -B | gzip > /server/backup/mysql_bak.$(date +%F).sql.gz
Enter password:
[root@master backup]# ll
total 4
-rw-r--r--. 1 root root 20 Jul 25 11:17 mysql_bak.2023-07-25.sql.gz
(1)、设置全局唯一的ID号
[root@slave1 ~]# vim /etc/my.cnf
[mysqld]
server_id=2
master:
[root@master ~]# scp /server/backup/mysql_bak.2023-07-25.sql.gz 192.168.186.100:/server/backup
The authenticity of host '192.168.186.100 (192.168.186.100)' can't be established.
ECDSA key fingerprint is SHA256:ZGkVpFFM8KD+H3SRgZmWUpD+IlNK9Dwqcd+TAiGoyzM.
ECDSA key fingerprint is MD5:c2:93:1c:0a:8e:e1:4e:86:cc:90:78:5d:81:42:76:aa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.186.100' (ECDSA) to the list of known hosts.
[email protected]'s password:
mysql_bak.2023-07-25.sql.gz 100% 20 21.8KB/s 00:00
slave1:
[root@slave1 ~]# ll /server/backup/
总用量 4
-rw-r--r-- 1 root root 20 7月 25 23:35 mysql_bak.2023-07-25.sql.gz
[root@slave1 ~]# cd /server/backup/
[root@slave1 backup]# gzip -d mysql_bak.2023-07-25.sql.gz
[root@slave1 backup]# ll
总用量 0
-rw-r--r-- 1 root root 0 7月 25 23:35 mysql_bak.2023-07-25.sql
mysql> change master to
MASTER_HOST='192.168.186.251',
MASTER_PORT=3306,
MASTER_USER='wp',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000025',
MASTER_LOG_POS=194;
mysql> start slave;
检查状态:
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.186.251
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 194
Relay_Log_File: slave1-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes
Slave_SQL_Running: Yes