mysql互為主從配置
阿新 • • 發佈:2019-02-17
一、MYSQL主從備份的原理
mysql支援單向、非同步複製,在複製的過程中,一個為主伺服器,另一個為從伺服器。主伺服器進行的操作sql 操作都會記錄到 bin-log 指定的檔案中,如果沒有指定,一般為mysql-bin.0000XX檔案中。從伺服器會將主伺服器更新的日誌複製到從伺服器,然後在從伺服器順序做sql 操作,所以主服務的二進位制日誌必須開啟。
從伺服器從主伺服器備份不會干擾主伺服器。
1. mysql 版本最好相同。
A : 192.168.0.103 linux x86_64 mysql
B : 192.168.0.107 linux x86_64 mysql
兩臺機器均啟動mysql.
2. 關閉防火牆iptables ,這樣可以使得兩臺機器的mysql相互互動。
3.建立mysql使用者&配合mysql配置檔案my.cnf(最好兩臺機器的使用者名稱稱相同)
在192.168.0.103 : useradd replication
在192.168.0.107 : useradd replication
配置my.cnf
192.168.0.103
log-bin : 表示開啟bin-log日誌功能,這樣I/O才會將日誌複製到從機的rely-log中。 server-id : 表示伺服器的ID,此為唯一機器標識。 binlog-do-db : 標識對指定資料庫做日誌記錄,如果對多個數據庫做日誌記錄,需加','分隔。 binlog-ignore-db : 標識不對指定資料庫做日誌記錄。 192.168.0.107
4.授予從機使用者同步日誌的許可權。 grant replication slave on *.* to 'replication'@'192.168.0.107' identified by 'replication'
5.授予主機同步從機日誌的許可權(see #4)(在從機192.168.0.107上操作)
192.168.0.107
insert data on test.EM in IP 192.168.0.103
you can see content in test.EM is similar to content on content in 192.168.0.103
二、複製過程三部曲
1. Salve 啟動I/O程序連線Master,請求Master指定日誌檔案的指定位置或者最開始位置之後的日誌。 2. Master接到請求以後,負責複製的I/O程序將日誌名稱、更新日誌的位置,日誌的內容傳輸到Salve. 3. Salve講讀取到的日誌的內容追加到rely-log檔案中,而把讀取到的日誌名稱、更新日誌的位置寫到master-info檔案中,以便下次讀取的時候告訴主伺服器從日誌檔案的什麼位置開始讀取。 Salve將日誌內容中的sql 操作在自己的伺服器上相同的執行一遍。三、例項配置
[mysqld]
log-bin=mysql-bin
server-id = 1
binlog-do-db=test
binlog-ignore-db=mysql
log-bin : 表示開啟bin-log日誌功能,這樣I/O才會將日誌複製到從機的rely-log中。 server-id : 表示伺服器的ID,此為唯一機器標識。 binlog-do-db : 標識對指定資料庫做日誌記錄,如果對多個數據庫做日誌記錄,需加','分隔。 binlog-ignore-db : 標識不對指定資料庫做日誌記錄。 192.168.0.107
mysqld
log-bin=mysql-bin server-id = 2 binlog-do-db=test binlog-ignore-db=mysql
4.授予從機使用者同步日誌的許可權。 grant replication slave on *.* to 'replication'@'192.168.0.107' identified by 'replication'
*************************** 1. row ***************************
Host: 192.168.0.107
User: replication
Password: *D36660B5249B066D7AC5A1A14CECB71D36944CBC
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: Y
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
1 row in set (0.00 sec)
5.授予主機同步從機日誌的許可權(see #4)(在從機192.168.0.107上操作)
*************************** 1. row ***************************
Host: 192.168.0.103
User: replication
Password: *D36660B5249B066D7AC5A1A14CECB71D36944CBC
Select_priv: N
Insert_priv: N
Update_priv: N
Delete_priv: N
Create_priv: N
Drop_priv: N
Reload_priv: N
Shutdown_priv: N
Process_priv: N
File_priv: N
Grant_priv: N
References_priv: N
Index_priv: N
Alter_priv: N
Show_db_priv: N
Super_priv: N
Create_tmp_table_priv: N
Lock_tables_priv: N
Execute_priv: N
Repl_slave_priv: Y
Repl_client_priv: N
Create_view_priv: N
Show_view_priv: N
Create_routine_priv: N
Alter_routine_priv: N
Create_user_priv: N
Event_priv: N
Trigger_priv: N
ssl_type:
ssl_cipher:
x509_issuer:
x509_subject:
max_questions: 0
max_updates: 0
max_connections: 0
max_user_connections: 0
1 row in set (0.00 sec)
接下里就可以用:
在192.168.0.103 : mysql -h 192.168.0.107 -u replication -p 連線192.168.0.107上的mysql ,進行同步test 庫中的表,pleae see test below:
For status now;
192.168.0.103
mysql> select * from EM;
Empty set (0.00 sec)
192.168.0.107
mysql> select * from EM;
Empty set (0.00 sec)
insert data on test.EM in IP 192.168.0.103
mysql> insert into EM values (22,'Kevin');
Query OK, 1 row affected (0.00 sec)
mysql> select * from EM;
+------+-------+
| a | b |
+------+-------+
| 22 | Kevin |
+------+-------+
1 row in set (0.00 sec)
you can see content in test.EM is similar to content on content in 192.168.0.103
mysql> select * from EM;
+------+-------+
| a | b |
+------+-------+
| 22 | Kevin |
+------+-------+
1 row in set (0.00 sec)