目標:搭建兩臺MySQL服務器,一臺作爲主服務器,一臺作爲從服務器,實現主從複製
環境:
主數據庫: 192.168.169.138
從數據庫: 192.168.169.137
查看mysql啓動狀態:service mysqld status
配置步驟:
1、保證兩個數據庫中的庫和數據是一致的;
2、在主數據中創建一個同步賬號(可不創建使用現有的),如果僅僅爲了主從複製創建賬號,只需要授予REPLICATION SLAVE權限。
1)、創建一個賬號,賬號:master 密碼: 123456
CREATE USER 'master'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'master'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
3、配置主數據庫
1)要主數據庫,你必須要啓用二進制日誌(binary logging),並且創建一個唯一的Server ID,這步驟可能要重啓MySQL。
2)主服務器發送變更記錄到從服務器依賴的是二進制日誌,如果沒啓用二進制日誌,複製操作不能實現(主庫複製到從庫)。
3)複製組中的每臺服務器都要配置唯一的Server ID,取值範圍是1到(232)−1,你自己決定取值。
4)配置二進制日誌和Server ID,你需要關閉MySQL和編輯my.cnf或者my.ini文件,在 [mysqld] 節點下添加配置。
service mysqld stop
vim /etc/my.conf
service mysqld start
service mysqld status
5)下面是啓用二進制日誌,日誌文件名以“mysql-bin-master”作爲前綴,Server ID配置爲1,如下,在[mysqld] 下加上如下代碼:
vim /etc/my/cnf
#mysql主從備份
server-id = 1 #本機數據庫ID 標示
log-bin=mysql-bin-master #啓用二進制日誌
binlog-do-db=master-slave-test #可以被從服務器複製的庫。二進制需要同步的數據庫名
binlog-ignore-db=ignore-master-slave-test #不可以被從服務器複製的庫
# innodb_flush_log_at_trx_commit 控制redo log日誌什麼時候寫入page cache(log file) 和>磁盤
#0:log buffer將每秒一次地寫入log file中,並且log file的flush(刷到磁盤)操作同時進行。該
模式下在事務提交的時候,不會主動觸發寫入磁盤的操作。
#1:每次事務提交時MySQL都會把log buffer的數據寫入log file,並且flush(刷到磁盤)中去,該>模式爲系統默認。
#2:每次事務提交時MySQL都會把log buffer的數據寫入log file,但是flush(刷到磁盤)操作並不>會同時進行。該模式下,MySQL會每秒執行一次 flush(刷到磁盤)操作
innodb_flush_log_at_trx_commit=1
#sync_binlog 控制binlog日誌中binlog cache什麼時候寫入binlog file 和磁盤
#sync_binlog=0 表示每次提交事務都只寫binlog file,不寫入磁盤;
#sync_binlog>0 表示每次提交事務都寫binlog file,但累積 N 個事務後才寫入磁盤。
sync_binlog=1
4、重啓mysql
service mysqld stop
service mysqld start
5、查看主服務器狀態:
mysql -uroot -p
mysql> show master status;
注意:記錄好File和Position,後面要用
6、配置從數據庫:
1)從服務器,同理,要分配一個唯一的Server ID,需要關閉MySQL,修改好後再重啓,如下:
在[mysqld] 下加上如下代碼:
vim /etc/my.cnf
ql主從備份
server-id = 2 #本機數據庫ID 標示
replicate-do-db=master-slave-test #在master端不指定binlog-do-db,在slave端用replication-do-db來過濾
replicate-ignore-db=ignore-master-slave-test #不可以被從服務器複製的庫
relay-log=slave-relay-bin
2)配置連接主服務器的信息:
mysql -uroot -p
ysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.169.138',
-> MASTER_USER='master',
-> MASTER_PASSWORD='123456',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='mysql-bin-master.000009',
-> MASTER_LOG_POS=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
3)查看狀態
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.169.138
Master_User: master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin-master.000009
Read_Master_Log_Pos: 329
Relay_Log_File: salve-relay-bin.000002
Relay_Log_Pos: 499
Relay_Master_Log_File: mysql-bin-master.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: master-slave-test
Replicate_Ignore_DB: ignore-master-slave-test
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: 329
Relay_Log_Space: 672
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: f639af40-88d0-11e8-99ae-000c294f1069
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 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)
4)若 Slave_SQL_Running: no 請重複執行以下內容,直至yes:
stop slave;
set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
start slave;
測試
在master 服務器 的master-slave-test 數據庫,創建一個表:
CREATE TABLE `NewTable` (
`id` int NOT NULL AUTO_INCREMENT ,
`name` varchar(50) NULL ,
PRIMARY KEY (`id`)
)
;
查看從庫:
在主庫新增一條記錄:
INSERT INTO `master-slave-test`.`NewTable` (`id`, `name`) VALUES ('1', 'lisi');
查看從庫: