準備工作
環(huán)境
- 兩臺虛擬機绎狭,地址:master-192.168.52.130/slave-192.168.52.131关带;
- 系統(tǒng)版本:Centos 7;
- 軟件版本:mysql 5.6沼撕;
數(shù)據(jù)庫
- 同樣的配置:端口-3307等宋雏;
- 同樣的數(shù)據(jù):database-TestDB等;
主從配置
主庫配置
- 編輯 /etc/my.cnf:
[mysqld]
basedir=/var/lib/mysql
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/mysql.sock
port=3307
log-bin=/var/lib/mysql/mysql-bin.log
explicit_defaults_for_timestamp=true
innodb_flush_log_at_trx_commit=1
sync_binlog=1
server-id=1
binlog-do-db=TestDB
binlog-ignore-db=performance_schema
binlog-ignore-db=mysql
binlog-ignore-db=test
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
配置項解釋:
server-id=1 標識
binlog-do-db=TestDB 需要同步的數(shù)據(jù)庫
binlog-ignore-db=performance_schema 不需要同步的數(shù)據(jù)庫
- 創(chuàng)建同步用戶务豺,在主服務器上為從服務器建立一個連接帳戶磨总,該帳戶必須授予REPLICAITON SLAVE權(quán)限。在主服務器登陸mysql上執(zhí)行:
grant replication slave on *.* to 'master'@'%' identified by '123456';
- 重啟之后笼沥,查看狀態(tài):
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 120
Binlog_Do_DB: TestDB
Binlog_Ignore_DB: performance_schema,mysql,test
Executed_Gtid_Set:
1 row in set (0.01 sec)
注意上面的 File 和 Position 蚪燕,從庫slave 配置的時候會使用。
從庫配置
- 編輯 /etc/my.cnf
[mysqld]
...
server_id=2
log-bin=mysql-bin.log
replicate-do-db=TestDB
- change master語句指定同步位置:
mysql>change master to master_host='192.168.52.130', master_user='master', master_password='123456', master_log_file='mysql-bin.000007', master_log_pos=120,master_port=3307;
- 啟用/關閉 slave;
start slave;
stop slave;
- 重啟奔浅,查看slave :show slave status\G;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.52.130
Master_User: master
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 120
Relay_Log_File: mysqld-relay-bin.000010
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: TestDB
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: 120
Relay_Log_Space: 620
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: 1cacfa49-04c4-11e7-b744-000c29d5b78c
Master_Info_File: /var/lib/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)
測試
在主庫進行數(shù)據(jù)的操作馆纳,查看從庫的數(shù)據(jù)。