1.MySQL主从介绍
MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
主从过程大致有3个步骤
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里中继日志
3)从根据relaylog里面的sql语句按顺序执行
l 主上有一个log dump线程,用来和从的I/O线程传递binlog
l 从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地
2.MySQL主从原理图
使用场景
1、数据的备份。
单纯的读。当主服务器损坏,从服务器可以顶替
2、主从同时被使用
当主的服务器压力过大,从节点也被用来读数据。
准备
| 主机名 | IP | MySQL版本 |
|---|---|---|
| server | 192.168.200.41 | mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz |
| client | 192.168.200.42 |
过程
主节点操作
## 创建mysql用户和密码[root@localhost ~]# mysqladmin -uroot password '000000'Warning: Using a password on the command line interface can be insecure。# .err结尾的文件为错误日志[root@localhost mysql]# ls -a /data/mysql/. auto.cnf ib_logfile0 mysql server.err test.. ibdata1 ib_logfile1 performance_schema server.pid# 修改配置文件 /etc/my.cnf 确定有log_bin 和server_id[root@localhost mysql]# vim /etc/my.cnflog_bin=yx1server_id = 13[root@localhost mysql]# service mysqld restartShutting down MySQL.. SUCCESS!Starting MySQL. SUCCESS!# 可以看到多出log_bin名为开头的两个文件,yx1.000001、yx1.index# 这两个文件为bin_log文件和索引文件 可以参考流程图[root@localhost mysql]# ls -a /data/mysql/. ibdata1 localhost.localdomain.err performance_schema yx00000 yx1.000002 yx1.index.. ib_logfile0 localhost.localdomain.pid test yx01 yx1.000003auto.cnf ib_logfile1 mysql yx yx1.000001 yx11111# 创建主从用户 权限mysql> grant replication slave on *.* to 'repl'@192.168.200.14 identified by '000000';Query OK, 0 rows affected (0.00 sec)## 刷新权限mysql> flush privileges;## 查看server节点状态mysql> show master status ;+------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+------------+----------+--------------+------------------+-------------------+| yx1.000003 | 223 | | | |+------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
从节点配置
# 从节点修改配置文件,确保有server_id 这个字段,
# 字段id不要一样,一般以ip最后一位结尾
[root@localhost ~]# vim /etc/my.cnf
server_id = 14
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
# 创建连接, 主节点信息,bin_log文件名及大小
mysql> change master to master_host='192.168.200.14',master_user='repl',master_password='000000',master_log_file='yx1.000003',master_log_pos=223;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.13
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: yx1.000003
Read_Master_Log_Pos: 120
Relay_Log_File: localhost-relay-bin.000010
Relay_Log_Pos: 277
Relay_Master_Log_File: yx1.000003
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: 120
Relay_Log_Space: 611
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: 13
Master_UUID: 055c4999-14f8-11ec-bb96-000c29a507ef
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
ERROR:
No query specified
测试
## 在主节点数据库创建一个库
mysql> create database yx00000;
Query OK, 1 row affected (0.00 sec)
## 在从节点查看
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| yx |
| yx00000 |
| yx11111 |
+--------------------+
7 rows in set (0.00 sec)
# 000001文件大小为主节点操作命令组成
# 可以通过bin_log文件恢复数据
错误
Slave_SQL_Running: no
这个地方错误是因为我一开始在从节点上边新建了一个表
所以Slave_SQL_Running是no
先stop slave;关闭从节点的服务
在从节点上编辑
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
做完启动就再查看节点状态就发现Slave_SQL_Running :yes了
## Slave_IO_Running:连接到主库,并读取主库的日志到本地,生成本地日志文件
## Slave_SQL_Running:读取本地日志文件,并执行日志里的SQL命令。


