mysql的安裝過程比較簡單,這裡略過 1.在maste主機上,配置my.cnf 在[mysqld]配置段添加如下欄位中增加 log-bin=mysql-bin.log binlog-do-db=blog #要同步的資料庫的名字 binlog-do-db=index #要同步的資料庫的名字 server-id=1 重啟mysql #service mysqld restart 進入mysql中 #mysql -u root -ppassword 增加同步帳號 mysql>grant replication slave on *.* to 'repication'@'%' identified by 'password'; mysql>use database; mysql>flush tables with read lock; mysql>show master status: --------------------------------------------------------------------- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+------------------+------------------+ | mysql-bin.000002 | 1448 | blog,index | | +------------------+----------+------------------+------------------+ 此時不要關閉此終端! 記住上表前兩個欄位的資訊 mysql-bin.000002 和 1448 。 另開一個終端: 拿到主要資料庫上的“資料庫快照集” #tar -czvf database.tar.gz /data/database 切換到上一個終端 mysql>unlock tables; #解鎖,讓主要資料庫192.168.1.10上的資料庫正常更新 2.配置slave資料庫 首先建立資料庫 #mysql -uroot -ppassword mysql>create database databasename #建一個與主伺服器的資料庫名字相同的資料庫 mysql>exit; 3.在兩個從資料庫的[mysqld]配置段添加如下欄位中增加 # 192.168.1.11 server-id=2 master-host=192.168.1.10 master-user=replication master-password=password master-connect-retry=60 replicate-do-db=blog replicate-do-db=index #192.168.1.12 server-id=3 master-host=192.168.1.10 master-user=replication master-password=password master-connect-retry=60 replicate-do-db=blog replicate-do-db=index 3.分別重啟兩個資料庫 #service mysqld restart 4.分別進入兩個從資料庫中 在salve2上 #mysql -uroot -ppassword mysql>slave stop; > CHANGE MASTER TO > MASTER_HOST='192.168.1.10', > MASTER_USER='replication', > MASTER_PASSWORD='password', > MASTER_LOG_FILE='mysql-bin.000002', > MASTER_LOG_POS=1448; MYSQL>START SLAVE; 在salve2上 #mysql -uroot -ppassword mysql>slave stop; > CHANGE MASTER TO > MASTER_HOST='192.168.1.10', > MASTER_USER='replication', > MASTER_PASSWORD='password', > MASTER_LOG_FILE='mysql-bin.000002', > MASTER_LOG_POS=1448; MYSQL>START SLAVE; 在slave端 : mysql> show processlist; +----+-------------+-----------+------+---------+------------+-----------------------------------------------------------------------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+-------------+-----------+------+---------+------------+-----------------------------------------------------------------------+------------------+ | 4 | system user | | NULL | Connect | 48 | Waiting for master to send event | NULL | | 5 | system user | | NULL | Connect | 4294923022 | Has read all relay log; waiting for the slave I/O thread to update it | NULL | | 6 | root | localhost | NULL | Query | 0 | NULL | show processlist | +----+-------------+-----------+------+---------+------------+---------------------------------------------------------- 以上資訊表示同步成功! 4.同步測試過程 分別在主要資料庫上建立一個表,插入一條資料 在兩個從資料庫上看看是否更新 過程略。 |