mysql数据库添加从库

 mysql 主从配置在业务中应用不少,虽然了解其中机制,但从未进行处理过,为了深入了解下,对mysql进行了主从配置

主库:3306

从库:3307

1 主库配置:

[mysqld]
#用自己数据库对应的即可
log-bin = /data0/mysql/3306/binlog/binlog
log-bin-index = /data0/mysql/3306/binlog/binlog.index

server-id = 1  #与从库配置与主库不能相同

 注意:注意原配置文件中是否有binlog-do-db,如果指定了数据库,那只有这个数据库能够进行主从。根据需要判断是否需要配置(重启服务)

 添加复制权限的用户(可读取2进制文件):

 GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '用户名'@'IP' IDENTIFIED BY '密码';

mysql>
show master status;
查看file 和position
 

2 从库配置:

[mysqld]
#需要配置relaylog如果没有请创建对应权限的目录
relay-log-index = /data0/mysql/3307/relaylog/relaylog  #中继日志索引文件
relay-log-info-file = /data0/mysql/3307/relaylog/relaylog #中继日志文件

replicate-ignore-db=mysql                      #屏蔽对mysql的同步,可以设置多个用","分隔
#binlog-do-db = test                              如果开启,则是只对test进行同步
server-id = 2 #与主库配置不同

同样需要注意binlog-do-db的配置

   重启服务

数据库操作

mysql>
>change master to master_host = '主库ip',
>master_port = 3306,
>master_user = '用户名',
>master_password = '密码',
>master_log_file = 'binlog.***',
>master_log_pos = 101; 
 

master_log_file 和master_log_pos在主库中查询

然后:

mysql> slave start;

# 查看从库状态

mysql> show slave status\G;

信息中:    

    Slave_IO_Running: Yes

    Slave_SQL_Running: Yes

说明配置成功,测试主库写入从库是否能够同步成功。

猜你喜欢

转载自alfred-long.iteye.com/blog/1703721