原理:建立兩個數(shù)據(jù)庫化焕,庫中生成兩個線程(i/o 萄窜,SQL)。
主庫中i/o線程會將修改的數(shù)據(jù)保存在二進(jìn)制bin-log文件中撒桨,而從庫也會開啟i/o線程從而將主庫上bin-log日志文件寫到本地的Realy log(中繼日志)里面脂倦。
從服務(wù)器上面同時開啟一個 SQL 線程,定時檢查 Realy log元莫,如果有更新立即把更新的內(nèi)容在本機(jī)的數(shù)據(jù)庫上面執(zhí)行一遍赖阻。從而最后主從庫達(dá)成一致。
步驟:
主服務(wù)器(192.168.140.145):
1.進(jìn)入mysql配置文件中在【mysqld】添加如下內(nèi)容
vim /etc/my.cnf
[mysqld]
bin-log=/var/log/mysql/mysql-bin
sever-id=1
mkdir /var/log/mysql
chown -R mysql.mysql /var/log/mysql
systemctl restart mysqld
3.建立個用戶,并授權(quán)
mysqld -uroot -pWww.2.com
mysql> grant replication slave on *.* 'wdx'@'%' identified by 'Www.2.com';
4.導(dǎo)出數(shù)據(jù),并遠(yuǎn)程拷貝到從服務(wù)器192.168.140.146
mysqldump --all-databases --master-data=1 --user=root --password=Www.2.com >dump.sql
scp dump.sql root@192.168.140.146:/root/
從服務(wù)器(192.168.140.146):
用新建用戶登入數(shù)據(jù)庫測試
mysql -uwdx -pWww.2.com -h192.168.140.145
1.導(dǎo)入數(shù)據(jù)庫
mysql -uroot -pWww.2.com < dump.sql
mysql -uroot -pWww.2.com
mysql> change master to
->master_host='192.168.140.145',
->master_user='wdx',
->master_password='Www.2.com',
->master_log_file=''mysql-bin.000001'
-> master_log_pos=600;
mysql>start slave;
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.140.145
Master_User: wdx
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 600
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
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: 600
Relay_Log_Space: 531
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: 1
Master_UUID: 23be8b2a-2ddd-11e9-b5ba-000c29e25853
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
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
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)