mysql aa复制_MySQL主从复制实现
上回提到了用ThinkPHP框架來實(shí)現(xiàn)數(shù)據(jù)庫的讀寫分離,現(xiàn)在就來簡單說說MySQL的主從復(fù)制。
形式
一主一從(也就是這里要實(shí)現(xiàn)的形式)
主主復(fù)制
一主多從
多主一從(MySQL5.7開始支持)
聯(lián)級(jí)復(fù)制
如圖:圖來自互聯(lián)網(wǎng)
條件
主庫開啟binlog日志(設(shè)置log-bin參數(shù))
主從server-id不同(這個(gè)要小心)
從庫服務(wù)器能連通主庫
原理
從庫生成兩個(gè)線程,一個(gè)I/O線程,一個(gè)SQL線程;
i/o線程去請(qǐng)求主庫 的binlog,并將得到的binlog日志寫到relay log(中繼日志) 文件中;
主庫會(huì)生成一個(gè) log dump 線程,用來給從庫 i/o線程傳binlog;
SQL 線程,會(huì)讀取relay log文件中的日志,并解析成具體操作,來實(shí)現(xiàn)主從的操作一致,而最終數(shù)據(jù)一致;
步驟
在主數(shù)據(jù)庫配置文件中加入以下代碼
[root@localhost ~]# vim /etc/my.cnf
log-bin=mysql-bin // 將mysql二進(jìn)制日志取名為mysql-bin
binlog_format=mixed // 二進(jìn)制日志的格式,有三種:statement/row/mixed,具體分別不多做解釋,這里使用mixed
server-id=111 // 為服務(wù)器設(shè)置一個(gè)獨(dú)一無二的id便于區(qū)分,這里使用ip地址的最后一位充當(dāng)server-id
配置完成后,保存并重啟主MySQL數(shù)據(jù)庫
在從數(shù)據(jù)庫重復(fù)同樣的操作,不一樣的是server-id要寫主數(shù)據(jù)庫的,這里寫的是server-id=110;
保存并重啟從MySQL數(shù)據(jù)庫
登錄主MySQL數(shù)據(jù)庫,在主數(shù)據(jù)庫為從數(shù)據(jù)庫分配一個(gè)賬號(hào),用于從數(shù)據(jù)庫共享主數(shù)據(jù)庫的日志文件
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)
查看主服務(wù)器BIN日志的信息(執(zhí)行完之后記錄下這兩值,然后在配置完從服務(wù)器之前不要對(duì)主服務(wù)器進(jìn)行任何操作,因?yàn)槊看尾僮鲾?shù)據(jù)庫時(shí)這兩值會(huì)發(fā)生改變);
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)
進(jìn)入從數(shù)據(jù)庫
如果之前有配置過主從復(fù)制,一定要先關(guān)閉slave
關(guān)閉命令為: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;
參數(shù)解釋:
master_host='192.168.33.110' //?設(shè)置要連接的主服務(wù)器的ip地址
master_user='slave' //?設(shè)置要連接的主服務(wù)器的用戶名
master_password='123456' //?設(shè)置要連接的主服務(wù)器的密碼
master_log_file='mysql-bin.000001' //?設(shè)置要連接的主服務(wù)器的bin日志的日志名稱,即show slave status命令得到的信息File列名稱
master_log_pos=315 //?設(shè)置要連接的主服務(wù)器的bin日志的記錄位置,即show slave status命令得到的信息Position列名稱,(這里注意,最后一項(xiàng)不需要加引號(hào)。否則配置失敗)
從數(shù)據(jù)庫配置完成,重啟MySQL服務(wù)
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)
如果這兩個(gè)全部是yes,才表示成功,否則失敗;Slave_IO_Running: Yes;Slave_SQL_Running: Yes
最后就可以測試是否可以了~~~
總結(jié)
以上是生活随笔為你收集整理的mysql aa复制_MySQL主从复制实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 车险到期了可以推迟多久交
- 下一篇: TCL集团拟更名 更能代表公司的业务方