mysql 机器
- 主机 IP :192.168.1.112
- 从机 IP :192.168.1.114
主机配置
- my.ini 配置 (修改后重启服务)
# 主从复制主服务器配置
server-id = 2222
#开启log-bin日志
log-bin = mysql-bin
#只复制employees 数据库
binlog-do-db = employees
配置说明
- server-id 标识服务器的id
- 以二进制log 复制
- binlog-do-db 要复制的数据
- 查询主机状态,记录主机 file 与 position
mysql> show master status
-> ;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | employees | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
-> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)
- 主机添加允许从机复制账号
mysql> grant replication slave on *.* to replMaster@‘192.168.1.114‘ identified by ‘replMaster‘
从机配置
- my.ini (修改后重启服务)
#mysql主从复制 slave 配置
server-id = 203
#只同步 employees 数据库
replicate-do-db = employees
relay-log-index = slave-relay-bin.index
relay-log = slave-relay-bin
- 执行同步命令,设置主数据库ip,同步帐号密码,同步位置
master_log_pos,master_log_file 就是在主机执行 show master status 后,获取的值
mysql> change master to master_host=‘192.168.1.112‘,master_user=‘replMaster‘,master_password=‘sqlsa‘,master_log
_pos=1606,master_log_file=‘mysql-bin.000009‘ ;
Query OK, 0 rows affected, 2 warnings (0.56 sec)
mysql> start slave;
Query OK, 0 rows affected (0.07 sec)
- 查看从机状态
mysql> show slave status\G
如果一切设置正确,那么mysql 主从复制就一切OK 了。
- 验证