一、 安裝環境
RedHat AS4.0
MySQL 5.1.42 :MySQL-server-community-5.1.42-0.rhel4.i386.rpm
MySQL-client-community-5.1.42-0.rhel4.i386.rpm
Rl01(作為master server):16.173.241.51
R03(作為slave server):16.173.241.53
二、安裝
由于是rpm包,則都用rpm–vihfullfilename指令來安裝。
三、配置
在兩台server上通過更改檔案/etc/my.cnf來進行配置,若沒有此檔案,可從/usr/share/mysql/中根據主機配置的不同選擇相應檔案拷貝過去,這裡選擇拷貝my-huge.cnf并更名為my.cnf。
Master server配置:
在my.cnf檔案中修改:
log-bin=mysql-bin
server-id= 1
binlog-do-db= rep
注:
1.log-bin表示打開binlog,打開該選項才可以通過I/O寫到Slave的relay-log,也是可以進行replication的前提
2.一般master server的server-id設為1
3.binlog-do-db=rep表示需要備份的資料庫是rep這個資料庫
Slave server配置:
在my.cnf檔案中修改:
server-id= 2
master-host=16.173.241.51
master-user=rep
master-password =rep
master-port=3306
replicate-do-db =rep
log-bin=mysql-bin
注:
1.master-host表示本機做slave時master server的位址
2.master-user表示master上開放的一個有權限的使用者,使其可以從slave連接配接到master并進行複制
3.master-password表示授權使用者的密碼
4. replicate-do-db表示同步master server的rep資料庫
5. log-bin打開logbin選項以能寫到slave的I/O線程
配置完後重新開機兩台機器的mysql服務,使配置生效
四、 在master server上配置設定權限
登陸master server的mysql,執行如下指令:
grant all privileges on rep.* to [email protected] identified by ‘rep’;
這條指令表示賦予主機:16.173.241.53上的賬号rep/rep對本機上的資料庫rep的所有操作權限。這裡為了友善,賦予了所有權限,也可将all privileges替換為replication slave,file 。
然後執行指令flush privileges來更新權限
注:
1.這條指令裡的賬号rep/rep也設定在slave server的my.cnf檔案中。
2.配置完後如果在slave server上碰到通路權限的問題,可重複配置設定權限的過程來設定其他的賬号,并在slave server的/var/lib/mysql目錄中,删除master.info檔案和relay-log.info,relay-log.index等相關relay檔案,并重新開機slave server的mysql服務,一般問題都能解決。
五、驗證
在master server上建立資料庫rep,并建立表test
mysql> create database rep;
Query OK, 1 row affected (0.00 sec)
mysql> use rep
Database changed
mysql> create table test (a int(10),b int(10));
Query OK, 0 rows affected (0.01 sec)
傳回slave server檢視:
mysql> show databases;
+--------------------+
| Database|
+--------------------+
| information_schema |
| mysql|
| rep|
| test|
+--------------------+
4 rows in set (0.00 sec)
mysql> use rep
Database changed
mysql> show tables;
+---------------+
| Tables_in_rep |
+---------------+
| test|
+---------------+
1 row in set (0.00 sec)
這裡就說明兩台server同步成功。
六、常用檢視指令
Master server上:
show master status
mysql> show master status;
+------------------+----------+--------------+------------------+
| File| Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |288 | rep,rep||
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
這裡的position不能為0,如果為0則表示有問題,可檢查/etc/my.cnf中的配置,server-id是否正确和是否打開了log-bin
show processlist
mysql> show processlist;
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
| Id | User | Host| db| Command| Time | State| Info|
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
| 27 | rep| tssavl03.chn.hp.com:55699 | NULL | Binlog Dump |935 | Has sent all binlog to slave; waiting for binlog to be updated | NULL|
| 28 | root | localhost| NULL | Query|0 | NULL| show processlist |
+----+------+---------------------------+------+-------------+------+----------------------------------------------------------------+------------------+
2 rows in set (0.00 sec)
若replication成功了,則這裡會有如上兩個程序
Slave server上:
show processlist
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
| Id | User| Host| db| Command | Time | State| Info|
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
|1 | system user || NULL | Connect | 5167 | Waiting for master to send event| NULL|
|2 | system user || NULL | Connect | 2634 | Has read all relay log; waiting for the slave I/O thread to update it | NULL|
|4 | root| localhost | NULL | Query|0 | NULL| show processlist |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)
Replication成功後slave server上也應該有如上三個程序。
檢視/var/lib/mysql下的錯誤日志檔案,這裡在slave server上的日志檔案是R03.err,一般的錯誤都能在這裡查出。