mysql 主从笔记

主库配置
一、修改主库配置文件 开启binlog,并设置server-id,每次修改配置文件后都要重启mysql服务才会生效 
server
-id = 100
log
-bin = mysql-bin
binlog_format
= mixed #默认是mixed
binlog_do_db
= #这里写需要同步的数据库名称
开启binlog 从机不用开启
###不同步mysql库和test库

binlog-ignore-db = mysql
binlog-ignore-db = test 
expire_logs_days
= 5 #设置binlog过期时间为5天时间可以自定义,正式环境建议长期保存
二、创建同步账号密码 授权给从数据库服务器
mysql
>create user repl; //创建新用户 //repl用户必须具有REPLICATION SLAVE权限,除此之外没有必要添加不必要的权限,密码为mysql。
说明一下192.168.0.%,这个配置是指明repl用户所在服务器,这里%是通配符,表示192.168.113.0-192.168.113.255的Server都可以以repl用户登陆主服务器。


mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'mysql';
查看用户权限:
show grants
for 'repl'@'192.168.113.%';
查看master状态:
show master status;
重置master,初次可以忽略此步骤:
mysql
> reset master;
从库配置
一、从库配置文件同主库一样只需修改 server
-id 即可,binlog可开可不开,看你心情了
配置连接master
change master to master_host
='192.168.113.130', master_password='123456', master_port=3306, master_user='repl', master_log_file='mysql-bin.000001', master_log_pos=120;


Query OK, 0 rows affected, 2 warnings (0.09 sec)

mysql> start slave;
mysql> show slave status;

看到如图所示表示已经成功连接到了master

清除binlog 命令
PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTERVAL 5 DAY);

 

 

posted @ 2019-09-04 15:42  来自牧羊人  阅读(276)  评论(0编辑  收藏  举报