1、在兩台伺服器上安裝好mysql
2、主伺服器配置 vi /etc/my.cnf
在[mysqld]下添加一下參數
server-id=190 #伺服器唯一辨別,常用IP最後一位
log_bin=master-bin
log_bin_index=master-bin.index
binlog-ignore-db=mysql #不需要同步的資料庫,如多個庫,重複設定
binlog-ignore-db=information_schema
3、進入MySQL資料庫,建立從庫使用者權限:
mysql>grant replication slave on *.* to 'root'@'從伺服器的ip' identified by '從伺服器的密碼 ' ;
4、檢視建立結果:mysql>select host,user from mysql.user;
5、重新整理權限:mysql>flush privileges;
6、重新開機MySQL :service mysqld restart/service mysql stop/service mysql start
7、登入MySQL檢視狀态:show master status;記錄File和Position
8、從伺服器配置 vi /etc/my.cnf
server-id=191
relay-log=slave-relay-bin
relay-log-index=slave-relay-bin.index
replicate-do-db=test #需要同步的資料庫,如多個庫,重複設定
replicate-do-db=test1
9、重新開機MySQL
10、在從伺服器上連接配接master主伺服器
change master to master_host='主伺服器的ip',master_port=3306,master_user='root',master_password='主伺服器的密碼',master_log_file='master-bin.000009',master_log_pos=319701631;
11、啟動slave同步資料
mysql> start slave;
12、檢視slave資訊,兩個yes代表成功。
mysql> show slave status\G;