二十七節課預習
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主從介紹
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
從上有兩個線程,其中
主從復制原理圖:
二、 準備工作
實現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配置文件
[root@mysql-master ~]# vim /etc/my.cnf//修改如下字段
server_id = 210
log_bin=master_bin_log//重啟服務
[root@mysql-master ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@mysql-master ~]# 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.創建測試庫及數據
[root@mysql-master ~]# 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)
//備份測試數據庫
[root@mysql-master ~]# 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.修改配置文件
[root@mysql-slave ~]# vim /etc/my.cnf//修改如下內容
server_id = 220
[root@mysql-slave ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.. SUCCESS!
[root@mysql-slave ~]#
2.將主上備份的測試數據庫復制到從庫並恢復
//在主上執行
[root@mysql-master ~]# 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
//在從上執行
[root@mysql-slave ~]# mv ms_test.sql /data/mysql/
[root@mysql-slave ~]# 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
[root@mysql-slave ~]# 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
[root@mysql-slave ~]# 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.編譯主配置文件
[root@mysql-master ~]# vim /etc/my.cnf//設定僅同步ms_test數據庫,添加下行
binlog-do-db=ms_test
[root@mysql-master ~]# vim /etc/my.cnf
[root@mysql-master ~]# /etc/init.d/mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL.. SUCCESS!
2.在主上插入新的記錄
[root@mysql-master ~]# 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)
二十七節課預習