mysql aa复制_MySQL主从复制实现
上回提到了用ThinkPHP框架來實現數據庫的讀寫分離,現在就來簡單說說MySQL的主從復制。
形式
一主一從(也就是這里要實現的形式)
主主復制
一主多從
多主一從(MySQL5.7開始支持)
聯級復制
如圖:圖來自互聯網
條件
主庫開啟binlog日志(設置log-bin參數)
主從server-id不同(這個要小心)
從庫服務器能連通主庫
原理
從庫生成兩個線程,一個I/O線程,一個SQL線程;
i/o線程去請求主庫 的binlog,并將得到的binlog日志寫到relay log(中繼日志) 文件中;
主庫會生成一個 log dump 線程,用來給從庫 i/o線程傳binlog;
SQL 線程,會讀取relay log文件中的日志,并解析成具體操作,來實現主從的操作一致,而最終數據一致;
步驟
在主數據庫配置文件中加入以下代碼
[root@localhost ~]# vim /etc/my.cnf
log-bin=mysql-bin // 將mysql二進制日志取名為mysql-bin
binlog_format=mixed // 二進制日志的格式,有三種:statement/row/mixed,具體分別不多做解釋,這里使用mixed
server-id=111 // 為服務器設置一個獨一無二的id便于區分,這里使用ip地址的最后一位充當server-id
配置完成后,保存并重啟主MySQL數據庫
在從數據庫重復同樣的操作,不一樣的是server-id要寫主數據庫的,這里寫的是server-id=110;
保存并重啟從MySQL數據庫
登錄主MySQL數據庫,在主數據庫為從數據庫分配一個賬號,用于從數據庫共享主數據庫的日志文件
GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';
mysql> GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
查看主服務器BIN日志的信息(執行完之后記錄下這兩值,然后在配置完從服務器之前不要對主服務器進行任何操作,因為每次操作數據庫時這兩值會發生改變);
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 315 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
進入從數據庫
如果之前有配置過主從復制,一定要先關閉slave
關閉命令為:stop slave
配置開始:change master to master_host='192.168.33.110',master_user='slave',master_password='123456',
master_log_file='mysql-bin.000001',master_log_pos=315;
參數解釋:
master_host='192.168.33.110' //?設置要連接的主服務器的ip地址
master_user='slave' //?設置要連接的主服務器的用戶名
master_password='123456' //?設置要連接的主服務器的密碼
master_log_file='mysql-bin.000001' //?設置要連接的主服務器的bin日志的日志名稱,即show slave status命令得到的信息File列名稱
master_log_pos=315 //?設置要連接的主服務器的bin日志的記錄位置,即show slave status命令得到的信息Position列名稱,(這里注意,最后一項不需要加引號。否則配置失敗)
從數據庫配置完成,重啟MySQL服務
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.33.110
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1036
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 1004
Relay_Master_Log_File: mysql-bin.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: 1036
Relay_Log_Space: 1181
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: 111
Master_UUID: 39a19e0a-7957-11e6-aa19-0800272020f4
Master_Info_File: /Data/data/mysql/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)
如果這兩個全部是yes,才表示成功,否則失敗;Slave_IO_Running: Yes;Slave_SQL_Running: Yes
最后就可以測試是否可以了~~~
總結
以上是生活随笔為你收集整理的mysql aa复制_MySQL主从复制实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车险到期了可以推迟多久交
- 下一篇: TCL集团拟更名 更能代表公司的业务方