MySQL主从复制
1.master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件,binary log events
2.slave开始一个工作线程I/O线程,在master上打开一个普通的连接,然后在binlog dump process,Binlog dump process 从master的二进制日志中
读取事件。slave将master的binary log events拷贝到它的中继日志(relay log)。
3.SQL slave thread(SQL从线程)处理该过程的最后一步。SQL线程从中继日志读取事件,并重新执行其中的事件而更新slave的数据,使其与master中的数据一致。
一、MySQL主从同步M-S
192.168.31.62:Master192.168.31.63:Slave
配置主数据库服务器
1.配置my.cnf
配置my.cnf[root@ShuaiJhou ~]# systemctl stop mysqld
[root@ShuaiJhou ~]# vim /etc/my.cnf
log-in=mysql-bin-master #启用二进制日志
server-id=1 #本机数据库ID标志
binlog-do-db=test #可以被从服务器复制的库,二进制需要同步的数据库名
binlog-ignore-db=mysql #不可以被从服务器的库
[root@ShuaiJhou ~]# systemctl start mysqld
mysql> show variables like'version';
+---------------+--------+
| Variable_name | Value |
+---------------+--------+
| version | 5.7.26 |
+---------------+--------+
1 row in set (0.08 sec)
mysql>
mysql>set global validate_password_policy=0;
Query OK, 0 rows affected (0.01 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>grant replication slave on *.* to slave@'192.168.31.%' identified by "123456";
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-bin-master.000001 | 601 | test | mysql | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
[root@ShuaiJhou ~]# ls /var/lib/mysql
查看二进制文件
mysql> show binlog events\G
mysql> show binlog events\G
复制前要保证同步的数据库一致
[root@ShuaiJhou ~]# mysqldump -uroot -p123456 test>test.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@ShuaiJhou ~]# ll
total 8
-rw-------. 1 root root 1771 May 10 21:33 anaconda-ks.cfg
-rw-r--r-- 1 root root 2068 Jun 15 17:38 test.sql
[root@ShuaiJhou ~]#
将导出的数据库传给从服务器
[root@ShuaiJhou ~]# scp test.sql 192.168.31.63:/root/
配置从数据库服务器
两台数据库服务器MySQL版本要一致1.测试连接到主服务器是否成功
[root@ZhouShuai ~]# mysql -u slave -p123456 -h 192.168.31.62
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)
mysql>
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> exit
Bye
[root@ZhouShuai ~]# mysql -uroot -p123456 test<test.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@ZhouShuai ~]# mysql -uroot -p123456
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql>
修改从服务器配置文件:
从服务器没必要开启bin-log日志
[root@ZhouShuai ~]# systemctl stop mysqld
[root@ZhouShuai ~]# vim /etc/my.cnf
server-id = 2 #从服务器ID号,不要和主ID相同 ,如果设置多个从服务器,每个从服务器必须有一个唯一的server-id值,
必须与主服务器的以及其它从服务器的不相同。可以认为server-id值类似于IP地址:
这些ID值能唯一识别复制服务器群集中的每个服务器实例。
[root@ZhouShuai ~]# systemctl stop mysqld
[root@ZhouShuai ~]# vim /etc/my.cnf
[root@ZhouShuai ~]# systemctl start mysqld
[root@ZhouShuai ~]# mysql -uroot -p123456
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to
-> master_host='192.168.31.62',
-> master_user='slave',
-> master_password='123456';
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
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.31.62
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000001
Read_Master_Log_Pos: 601
Relay_Log_File: ZhouShuai-relay-bin.000002
Relay_Log_Pos: 828
Relay_Master_Log_File: mysql-bin-master.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1 row in set (0.01 sec)
mysql>
Slave_IO_Running :一个负责与主机的IO通信
Slave_SQL_Running:负责自己的slave mysql进程
两个为YES 就成功了!
再到主服务器上查看状态:
[root@ShuaiJhou ~]# mysql -uroot -p123456
mysql> show processlist \G
*************************** 1. row ***************************
Id: 6
User: slave
Host: 192.168.31.63:32816
db: NULL
Command: Binlog Dump
Time: 193
State: Master has sent all binlog to slave; waiting for more updates
Info: NULL
*************************** 2. row ***************************
Id: 7
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: starting
Info: show processlist
2 rows in set (0.00 sec)
mysql>
插入数据测试同步
成功排错:
如果遇到主从不同步,看一下主从bin-log的位置,然后再同步。
mysql> show master status;
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| mysql-bin-master.000001 | 897 | test | mysql | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
在主服务器上看二进制日志事件列表
mysql> show binlog events \G
*************************** 11. row ***************************
Log_name: mysql-bin-master.000001
Pos: 866
Event_type: Xid
Server_id: 1
End_log_pos: 897
Info: COMMIT /* xid=69 */
11 rows in set (0.00 sec)
从服务器执行MySQL命令下:
mysql> stop slave; #先停止slave服务
mysql> change master to master_log_file='mysql-bin-master.000001',master_log_pos=1164;
#根据上面主服务器的show master status的结果,进行从服务器的二进制数据库记录回归,达到同步的效果
mysql> start slave; #启动从服务器同步服务
mysql> show slave status\G; #用show slave status\G;看一下从服务器的同步情况
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果都是yes,那代表已经在同步
重启从服务器,再查看状态:]
mysql> stop slave; #停止从服务器
mysql> start slave; #开启从服务器
排错思路:
- 二进制日志没有开启
- IPTABLES 没有放开端口
- 对应的主机 IP地址写错了
网友评论