环境及要求
主:192.168.1.251
从(主主复制中的主):192.168.1.253
主从复制
主服务器
- 配置文件
vim /etc/my.cnf
[mysqld]
skip_name_resolve=0 #禁止域名解析
server-id=251 #设置主服务的ID (id可以自己随便设置但是要保证和slave的id不一样)
log-bin=master-bin #log-bin日志文件名
sync_binlog=1 #【可选】开启binlog日志同步功能
binlog-lgnore-db=mysql #【可选】设置不要复制的数据库
binlog-do-db=需要复制的主数据库名字 #【可选】设置需要复制的数据
- 重启mysql后生成日志文件
systemctl restart mariadb
cat /data/mysql/master-bin.000001 #把里面记录的日志文件名记录下来等一下从库要用到
- 授权后查看主服务器状态
mysql -uroot -p
grant replication slave,replication client on *.* to "mariadbsync"@"192.168.1.253" identified by "123456";
flush privileges;
show master status; #记录下245(pos)等下配置从库要用
+--------------------+----------+--------------+---------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+---------------+
| master-bin.000001 | 245 | | |
+--------------------+----------+--------------+---------------+
从服务器
- 配置文件
vim /etc/my.cnf
[mysqld]
skip_name_resolve=0 #禁止域名解析
server-id=253
relay-log=slave-bin #relay-log日志文件名
- 重启mysq后配置从库
systemctl restart mariadb
mysql -uroot -p
change master to
MASTER_HOST='192.168.1.251', #主库IP
MASTER_USER='mariadbsync', #账号
MASTER_PASSWORD='123456', #密码
MASTER_LOG_FILE='master-bin.000001', #主库日志文件名
MASTER_LOG_POS=245; #主库的pos为245
- 开启从库与状态查看
mysql -uroot -p
start slave; #启动
stop slave; #关闭
show slave status\G #查看状态
主主复制
主1
- 配置文件
vim /etc/my.cnf
[mysqld]
skip_name_resolve=0
server-id=251
log-bin=master-bin-251
relay-log=slave-bin-251
auto_increment_offset=1 #【可选】自增值
auto_increment_increment=2 #【可选】漂移值,也就是步长
- 授权后查看主服务器状态
mysql -uroot -p
grant replication slave,replication client on *.* to "mariadbsync"@"192.168.1.253" identified by "123456";
flush privileges;
show master status; #记录下245(pos)等下配置从库要用
+--------------------+----------+--------------+---------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+---------------+
| master-bin-251.000001 | 245 | | |
+--------------------+----------+--------------+---------------+
# 以下命令需要主2配置玩之后才能执行
change master to
master_host='192.168.1.253'
master_user='mariadbsync'
master_password='123456'
master_log_file='master-bin-253.000003'
master_log_pos=231;
start slave;
SHOW SLAVE STATUS\G
主2
- 配置文件
vim /etc/my.cnf
[mysqld]
skip_name_resolve=0
server-id=253
log-bin=master-bin-253
relay-log=slave-bin-253
auto_increment_offset=2 #【可选】自增值
auto_increment_increment=2 #【可选】漂移值,也就是步长
- 授权后查看主服务器状态
mysql -uroot -p
grant replication slave,replication client on *.* to "mariadbsync"@"192.168.1.251" identified by "123456";
flush privileges;
show master status; #记录下245(pos)等下配置从库要用
+--------------------+----------+--------------+---------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+---------------+
| master-bin-253.000003 | 231 | | |
+--------------------+----------+--------------+---------------+
# 以下命令需要主2配置玩之后才能执行
change master to
master_host='192.168.1.251'
master_user='mariadbsync'
master_password='123456'
master_log_file='master-bin-251.000001'
master_log_pos=245;
start slave;
SHOW SLAVE STATUS\G