假設有兩臺MySQL服務器:
主服務器:192.168.10.1
從服務器:192.168.10.2
1.配置主服務器my.cnf文件,修改my.cnf文件裏的mysqld模塊,添加如下內容:
[mysqld]
server-id = 1
log-bin = mysql-bin
binlog-do-db = your-db-name1 //需要同步的數據庫名稱,如果有多個數據庫,將此行復制多條
binlog-do-db = your-db-name2
binlog-ignore-db = mysql //需要忽略的數據庫名稱
2.主服務器添加用戶並作主從複製授權
mysql>create user 'slave'@'%' identified by 'your password';
mysql>grant all privileges on *.* to 'slave'@'%' identified by 'your password' with grant option;
mysql>flush privileges;
mysql>grant replication slave on *.* to 'slave'@'%' identified by 'your password' with grant option;
mysql>flush privileges;
3.重啓主服務器
4.登錄主服務器MySQL,並獲取bin日誌的名稱和偏移位置,即File和Position(這兩個值會在從服務器的配置中用到)
mysql -h192.168.10.1 -P3306 -uroot -p
mysql>show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 376 | didi | mysql | |
+------------------+----------+--------------+------------------+-------------------+
5.配置從服務器my.cnf文件,修改my.cnf文件裏的mysqld模塊,添加如下內容:
[mysqld]
server-id = 2
replicate-do-db = your-db-name1 //需要同步的數據庫名稱,如果有多個數據庫,將此行復制多條
replicate-do-db = your-db-name2
replicate-ignore-db = mysql //需要忽略的數據庫名稱
6.登錄從服務器MySQL,設置主庫信息
master_host:主庫IP
master_user:用於主從複製的用戶名
master_password:密碼
master_log_file:主庫bin日誌文件名
master_log_pos:主庫bin日誌偏移量
msyql -h192.168.10.2 -P3306 -uroot -p
mysql>change master to master_host="192.168.10.1", master_user="slave", "master_password"="your slave password", master_log_file="mysql-bin.000001", master_log_pos=376;
7.重啓從服務器
8.啓動slave
msyql -h192.168.10.2 -P3306 -uroot -p
mysql>start slave;
配置完畢。