在公司工作之餘看了MySQL的Master-Slave設定,大緻的意思和流程已然懂了,但從未實踐過。今天有時間實踐一下,手頭有兩台Win7機器,就湊合着用吧!!
首先設定是關系A為Master,B為Slave.(兩台機器的Mysql都是5.0+,但不同)
設定A 的my.ini
server-id = 1
log-bin=mysql-bin
binlog_do_db=test1
binlog_ignore_db=mysql
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_checksum=none
說明:
server-id = 1 #機器ID
log-bin=mysql-bin #開啟二進制日志
binlog_do_db=test1 #要同步的資料庫
binlog_ignore_db=mysql #忽略mysql資料庫
sync_binlog=1 #及時重新整理寫入二進制日志
binlog_checksum=none #版本不同且A大于5.6是以要設定一下
在A上建立test1資料庫 和tab表
在Master上建立slave使用者
grant all privileges on *.* to [email protected]'%' identified by 'slave';
這是個超級使用者啊,哈哈
A即Master基本配置完畢
開始配置B-Slave
設定B的my.ini
log-bin=mysql-bin
binlog_do_db=test1
binlog_ignore_db=mysql
說明同上
并在B上建立和A完全相同的test1資料庫 和tab表
至此配置都已完成,下面開始尋找要同步的二進制日志的同步點
在A輸入
mysql>show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000053
Position: 606
Binlog_Do_DB: test1
Binlog_Ignore_DB: mysql
Executed_Gtid_Set:
即test1資料庫的二進制日志是mysql-bin.000053 位置是606
在B上輸入
change master to master_host='A的ip',master_user='slave',master_password='slave',master_log_file='mysql-bin.000053',master_log_pos=606;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.209.78.13
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000053
Read_Master_Log_Pos: 333
Relay_Log_File: Lee-PC-relay-bin.000002
Relay_Log_Pos: 492
Relay_Master_Log_File: mysql-bin.000053
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test1
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
如果看到了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
說明你成功了。
那就趕緊測試一下吧。
在A上insert一條資料,看看B上傳過來了沒
教程至此完畢
說明:若在B上show slave status\G;時出現connecting to master時 原因可能是
- 密碼錯誤
- A的3306端口不通
- 等一切B機器連不上A機器的問題