17.1 MySQL主从介绍
17.2 准备工作
17.3 配置主
17.4 配置从
17.5 测试主从同步
有的同学,遇到主从不能正常同步,提示uuid相同的错误。这是因为克隆机器导致。
https://www.2cto.com/database/201412/364479.html
扩展部分
不停库不锁表在线主从配置
http://seanlook.com/2015/12/14/mysql-replicas/
主从不同步
http://www.rfyy.net/archives/2309.html
http://blog.51cto.com/storysky/259280
主主
关于 auto_increment https://blog.csdn.net/leshami/article/details/39779509
http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 实现读写分离
http://blog.51cto.com/zzclinux/1980487
mysql-proxy类似的产品有:
mycat 基于阿里的开源软件cobar,官网 www.mycat.io
https://my.oschina.net/ruoli/blog/1789370
mycat实现分库分表
https://www.cnblogs.com/joylee/p/7513038.html
atlas 出自于360,不维护不更新了 https://blog.csdn.net/AnPHPer/article/details/80566385
mysql环形主从
http://ask.apelearn.com/question/11437
mysql架构演变 http://www.aminglinux.com/bbs/thread-8025-1-1.html
MHA架构
http://blog.51cto.com/xiaoshuaigege/2060768
比较复杂的mysql集群架构 http://ask.apelearn.com/question/17026
MySQL主从介绍
MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。主从过程大致有3个步骤
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
3)从根据relaylog里面的sql语句按顺序执行
主上有一个log dump线程,用来和从的I/O线程传递binlog
从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地
主从复制原理图:
二、 准备工作
实现MySQL主从复制需要进行的配置:
主服务器:
开启二进制日志
配置唯一的server-id
获得master二进制日志文件名及位置
创建一个用于slave和master通信的用户账号
从服务器:
配置唯一的server-id
使用master分配的用户账号读取master二进制日志
启用slave服务
主从数据库版本最好一致
主从数据库内数据保持一致
环境准备:
主数据库:mysql-master 192.168.253.143 /Centos7.4 3.10.0-693.el7.x86_64 mysql-5.6.36
从数据库:mysql-slave 192.168.253.144 /Centos7.4 3.10.0-693.el7.x86_64 mysql-5.6.36
三、 配置主
1.编辑/etc/my.cnf配置文件
[[email protected] ~]# vim /etc/my.cnf//修改如下字段
server_id = 210
log_bin=master_bin_log//重启服务
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[[email protected] ~]# ls /data/mysql/
auto.cnf ib_logfile0 localhost.localdomain.err master_bin_log.index mysql-master.err performance_schema
ibdata1 ib_logfile1 master_bin_log.000001 mysql mysql-master.pid test
2.创建测试库及数据
[[email protected] ~]# mysql -uroot
mysql> create database ms_test;
Query OK, 1 row affected (0.00 sec)
mysql> use ms_test;
Database changed
mysql> create table tb1(`id` int(4),`name` char(40));
Query OK, 0 rows affected (0.13 sec)
mysql> insert into tb1 values(1,‘long‘);
Query OK, 1 row affected (0.00 sec)
mysql> insert into tb1 values(2,‘oooo‘);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tb1 values(3,‘ccc‘);
Query OK, 1 row affected (0.00 sec)
//备份测试数据库
[[email protected] ~]# mysqldump -uroot ms_test > ms_test.sql
3.创建同步数据帐户
mysql> grant replication slave on *.* to ‘ms_user‘@‘192.168.253.143‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for ‘ms_user‘@‘192.168.253.143‘;
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO ‘ms_user‘@‘192.168.253.143‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ |
+----------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
//锁定表状态
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
//显示主状态
mysql> show master status;
+-----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------------+----------+--------------+------------------+-------------------+
| master_bin_log.000001 | 1240 | | | |
+-----------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
四、 配置从
1.修改配置文件
[[email protected] ~]# vim /etc/my.cnf//修改如下内容
server_id = 220
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[[email protected] ~]#
2.将主上备份的测试数据库复制到从库并恢复
//在主上执行
[[email protected] ~]# scp ms_test.sql [email protected]:/root
The authenticity of host ‘192.168.253.143 (192.168.253.143)‘ can‘t be established.
ECDSA key fingerprint is SHA256:zx1ETx0EY8r1LzVAx806KJRBjPOnR/b3vss7sp1y6Hs.
ECDSA key fingerprint is MD5:1f:39:43:b7:16:b4:37:b2:15:4d:92:d4:a5:b6:a4:c7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.253.143‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
ms_test.sql 100% 1842 740.6KB/s 00:00
//在从上执行
[[email protected] ~]# mv ms_test.sql /data/mysql/
[[email protected] ~]# ls /data/mysql/
auto.cnf ib_logfile0 localhost.localdomain.err mysql mysql-slave.pid test
ibdata1 ib_logfile1 ms_test.sql mysql-slave.err performance_schema
//创建ms_test
[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> create database ms_test;
Query OK, 1 row affected (0.00 sec)
mysql> quit
Bye//恢复ms_test
[[email protected] ~]# mysql -uroot ms_test </data/mysql/ms_test.sql
3.设定同步
//从上执行mysql> stop slave;Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> change master to master_host=‘192.168.253.144‘, master_user=‘ms_user‘, master_password=‘123456‘, master_log_file=‘master_bin_log.000001‘, master_log_pos=1240;Query OK, 0 rows affected, 2 warnings (0.08 sec)
mysql> start slave;Query OK, 0 rows affected (0.00 sec)
//查看从状态
mysql> show slave status\Gmysql> show slave status\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.253.144 Master_User: ms_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master_bin_log.000001 Read_Master_Log_Pos: 1240 Relay_Log_File: mysql-slave-relay-bin.000002 Relay_Log_Pos: 288 Relay_Master_Log_File: master_bin_log.000001 Slave_IO_Running: Yes Slave_SQL_Running: 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: 1240 Relay_Log_Space: 467 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: 0Master_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: 210 Master_UUID: 060b64b3-899b-11e8-9ed3-000c29609adb 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: 01 row in set (0.00 sec)
//主上执行mysql> unlock tables;Query OK, 0 rows affected (0.00 sec)
五、 测试主从同步
1.编译主配置文件
[[email protected] ~]# vim /etc/my.cnf//设定仅同步ms_test数据库,添加下行
binlog-do-db=ms_test
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
2.在主上插入新的记录
[[email protected] ~]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.36-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
mysql> use ms_test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_ms_test |
+-------------------+
| tb1 |
+-------------------+1 row in set (0.00 sec)
mysql> select * from tb1;
+------+------+
| id | name |
+------+------+
| 1 | long |
| 2 | oooo |
| 3 | ccc |
+------+------+3 rows in set (0.00 sec)
mysql> insert into tb1 values(4,‘dddd‘);
Query OK, 1 row affected (0.00 sec)
mysql> select * from tb1;
+------+------+
| id | name |
+------+------+
| 1 | long |
| 2 | oooo |
| 3 | ccc |
| 4 | dddd |
+------+------+4 rows in set (0.00 sec)
3.在从上查看是否有同步
mysql> use ms_test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_ms_test |
+-------------------+
| tb1 |
+-------------------+1 row in set (0.00 sec)
mysql> select * from tb1;
+------+------+
| id | name |
+------+------+
| 1 | long |
| 2 | oooo |
| 3 | ccc |
| 4 | dddd |
+------+------+4 rows in set (0.00 sec)
原文地址:https://www.cnblogs.com/linuxzhang/p/9782380.html