一. 爲什麼要對MySQL做主從同步複製
1.MySQL主從方案主要作用
1.讀寫分離,使數據庫能支撐更大的併發
在報表中尤其重要。由於部分報表sql語句非常的慢,導致鎖表,影響前臺服務。如果前臺使用master,報表使用slave,那麼報表sql將不會造成前臺鎖,保證了前臺速度。
2.發揚不同表引擎的優點
目前Myisam表的查詢速度比innodb略快,而寫入併發innodb比myIsam要好。那麼,我們可以使用innodb作爲master,處理高併發寫入,使用master作爲slave,接受查詢。或在myisam slave中建立全文索引,解決innodb無全文索引的弱點。
3.熱備
slave和master的數據“準實時”同步。
2.複製技術能夠解決的問題
1.數據分佈 (Data distribution )
2.負載平衡(load balancing)
3.備份(Backups)
二、MySQL主從同步複製原理
1)在Slave 服務器上執行sart slave命令開啓主從複製開關,開始進行主從複製。
2)此時,Slave服務器的IO線程會通過在master上已經授權的複製用戶權限請求連接master服務器,並請求從執行binlog日誌文件的指定位置(日誌文件名和位置就是在配置主從複製服務時執行change
master命令指定的)之後開始發送binlog日誌內容
3)Master服務器接收到來自Slave服務器的IO線程的請求後,其上負責複製的IO線程會根據Slave服務器的IO線程請求的信息分批讀取指定binlog日誌文件指定位置之後的binlog日誌信息,然後返回給Slave端的IO線程。返回的信息中除了binlog日誌內容外,還有在Master服務器端記錄的IO線程。返回的信息中除了binlog中的下一個指定更新位置。
4)當Slave服務器的IO線程獲取到Master服務器上IO線程發送的日誌內容、日誌文件及位置點後,會將binlog日誌內容依次寫到Slave端自身的Relay Log(即中繼日誌)文件(mysql-relay-bin.xxx)的最末端,並將新的binlog文件名和位置記錄到master-info文件中,以便下一次讀取master端新binlog日誌時能告訴Master服務器從新binlog日誌的指定文件及位置開始讀取新的binlog日誌內容
三、多實例主從搭建
多實例搭建見上一篇點擊打開鏈接
[root@localhost data]# ps -ef |grep mysql
root 17456 29867 0 17:07 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3306/my.cnf
mysql 17678 17456 0 17:07 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3306/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3306/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/data/mysql3306/data/mysql3306.sock --port=3306
root 17885 29867 0 17:12 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql3307/my.cnf
mysql 18107 17885 0 17:12 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql3307/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql3307/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid --socket=/data/mysql3307/data/mysql3307.sock --port=3307
root 18296 29867 0 17:26 pts/0 00:00:00 grep mysql
[root@localhost data]# grep log-bin /data/mysql3306/my.cnf
log-bin=/data/mysql3306/data/mysql-bin
[root@localhost data]# grep server_id /data/mysql3306/my.cnf
server_id=3306
[root@localhost data]# grep server_id /data/mysql3307/my.cnf
server_id=3307
mysql> grant replication slave on *.* to 'repl'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+------+-----------------------+
| user | host |
+------+-----------------------+
| root | 127.0.0.1 |
| root | ::1 |
| repl | 127.0.0.1 |
| root | localhost |
| root | localhost.localdomain |
+------+-----------------------+
5 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> change master to
-> master_host='127.0.0.1',
-> master_port=3306,
-> master_user='repl',
-> master_password='123456',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.16 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 690
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000002
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: 690
Relay_Log_Space: 460
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: 3306
Master_UUID: 4ca6cf25-5756-11e8-9709-005056a56f27
Master_Info_File: /data/mysql3307/data/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)