#RHEL5 MYSQL5 master/slave 主從複制 配置
# 确認兩台伺服器的MYSQL版本,用mysql> SHOW VARIABLES;檢視。
# 注意: 最好采用相同的版本,如果達不到要求,必須要保證Master的版本不能高于Slave版本.
mysql>GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'repl';
#開始之前必須人工同步主從MYSQL,可按以下方法,也可用mysqldump,要預同步的資料庫保持一緻。
------------人工同步資料庫-----------------
mysql> SHOW VARIABLES; # 先找到basedir位置
service mysqld stop # 停下master與slave伺服器
rm -rf /usr/lib/mysql/* # 删掉從伺服器的全部資料檔案
scp mysql_master:/usr/lib/mysql/* /usr/lib/mysql/
-----------修改my.cnf配置檔案---------------
#MASTER 配置
[mysqld]
server-id=1 # 此數随意 不同于slave即可
log-bin=mysql-bin # 開啟 Binary日志, 必須
#binlog-do-db=test # 可選
#binlog-ignore-db=mysql # 可選
-----------
#SLAVE 配置
server-id=2 # 此數随意 不同于master即可
#replicate-do-db=test # 需要備份的資料庫名
#replicate-ignore-db=mysql # 忽略的資料庫
------------設定自動同步------------------
主從:service mysqld start # 啟動MYSQL伺服器
主mysql> SHOW MASTER STATUS; # 記住MASTER_LOG_FILE與MASTER_LOG_POS
從mysql> CHANGE MASTER TO
MASTER_HOST='192.168.0.2',
MASTER_USER='repl',
MASTER_PASSWORD='repl',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=98;
從mysql> START SLAVE; # 啟動slave伺服器同步線程
---------------測試-------------------
從mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.2
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 235
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: 98
Relay_Log_Space: 235
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
1 row in set (0.00 sec)
從mysql> show processlist; # 檢視是否已經正常啟動
好了,現在MASTER上插入資料,再去SLAVE上瞧瞧
-----------------------------