目錄html
1、 MySQL主從介紹
2、 準備工做
3、 配置主
4、 配置從
5、 測試主從同步
6、 擴展mysql
MySQL主從又叫作Replication、AB複製。簡單講就是A和B兩臺機器作主從後,在A上寫數據,另一臺B也會跟着寫數據,二者數據實時同步的linux
MySQL主從是基於binlog的,主上須開啓binlog才能進行主從。主從過程大體有3個步驟sql
1)主將更改操做記錄到binlog裏數據庫
2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏vim
3)從根據relaylog裏面的sql語句按順序執行服務器
主上有一個log dump線程,用來和從的I/O線程傳遞binlog架構
從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另一個SQL線程用來把relaylog裏面的sql語句落地dom
主從複製原理圖:ide
實現MySQL主從複製須要進行的配置:
主服務器:
開啓二進制日誌
配置惟一的server-id
得到master二進制日誌文件名及位置
建立一個用於slave和master通訊的用戶帳號
從服務器:
配置惟一的server-id
使用master分配的用戶帳號讀取master二進制日誌
啓用slave服務
主從數據庫版本最好一致
主從數據庫內數據保持一致
環境準備:
主數據庫:mysql-master 10.0.1.26 /Centos7.4 3.10.0-693.el7.x86_64 mysql-5.6.36
從數據庫:mysql-slave 10.0.1.27 /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 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> 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'@'10.0.1.27' identified by '123456'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for 'ms_user'@'10.0.1.27'; +----------------------------------------------------------------------------------------------------------------------------+ | Grants for ms_user@10.0.1.27 | +----------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO 'ms_user'@'10.0.1.27' 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 root@10.0.1.27:/root The authenticity of host '10.0.1.27 (10.0.1.27)' 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 '10.0.1.27' (ECDSA) to the list of known hosts. root@10.0.1.27'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='10.0.1.26', 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\G mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.1.26 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: 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: 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: 0 1 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)
不停庫不鎖表在線主從配置
http://seanlook.com/2015/12/14/mysql-replicas/
mysql主從常見問題
http://www.10tiao.com/html/706/201603/403220961/1.html
mysql主從延遲
http://f.dataguru.cn/thread-461916-1-1.html
深刻探究主從延遲
http://ningg.top/inside-mysql-master-slave-delay/
mysql主從不一樣步如何作
http://www.jb51.net/article/33052.htm
mysql 主主
http://www.cnblogs.com/ygqygq2/p/6045279.html
mysql-proxy 實現讀寫分離
http://my.oschina.net/barter/blog/93354
mycat實現讀寫分離
http://www.th7.cn/db/mysql/201708/250280.shtml
atlas相關
http://www.oschina.net/p/atlas
mysql一主多從
http://blog.sina.com.cn/s/blog_4c197d4201017qjs.html
mysql環形主從
http://ask.apelearn.com/question/11437
cobar實現分庫分表
http://blog.csdn.net/huoyunshen88/article/details/37927553
mysql分庫分表方案
http://my.oschina.net/ydsakyclguozi/blog/199498
mysql架構演變
http://www.aminglinux.com/bbs/thread-8025-1-1.htmlf
MHA架構
http://www.dataguru.cn/thread-457284-1-1.html
比較複雜的mysql集羣架構
http://ask.apelearn.com/question/17026